1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/permissions/permission_manager.h"
#include <memory>
#include "base/bind.h"
#include "base/macros.h"
#include "base/metrics/field_trial_params.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/permissions/features.h"
#include "components/permissions/permission_context_base.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/permission_result.h"
#include "components/permissions/test/mock_permission_prompt_factory.h"
#include "components/permissions/test/test_permissions_client.h"
#include "content/public/browser/permission_type.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/feature_policy/feature_policy.mojom.h"
#if defined(OS_ANDROID)
#include "base/android/build_info.h"
#endif // defined(OS_ANDROID)
using blink::mojom::PermissionStatus;
using content::PermissionType;
namespace permissions {
namespace {
class FakePermissionContext : public PermissionContextBase {
public:
FakePermissionContext(
content::BrowserContext* browser_context,
ContentSettingsType content_settings_type,
blink::mojom::FeaturePolicyFeature feature_policy_feature)
: PermissionContextBase(browser_context,
content_settings_type,
feature_policy_feature) {}
// PermissionContextBase:
bool IsRestrictedToSecureOrigins() const override { return true; }
};
class FakePermissionContextAlwaysAllow : public FakePermissionContext {
public:
FakePermissionContextAlwaysAllow(
content::BrowserContext* browser_context,
ContentSettingsType content_settings_type,
blink::mojom::FeaturePolicyFeature feature_policy_feature)
: FakePermissionContext(browser_context,
content_settings_type,
feature_policy_feature) {}
// PermissionContextBase:
ContentSetting GetPermissionStatusInternal(
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
const GURL& embedding_origin) const override {
return CONTENT_SETTING_ALLOW;
}
};
PermissionManager::PermissionContextMap CreatePermissionContexts(
content::BrowserContext* browser_context) {
PermissionManager::PermissionContextMap permission_contexts;
permission_contexts[ContentSettingsType::GEOLOCATION] =
std::make_unique<FakePermissionContext>(
browser_context, ContentSettingsType::GEOLOCATION,
blink::mojom::FeaturePolicyFeature::kGeolocation);
permission_contexts[ContentSettingsType::NOTIFICATIONS] =
std::make_unique<FakePermissionContext>(
browser_context, ContentSettingsType::NOTIFICATIONS,
blink::mojom::FeaturePolicyFeature::kNotFound);
permission_contexts[ContentSettingsType::MIDI_SYSEX] =
std::make_unique<FakePermissionContext>(
browser_context, ContentSettingsType::MIDI_SYSEX,
blink::mojom::FeaturePolicyFeature::kMidiFeature);
permission_contexts[ContentSettingsType::MIDI] =
std::make_unique<FakePermissionContextAlwaysAllow>(
browser_context, ContentSettingsType::MIDI,
blink::mojom::FeaturePolicyFeature::kMidiFeature);
#if defined(OS_ANDROID)
permission_contexts[ContentSettingsType::PROTECTED_MEDIA_IDENTIFIER] =
std::make_unique<FakePermissionContext>(
browser_context, ContentSettingsType::PROTECTED_MEDIA_IDENTIFIER,
blink::mojom::FeaturePolicyFeature::kEncryptedMedia);
#endif
return permission_contexts;
}
#if defined(OS_ANDROID)
// See https://crbug.com/904883.
auto GetDefaultProtectedMediaIdentifierPermissionStatus() {
return base::android::BuildInfo::GetInstance()->sdk_int() >=
base::android::SDK_VERSION_MARSHMALLOW
? PermissionStatus::GRANTED
: PermissionStatus::ASK;
}
auto GetDefaultProtectedMediaIdentifierContentSetting() {
return base::android::BuildInfo::GetInstance()->sdk_int() >=
base::android::SDK_VERSION_MARSHMALLOW
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_ASK;
}
#endif // defined(OS_ANDROID)
} // namespace
class PermissionManagerTest : public content::RenderViewHostTestHarness {
public:
void OnPermissionChange(PermissionStatus permission) {
if (!quit_closure_.is_null())
quit_closure_.Run();
callback_called_ = true;
callback_result_ = permission;
}
protected:
PermissionManagerTest()
: url_("https://example.com"),
other_url_("https://foo.com"),
callback_called_(false),
callback_result_(PermissionStatus::ASK) {}
PermissionManager* GetPermissionControllerDelegate() {
return static_cast<PermissionManager*>(
browser_context_->GetPermissionControllerDelegate());
}
HostContentSettingsMap* GetHostContentSettingsMap() {
return PermissionsClient::Get()->GetSettingsMap(browser_context_.get());
}
void CheckPermissionStatus(PermissionType type, PermissionStatus expected) {
EXPECT_EQ(expected, GetPermissionControllerDelegate()->GetPermissionStatus(
type, url_.GetOrigin(), url_.GetOrigin()));
}
void CheckPermissionResult(ContentSettingsType type,
ContentSetting expected_status,
PermissionStatusSource expected_status_source) {
PermissionResult result =
GetPermissionControllerDelegate()->GetPermissionStatus(
type, url_.GetOrigin(), url_.GetOrigin());
EXPECT_EQ(expected_status, result.content_setting);
EXPECT_EQ(expected_status_source, result.source);
}
void SetPermission(ContentSettingsType type, ContentSetting value) {
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url_, url_, type, std::string(), value);
}
int RequestPermission(PermissionType type,
content::RenderFrameHost* rfh,
const GURL& origin) {
base::RunLoop loop;
quit_closure_ = loop.QuitClosure();
int result = GetPermissionControllerDelegate()->RequestPermission(
type, rfh, origin, true,
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
loop.Run();
return result;
}
const GURL& url() const { return url_; }
const GURL& other_url() const { return other_url_; }
bool callback_called() const { return callback_called_; }
PermissionStatus callback_result() const { return callback_result_; }
void Reset() {
callback_called_ = false;
callback_result_ = PermissionStatus::ASK;
}
bool PendingRequestsEmpty() {
return GetPermissionControllerDelegate()->pending_requests_.IsEmpty();
}
// The header policy should only be set once on page load, so we refresh the
// page to simulate that.
void RefreshPageAndSetHeaderPolicy(content::RenderFrameHost** rfh,
blink::mojom::FeaturePolicyFeature feature,
const std::vector<std::string>& origins) {
content::RenderFrameHost* current = *rfh;
SimulateNavigation(¤t, current->GetLastCommittedURL());
std::vector<url::Origin> parsed_origins;
for (const std::string& origin : origins)
parsed_origins.push_back(url::Origin::Create(GURL(origin)));
content::RenderFrameHostTester::For(current)->SimulateFeaturePolicyHeader(
feature, parsed_origins);
*rfh = current;
}
content::RenderFrameHost* AddChildRFH(content::RenderFrameHost* parent,
const char* origin) {
content::RenderFrameHost* result =
content::RenderFrameHostTester::For(parent)->AppendChild("");
content::RenderFrameHostTester::For(result)
->InitializeRenderFrameIfNeeded();
SimulateNavigation(&result, GURL(origin));
return result;
}
private:
void SetUp() override {
RenderViewHostTestHarness::SetUp();
browser_context_ = std::make_unique<content::TestBrowserContext>();
browser_context_->SetPermissionControllerDelegate(
std::make_unique<PermissionManager>(
browser_context_.get(),
CreatePermissionContexts(browser_context_.get())));
NavigateAndCommit(url());
}
void TearDown() override {
GetPermissionControllerDelegate()->Shutdown();
browser_context_ = nullptr;
RenderViewHostTestHarness::TearDown();
}
void SimulateNavigation(content::RenderFrameHost** rfh, const GURL& url) {
auto navigation_simulator =
content::NavigationSimulator::CreateRendererInitiated(url, *rfh);
navigation_simulator->Commit();
*rfh = navigation_simulator->GetFinalRenderFrameHost();
}
const GURL url_;
const GURL other_url_;
bool callback_called_;
PermissionStatus callback_result_;
base::Closure quit_closure_;
std::unique_ptr<content::TestBrowserContext> browser_context_;
TestPermissionsClient client_;
};
TEST_F(PermissionManagerTest, GetPermissionStatusDefault) {
CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK);
CheckPermissionStatus(PermissionType::NOTIFICATIONS, PermissionStatus::ASK);
CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK);
#if defined(OS_ANDROID)
CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER,
GetDefaultProtectedMediaIdentifierPermissionStatus());
#endif
}
TEST_F(PermissionManagerTest, GetPermissionStatusAfterSet) {
SetPermission(ContentSettingsType::GEOLOCATION, CONTENT_SETTING_ALLOW);
CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED);
SetPermission(ContentSettingsType::NOTIFICATIONS, CONTENT_SETTING_ALLOW);
CheckPermissionStatus(PermissionType::NOTIFICATIONS,
PermissionStatus::GRANTED);
SetPermission(ContentSettingsType::MIDI_SYSEX, CONTENT_SETTING_ALLOW);
CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::GRANTED);
#if defined(OS_ANDROID)
SetPermission(ContentSettingsType::PROTECTED_MEDIA_IDENTIFIER,
CONTENT_SETTING_ALLOW);
CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER,
PermissionStatus::GRANTED);
#endif
}
TEST_F(PermissionManagerTest, CheckPermissionResultDefault) {
CheckPermissionResult(ContentSettingsType::MIDI_SYSEX, CONTENT_SETTING_ASK,
PermissionStatusSource::UNSPECIFIED);
CheckPermissionResult(ContentSettingsType::NOTIFICATIONS, CONTENT_SETTING_ASK,
PermissionStatusSource::UNSPECIFIED);
CheckPermissionResult(ContentSettingsType::GEOLOCATION, CONTENT_SETTING_ASK,
PermissionStatusSource::UNSPECIFIED);
#if defined(OS_ANDROID)
CheckPermissionResult(ContentSettingsType::PROTECTED_MEDIA_IDENTIFIER,
GetDefaultProtectedMediaIdentifierContentSetting(),
PermissionStatusSource::UNSPECIFIED);
#endif
}
TEST_F(PermissionManagerTest, CheckPermissionResultAfterSet) {
SetPermission(ContentSettingsType::GEOLOCATION, CONTENT_SETTING_ALLOW);
CheckPermissionResult(ContentSettingsType::GEOLOCATION, CONTENT_SETTING_ALLOW,
PermissionStatusSource::UNSPECIFIED);
SetPermission(ContentSettingsType::NOTIFICATIONS, CONTENT_SETTING_ALLOW);
CheckPermissionResult(ContentSettingsType::NOTIFICATIONS,
CONTENT_SETTING_ALLOW,
PermissionStatusSource::UNSPECIFIED);
SetPermission(ContentSettingsType::MIDI_SYSEX, CONTENT_SETTING_ALLOW);
CheckPermissionResult(ContentSettingsType::MIDI_SYSEX, CONTENT_SETTING_ALLOW,
PermissionStatusSource::UNSPECIFIED);
#if defined(OS_ANDROID)
SetPermission(ContentSettingsType::PROTECTED_MEDIA_IDENTIFIER,
CONTENT_SETTING_ALLOW);
CheckPermissionResult(ContentSettingsType::PROTECTED_MEDIA_IDENTIFIER,
CONTENT_SETTING_ALLOW,
PermissionStatusSource::UNSPECIFIED);
#endif
}
TEST_F(PermissionManagerTest, SubscriptionDestroyedCleanlyWithoutUnsubscribe) {
// Test that the PermissionManager shuts down cleanly with subscriptions that
// haven't been removed, crbug.com/720071.
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
}
TEST_F(PermissionManagerTest, SubscribeUnsubscribeAfterShutdown) {
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
// Simulate Keyed Services shutdown pass. Note: Shutdown will be called second
// time during browser_context destruction. This is ok for now: Shutdown is
// reenterant.
GetPermissionControllerDelegate()->Shutdown();
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
// Check that subscribe/unsubscribe after shutdown don't crash.
int subscription2_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription2_id);
}
TEST_F(PermissionManagerTest, SameTypeChangeNotifies) {
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_TRUE(callback_called());
EXPECT_EQ(PermissionStatus::GRANTED, callback_result());
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
TEST_F(PermissionManagerTest, DifferentTypeChangeDoesNotNotify) {
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), GURL(), ContentSettingsType::NOTIFICATIONS, std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_FALSE(callback_called());
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
TEST_F(PermissionManagerTest, ChangeAfterUnsubscribeDoesNotNotify) {
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_FALSE(callback_called());
}
TEST_F(PermissionManagerTest, DifferentPrimaryUrlDoesNotNotify) {
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
other_url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_FALSE(callback_called());
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
TEST_F(PermissionManagerTest, DifferentSecondaryUrlDoesNotNotify) {
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), other_url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_FALSE(callback_called());
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
TEST_F(PermissionManagerTest, WildCardPatternNotifies) {
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetHostContentSettingsMap()->SetDefaultContentSetting(
ContentSettingsType::GEOLOCATION, CONTENT_SETTING_ALLOW);
EXPECT_TRUE(callback_called());
EXPECT_EQ(PermissionStatus::GRANTED, callback_result());
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
TEST_F(PermissionManagerTest, ClearSettingsNotifies) {
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ALLOW);
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetHostContentSettingsMap()->ClearSettingsForOneType(
ContentSettingsType::GEOLOCATION);
EXPECT_TRUE(callback_called());
EXPECT_EQ(PermissionStatus::ASK, callback_result());
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
TEST_F(PermissionManagerTest, NewValueCorrectlyPassed) {
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_TRUE(callback_called());
EXPECT_EQ(PermissionStatus::DENIED, callback_result());
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
TEST_F(PermissionManagerTest, ChangeWithoutPermissionChangeDoesNotNotify) {
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ALLOW);
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_FALSE(callback_called());
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
TEST_F(PermissionManagerTest, ChangesBackAndForth) {
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ASK);
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_TRUE(callback_called());
EXPECT_EQ(PermissionStatus::GRANTED, callback_result());
Reset();
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ASK);
EXPECT_TRUE(callback_called());
EXPECT_EQ(PermissionStatus::ASK, callback_result());
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
TEST_F(PermissionManagerTest, ChangesBackAndForthWorker) {
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ASK);
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, nullptr, url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_TRUE(callback_called());
EXPECT_EQ(PermissionStatus::GRANTED, callback_result());
Reset();
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ASK);
EXPECT_TRUE(callback_called());
EXPECT_EQ(PermissionStatus::ASK, callback_result());
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
TEST_F(PermissionManagerTest, SubscribeMIDIPermission) {
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::MIDI, main_rfh(), url(),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK);
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ALLOW);
CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED);
EXPECT_FALSE(callback_called());
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
TEST_F(PermissionManagerTest, PermissionIgnoredCleanup) {
content::WebContents* contents = web_contents();
PermissionRequestManager::CreateForWebContents(contents);
PermissionRequestManager* manager =
PermissionRequestManager::FromWebContents(contents);
auto prompt_factory = std::make_unique<MockPermissionPromptFactory>(manager);
NavigateAndCommit(url());
GetPermissionControllerDelegate()->RequestPermission(
PermissionType::GEOLOCATION, main_rfh(), url(), /*user_gesture=*/true,
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
EXPECT_FALSE(PendingRequestsEmpty());
NavigateAndCommit(GURL("https://foobar.com"));
EXPECT_TRUE(callback_called());
EXPECT_TRUE(PendingRequestsEmpty());
}
// Check PermissionResult shows requests denied due to insecure
// origins.
TEST_F(PermissionManagerTest, InsecureOrigin) {
GURL insecure_frame("http://www.example.com/geolocation");
NavigateAndCommit(insecure_frame);
PermissionResult result =
GetPermissionControllerDelegate()->GetPermissionStatusForFrame(
ContentSettingsType::GEOLOCATION, web_contents()->GetMainFrame(),
insecure_frame);
EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting);
EXPECT_EQ(PermissionStatusSource::INSECURE_ORIGIN, result.source);
GURL secure_frame("https://www.example.com/geolocation");
NavigateAndCommit(secure_frame);
result = GetPermissionControllerDelegate()->GetPermissionStatusForFrame(
ContentSettingsType::GEOLOCATION, web_contents()->GetMainFrame(),
secure_frame);
EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting);
EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source);
}
TEST_F(PermissionManagerTest, InsecureOriginIsNotOverridable) {
const url::Origin kInsecureOrigin =
url::Origin::Create(GURL("http://example.com/geolocation"));
const url::Origin kSecureOrigin =
url::Origin::Create(GURL("https://example.com/geolocation"));
EXPECT_FALSE(
GetPermissionControllerDelegate()->IsPermissionOverridableByDevTools(
PermissionType::GEOLOCATION, kInsecureOrigin));
EXPECT_TRUE(
GetPermissionControllerDelegate()->IsPermissionOverridableByDevTools(
PermissionType::GEOLOCATION, kSecureOrigin));
}
TEST_F(PermissionManagerTest, MissingContextIsNotOverridable) {
// Permissions that are not implemented should be denied overridability.
#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
EXPECT_FALSE(
GetPermissionControllerDelegate()->IsPermissionOverridableByDevTools(
PermissionType::PROTECTED_MEDIA_IDENTIFIER,
url::Origin::Create(GURL("http://localhost"))));
#endif
EXPECT_TRUE(
GetPermissionControllerDelegate()->IsPermissionOverridableByDevTools(
PermissionType::MIDI_SYSEX,
url::Origin::Create(GURL("http://localhost"))));
}
TEST_F(PermissionManagerTest, KillSwitchOnIsNotOverridable) {
const url::Origin kLocalHost = url::Origin::Create(GURL("http://localhost"));
EXPECT_TRUE(
GetPermissionControllerDelegate()->IsPermissionOverridableByDevTools(
PermissionType::GEOLOCATION, kLocalHost));
// Turn on kill switch for GEOLOCATION.
std::map<std::string, std::string> params;
params[PermissionUtil::GetPermissionString(
ContentSettingsType::GEOLOCATION)] =
PermissionContextBase::kPermissionsKillSwitchBlockedValue;
base::AssociateFieldTrialParams(
PermissionContextBase::kPermissionsKillSwitchFieldStudy, "TestGroup",
params);
base::FieldTrialList::CreateFieldTrial(
PermissionContextBase::kPermissionsKillSwitchFieldStudy, "TestGroup");
EXPECT_FALSE(
GetPermissionControllerDelegate()->IsPermissionOverridableByDevTools(
PermissionType::GEOLOCATION, kLocalHost));
}
TEST_F(PermissionManagerTest, GetPermissionStatusDelegation) {
const char* kOrigin1 = "https://example.com";
const char* kOrigin2 = "https://google.com";
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(features::kPermissionDelegation);
NavigateAndCommit(GURL(kOrigin1));
content::RenderFrameHost* parent = main_rfh();
content::RenderFrameHost* child = AddChildRFH(parent, kOrigin2);
// By default the parent should be able to request access, but not the child.
EXPECT_EQ(CONTENT_SETTING_ASK,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
parent, GURL(kOrigin1))
.content_setting);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
child, GURL(kOrigin2))
.content_setting);
// Enabling geolocation by FP should allow the child to request access also.
RefreshPageAndSetHeaderPolicy(
&parent, blink::mojom::FeaturePolicyFeature::kGeolocation,
{kOrigin1, kOrigin2});
child = AddChildRFH(parent, kOrigin2);
EXPECT_EQ(CONTENT_SETTING_ASK,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
child, GURL(kOrigin2))
.content_setting);
// When the child requests location a prompt should be displayed for the
// parent.
PermissionRequestManager::CreateForWebContents(web_contents());
PermissionRequestManager* manager =
PermissionRequestManager::FromWebContents(web_contents());
auto prompt_factory = std::make_unique<MockPermissionPromptFactory>(manager);
prompt_factory->set_response_type(PermissionRequestManager::ACCEPT_ALL);
prompt_factory->DocumentOnLoadCompletedInMainFrame();
RequestPermission(PermissionType::GEOLOCATION, child, GURL(kOrigin2));
EXPECT_TRUE(prompt_factory->RequestOriginSeen(GURL(kOrigin1)));
// Now the child frame should have location, as well as the parent frame.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
parent, GURL(kOrigin1))
.content_setting);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
child, GURL(kOrigin2))
.content_setting);
// Revoking access from the parent should cause the child not to have access
// either.
GetPermissionControllerDelegate()->ResetPermission(
PermissionType::GEOLOCATION, GURL(kOrigin1), GURL(kOrigin1));
EXPECT_EQ(CONTENT_SETTING_ASK,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
parent, GURL(kOrigin1))
.content_setting);
EXPECT_EQ(CONTENT_SETTING_ASK,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
child, GURL(kOrigin2))
.content_setting);
// If the parent changes its policy, the child should be blocked.
RefreshPageAndSetHeaderPolicy(
&parent, blink::mojom::FeaturePolicyFeature::kGeolocation, {kOrigin1});
child = AddChildRFH(parent, kOrigin2);
EXPECT_EQ(CONTENT_SETTING_ASK,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
parent, GURL(kOrigin1))
.content_setting);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
child, GURL(kOrigin2))
.content_setting);
prompt_factory.reset();
}
TEST_F(PermissionManagerTest, SubscribeWithPermissionDelegation) {
const char* kOrigin1 = "https://example.com";
const char* kOrigin2 = "https://google.com";
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(features::kPermissionDelegation);
NavigateAndCommit(GURL(kOrigin1));
content::RenderFrameHost* parent = main_rfh();
content::RenderFrameHost* child = AddChildRFH(parent, kOrigin2);
int subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, child, GURL(kOrigin2),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
EXPECT_FALSE(callback_called());
// Location should be blocked for the child because it's not delegated.
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
child, GURL(kOrigin2))
.content_setting);
// Allow access for the top level origin.
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_ALLOW);
// The child's permission should still be block and no callback should be run.
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
child, GURL(kOrigin2))
.content_setting);
EXPECT_FALSE(callback_called());
// Enabling geolocation by FP should allow the child to request access also.
RefreshPageAndSetHeaderPolicy(
&parent, blink::mojom::FeaturePolicyFeature::kGeolocation,
{kOrigin1, kOrigin2});
child = AddChildRFH(parent, kOrigin2);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
child, GURL(kOrigin2))
.content_setting);
subscription_id =
GetPermissionControllerDelegate()->SubscribePermissionStatusChange(
PermissionType::GEOLOCATION, child, GURL(kOrigin2),
base::Bind(&PermissionManagerTest::OnPermissionChange,
base::Unretained(this)));
EXPECT_FALSE(callback_called());
// Blocking access to the parent should trigger the callback to be run for the
// child also.
GetHostContentSettingsMap()->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_TRUE(callback_called());
EXPECT_EQ(PermissionStatus::DENIED, callback_result());
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetPermissionControllerDelegate()
->GetPermissionStatusForFrame(ContentSettingsType::GEOLOCATION,
child, GURL(kOrigin2))
.content_setting);
GetPermissionControllerDelegate()->UnsubscribePermissionStatusChange(
subscription_id);
}
} // namespace permissions
|