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