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 // Null check required here as the quit_closure_ can also be run and reset |
| 124 // first from within DecidePermission. |
| 125 if (quit_closure_) { |
| 126 quit_closure_.Run(); |
| 127 quit_closure_.Reset(); |
| 128 } |
75 } | 129 } |
76 | 130 |
77 ContentSetting GetContentSettingFromMap(const GURL& url_a, | 131 ContentSetting GetContentSettingFromMap(const GURL& url_a, |
78 const GURL& url_b) { | 132 const GURL& url_b) { |
79 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); | 133 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); |
80 return map->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), | 134 return map->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), |
81 content_settings_type(), std::string()); | 135 content_settings_type(), std::string()); |
82 } | 136 } |
83 | 137 |
| 138 void RequestPermission(content::WebContents* web_contents, |
| 139 const PermissionRequestID& id, |
| 140 const GURL& requesting_frame, |
| 141 bool user_gesture, |
| 142 const BrowserPermissionCallback& callback) override { |
| 143 base::RunLoop run_loop; |
| 144 quit_closure_ = run_loop.QuitClosure(); |
| 145 PermissionContextBase::RequestPermission(web_contents, id, requesting_frame, |
| 146 true /* user_gesture */, callback); |
| 147 run_loop.Run(); |
| 148 } |
| 149 |
| 150 void DecidePermission(content::WebContents* web_contents, |
| 151 const PermissionRequestID& id, |
| 152 const GURL& requesting_origin, |
| 153 const GURL& embedding_origin, |
| 154 bool user_gesture, |
| 155 const BrowserPermissionCallback& callback) override { |
| 156 PermissionContextBase::DecidePermission(web_contents, id, requesting_origin, |
| 157 embedding_origin, user_gesture, |
| 158 callback); |
| 159 if (respond_permission_) { |
| 160 respond_permission_.Run(); |
| 161 respond_permission_.Reset(); |
| 162 } else { |
| 163 // Stop the run loop from spinning indefinitely if no response callback |
| 164 // has been set, as is the case with TestParallelRequests. |
| 165 quit_closure_.Run(); |
| 166 quit_closure_.Reset(); |
| 167 } |
| 168 } |
| 169 |
| 170 // Permission request will need to be responded to, so pass a callback to be |
| 171 // run once the request has completed and the decision has been made. |
| 172 void SetRespondPermissionCallback(base::Closure callback) { |
| 173 respond_permission_ = callback; |
| 174 } |
| 175 |
84 protected: | 176 protected: |
85 void UpdateTabContext(const PermissionRequestID& id, | 177 void UpdateTabContext(const PermissionRequestID& id, |
86 const GURL& requesting_origin, | 178 const GURL& requesting_origin, |
87 bool allowed) override { | 179 bool allowed) override { |
88 tab_context_updated_ = true; | 180 tab_context_updated_ = true; |
89 } | 181 } |
90 | 182 |
91 bool IsRestrictedToSecureOrigins() const override { | 183 bool IsRestrictedToSecureOrigins() const override { |
92 return false; | 184 return false; |
93 } | 185 } |
94 | 186 |
95 private: | 187 private: |
96 std::vector<ContentSetting> decisions_; | 188 std::vector<ContentSetting> decisions_; |
97 bool tab_context_updated_; | 189 bool tab_context_updated_; |
98 | 190 base::Closure quit_closure_; |
| 191 // Callback for responding to a permission once the request has been completed |
| 192 // (valid URL, kill switch disabled, not blacklisted) |
| 193 base::Closure respond_permission_; |
99 DISALLOW_COPY_AND_ASSIGN(TestPermissionContext); | 194 DISALLOW_COPY_AND_ASSIGN(TestPermissionContext); |
100 }; | 195 }; |
101 | 196 |
102 class TestKillSwitchPermissionContext : public TestPermissionContext { | 197 class TestKillSwitchPermissionContext : public TestPermissionContext { |
103 public: | 198 public: |
104 TestKillSwitchPermissionContext( | 199 TestKillSwitchPermissionContext( |
105 Profile* profile, | 200 Profile* profile, |
106 const content::PermissionType permission_type, | 201 const content::PermissionType permission_type, |
107 const ContentSettingsType content_settings_type) | 202 const ContentSettingsType content_settings_type) |
108 : TestPermissionContext(profile, permission_type, content_settings_type), | 203 : 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, | 268 TestPermissionContext permission_context(profile(), permission, |
174 content_settings_type); | 269 content_settings_type); |
175 GURL url("https://www.google.com"); | 270 GURL url("https://www.google.com"); |
176 NavigateAndCommit(url); | 271 NavigateAndCommit(url); |
177 base::HistogramTester histograms; | 272 base::HistogramTester histograms; |
178 | 273 |
179 const PermissionRequestID id( | 274 const PermissionRequestID id( |
180 web_contents()->GetRenderProcessHost()->GetID(), | 275 web_contents()->GetRenderProcessHost()->GetID(), |
181 web_contents()->GetMainFrame()->GetRoutingID(), | 276 web_contents()->GetMainFrame()->GetRoutingID(), |
182 -1); | 277 -1); |
| 278 permission_context.SetRespondPermissionCallback( |
| 279 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 280 base::Unretained(this), &permission_context, id, url, |
| 281 persist, decision)); |
183 permission_context.RequestPermission( | 282 permission_context.RequestPermission( |
184 web_contents(), | 283 web_contents(), |
185 id, url, true /* user_gesture */, | 284 id, url, true /* user_gesture */, |
186 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 285 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
187 base::Unretained(&permission_context))); | 286 base::Unretained(&permission_context))); |
188 | |
189 RespondToPermission(&permission_context, id, url, persist, decision); | |
190 ASSERT_EQ(1u, permission_context.decisions().size()); | 287 ASSERT_EQ(1u, permission_context.decisions().size()); |
191 EXPECT_EQ(decision, permission_context.decisions()[0]); | 288 EXPECT_EQ(decision, permission_context.decisions()[0]); |
192 EXPECT_TRUE(permission_context.tab_context_updated()); | 289 EXPECT_TRUE(permission_context.tab_context_updated()); |
193 | 290 |
194 std::string decision_string; | 291 std::string decision_string; |
195 if (decision == CONTENT_SETTING_ALLOW) | 292 if (decision == CONTENT_SETTING_ALLOW) |
196 decision_string = "Accepted"; | 293 decision_string = "Accepted"; |
197 else if (decision == CONTENT_SETTING_BLOCK) | 294 else if (decision == CONTENT_SETTING_BLOCK) |
198 decision_string = "Denied"; | 295 decision_string = "Denied"; |
199 else if (decision == CONTENT_SETTING_ASK) | 296 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. | 327 // from dismiss to block, and hence change the persisted content setting. |
231 for (uint32_t i = 0; i < iterations; ++i) { | 328 for (uint32_t i = 0; i < iterations; ++i) { |
232 TestPermissionContext permission_context( | 329 TestPermissionContext permission_context( |
233 profile(), permission_type, content_settings_type); | 330 profile(), permission_type, content_settings_type); |
234 ContentSetting expected = | 331 ContentSetting expected = |
235 (i < (iterations - 1)) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; | 332 (i < (iterations - 1)) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; |
236 | 333 |
237 const PermissionRequestID id( | 334 const PermissionRequestID id( |
238 web_contents()->GetRenderProcessHost()->GetID(), | 335 web_contents()->GetRenderProcessHost()->GetID(), |
239 web_contents()->GetMainFrame()->GetRoutingID(), i); | 336 web_contents()->GetMainFrame()->GetRoutingID(), i); |
| 337 |
| 338 permission_context.SetRespondPermissionCallback( |
| 339 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 340 base::Unretained(this), &permission_context, id, url, |
| 341 false, CONTENT_SETTING_ASK)); |
| 342 |
240 permission_context.RequestPermission( | 343 permission_context.RequestPermission( |
241 web_contents(), id, url, true /* user_gesture */, | 344 web_contents(), id, url, true /* user_gesture */, |
242 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 345 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
243 base::Unretained(&permission_context))); | 346 base::Unretained(&permission_context))); |
244 | |
245 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
246 CONTENT_SETTING_ASK); | |
247 histograms.ExpectTotalCount( | 347 histograms.ExpectTotalCount( |
248 "Permissions.Prompt.Dismissed.PriorDismissCount." + | 348 "Permissions.Prompt.Dismissed.PriorDismissCount." + |
249 PermissionUtil::GetPermissionString(permission_type), | 349 PermissionUtil::GetPermissionString(permission_type), |
250 i + 1); | 350 i + 1); |
251 histograms.ExpectBucketCount( | 351 histograms.ExpectBucketCount( |
252 "Permissions.Prompt.Dismissed.PriorDismissCount." + | 352 "Permissions.Prompt.Dismissed.PriorDismissCount." + |
253 PermissionUtil::GetPermissionString(permission_type), | 353 PermissionUtil::GetPermissionString(permission_type), |
254 i, 1); | 354 i, 1); |
255 | 355 |
256 ASSERT_EQ(1u, permission_context.decisions().size()); | 356 ASSERT_EQ(1u, permission_context.decisions().size()); |
(...skipping 17 matching lines...) Expand all Loading... |
274 | 374 |
275 // First, ensure that > 3 dismissals behaves correctly. | 375 // First, ensure that > 3 dismissals behaves correctly. |
276 for (uint32_t i = 0; i < 4; ++i) { | 376 for (uint32_t i = 0; i < 4; ++i) { |
277 TestPermissionContext permission_context( | 377 TestPermissionContext permission_context( |
278 profile(), content::PermissionType::GEOLOCATION, | 378 profile(), content::PermissionType::GEOLOCATION, |
279 CONTENT_SETTINGS_TYPE_GEOLOCATION); | 379 CONTENT_SETTINGS_TYPE_GEOLOCATION); |
280 | 380 |
281 const PermissionRequestID id( | 381 const PermissionRequestID id( |
282 web_contents()->GetRenderProcessHost()->GetID(), | 382 web_contents()->GetRenderProcessHost()->GetID(), |
283 web_contents()->GetMainFrame()->GetRoutingID(), i); | 383 web_contents()->GetMainFrame()->GetRoutingID(), i); |
| 384 |
| 385 permission_context.SetRespondPermissionCallback( |
| 386 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 387 base::Unretained(this), &permission_context, id, url, |
| 388 false, CONTENT_SETTING_ASK)); |
284 permission_context.RequestPermission( | 389 permission_context.RequestPermission( |
285 web_contents(), id, url, true /* user_gesture */, | 390 web_contents(), id, url, true /* user_gesture */, |
286 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 391 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
287 base::Unretained(&permission_context))); | 392 base::Unretained(&permission_context))); |
288 | |
289 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
290 CONTENT_SETTING_ASK); | |
291 histograms.ExpectTotalCount( | 393 histograms.ExpectTotalCount( |
292 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", | 394 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", |
293 i + 1); | 395 i + 1); |
294 histograms.ExpectBucketCount( | 396 histograms.ExpectBucketCount( |
295 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", i, 1); | 397 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", i, 1); |
296 ASSERT_EQ(1u, permission_context.decisions().size()); | 398 ASSERT_EQ(1u, permission_context.decisions().size()); |
297 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); | 399 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); |
298 EXPECT_TRUE(permission_context.tab_context_updated()); | 400 EXPECT_TRUE(permission_context.tab_context_updated()); |
299 EXPECT_EQ(CONTENT_SETTING_ASK, | 401 EXPECT_EQ(CONTENT_SETTING_ASK, |
300 permission_context.GetContentSettingFromMap(url, url)); | 402 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) { | 460 for (uint32_t i = 0; i < 5; ++i) { |
359 TestPermissionContext permission_context( | 461 TestPermissionContext permission_context( |
360 profile(), content::PermissionType::MIDI_SYSEX, | 462 profile(), content::PermissionType::MIDI_SYSEX, |
361 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | 463 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
362 | 464 |
363 ContentSetting expected = | 465 ContentSetting expected = |
364 (i < 4) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; | 466 (i < 4) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; |
365 const PermissionRequestID id( | 467 const PermissionRequestID id( |
366 web_contents()->GetRenderProcessHost()->GetID(), | 468 web_contents()->GetRenderProcessHost()->GetID(), |
367 web_contents()->GetMainFrame()->GetRoutingID(), i); | 469 web_contents()->GetMainFrame()->GetRoutingID(), i); |
| 470 permission_context.SetRespondPermissionCallback( |
| 471 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 472 base::Unretained(this), &permission_context, id, url, |
| 473 false, CONTENT_SETTING_ASK)); |
368 permission_context.RequestPermission( | 474 permission_context.RequestPermission( |
369 web_contents(), id, url, true /* user_gesture */, | 475 web_contents(), id, url, true /* user_gesture */, |
370 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 476 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
371 base::Unretained(&permission_context))); | 477 base::Unretained(&permission_context))); |
372 | 478 |
373 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
374 CONTENT_SETTING_ASK); | |
375 EXPECT_EQ(1u, permission_context.decisions().size()); | 479 EXPECT_EQ(1u, permission_context.decisions().size()); |
376 ASSERT_EQ(expected, permission_context.decisions()[0]); | 480 ASSERT_EQ(expected, permission_context.decisions()[0]); |
377 EXPECT_TRUE(permission_context.tab_context_updated()); | 481 EXPECT_TRUE(permission_context.tab_context_updated()); |
378 EXPECT_EQ(expected, | 482 EXPECT_EQ(expected, |
379 permission_context.GetContentSettingFromMap(url, url)); | 483 permission_context.GetContentSettingFromMap(url, url)); |
380 | 484 |
381 histograms.ExpectTotalCount( | 485 histograms.ExpectTotalCount( |
382 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i + 1); | 486 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i + 1); |
383 histograms.ExpectBucketCount( | 487 histograms.ExpectBucketCount( |
384 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i, 1); | 488 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i, 1); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 ContentSetting expected_default) { | 528 ContentSetting expected_default) { |
425 TestPermissionContext permission_context(profile(), permission_type, | 529 TestPermissionContext permission_context(profile(), permission_type, |
426 content_settings_type); | 530 content_settings_type); |
427 GURL url("https://www.google.com"); | 531 GURL url("https://www.google.com"); |
428 NavigateAndCommit(url); | 532 NavigateAndCommit(url); |
429 | 533 |
430 const PermissionRequestID id( | 534 const PermissionRequestID id( |
431 web_contents()->GetRenderProcessHost()->GetID(), | 535 web_contents()->GetRenderProcessHost()->GetID(), |
432 web_contents()->GetMainFrame()->GetRoutingID(), | 536 web_contents()->GetMainFrame()->GetRoutingID(), |
433 -1); | 537 -1); |
| 538 permission_context.SetRespondPermissionCallback( |
| 539 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 540 base::Unretained(this), &permission_context, id, url, true, |
| 541 CONTENT_SETTING_ALLOW)); |
| 542 |
434 permission_context.RequestPermission( | 543 permission_context.RequestPermission( |
435 web_contents(), | 544 web_contents(), |
436 id, url, true /* user_gesture */, | 545 id, url, true /* user_gesture */, |
437 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 546 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
438 base::Unretained(&permission_context))); | 547 base::Unretained(&permission_context))); |
439 | 548 |
440 RespondToPermission(&permission_context, id, url, true, /* persist */ | |
441 CONTENT_SETTING_ALLOW); | |
442 ASSERT_EQ(1u, permission_context.decisions().size()); | 549 ASSERT_EQ(1u, permission_context.decisions().size()); |
443 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); | 550 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); |
444 EXPECT_TRUE(permission_context.tab_context_updated()); | 551 EXPECT_TRUE(permission_context.tab_context_updated()); |
445 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 552 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
446 permission_context.GetContentSettingFromMap(url, url)); | 553 permission_context.GetContentSettingFromMap(url, url)); |
447 | 554 |
448 // Try to reset permission. | 555 // Try to reset permission. |
449 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); | 556 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); |
450 ContentSetting setting_after_reset = | 557 ContentSetting setting_after_reset = |
451 permission_context.GetContentSettingFromMap(url, url); | 558 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"); | 590 GURL url("http://www.google.com"); |
484 NavigateAndCommit(url); | 591 NavigateAndCommit(url); |
485 | 592 |
486 const PermissionRequestID id0( | 593 const PermissionRequestID id0( |
487 web_contents()->GetRenderProcessHost()->GetID(), | 594 web_contents()->GetRenderProcessHost()->GetID(), |
488 web_contents()->GetMainFrame()->GetRoutingID(), 0); | 595 web_contents()->GetMainFrame()->GetRoutingID(), 0); |
489 const PermissionRequestID id1( | 596 const PermissionRequestID id1( |
490 web_contents()->GetRenderProcessHost()->GetID(), | 597 web_contents()->GetRenderProcessHost()->GetID(), |
491 web_contents()->GetMainFrame()->GetRoutingID(), 1); | 598 web_contents()->GetMainFrame()->GetRoutingID(), 1); |
492 | 599 |
| 600 bool persist = (response == CONTENT_SETTING_ALLOW || |
| 601 response == CONTENT_SETTING_BLOCK); |
| 602 |
| 603 // Request a permission without setting the callback to DecidePermission. |
493 permission_context.RequestPermission( | 604 permission_context.RequestPermission( |
494 web_contents(), id0, url, true /* user_gesture */, | 605 web_contents(), id0, url, true /* user_gesture */, |
495 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 606 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
496 base::Unretained(&permission_context))); | 607 base::Unretained(&permission_context))); |
| 608 |
| 609 EXPECT_EQ(0u, permission_context.decisions().size()); |
| 610 |
| 611 // Set the callback, and make a second permission request. |
| 612 permission_context.SetRespondPermissionCallback( |
| 613 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 614 base::Unretained(this), &permission_context, id0, url, |
| 615 persist, response)); |
497 permission_context.RequestPermission( | 616 permission_context.RequestPermission( |
498 web_contents(), id1, url, true /* user_gesture */, | 617 web_contents(), id1, url, true /* user_gesture */, |
499 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 618 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
500 base::Unretained(&permission_context))); | 619 base::Unretained(&permission_context))); |
501 | 620 |
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()); | 621 ASSERT_EQ(2u, permission_context.decisions().size()); |
509 EXPECT_EQ(response, permission_context.decisions()[0]); | 622 EXPECT_EQ(response, permission_context.decisions()[0]); |
510 EXPECT_EQ(response, permission_context.decisions()[1]); | 623 EXPECT_EQ(response, permission_context.decisions()[1]); |
511 EXPECT_TRUE(permission_context.tab_context_updated()); | 624 EXPECT_TRUE(permission_context.tab_context_updated()); |
512 | 625 |
513 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); | 626 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); |
514 } | 627 } |
515 | 628 |
| 629 void TestPermissionsBlacklisting( |
| 630 content::PermissionType permission_type, |
| 631 ContentSettingsType content_settings_type, |
| 632 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
| 633 const GURL& url, |
| 634 int timeout, |
| 635 ContentSetting response) { |
| 636 NavigateAndCommit(url); |
| 637 base::test::ScopedFeatureList scoped_feature_list; |
| 638 scoped_feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); |
| 639 TestPermissionContext permission_context(profile(), permission_type, |
| 640 content_settings_type); |
| 641 permission_context.SetSafeBrowsingDatabaseManagerAndTimeoutForTest( |
| 642 db_manager, timeout); |
| 643 const PermissionRequestID id( |
| 644 web_contents()->GetRenderProcessHost()->GetID(), |
| 645 web_contents()->GetMainFrame()->GetRoutingID(), -1); |
| 646 // The response callback needs to be set here to test a response being made |
| 647 // in the case of a site not being blacklisted or a safe browsing timeout. |
| 648 permission_context.SetRespondPermissionCallback(base::Bind( |
| 649 &PermissionContextBaseTests::RespondToPermission, |
| 650 base::Unretained(this), &permission_context, id, url, false, response)); |
| 651 permission_context.RequestPermission( |
| 652 web_contents(), id, url, true /* user_gesture */, |
| 653 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 654 base::Unretained(&permission_context))); |
| 655 |
| 656 ASSERT_EQ(1u, permission_context.decisions().size()); |
| 657 EXPECT_EQ(response, permission_context.decisions()[0]); |
| 658 } |
| 659 |
516 private: | 660 private: |
517 // ChromeRenderViewHostTestHarness: | 661 // ChromeRenderViewHostTestHarness: |
518 void SetUp() override { | 662 void SetUp() override { |
519 ChromeRenderViewHostTestHarness::SetUp(); | 663 ChromeRenderViewHostTestHarness::SetUp(); |
520 #if defined(OS_ANDROID) | 664 #if defined(OS_ANDROID) |
521 InfoBarService::CreateForWebContents(web_contents()); | 665 InfoBarService::CreateForWebContents(web_contents()); |
522 #else | 666 #else |
523 PermissionRequestManager::CreateForWebContents(web_contents()); | 667 PermissionRequestManager::CreateForWebContents(web_contents()); |
524 #endif | 668 #endif |
525 } | 669 } |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 TestParallelRequests(CONTENT_SETTING_ALLOW); | 804 TestParallelRequests(CONTENT_SETTING_ALLOW); |
661 } | 805 } |
662 | 806 |
663 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { | 807 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { |
664 TestParallelRequests(CONTENT_SETTING_BLOCK); | 808 TestParallelRequests(CONTENT_SETTING_BLOCK); |
665 } | 809 } |
666 | 810 |
667 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { | 811 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { |
668 TestParallelRequests(CONTENT_SETTING_ASK); | 812 TestParallelRequests(CONTENT_SETTING_ASK); |
669 } | 813 } |
| 814 |
| 815 // Tests a blacklisted (URL, permission) pair has had its permission request |
| 816 // blocked. |
| 817 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingBlocked) { |
| 818 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 819 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); |
| 820 const GURL url("https://www.example.com"); |
| 821 std::set<std::string> blacklisted_permissions{ |
| 822 PermissionUtil::GetPermissionString( |
| 823 content::PermissionType::GEOLOCATION)}; |
| 824 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 825 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, |
| 826 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, |
| 827 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK); |
| 828 } |
| 829 |
| 830 // Tests that a URL with a blacklisted permission is permitted to request a |
| 831 // non-blacklisted permission. |
| 832 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { |
| 833 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 834 new MockSafeBrowsingDatabaseManager(true /* perform_callback */); |
| 835 const GURL url("https://www.example.com"); |
| 836 std::set<std::string> blacklisted_permissions{ |
| 837 PermissionUtil::GetPermissionString( |
| 838 content::PermissionType::GEOLOCATION)}; |
| 839 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 840 TestPermissionsBlacklisting( |
| 841 content::PermissionType::GEOLOCATION, CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 842 db_manager, url, 2000 /* timeout in ms */, CONTENT_SETTING_BLOCK); |
| 843 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS, |
| 844 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, |
| 845 url, 2000 /* timeout in ms */, |
| 846 CONTENT_SETTING_ALLOW); |
| 847 } |
| 848 |
| 849 // Tests that a URL with a blacklisted permisison is permitted to request that |
| 850 // permission if Safe Browsing has timed out. |
| 851 TEST_F(PermissionContextBaseTests, TestSafeBrowsingTimeout) { |
| 852 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 853 new MockSafeBrowsingDatabaseManager(false /* perform_callback */); |
| 854 const GURL url("https://www.example.com"); |
| 855 std::set<std::string> blacklisted_permissions{ |
| 856 PermissionUtil::GetPermissionString( |
| 857 content::PermissionType::GEOLOCATION)}; |
| 858 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 859 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, |
| 860 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, |
| 861 url, 0 /* timeout in ms */, CONTENT_SETTING_ASK); |
| 862 } |
OLD | NEW |