OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/permissions/permission_context_base.h" | 5 #include "chrome/browser/permissions/permission_context_base.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | |
8 #include <string> | 9 #include <string> |
9 #include <utility> | 10 #include <utility> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/bind.h" | 13 #include "base/bind.h" |
13 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
16 #include "base/metrics/field_trial.h" | 17 #include "base/metrics/field_trial.h" |
18 #include "base/run_loop.h" | |
17 #include "base/test/histogram_tester.h" | 19 #include "base/test/histogram_tester.h" |
18 #include "base/test/mock_entropy_provider.h" | 20 #include "base/test/mock_entropy_provider.h" |
19 #include "base/test/scoped_feature_list.h" | 21 #include "base/test/scoped_feature_list.h" |
20 #include "build/build_config.h" | 22 #include "build/build_config.h" |
21 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 23 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
22 #include "chrome/browser/infobars/infobar_service.h" | 24 #include "chrome/browser/infobars/infobar_service.h" |
23 #include "chrome/browser/permissions/permission_decision_auto_blocker.h" | 25 #include "chrome/browser/permissions/permission_decision_auto_blocker.h" |
24 #include "chrome/browser/permissions/permission_queue_controller.h" | 26 #include "chrome/browser/permissions/permission_queue_controller.h" |
25 #include "chrome/browser/permissions/permission_request_id.h" | 27 #include "chrome/browser/permissions/permission_request_id.h" |
26 #include "chrome/browser/permissions/permission_util.h" | 28 #include "chrome/browser/permissions/permission_util.h" |
27 #include "chrome/common/chrome_features.h" | 29 #include "chrome/common/chrome_features.h" |
28 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 31 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
30 #include "chrome/test/base/testing_profile.h" | 32 #include "chrome/test/base/testing_profile.h" |
31 #include "components/content_settings/core/browser/host_content_settings_map.h" | 33 #include "components/content_settings/core/browser/host_content_settings_map.h" |
32 #include "components/content_settings/core/common/content_settings.h" | 34 #include "components/content_settings/core/common/content_settings.h" |
33 #include "components/content_settings/core/common/content_settings_types.h" | 35 #include "components/content_settings/core/common/content_settings_types.h" |
36 #include "components/safe_browsing_db/database_manager.h" | |
37 #include "components/safe_browsing_db/test_database_manager.h" | |
34 #include "components/variations/variations_associated_data.h" | 38 #include "components/variations/variations_associated_data.h" |
39 #include "content/public/browser/browser_thread.h" | |
35 #include "content/public/browser/permission_type.h" | 40 #include "content/public/browser/permission_type.h" |
36 #include "content/public/browser/render_frame_host.h" | 41 #include "content/public/browser/render_frame_host.h" |
37 #include "content/public/browser/web_contents.h" | 42 #include "content/public/browser/web_contents.h" |
38 #include "content/public/test/mock_render_process_host.h" | 43 #include "content/public/test/mock_render_process_host.h" |
39 #include "testing/gtest/include/gtest/gtest.h" | 44 #include "testing/gtest/include/gtest/gtest.h" |
40 | 45 |
41 #if !defined(OS_ANDROID) | 46 #if !defined(OS_ANDROID) |
42 #include "chrome/browser/permissions/permission_request_manager.h" | 47 #include "chrome/browser/permissions/permission_request_manager.h" |
43 #endif | 48 #endif |
44 | 49 |
45 const char* const kPermissionsKillSwitchFieldStudy = | 50 const char* const kPermissionsKillSwitchFieldStudy = |
46 PermissionContextBase::kPermissionsKillSwitchFieldStudy; | 51 PermissionContextBase::kPermissionsKillSwitchFieldStudy; |
47 const char* const kPermissionsKillSwitchBlockedValue = | 52 const char* const kPermissionsKillSwitchBlockedValue = |
48 PermissionContextBase::kPermissionsKillSwitchBlockedValue; | 53 PermissionContextBase::kPermissionsKillSwitchBlockedValue; |
49 const char kPermissionsKillSwitchTestGroup[] = "TestGroup"; | 54 const char kPermissionsKillSwitchTestGroup[] = "TestGroup"; |
50 const char* const kPromptGroupName = kPermissionsKillSwitchTestGroup; | 55 const char* const kPromptGroupName = kPermissionsKillSwitchTestGroup; |
51 const char kPromptTrialName[] = "PermissionPromptsUX"; | 56 const char kPromptTrialName[] = "PermissionPromptsUX"; |
52 | 57 |
58 class MockSafeBrowsingDatabaseManager | |
59 : public safe_browsing::TestSafeBrowsingDatabaseManager { | |
60 public: | |
61 explicit MockSafeBrowsingDatabaseManager(bool perform_callback) | |
62 : perform_callback_(perform_callback) {} | |
63 | |
64 bool CheckApiBlacklistUrl( | |
65 const GURL& url, | |
66 safe_browsing::SafeBrowsingDatabaseManager::Client* client) override { | |
67 if (perform_callback_) { | |
68 safe_browsing::ThreatMetadata metadata; | |
69 const auto& blacklisted_permissions = permissions_blacklist_.find(url); | |
70 if (blacklisted_permissions != permissions_blacklist_.end()) | |
71 metadata.api_permissions = blacklisted_permissions->second; | |
72 client->OnCheckApiBlacklistUrlResult(url, metadata); | |
73 } | |
74 // Returns false if scheme is HTTP/HTTPS and able to be checked. | |
75 return false; | |
76 } | |
77 | |
78 bool CancelApiCheck(Client* client) override { | |
79 DCHECK(!perform_callback_); | |
80 // Returns true when client check could be stopped. | |
81 return true; | |
82 } | |
83 | |
84 void BlacklistUrlPermissions(const GURL& url, | |
85 const std::set<std::string> permissions) { | |
86 permissions_blacklist_[url] = permissions; | |
87 } | |
88 | |
89 protected: | |
90 ~MockSafeBrowsingDatabaseManager() override {} | |
91 | |
92 private: | |
93 bool perform_callback_; | |
94 std::map<GURL, std::set<std::string>> permissions_blacklist_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); | |
97 }; | |
98 | |
53 class TestPermissionContext : public PermissionContextBase { | 99 class TestPermissionContext : public PermissionContextBase { |
54 public: | 100 public: |
55 TestPermissionContext(Profile* profile, | 101 TestPermissionContext(Profile* profile, |
56 const content::PermissionType permission_type, | 102 const content::PermissionType permission_type, |
57 const ContentSettingsType content_settings_type) | 103 const ContentSettingsType content_settings_type) |
58 : PermissionContextBase(profile, permission_type, content_settings_type), | 104 : PermissionContextBase(profile, permission_type, content_settings_type), |
59 tab_context_updated_(false) {} | 105 tab_context_updated_(false) {} |
60 | 106 |
61 ~TestPermissionContext() override {} | 107 ~TestPermissionContext() override {} |
62 | 108 |
63 #if defined(OS_ANDROID) | 109 #if defined(OS_ANDROID) |
64 PermissionQueueController* GetInfoBarController() { | 110 PermissionQueueController* GetInfoBarController() { |
65 return GetQueueController(); | 111 return GetQueueController(); |
66 } | 112 } |
67 #endif | 113 #endif |
68 | 114 |
69 const std::vector<ContentSetting>& decisions() const { return decisions_; } | 115 const std::vector<ContentSetting>& decisions() const { return decisions_; } |
70 | 116 |
71 bool tab_context_updated() const { return tab_context_updated_; } | 117 bool tab_context_updated() const { return tab_context_updated_; } |
72 | 118 |
119 // Once a decision for the requested permission has been made, run the | |
120 // callback. | |
73 void TrackPermissionDecision(ContentSetting content_setting) { | 121 void TrackPermissionDecision(ContentSetting content_setting) { |
74 decisions_.push_back(content_setting); | 122 decisions_.push_back(content_setting); |
123 if (quit_closure_) { | |
raymes
2017/01/10 02:57:15
Add a comment explaining why this might be a null
| |
124 quit_closure_.Run(); | |
125 quit_closure_.Reset(); | |
126 } | |
75 } | 127 } |
76 | 128 |
77 ContentSetting GetContentSettingFromMap(const GURL& url_a, | 129 ContentSetting GetContentSettingFromMap(const GURL& url_a, |
78 const GURL& url_b) { | 130 const GURL& url_b) { |
79 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); | 131 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); |
80 return map->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), | 132 return map->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), |
81 content_settings_type(), std::string()); | 133 content_settings_type(), std::string()); |
82 } | 134 } |
83 | 135 |
136 void RequestPermission(content::WebContents* web_contents, | |
137 const PermissionRequestID& id, | |
138 const GURL& requesting_frame, | |
139 bool user_gesture, | |
140 const BrowserPermissionCallback& callback) override { | |
141 base::RunLoop run_loop; | |
142 quit_closure_ = run_loop.QuitClosure(); | |
143 PermissionContextBase::RequestPermission(web_contents, id, requesting_frame, | |
144 true /* user_gesture */, callback); | |
145 run_loop.Run(); | |
146 } | |
147 | |
148 void DecidePermission(content::WebContents* web_contents, | |
149 const PermissionRequestID& id, | |
150 const GURL& requesting_origin, | |
151 const GURL& embedding_origin, | |
152 bool user_gesture, | |
153 const BrowserPermissionCallback& callback) override { | |
154 PermissionContextBase::DecidePermission(web_contents, id, requesting_origin, | |
155 embedding_origin, user_gesture, | |
156 callback); | |
157 if (respond_permission_) { | |
158 respond_permission_.Run(); | |
159 respond_permission_.Reset(); | |
160 } else if (quit_closure_) { | |
raymes
2017/01/10 02:57:15
Can we assume quit_closure_ is not null here?
meredithl
2017/01/10 03:40:46
I assume its safe to, as if decide_permission_ is
| |
161 // Stop the run loop from spinning indefinitely if no response callback | |
162 // has been set, as is the case with TestParallelRequests. | |
163 quit_closure_.Run(); | |
164 quit_closure_.Reset(); | |
165 } | |
166 } | |
167 | |
168 // Permission request will need to be responded to, so pass a callback to be | |
169 // run once the request has completed and the decision has been made. | |
170 void SetRespondPermissionCallback(base::Closure callback) { | |
171 respond_permission_ = callback; | |
172 } | |
173 | |
84 protected: | 174 protected: |
85 void UpdateTabContext(const PermissionRequestID& id, | 175 void UpdateTabContext(const PermissionRequestID& id, |
86 const GURL& requesting_origin, | 176 const GURL& requesting_origin, |
87 bool allowed) override { | 177 bool allowed) override { |
88 tab_context_updated_ = true; | 178 tab_context_updated_ = true; |
89 } | 179 } |
90 | 180 |
91 bool IsRestrictedToSecureOrigins() const override { | 181 bool IsRestrictedToSecureOrigins() const override { |
92 return false; | 182 return false; |
93 } | 183 } |
94 | 184 |
95 private: | 185 private: |
96 std::vector<ContentSetting> decisions_; | 186 std::vector<ContentSetting> decisions_; |
97 bool tab_context_updated_; | 187 bool tab_context_updated_; |
98 | 188 base::Closure quit_closure_; |
189 // Callback for responding to a permission once the request has been completed | |
190 // (valid URL, kill switch disabled, not blacklisted) | |
191 base::Closure respond_permission_; | |
99 DISALLOW_COPY_AND_ASSIGN(TestPermissionContext); | 192 DISALLOW_COPY_AND_ASSIGN(TestPermissionContext); |
100 }; | 193 }; |
101 | 194 |
102 class TestKillSwitchPermissionContext : public TestPermissionContext { | 195 class TestKillSwitchPermissionContext : public TestPermissionContext { |
103 public: | 196 public: |
104 TestKillSwitchPermissionContext( | 197 TestKillSwitchPermissionContext( |
105 Profile* profile, | 198 Profile* profile, |
106 const content::PermissionType permission_type, | 199 const content::PermissionType permission_type, |
107 const ContentSettingsType content_settings_type) | 200 const ContentSettingsType content_settings_type) |
108 : TestPermissionContext(profile, permission_type, content_settings_type), | 201 : TestPermissionContext(profile, permission_type, content_settings_type), |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 TestPermissionContext permission_context(profile(), permission, | 266 TestPermissionContext permission_context(profile(), permission, |
174 content_settings_type); | 267 content_settings_type); |
175 GURL url("https://www.google.com"); | 268 GURL url("https://www.google.com"); |
176 NavigateAndCommit(url); | 269 NavigateAndCommit(url); |
177 base::HistogramTester histograms; | 270 base::HistogramTester histograms; |
178 | 271 |
179 const PermissionRequestID id( | 272 const PermissionRequestID id( |
180 web_contents()->GetRenderProcessHost()->GetID(), | 273 web_contents()->GetRenderProcessHost()->GetID(), |
181 web_contents()->GetMainFrame()->GetRoutingID(), | 274 web_contents()->GetMainFrame()->GetRoutingID(), |
182 -1); | 275 -1); |
276 permission_context.SetRespondPermissionCallback( | |
277 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
278 base::Unretained(this), &permission_context, id, url, | |
279 persist, decision)); | |
183 permission_context.RequestPermission( | 280 permission_context.RequestPermission( |
184 web_contents(), | 281 web_contents(), |
185 id, url, true /* user_gesture */, | 282 id, url, true /* user_gesture */, |
186 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 283 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
187 base::Unretained(&permission_context))); | 284 base::Unretained(&permission_context))); |
188 | |
189 RespondToPermission(&permission_context, id, url, persist, decision); | |
190 ASSERT_EQ(1u, permission_context.decisions().size()); | 285 ASSERT_EQ(1u, permission_context.decisions().size()); |
191 EXPECT_EQ(decision, permission_context.decisions()[0]); | 286 EXPECT_EQ(decision, permission_context.decisions()[0]); |
192 EXPECT_TRUE(permission_context.tab_context_updated()); | 287 EXPECT_TRUE(permission_context.tab_context_updated()); |
193 | 288 |
194 std::string decision_string; | 289 std::string decision_string; |
195 if (decision == CONTENT_SETTING_ALLOW) | 290 if (decision == CONTENT_SETTING_ALLOW) |
196 decision_string = "Accepted"; | 291 decision_string = "Accepted"; |
197 else if (decision == CONTENT_SETTING_BLOCK) | 292 else if (decision == CONTENT_SETTING_BLOCK) |
198 decision_string = "Denied"; | 293 decision_string = "Denied"; |
199 else if (decision == CONTENT_SETTING_ASK) | 294 else if (decision == CONTENT_SETTING_ASK) |
(...skipping 30 matching lines...) Expand all Loading... | |
230 // from dismiss to block, and hence change the persisted content setting. | 325 // from dismiss to block, and hence change the persisted content setting. |
231 for (uint32_t i = 0; i < iterations; ++i) { | 326 for (uint32_t i = 0; i < iterations; ++i) { |
232 TestPermissionContext permission_context( | 327 TestPermissionContext permission_context( |
233 profile(), permission_type, content_settings_type); | 328 profile(), permission_type, content_settings_type); |
234 ContentSetting expected = | 329 ContentSetting expected = |
235 (i < (iterations - 1)) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; | 330 (i < (iterations - 1)) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; |
236 | 331 |
237 const PermissionRequestID id( | 332 const PermissionRequestID id( |
238 web_contents()->GetRenderProcessHost()->GetID(), | 333 web_contents()->GetRenderProcessHost()->GetID(), |
239 web_contents()->GetMainFrame()->GetRoutingID(), i); | 334 web_contents()->GetMainFrame()->GetRoutingID(), i); |
335 | |
336 permission_context.SetRespondPermissionCallback( | |
337 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
338 base::Unretained(this), &permission_context, id, url, | |
339 false, CONTENT_SETTING_ASK)); | |
340 | |
240 permission_context.RequestPermission( | 341 permission_context.RequestPermission( |
241 web_contents(), id, url, true /* user_gesture */, | 342 web_contents(), id, url, true /* user_gesture */, |
242 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 343 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
243 base::Unretained(&permission_context))); | 344 base::Unretained(&permission_context))); |
244 | |
245 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
246 CONTENT_SETTING_ASK); | |
247 histograms.ExpectTotalCount( | 345 histograms.ExpectTotalCount( |
248 "Permissions.Prompt.Dismissed.PriorDismissCount." + | 346 "Permissions.Prompt.Dismissed.PriorDismissCount." + |
249 PermissionUtil::GetPermissionString(permission_type), | 347 PermissionUtil::GetPermissionString(permission_type), |
250 i + 1); | 348 i + 1); |
251 histograms.ExpectBucketCount( | 349 histograms.ExpectBucketCount( |
252 "Permissions.Prompt.Dismissed.PriorDismissCount." + | 350 "Permissions.Prompt.Dismissed.PriorDismissCount." + |
253 PermissionUtil::GetPermissionString(permission_type), | 351 PermissionUtil::GetPermissionString(permission_type), |
254 i, 1); | 352 i, 1); |
255 | 353 |
256 ASSERT_EQ(1u, permission_context.decisions().size()); | 354 ASSERT_EQ(1u, permission_context.decisions().size()); |
(...skipping 17 matching lines...) Expand all Loading... | |
274 | 372 |
275 // First, ensure that > 3 dismissals behaves correctly. | 373 // First, ensure that > 3 dismissals behaves correctly. |
276 for (uint32_t i = 0; i < 4; ++i) { | 374 for (uint32_t i = 0; i < 4; ++i) { |
277 TestPermissionContext permission_context( | 375 TestPermissionContext permission_context( |
278 profile(), content::PermissionType::GEOLOCATION, | 376 profile(), content::PermissionType::GEOLOCATION, |
279 CONTENT_SETTINGS_TYPE_GEOLOCATION); | 377 CONTENT_SETTINGS_TYPE_GEOLOCATION); |
280 | 378 |
281 const PermissionRequestID id( | 379 const PermissionRequestID id( |
282 web_contents()->GetRenderProcessHost()->GetID(), | 380 web_contents()->GetRenderProcessHost()->GetID(), |
283 web_contents()->GetMainFrame()->GetRoutingID(), i); | 381 web_contents()->GetMainFrame()->GetRoutingID(), i); |
382 | |
383 permission_context.SetRespondPermissionCallback( | |
384 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
385 base::Unretained(this), &permission_context, id, url, | |
386 false, CONTENT_SETTING_ASK)); | |
284 permission_context.RequestPermission( | 387 permission_context.RequestPermission( |
285 web_contents(), id, url, true /* user_gesture */, | 388 web_contents(), id, url, true /* user_gesture */, |
286 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 389 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
287 base::Unretained(&permission_context))); | 390 base::Unretained(&permission_context))); |
288 | |
289 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
290 CONTENT_SETTING_ASK); | |
291 histograms.ExpectTotalCount( | 391 histograms.ExpectTotalCount( |
292 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", | 392 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", |
293 i + 1); | 393 i + 1); |
294 histograms.ExpectBucketCount( | 394 histograms.ExpectBucketCount( |
295 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", i, 1); | 395 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", i, 1); |
296 ASSERT_EQ(1u, permission_context.decisions().size()); | 396 ASSERT_EQ(1u, permission_context.decisions().size()); |
297 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); | 397 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); |
298 EXPECT_TRUE(permission_context.tab_context_updated()); | 398 EXPECT_TRUE(permission_context.tab_context_updated()); |
299 EXPECT_EQ(CONTENT_SETTING_ASK, | 399 EXPECT_EQ(CONTENT_SETTING_ASK, |
300 permission_context.GetContentSettingFromMap(url, url)); | 400 permission_context.GetContentSettingFromMap(url, url)); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 for (uint32_t i = 0; i < 5; ++i) { | 458 for (uint32_t i = 0; i < 5; ++i) { |
359 TestPermissionContext permission_context( | 459 TestPermissionContext permission_context( |
360 profile(), content::PermissionType::MIDI_SYSEX, | 460 profile(), content::PermissionType::MIDI_SYSEX, |
361 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | 461 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
362 | 462 |
363 ContentSetting expected = | 463 ContentSetting expected = |
364 (i < 4) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; | 464 (i < 4) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; |
365 const PermissionRequestID id( | 465 const PermissionRequestID id( |
366 web_contents()->GetRenderProcessHost()->GetID(), | 466 web_contents()->GetRenderProcessHost()->GetID(), |
367 web_contents()->GetMainFrame()->GetRoutingID(), i); | 467 web_contents()->GetMainFrame()->GetRoutingID(), i); |
468 permission_context.SetRespondPermissionCallback( | |
469 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
470 base::Unretained(this), &permission_context, id, url, | |
471 false, CONTENT_SETTING_ASK)); | |
368 permission_context.RequestPermission( | 472 permission_context.RequestPermission( |
369 web_contents(), id, url, true /* user_gesture */, | 473 web_contents(), id, url, true /* user_gesture */, |
370 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 474 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
371 base::Unretained(&permission_context))); | 475 base::Unretained(&permission_context))); |
372 | 476 |
373 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
374 CONTENT_SETTING_ASK); | |
375 EXPECT_EQ(1u, permission_context.decisions().size()); | 477 EXPECT_EQ(1u, permission_context.decisions().size()); |
376 ASSERT_EQ(expected, permission_context.decisions()[0]); | 478 ASSERT_EQ(expected, permission_context.decisions()[0]); |
377 EXPECT_TRUE(permission_context.tab_context_updated()); | 479 EXPECT_TRUE(permission_context.tab_context_updated()); |
378 EXPECT_EQ(expected, | 480 EXPECT_EQ(expected, |
379 permission_context.GetContentSettingFromMap(url, url)); | 481 permission_context.GetContentSettingFromMap(url, url)); |
380 | 482 |
381 histograms.ExpectTotalCount( | 483 histograms.ExpectTotalCount( |
382 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i + 1); | 484 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i + 1); |
383 histograms.ExpectBucketCount( | 485 histograms.ExpectBucketCount( |
384 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i, 1); | 486 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i, 1); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
424 ContentSetting expected_default) { | 526 ContentSetting expected_default) { |
425 TestPermissionContext permission_context(profile(), permission_type, | 527 TestPermissionContext permission_context(profile(), permission_type, |
426 content_settings_type); | 528 content_settings_type); |
427 GURL url("https://www.google.com"); | 529 GURL url("https://www.google.com"); |
428 NavigateAndCommit(url); | 530 NavigateAndCommit(url); |
429 | 531 |
430 const PermissionRequestID id( | 532 const PermissionRequestID id( |
431 web_contents()->GetRenderProcessHost()->GetID(), | 533 web_contents()->GetRenderProcessHost()->GetID(), |
432 web_contents()->GetMainFrame()->GetRoutingID(), | 534 web_contents()->GetMainFrame()->GetRoutingID(), |
433 -1); | 535 -1); |
536 permission_context.SetRespondPermissionCallback( | |
537 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
538 base::Unretained(this), &permission_context, id, url, true, | |
539 CONTENT_SETTING_ALLOW)); | |
540 | |
434 permission_context.RequestPermission( | 541 permission_context.RequestPermission( |
435 web_contents(), | 542 web_contents(), |
436 id, url, true /* user_gesture */, | 543 id, url, true /* user_gesture */, |
437 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 544 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
438 base::Unretained(&permission_context))); | 545 base::Unretained(&permission_context))); |
439 | 546 |
440 RespondToPermission(&permission_context, id, url, true, /* persist */ | |
441 CONTENT_SETTING_ALLOW); | |
442 ASSERT_EQ(1u, permission_context.decisions().size()); | 547 ASSERT_EQ(1u, permission_context.decisions().size()); |
443 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); | 548 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); |
444 EXPECT_TRUE(permission_context.tab_context_updated()); | 549 EXPECT_TRUE(permission_context.tab_context_updated()); |
445 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 550 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
446 permission_context.GetContentSettingFromMap(url, url)); | 551 permission_context.GetContentSettingFromMap(url, url)); |
447 | 552 |
448 // Try to reset permission. | 553 // Try to reset permission. |
449 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); | 554 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); |
450 ContentSetting setting_after_reset = | 555 ContentSetting setting_after_reset = |
451 permission_context.GetContentSettingFromMap(url, url); | 556 permission_context.GetContentSettingFromMap(url, url); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
483 GURL url("http://www.google.com"); | 588 GURL url("http://www.google.com"); |
484 NavigateAndCommit(url); | 589 NavigateAndCommit(url); |
485 | 590 |
486 const PermissionRequestID id0( | 591 const PermissionRequestID id0( |
487 web_contents()->GetRenderProcessHost()->GetID(), | 592 web_contents()->GetRenderProcessHost()->GetID(), |
488 web_contents()->GetMainFrame()->GetRoutingID(), 0); | 593 web_contents()->GetMainFrame()->GetRoutingID(), 0); |
489 const PermissionRequestID id1( | 594 const PermissionRequestID id1( |
490 web_contents()->GetRenderProcessHost()->GetID(), | 595 web_contents()->GetRenderProcessHost()->GetID(), |
491 web_contents()->GetMainFrame()->GetRoutingID(), 1); | 596 web_contents()->GetMainFrame()->GetRoutingID(), 1); |
492 | 597 |
598 bool persist = (response == CONTENT_SETTING_ALLOW || | |
599 response == CONTENT_SETTING_BLOCK); | |
600 | |
601 // Request a permission without setting the callback to DecidePermission. | |
493 permission_context.RequestPermission( | 602 permission_context.RequestPermission( |
494 web_contents(), id0, url, true /* user_gesture */, | 603 web_contents(), id0, url, true /* user_gesture */, |
495 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 604 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
496 base::Unretained(&permission_context))); | 605 base::Unretained(&permission_context))); |
606 | |
607 EXPECT_EQ(0u, permission_context.decisions().size()); | |
608 | |
609 // Set the callback, and make a second permission request. | |
610 permission_context.SetRespondPermissionCallback( | |
611 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
612 base::Unretained(this), &permission_context, id0, url, | |
613 persist, response)); | |
497 permission_context.RequestPermission( | 614 permission_context.RequestPermission( |
498 web_contents(), id1, url, true /* user_gesture */, | 615 web_contents(), id1, url, true /* user_gesture */, |
499 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 616 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
500 base::Unretained(&permission_context))); | 617 base::Unretained(&permission_context))); |
501 | 618 |
502 EXPECT_EQ(0u, permission_context.decisions().size()); | |
503 | |
504 bool persist = (response == CONTENT_SETTING_ALLOW || | |
505 response == CONTENT_SETTING_BLOCK); | |
506 RespondToPermission(&permission_context, id0, url, persist, response); | |
507 | |
508 ASSERT_EQ(2u, permission_context.decisions().size()); | 619 ASSERT_EQ(2u, permission_context.decisions().size()); |
509 EXPECT_EQ(response, permission_context.decisions()[0]); | 620 EXPECT_EQ(response, permission_context.decisions()[0]); |
510 EXPECT_EQ(response, permission_context.decisions()[1]); | 621 EXPECT_EQ(response, permission_context.decisions()[1]); |
511 EXPECT_TRUE(permission_context.tab_context_updated()); | 622 EXPECT_TRUE(permission_context.tab_context_updated()); |
512 | 623 |
513 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); | 624 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); |
514 } | 625 } |
515 | 626 |
627 void TestPermissionsBlacklisting( | |
628 content::PermissionType permission_type, | |
629 ContentSettingsType content_settings_type, | |
630 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, | |
631 const GURL& url, | |
632 int timeout, | |
633 ContentSetting response) { | |
634 NavigateAndCommit(url); | |
635 base::test::ScopedFeatureList scoped_feature_list; | |
636 scoped_feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); | |
637 TestPermissionContext permission_context(profile(), permission_type, | |
638 content_settings_type); | |
639 permission_context.SetSafeBrowsingDatabaseManagerAndTimeoutForTest( | |
640 db_manager, timeout); | |
641 const PermissionRequestID id( | |
642 web_contents()->GetRenderProcessHost()->GetID(), | |
643 web_contents()->GetMainFrame()->GetRoutingID(), -1); | |
644 permission_context.SetRespondPermissionCallback(base::Bind( | |
645 &PermissionContextBaseTests::RespondToPermission, | |
646 base::Unretained(this), &permission_context, id, url, false, response)); | |
647 permission_context.RequestPermission( | |
648 web_contents(), id, url, true /* user_gesture */, | |
649 base::Bind(&TestPermissionContext::TrackPermissionDecision, | |
650 base::Unretained(&permission_context))); | |
651 | |
652 ASSERT_EQ(1u, permission_context.decisions().size()); | |
653 EXPECT_EQ(response, permission_context.decisions()[0]); | |
654 } | |
655 | |
516 private: | 656 private: |
517 // ChromeRenderViewHostTestHarness: | 657 // ChromeRenderViewHostTestHarness: |
518 void SetUp() override { | 658 void SetUp() override { |
519 ChromeRenderViewHostTestHarness::SetUp(); | 659 ChromeRenderViewHostTestHarness::SetUp(); |
520 #if defined(OS_ANDROID) | 660 #if defined(OS_ANDROID) |
521 InfoBarService::CreateForWebContents(web_contents()); | 661 InfoBarService::CreateForWebContents(web_contents()); |
522 #else | 662 #else |
523 PermissionRequestManager::CreateForWebContents(web_contents()); | 663 PermissionRequestManager::CreateForWebContents(web_contents()); |
524 #endif | 664 #endif |
525 } | 665 } |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
660 TestParallelRequests(CONTENT_SETTING_ALLOW); | 800 TestParallelRequests(CONTENT_SETTING_ALLOW); |
661 } | 801 } |
662 | 802 |
663 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { | 803 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { |
664 TestParallelRequests(CONTENT_SETTING_BLOCK); | 804 TestParallelRequests(CONTENT_SETTING_BLOCK); |
665 } | 805 } |
666 | 806 |
667 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { | 807 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { |
668 TestParallelRequests(CONTENT_SETTING_ASK); | 808 TestParallelRequests(CONTENT_SETTING_ASK); |
669 } | 809 } |
810 | |
811 // Tests a blacklisted (URL, permission) pair has had its permission request | |
812 // blocked. | |
813 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingBlocked) { | |
814 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
815 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | |
816 const GURL url("https://www.example.com"); | |
817 std::set<std::string> blacklisted_permissions{ | |
818 PermissionUtil::GetPermissionString( | |
819 content::PermissionType::GEOLOCATION)}; | |
820 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
821 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, | |
822 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, | |
823 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK); | |
824 } | |
825 | |
826 // Tests that a URL with a blacklisted permission is permitted to request a | |
827 // non-blacklisted permission. | |
828 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { | |
829 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
830 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | |
831 const GURL url("https://www.example.com"); | |
832 std::set<std::string> blacklisted_permissions{ | |
833 PermissionUtil::GetPermissionString( | |
834 content::PermissionType::GEOLOCATION)}; | |
835 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
836 TestPermissionsBlacklisting( | |
837 content::PermissionType::GEOLOCATION, CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
838 db_manager, url, 2000 /* timeout in ms */, CONTENT_SETTING_BLOCK); | |
839 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS, | |
840 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, | |
841 url, 2000 /* timeout in ms */, | |
842 CONTENT_SETTING_ASK); | |
843 } | |
844 | |
845 // Tests that a URL with a blacklisted permisison is permitted to request that | |
846 // permission if Safe Browsing has timed out. | |
847 TEST_F(PermissionContextBaseTests, TestSafeBrowsingTimeout) { | |
848 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
849 new MockSafeBrowsingDatabaseManager(false /* perform_callback */); | |
850 const GURL url("https://www.example.com"); | |
851 std::set<std::string> blacklisted_permissions{ | |
852 PermissionUtil::GetPermissionString( | |
853 content::PermissionType::GEOLOCATION)}; | |
854 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
855 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, | |
856 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, | |
857 url, 0 /* timeout in ms */, CONTENT_SETTING_ASK); | |
858 } | |
OLD | NEW |