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 MockSafeBrowsingDatabaseManager() {} |
| 62 |
| 63 bool CheckApiBlacklistUrl(const GURL& url, Client* client) override { |
| 64 safe_browsing::ThreatMetadata metadata; |
| 65 if (permissions_blacklist_.find(url) != permissions_blacklist_.end()) |
| 66 metadata.api_permissions = permissions_blacklist_.find(url)->second; |
| 67 |
| 68 client->OnCheckApiBlacklistUrlResult(url, metadata); |
| 69 return true; |
| 70 } |
| 71 |
| 72 void BlacklistUrlPermissions(const GURL& url, |
| 73 const std::set<std::string> permissions) { |
| 74 permissions_blacklist_[url] = permissions; |
| 75 } |
| 76 |
| 77 protected: |
| 78 ~MockSafeBrowsingDatabaseManager() override {} |
| 79 |
| 80 private: |
| 81 std::map<GURL, std::set<std::string>> permissions_blacklist_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); |
| 84 }; |
| 85 |
53 class TestPermissionContext : public PermissionContextBase { | 86 class TestPermissionContext : public PermissionContextBase { |
54 public: | 87 public: |
55 TestPermissionContext(Profile* profile, | 88 TestPermissionContext(Profile* profile, |
56 const content::PermissionType permission_type, | 89 const content::PermissionType permission_type, |
57 const ContentSettingsType content_settings_type) | 90 const ContentSettingsType content_settings_type) |
58 : PermissionContextBase(profile, permission_type, content_settings_type), | 91 : PermissionContextBase(profile, permission_type, content_settings_type), |
59 tab_context_updated_(false) {} | 92 tab_context_updated_(false) {} |
60 | 93 |
61 ~TestPermissionContext() override {} | 94 ~TestPermissionContext() override {} |
62 | 95 |
63 #if defined(OS_ANDROID) | 96 #if defined(OS_ANDROID) |
64 PermissionQueueController* GetInfoBarController() { | 97 PermissionQueueController* GetInfoBarController() { |
65 return GetQueueController(); | 98 return GetQueueController(); |
66 } | 99 } |
67 #endif | 100 #endif |
68 | 101 |
69 const std::vector<ContentSetting>& decisions() const { return decisions_; } | 102 const std::vector<ContentSetting>& decisions() const { return decisions_; } |
70 | 103 |
71 bool tab_context_updated() const { return tab_context_updated_; } | 104 bool tab_context_updated() const { return tab_context_updated_; } |
72 | 105 |
| 106 // Once a decision for the requested permission has been made, run the |
| 107 // callback. |
73 void TrackPermissionDecision(ContentSetting content_setting) { | 108 void TrackPermissionDecision(ContentSetting content_setting) { |
74 decisions_.push_back(content_setting); | 109 decisions_.push_back(content_setting); |
| 110 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 111 quit_closure_); |
75 } | 112 } |
76 | 113 |
77 ContentSetting GetContentSettingFromMap(const GURL& url_a, | 114 ContentSetting GetContentSettingFromMap(const GURL& url_a, |
78 const GURL& url_b) { | 115 const GURL& url_b) { |
79 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); | 116 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); |
80 return map->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), | 117 return map->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), |
81 content_settings_type(), std::string()); | 118 content_settings_type(), std::string()); |
82 } | 119 } |
83 | 120 |
| 121 void RequestPermission(content::WebContents* web_contents, |
| 122 const PermissionRequestID& id, |
| 123 const GURL& requesting_frame, |
| 124 bool user_gesture, |
| 125 const BrowserPermissionCallback& callback) override { |
| 126 base::RunLoop run_loop; |
| 127 quit_closure_ = run_loop.QuitClosure(); |
| 128 PermissionContextBase::RequestPermission(web_contents, id, requesting_frame, |
| 129 true /* user_gesture */, callback); |
| 130 run_loop.Run(); |
| 131 } |
| 132 |
| 133 void DecidePermission(content::WebContents* web_contents, |
| 134 const PermissionRequestID& id, |
| 135 const GURL& requesting_origin, |
| 136 const GURL& embedding_origin, |
| 137 bool user_gesture, |
| 138 const BrowserPermissionCallback& callback) override { |
| 139 PermissionContextBase::DecidePermission(web_contents, id, requesting_origin, |
| 140 embedding_origin, user_gesture, |
| 141 callback); |
| 142 if (respond_permission_) |
| 143 respond_permission_.Run(); |
| 144 else |
| 145 quit_closure_.Run(); |
| 146 } |
| 147 |
| 148 // Permission request will need to be responded to, so pass a callback to be |
| 149 // run once the request has completed and the decision has been made. |
| 150 void SetRespondPermissionCallback(base::Closure callback) { |
| 151 respond_permission_ = callback; |
| 152 } |
| 153 |
84 protected: | 154 protected: |
85 void UpdateTabContext(const PermissionRequestID& id, | 155 void UpdateTabContext(const PermissionRequestID& id, |
86 const GURL& requesting_origin, | 156 const GURL& requesting_origin, |
87 bool allowed) override { | 157 bool allowed) override { |
88 tab_context_updated_ = true; | 158 tab_context_updated_ = true; |
89 } | 159 } |
90 | 160 |
91 bool IsRestrictedToSecureOrigins() const override { | 161 bool IsRestrictedToSecureOrigins() const override { |
92 return false; | 162 return false; |
93 } | 163 } |
94 | 164 |
95 private: | 165 private: |
96 std::vector<ContentSetting> decisions_; | 166 std::vector<ContentSetting> decisions_; |
97 bool tab_context_updated_; | 167 bool tab_context_updated_; |
98 | 168 base::Closure quit_closure_; |
| 169 // Callback for responding to a permission once the request has been |
| 170 // completed (valid URL, kill switch disabled, not blacklisted) |
| 171 base::Closure respond_permission_; |
99 DISALLOW_COPY_AND_ASSIGN(TestPermissionContext); | 172 DISALLOW_COPY_AND_ASSIGN(TestPermissionContext); |
100 }; | 173 }; |
101 | 174 |
102 class TestKillSwitchPermissionContext : public TestPermissionContext { | 175 class TestKillSwitchPermissionContext : public TestPermissionContext { |
103 public: | 176 public: |
104 TestKillSwitchPermissionContext( | 177 TestKillSwitchPermissionContext( |
105 Profile* profile, | 178 Profile* profile, |
106 const content::PermissionType permission_type, | 179 const content::PermissionType permission_type, |
107 const ContentSettingsType content_settings_type) | 180 const ContentSettingsType content_settings_type) |
108 : TestPermissionContext(profile, permission_type, content_settings_type), | 181 : TestPermissionContext(profile, permission_type, content_settings_type), |
109 field_trial_list_(base::MakeUnique<base::FieldTrialList>( | 182 field_trial_list_(base::MakeUnique<base::FieldTrialList>( |
110 base::MakeUnique<base::MockEntropyProvider>())) {} | 183 base::MakeUnique<base::MockEntropyProvider>())) {} |
111 | 184 |
112 void ResetFieldTrialList() { | 185 void ResetFieldTrialList() { |
113 // Destroy the existing FieldTrialList before creating a new one to avoid | 186 // Destroy the existing FieldTrialList before creating a new one to avoid |
114 // a DCHECK. | 187 // a DCHECK. |
115 field_trial_list_.reset(); | 188 field_trial_list_.reset(); |
116 field_trial_list_ = base::MakeUnique<base::FieldTrialList>( | 189 field_trial_list_ = base::MakeUnique<base::FieldTrialList>( |
117 base::MakeUnique<base::MockEntropyProvider>()); | 190 base::MakeUnique<base::MockEntropyProvider>()); |
118 variations::testing::ClearAllVariationParams(); | 191 variations::testing::ClearAllVariationParams(); |
119 } | 192 } |
120 | 193 |
121 private: | 194 private: |
122 std::unique_ptr<base::FieldTrialList> field_trial_list_; | 195 std::unique_ptr<base::FieldTrialList> field_trial_list_; |
123 | 196 |
124 DISALLOW_COPY_AND_ASSIGN(TestKillSwitchPermissionContext); | 197 DISALLOW_COPY_AND_ASSIGN(TestKillSwitchPermissionContext); |
125 }; | 198 }; |
126 | 199 |
| 200 class TestPermissionsBlacklistingContext : public TestPermissionContext { |
| 201 public: |
| 202 TestPermissionsBlacklistingContext( |
| 203 Profile* profile, |
| 204 const content::PermissionType permission_type, |
| 205 const ContentSettingsType content_settings_type, |
| 206 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager) |
| 207 : TestPermissionContext(profile, permission_type, content_settings_type), |
| 208 db_manager_(db_manager) {} |
| 209 |
| 210 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> |
| 211 GetSafeBrowsingDatabaseManager() override { |
| 212 return db_manager_; |
| 213 } |
| 214 |
| 215 private: |
| 216 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager_; |
| 217 }; |
| 218 |
127 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { | 219 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { |
128 protected: | 220 protected: |
129 PermissionContextBaseTests() {} | 221 PermissionContextBaseTests() {} |
130 ~PermissionContextBaseTests() override {} | 222 ~PermissionContextBaseTests() override {} |
131 | 223 |
132 // Accept or dismiss the permission bubble or infobar. | 224 // Accept or dismiss the permission bubble or infobar. |
133 void RespondToPermission(TestPermissionContext* context, | 225 void RespondToPermission(TestPermissionContext* context, |
134 const PermissionRequestID& id, | 226 const PermissionRequestID& id, |
135 const GURL& url, | 227 const GURL& url, |
136 bool persist, | 228 bool persist, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 TestPermissionContext permission_context(profile(), permission, | 265 TestPermissionContext permission_context(profile(), permission, |
174 content_settings_type); | 266 content_settings_type); |
175 GURL url("https://www.google.com"); | 267 GURL url("https://www.google.com"); |
176 NavigateAndCommit(url); | 268 NavigateAndCommit(url); |
177 base::HistogramTester histograms; | 269 base::HistogramTester histograms; |
178 | 270 |
179 const PermissionRequestID id( | 271 const PermissionRequestID id( |
180 web_contents()->GetRenderProcessHost()->GetID(), | 272 web_contents()->GetRenderProcessHost()->GetID(), |
181 web_contents()->GetMainFrame()->GetRoutingID(), | 273 web_contents()->GetMainFrame()->GetRoutingID(), |
182 -1); | 274 -1); |
| 275 permission_context.SetRespondPermissionCallback( |
| 276 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 277 base::Unretained(this), &permission_context, id, url, |
| 278 persist, decision)); |
183 permission_context.RequestPermission( | 279 permission_context.RequestPermission( |
184 web_contents(), | 280 web_contents(), |
185 id, url, true /* user_gesture */, | 281 id, url, true /* user_gesture */, |
186 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 282 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
187 base::Unretained(&permission_context))); | 283 base::Unretained(&permission_context))); |
188 | |
189 RespondToPermission(&permission_context, id, url, persist, decision); | |
190 ASSERT_EQ(1u, permission_context.decisions().size()); | 284 ASSERT_EQ(1u, permission_context.decisions().size()); |
191 EXPECT_EQ(decision, permission_context.decisions()[0]); | 285 EXPECT_EQ(decision, permission_context.decisions()[0]); |
192 EXPECT_TRUE(permission_context.tab_context_updated()); | 286 EXPECT_TRUE(permission_context.tab_context_updated()); |
193 | 287 |
194 std::string decision_string; | 288 std::string decision_string; |
195 if (decision == CONTENT_SETTING_ALLOW) | 289 if (decision == CONTENT_SETTING_ALLOW) |
196 decision_string = "Accepted"; | 290 decision_string = "Accepted"; |
197 else if (decision == CONTENT_SETTING_BLOCK) | 291 else if (decision == CONTENT_SETTING_BLOCK) |
198 decision_string = "Denied"; | 292 decision_string = "Denied"; |
199 else if (decision == CONTENT_SETTING_ASK) | 293 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. | 324 // from dismiss to block, and hence change the persisted content setting. |
231 for (uint32_t i = 0; i < iterations; ++i) { | 325 for (uint32_t i = 0; i < iterations; ++i) { |
232 TestPermissionContext permission_context( | 326 TestPermissionContext permission_context( |
233 profile(), permission_type, content_settings_type); | 327 profile(), permission_type, content_settings_type); |
234 ContentSetting expected = | 328 ContentSetting expected = |
235 (i < (iterations - 1)) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; | 329 (i < (iterations - 1)) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; |
236 | 330 |
237 const PermissionRequestID id( | 331 const PermissionRequestID id( |
238 web_contents()->GetRenderProcessHost()->GetID(), | 332 web_contents()->GetRenderProcessHost()->GetID(), |
239 web_contents()->GetMainFrame()->GetRoutingID(), i); | 333 web_contents()->GetMainFrame()->GetRoutingID(), i); |
| 334 |
| 335 permission_context.SetRespondPermissionCallback( |
| 336 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 337 base::Unretained(this), &permission_context, id, url, |
| 338 false, CONTENT_SETTING_ASK)); |
| 339 |
240 permission_context.RequestPermission( | 340 permission_context.RequestPermission( |
241 web_contents(), id, url, true /* user_gesture */, | 341 web_contents(), id, url, true /* user_gesture */, |
242 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 342 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
243 base::Unretained(&permission_context))); | 343 base::Unretained(&permission_context))); |
244 | |
245 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
246 CONTENT_SETTING_ASK); | |
247 histograms.ExpectTotalCount( | 344 histograms.ExpectTotalCount( |
248 "Permissions.Prompt.Dismissed.PriorDismissCount." + | 345 "Permissions.Prompt.Dismissed.PriorDismissCount." + |
249 PermissionUtil::GetPermissionString(permission_type), | 346 PermissionUtil::GetPermissionString(permission_type), |
250 i + 1); | 347 i + 1); |
251 histograms.ExpectBucketCount( | 348 histograms.ExpectBucketCount( |
252 "Permissions.Prompt.Dismissed.PriorDismissCount." + | 349 "Permissions.Prompt.Dismissed.PriorDismissCount." + |
253 PermissionUtil::GetPermissionString(permission_type), | 350 PermissionUtil::GetPermissionString(permission_type), |
254 i, 1); | 351 i, 1); |
255 | 352 |
256 ASSERT_EQ(1u, permission_context.decisions().size()); | 353 ASSERT_EQ(1u, permission_context.decisions().size()); |
(...skipping 17 matching lines...) Expand all Loading... |
274 | 371 |
275 // First, ensure that > 3 dismissals behaves correctly. | 372 // First, ensure that > 3 dismissals behaves correctly. |
276 for (uint32_t i = 0; i < 4; ++i) { | 373 for (uint32_t i = 0; i < 4; ++i) { |
277 TestPermissionContext permission_context( | 374 TestPermissionContext permission_context( |
278 profile(), content::PermissionType::GEOLOCATION, | 375 profile(), content::PermissionType::GEOLOCATION, |
279 CONTENT_SETTINGS_TYPE_GEOLOCATION); | 376 CONTENT_SETTINGS_TYPE_GEOLOCATION); |
280 | 377 |
281 const PermissionRequestID id( | 378 const PermissionRequestID id( |
282 web_contents()->GetRenderProcessHost()->GetID(), | 379 web_contents()->GetRenderProcessHost()->GetID(), |
283 web_contents()->GetMainFrame()->GetRoutingID(), i); | 380 web_contents()->GetMainFrame()->GetRoutingID(), i); |
| 381 |
| 382 permission_context.SetRespondPermissionCallback( |
| 383 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 384 base::Unretained(this), &permission_context, id, url, |
| 385 false, CONTENT_SETTING_ASK)); |
284 permission_context.RequestPermission( | 386 permission_context.RequestPermission( |
285 web_contents(), id, url, true /* user_gesture */, | 387 web_contents(), id, url, true /* user_gesture */, |
286 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 388 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
287 base::Unretained(&permission_context))); | 389 base::Unretained(&permission_context))); |
288 | |
289 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
290 CONTENT_SETTING_ASK); | |
291 histograms.ExpectTotalCount( | 390 histograms.ExpectTotalCount( |
292 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", | 391 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", |
293 i + 1); | 392 i + 1); |
294 histograms.ExpectBucketCount( | 393 histograms.ExpectBucketCount( |
295 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", i, 1); | 394 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", i, 1); |
296 ASSERT_EQ(1u, permission_context.decisions().size()); | 395 ASSERT_EQ(1u, permission_context.decisions().size()); |
297 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); | 396 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); |
298 EXPECT_TRUE(permission_context.tab_context_updated()); | 397 EXPECT_TRUE(permission_context.tab_context_updated()); |
299 EXPECT_EQ(CONTENT_SETTING_ASK, | 398 EXPECT_EQ(CONTENT_SETTING_ASK, |
300 permission_context.GetContentSettingFromMap(url, url)); | 399 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) { | 457 for (uint32_t i = 0; i < 5; ++i) { |
359 TestPermissionContext permission_context( | 458 TestPermissionContext permission_context( |
360 profile(), content::PermissionType::MIDI_SYSEX, | 459 profile(), content::PermissionType::MIDI_SYSEX, |
361 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | 460 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
362 | 461 |
363 ContentSetting expected = | 462 ContentSetting expected = |
364 (i < 4) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; | 463 (i < 4) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; |
365 const PermissionRequestID id( | 464 const PermissionRequestID id( |
366 web_contents()->GetRenderProcessHost()->GetID(), | 465 web_contents()->GetRenderProcessHost()->GetID(), |
367 web_contents()->GetMainFrame()->GetRoutingID(), i); | 466 web_contents()->GetMainFrame()->GetRoutingID(), i); |
| 467 permission_context.SetRespondPermissionCallback( |
| 468 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 469 base::Unretained(this), &permission_context, id, url, |
| 470 false, CONTENT_SETTING_ASK)); |
368 permission_context.RequestPermission( | 471 permission_context.RequestPermission( |
369 web_contents(), id, url, true /* user_gesture */, | 472 web_contents(), id, url, true /* user_gesture */, |
370 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 473 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
371 base::Unretained(&permission_context))); | 474 base::Unretained(&permission_context))); |
372 | 475 |
373 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
374 CONTENT_SETTING_ASK); | |
375 EXPECT_EQ(1u, permission_context.decisions().size()); | 476 EXPECT_EQ(1u, permission_context.decisions().size()); |
376 ASSERT_EQ(expected, permission_context.decisions()[0]); | 477 ASSERT_EQ(expected, permission_context.decisions()[0]); |
377 EXPECT_TRUE(permission_context.tab_context_updated()); | 478 EXPECT_TRUE(permission_context.tab_context_updated()); |
378 EXPECT_EQ(expected, | 479 EXPECT_EQ(expected, |
379 permission_context.GetContentSettingFromMap(url, url)); | 480 permission_context.GetContentSettingFromMap(url, url)); |
380 | 481 |
381 histograms.ExpectTotalCount( | 482 histograms.ExpectTotalCount( |
382 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i + 1); | 483 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i + 1); |
383 histograms.ExpectBucketCount( | 484 histograms.ExpectBucketCount( |
384 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i, 1); | 485 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i, 1); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 ContentSetting expected_default) { | 525 ContentSetting expected_default) { |
425 TestPermissionContext permission_context(profile(), permission_type, | 526 TestPermissionContext permission_context(profile(), permission_type, |
426 content_settings_type); | 527 content_settings_type); |
427 GURL url("https://www.google.com"); | 528 GURL url("https://www.google.com"); |
428 NavigateAndCommit(url); | 529 NavigateAndCommit(url); |
429 | 530 |
430 const PermissionRequestID id( | 531 const PermissionRequestID id( |
431 web_contents()->GetRenderProcessHost()->GetID(), | 532 web_contents()->GetRenderProcessHost()->GetID(), |
432 web_contents()->GetMainFrame()->GetRoutingID(), | 533 web_contents()->GetMainFrame()->GetRoutingID(), |
433 -1); | 534 -1); |
| 535 permission_context.SetRespondPermissionCallback( |
| 536 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 537 base::Unretained(this), &permission_context, id, url, true, |
| 538 CONTENT_SETTING_ALLOW)); |
| 539 |
434 permission_context.RequestPermission( | 540 permission_context.RequestPermission( |
435 web_contents(), | 541 web_contents(), |
436 id, url, true /* user_gesture */, | 542 id, url, true /* user_gesture */, |
437 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 543 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
438 base::Unretained(&permission_context))); | 544 base::Unretained(&permission_context))); |
439 | 545 |
440 RespondToPermission(&permission_context, id, url, true, /* persist */ | |
441 CONTENT_SETTING_ALLOW); | |
442 ASSERT_EQ(1u, permission_context.decisions().size()); | 546 ASSERT_EQ(1u, permission_context.decisions().size()); |
443 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); | 547 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); |
444 EXPECT_TRUE(permission_context.tab_context_updated()); | 548 EXPECT_TRUE(permission_context.tab_context_updated()); |
445 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 549 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
446 permission_context.GetContentSettingFromMap(url, url)); | 550 permission_context.GetContentSettingFromMap(url, url)); |
447 | 551 |
448 // Try to reset permission. | 552 // Try to reset permission. |
449 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); | 553 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); |
450 ContentSetting setting_after_reset = | 554 ContentSetting setting_after_reset = |
451 permission_context.GetContentSettingFromMap(url, url); | 555 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"); | 587 GURL url("http://www.google.com"); |
484 NavigateAndCommit(url); | 588 NavigateAndCommit(url); |
485 | 589 |
486 const PermissionRequestID id0( | 590 const PermissionRequestID id0( |
487 web_contents()->GetRenderProcessHost()->GetID(), | 591 web_contents()->GetRenderProcessHost()->GetID(), |
488 web_contents()->GetMainFrame()->GetRoutingID(), 0); | 592 web_contents()->GetMainFrame()->GetRoutingID(), 0); |
489 const PermissionRequestID id1( | 593 const PermissionRequestID id1( |
490 web_contents()->GetRenderProcessHost()->GetID(), | 594 web_contents()->GetRenderProcessHost()->GetID(), |
491 web_contents()->GetMainFrame()->GetRoutingID(), 1); | 595 web_contents()->GetMainFrame()->GetRoutingID(), 1); |
492 | 596 |
| 597 bool persist = (response == CONTENT_SETTING_ALLOW || |
| 598 response == CONTENT_SETTING_BLOCK); |
| 599 |
| 600 // Request a permission without setting the callback to DecidePermission. |
493 permission_context.RequestPermission( | 601 permission_context.RequestPermission( |
494 web_contents(), id0, url, true /* user_gesture */, | 602 web_contents(), id0, url, true /* user_gesture */, |
495 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 603 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
496 base::Unretained(&permission_context))); | 604 base::Unretained(&permission_context))); |
| 605 |
| 606 EXPECT_EQ(0u, permission_context.decisions().size()); |
| 607 |
| 608 // Set the callback, and make a second permission request. |
| 609 permission_context.SetRespondPermissionCallback( |
| 610 base::Bind(&PermissionContextBaseTests::RespondToPermission, |
| 611 base::Unretained(this), &permission_context, id0, url, |
| 612 persist, response)); |
497 permission_context.RequestPermission( | 613 permission_context.RequestPermission( |
498 web_contents(), id1, url, true /* user_gesture */, | 614 web_contents(), id1, url, true /* user_gesture */, |
499 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 615 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
500 base::Unretained(&permission_context))); | 616 base::Unretained(&permission_context))); |
501 | 617 |
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()); | 618 ASSERT_EQ(2u, permission_context.decisions().size()); |
509 EXPECT_EQ(response, permission_context.decisions()[0]); | 619 EXPECT_EQ(response, permission_context.decisions()[0]); |
510 EXPECT_EQ(response, permission_context.decisions()[1]); | 620 EXPECT_EQ(response, permission_context.decisions()[1]); |
511 EXPECT_TRUE(permission_context.tab_context_updated()); | 621 EXPECT_TRUE(permission_context.tab_context_updated()); |
512 | 622 |
513 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); | 623 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); |
514 } | 624 } |
515 | 625 |
| 626 void TestPermissionsBlacklisting( |
| 627 content::PermissionType permission_type, |
| 628 ContentSettingsType content_settings_type, |
| 629 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
| 630 const GURL& url, |
| 631 ContentSetting response) { |
| 632 NavigateAndCommit(url); |
| 633 base::test::ScopedFeatureList scoped_feature_list; |
| 634 scoped_feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); |
| 635 TestPermissionsBlacklistingContext permission_context( |
| 636 profile(), permission_type, content_settings_type, db_manager); |
| 637 |
| 638 const PermissionRequestID id( |
| 639 web_contents()->GetRenderProcessHost()->GetID(), |
| 640 web_contents()->GetMainFrame()->GetRoutingID(), -1); |
| 641 |
| 642 permission_context.SetRespondPermissionCallback(base::Bind( |
| 643 &PermissionContextBaseTests::RespondToPermission, |
| 644 base::Unretained(this), &permission_context, id, url, false, response)); |
| 645 permission_context.RequestPermission( |
| 646 web_contents(), id, url, true /* user_gesture */, |
| 647 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 648 base::Unretained(&permission_context))); |
| 649 |
| 650 ASSERT_EQ(1u, permission_context.decisions().size()); |
| 651 EXPECT_EQ(response, permission_context.decisions()[0]); |
| 652 } |
| 653 |
516 private: | 654 private: |
517 // ChromeRenderViewHostTestHarness: | 655 // ChromeRenderViewHostTestHarness: |
518 void SetUp() override { | 656 void SetUp() override { |
519 ChromeRenderViewHostTestHarness::SetUp(); | 657 ChromeRenderViewHostTestHarness::SetUp(); |
520 #if defined(OS_ANDROID) | 658 #if defined(OS_ANDROID) |
521 InfoBarService::CreateForWebContents(web_contents()); | 659 InfoBarService::CreateForWebContents(web_contents()); |
522 #else | 660 #else |
523 PermissionRequestManager::CreateForWebContents(web_contents()); | 661 PermissionRequestManager::CreateForWebContents(web_contents()); |
524 #endif | 662 #endif |
525 } | 663 } |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 TestParallelRequests(CONTENT_SETTING_ALLOW); | 800 TestParallelRequests(CONTENT_SETTING_ALLOW); |
663 } | 801 } |
664 | 802 |
665 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { | 803 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { |
666 TestParallelRequests(CONTENT_SETTING_BLOCK); | 804 TestParallelRequests(CONTENT_SETTING_BLOCK); |
667 } | 805 } |
668 | 806 |
669 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { | 807 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { |
670 TestParallelRequests(CONTENT_SETTING_ASK); | 808 TestParallelRequests(CONTENT_SETTING_ASK); |
671 } | 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(); |
| 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, 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(); |
| 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(content::PermissionType::GEOLOCATION, |
| 837 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, |
| 838 url, CONTENT_SETTING_BLOCK); |
| 839 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS, |
| 840 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, |
| 841 url, CONTENT_SETTING_ASK); |
| 842 } |
OLD | NEW |