Chromium Code Reviews| 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 std::map<GURL, std::set<std::string>>::iterator blacklisted_permissions = | |
|
raymes
2017/01/09 06:28:18
nit: It would be fine to use "auto" here. We also
meredithl
2017/01/10 02:24:57
Done. Still getting used to C++11 :)
| |
| 70 permissions_blacklist_.find(url); | |
| 71 if (blacklisted_permissions != permissions_blacklist_.end()) | |
| 72 metadata.api_permissions = blacklisted_permissions->second; | |
| 73 client->OnCheckApiBlacklistUrlResult(url, metadata); | |
| 74 } | |
| 75 // Returns false if scheme is HTTP/HTTPS and able to be checked. | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 bool CancelApiCheck(Client* client) override { | |
| 80 if (perform_callback_) | |
|
raymes
2017/01/09 06:28:17
nit: can have DCHECK(!perform_callback_) here to a
meredithl
2017/01/10 02:24:57
Done.
| |
| 81 NOTREACHED(); | |
| 82 // Returns true when client check could be stopped. | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 void BlacklistUrlPermissions(const GURL& url, | |
| 87 const std::set<std::string> permissions) { | |
| 88 permissions_blacklist_[url] = permissions; | |
| 89 } | |
| 90 | |
| 91 protected: | |
| 92 ~MockSafeBrowsingDatabaseManager() override {} | |
| 93 | |
| 94 private: | |
| 95 bool perform_callback_; | |
| 96 std::map<GURL, std::set<std::string>> permissions_blacklist_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); | |
| 99 }; | |
| 100 | |
| 53 class TestPermissionContext : public PermissionContextBase { | 101 class TestPermissionContext : public PermissionContextBase { |
| 54 public: | 102 public: |
| 55 TestPermissionContext(Profile* profile, | 103 TestPermissionContext(Profile* profile, |
| 56 const content::PermissionType permission_type, | 104 const content::PermissionType permission_type, |
| 57 const ContentSettingsType content_settings_type) | 105 const ContentSettingsType content_settings_type) |
| 58 : PermissionContextBase(profile, permission_type, content_settings_type), | 106 : PermissionContextBase(profile, permission_type, content_settings_type), |
| 59 tab_context_updated_(false) {} | 107 tab_context_updated_(false) {} |
| 60 | 108 |
| 61 ~TestPermissionContext() override {} | 109 ~TestPermissionContext() override {} |
| 62 | 110 |
| 63 #if defined(OS_ANDROID) | 111 #if defined(OS_ANDROID) |
| 64 PermissionQueueController* GetInfoBarController() { | 112 PermissionQueueController* GetInfoBarController() { |
| 65 return GetQueueController(); | 113 return GetQueueController(); |
| 66 } | 114 } |
| 67 #endif | 115 #endif |
| 68 | 116 |
| 69 const std::vector<ContentSetting>& decisions() const { return decisions_; } | 117 const std::vector<ContentSetting>& decisions() const { return decisions_; } |
| 70 | 118 |
| 71 bool tab_context_updated() const { return tab_context_updated_; } | 119 bool tab_context_updated() const { return tab_context_updated_; } |
| 72 | 120 |
| 121 // Once a decision for the requested permission has been made, run the | |
| 122 // callback. | |
| 73 void TrackPermissionDecision(ContentSetting content_setting) { | 123 void TrackPermissionDecision(ContentSetting content_setting) { |
| 74 decisions_.push_back(content_setting); | 124 decisions_.push_back(content_setting); |
| 125 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 126 quit_closure_); | |
|
raymes
2017/01/09 06:28:17
Can we just call quit_closure.Run() here instead o
meredithl
2017/01/10 02:24:57
It appears we can! These test methods have been ha
| |
| 75 } | 127 } |
| 76 | 128 |
| 77 ContentSetting GetContentSettingFromMap(const GURL& url_a, | 129 ContentSetting GetContentSettingFromMap(const GURL& url_a, |
| 78 const GURL& url_b) { | 130 const GURL& url_b) { |
| 79 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); | 131 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); |
| 80 return map->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), | 132 return map->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), |
| 81 content_settings_type(), std::string()); | 133 content_settings_type(), std::string()); |
| 82 } | 134 } |
| 83 | 135 |
| 136 void RequestPermission(content::WebContents* web_contents, | |
| 137 const PermissionRequestID& id, | |
| 138 const GURL& requesting_frame, | |
| 139 bool user_gesture, | |
| 140 const BrowserPermissionCallback& callback) override { | |
| 141 base::RunLoop run_loop; | |
| 142 quit_closure_ = run_loop.QuitClosure(); | |
| 143 PermissionContextBase::RequestPermission(web_contents, id, requesting_frame, | |
| 144 true /* user_gesture */, callback); | |
| 145 run_loop.Run(); | |
| 146 } | |
| 147 | |
| 148 void DecidePermission(content::WebContents* web_contents, | |
| 149 const PermissionRequestID& id, | |
| 150 const GURL& requesting_origin, | |
| 151 const GURL& embedding_origin, | |
| 152 bool user_gesture, | |
| 153 const BrowserPermissionCallback& callback) override { | |
| 154 PermissionContextBase::DecidePermission(web_contents, id, requesting_origin, | |
| 155 embedding_origin, user_gesture, | |
| 156 callback); | |
| 157 if (respond_permission_) | |
| 158 respond_permission_.Run(); | |
|
raymes
2017/01/09 06:28:17
Same here, we probably want to reset this after ru
meredithl
2017/01/10 02:24:57
Done.
| |
| 159 else | |
| 160 quit_closure_.Run(); | |
|
raymes
2017/01/09 06:28:18
I wonder if we can just remove this line?
meredithl
2017/01/10 02:24:57
respond_permission_ only gets set in the TestParal
| |
| 161 } | |
| 162 | |
| 163 // Permission request will need to be responded to, so pass a callback to be | |
| 164 // run once the request has completed and the decision has been made. | |
| 165 void SetRespondPermissionCallback(base::Closure callback) { | |
| 166 respond_permission_ = callback; | |
| 167 } | |
| 168 | |
| 84 protected: | 169 protected: |
| 85 void UpdateTabContext(const PermissionRequestID& id, | 170 void UpdateTabContext(const PermissionRequestID& id, |
| 86 const GURL& requesting_origin, | 171 const GURL& requesting_origin, |
| 87 bool allowed) override { | 172 bool allowed) override { |
| 88 tab_context_updated_ = true; | 173 tab_context_updated_ = true; |
| 89 } | 174 } |
| 90 | 175 |
| 91 bool IsRestrictedToSecureOrigins() const override { | 176 bool IsRestrictedToSecureOrigins() const override { |
| 92 return false; | 177 return false; |
| 93 } | 178 } |
| 94 | 179 |
| 95 private: | 180 private: |
| 96 std::vector<ContentSetting> decisions_; | 181 std::vector<ContentSetting> decisions_; |
| 97 bool tab_context_updated_; | 182 bool tab_context_updated_; |
| 98 | 183 base::Closure quit_closure_; |
| 184 // Callback for responding to a permission once the request has been completed | |
| 185 // (valid URL, kill switch disabled, not blacklisted) | |
| 186 base::Closure respond_permission_; | |
| 99 DISALLOW_COPY_AND_ASSIGN(TestPermissionContext); | 187 DISALLOW_COPY_AND_ASSIGN(TestPermissionContext); |
| 100 }; | 188 }; |
| 101 | 189 |
| 102 class TestKillSwitchPermissionContext : public TestPermissionContext { | 190 class TestKillSwitchPermissionContext : public TestPermissionContext { |
| 103 public: | 191 public: |
| 104 TestKillSwitchPermissionContext( | 192 TestKillSwitchPermissionContext( |
| 105 Profile* profile, | 193 Profile* profile, |
| 106 const content::PermissionType permission_type, | 194 const content::PermissionType permission_type, |
| 107 const ContentSettingsType content_settings_type) | 195 const ContentSettingsType content_settings_type) |
| 108 : TestPermissionContext(profile, permission_type, content_settings_type), | 196 : 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, | 261 TestPermissionContext permission_context(profile(), permission, |
| 174 content_settings_type); | 262 content_settings_type); |
| 175 GURL url("https://www.google.com"); | 263 GURL url("https://www.google.com"); |
| 176 NavigateAndCommit(url); | 264 NavigateAndCommit(url); |
| 177 base::HistogramTester histograms; | 265 base::HistogramTester histograms; |
| 178 | 266 |
| 179 const PermissionRequestID id( | 267 const PermissionRequestID id( |
| 180 web_contents()->GetRenderProcessHost()->GetID(), | 268 web_contents()->GetRenderProcessHost()->GetID(), |
| 181 web_contents()->GetMainFrame()->GetRoutingID(), | 269 web_contents()->GetMainFrame()->GetRoutingID(), |
| 182 -1); | 270 -1); |
| 271 permission_context.SetRespondPermissionCallback( | |
| 272 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
| 273 base::Unretained(this), &permission_context, id, url, | |
| 274 persist, decision)); | |
| 183 permission_context.RequestPermission( | 275 permission_context.RequestPermission( |
| 184 web_contents(), | 276 web_contents(), |
| 185 id, url, true /* user_gesture */, | 277 id, url, true /* user_gesture */, |
| 186 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 278 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 187 base::Unretained(&permission_context))); | 279 base::Unretained(&permission_context))); |
| 188 | |
| 189 RespondToPermission(&permission_context, id, url, persist, decision); | |
| 190 ASSERT_EQ(1u, permission_context.decisions().size()); | 280 ASSERT_EQ(1u, permission_context.decisions().size()); |
| 191 EXPECT_EQ(decision, permission_context.decisions()[0]); | 281 EXPECT_EQ(decision, permission_context.decisions()[0]); |
| 192 EXPECT_TRUE(permission_context.tab_context_updated()); | 282 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 193 | 283 |
| 194 std::string decision_string; | 284 std::string decision_string; |
| 195 if (decision == CONTENT_SETTING_ALLOW) | 285 if (decision == CONTENT_SETTING_ALLOW) |
| 196 decision_string = "Accepted"; | 286 decision_string = "Accepted"; |
| 197 else if (decision == CONTENT_SETTING_BLOCK) | 287 else if (decision == CONTENT_SETTING_BLOCK) |
| 198 decision_string = "Denied"; | 288 decision_string = "Denied"; |
| 199 else if (decision == CONTENT_SETTING_ASK) | 289 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. | 320 // from dismiss to block, and hence change the persisted content setting. |
| 231 for (uint32_t i = 0; i < iterations; ++i) { | 321 for (uint32_t i = 0; i < iterations; ++i) { |
| 232 TestPermissionContext permission_context( | 322 TestPermissionContext permission_context( |
| 233 profile(), permission_type, content_settings_type); | 323 profile(), permission_type, content_settings_type); |
| 234 ContentSetting expected = | 324 ContentSetting expected = |
| 235 (i < (iterations - 1)) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; | 325 (i < (iterations - 1)) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; |
| 236 | 326 |
| 237 const PermissionRequestID id( | 327 const PermissionRequestID id( |
| 238 web_contents()->GetRenderProcessHost()->GetID(), | 328 web_contents()->GetRenderProcessHost()->GetID(), |
| 239 web_contents()->GetMainFrame()->GetRoutingID(), i); | 329 web_contents()->GetMainFrame()->GetRoutingID(), i); |
| 330 | |
| 331 permission_context.SetRespondPermissionCallback( | |
| 332 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
| 333 base::Unretained(this), &permission_context, id, url, | |
| 334 false, CONTENT_SETTING_ASK)); | |
| 335 | |
| 240 permission_context.RequestPermission( | 336 permission_context.RequestPermission( |
| 241 web_contents(), id, url, true /* user_gesture */, | 337 web_contents(), id, url, true /* user_gesture */, |
| 242 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 338 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 243 base::Unretained(&permission_context))); | 339 base::Unretained(&permission_context))); |
| 244 | |
| 245 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
| 246 CONTENT_SETTING_ASK); | |
| 247 histograms.ExpectTotalCount( | 340 histograms.ExpectTotalCount( |
| 248 "Permissions.Prompt.Dismissed.PriorDismissCount." + | 341 "Permissions.Prompt.Dismissed.PriorDismissCount." + |
| 249 PermissionUtil::GetPermissionString(permission_type), | 342 PermissionUtil::GetPermissionString(permission_type), |
| 250 i + 1); | 343 i + 1); |
| 251 histograms.ExpectBucketCount( | 344 histograms.ExpectBucketCount( |
| 252 "Permissions.Prompt.Dismissed.PriorDismissCount." + | 345 "Permissions.Prompt.Dismissed.PriorDismissCount." + |
| 253 PermissionUtil::GetPermissionString(permission_type), | 346 PermissionUtil::GetPermissionString(permission_type), |
| 254 i, 1); | 347 i, 1); |
| 255 | 348 |
| 256 ASSERT_EQ(1u, permission_context.decisions().size()); | 349 ASSERT_EQ(1u, permission_context.decisions().size()); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 274 | 367 |
| 275 // First, ensure that > 3 dismissals behaves correctly. | 368 // First, ensure that > 3 dismissals behaves correctly. |
| 276 for (uint32_t i = 0; i < 4; ++i) { | 369 for (uint32_t i = 0; i < 4; ++i) { |
| 277 TestPermissionContext permission_context( | 370 TestPermissionContext permission_context( |
| 278 profile(), content::PermissionType::GEOLOCATION, | 371 profile(), content::PermissionType::GEOLOCATION, |
| 279 CONTENT_SETTINGS_TYPE_GEOLOCATION); | 372 CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 280 | 373 |
| 281 const PermissionRequestID id( | 374 const PermissionRequestID id( |
| 282 web_contents()->GetRenderProcessHost()->GetID(), | 375 web_contents()->GetRenderProcessHost()->GetID(), |
| 283 web_contents()->GetMainFrame()->GetRoutingID(), i); | 376 web_contents()->GetMainFrame()->GetRoutingID(), i); |
| 377 | |
| 378 permission_context.SetRespondPermissionCallback( | |
| 379 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
| 380 base::Unretained(this), &permission_context, id, url, | |
| 381 false, CONTENT_SETTING_ASK)); | |
| 284 permission_context.RequestPermission( | 382 permission_context.RequestPermission( |
| 285 web_contents(), id, url, true /* user_gesture */, | 383 web_contents(), id, url, true /* user_gesture */, |
| 286 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 384 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 287 base::Unretained(&permission_context))); | 385 base::Unretained(&permission_context))); |
| 288 | |
| 289 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
| 290 CONTENT_SETTING_ASK); | |
| 291 histograms.ExpectTotalCount( | 386 histograms.ExpectTotalCount( |
| 292 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", | 387 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", |
| 293 i + 1); | 388 i + 1); |
| 294 histograms.ExpectBucketCount( | 389 histograms.ExpectBucketCount( |
| 295 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", i, 1); | 390 "Permissions.Prompt.Dismissed.PriorDismissCount.Geolocation", i, 1); |
| 296 ASSERT_EQ(1u, permission_context.decisions().size()); | 391 ASSERT_EQ(1u, permission_context.decisions().size()); |
| 297 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); | 392 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); |
| 298 EXPECT_TRUE(permission_context.tab_context_updated()); | 393 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 299 EXPECT_EQ(CONTENT_SETTING_ASK, | 394 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 300 permission_context.GetContentSettingFromMap(url, url)); | 395 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) { | 453 for (uint32_t i = 0; i < 5; ++i) { |
| 359 TestPermissionContext permission_context( | 454 TestPermissionContext permission_context( |
| 360 profile(), content::PermissionType::MIDI_SYSEX, | 455 profile(), content::PermissionType::MIDI_SYSEX, |
| 361 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | 456 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
| 362 | 457 |
| 363 ContentSetting expected = | 458 ContentSetting expected = |
| 364 (i < 4) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; | 459 (i < 4) ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK; |
| 365 const PermissionRequestID id( | 460 const PermissionRequestID id( |
| 366 web_contents()->GetRenderProcessHost()->GetID(), | 461 web_contents()->GetRenderProcessHost()->GetID(), |
| 367 web_contents()->GetMainFrame()->GetRoutingID(), i); | 462 web_contents()->GetMainFrame()->GetRoutingID(), i); |
| 463 permission_context.SetRespondPermissionCallback( | |
| 464 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
| 465 base::Unretained(this), &permission_context, id, url, | |
| 466 false, CONTENT_SETTING_ASK)); | |
| 368 permission_context.RequestPermission( | 467 permission_context.RequestPermission( |
| 369 web_contents(), id, url, true /* user_gesture */, | 468 web_contents(), id, url, true /* user_gesture */, |
| 370 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 469 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 371 base::Unretained(&permission_context))); | 470 base::Unretained(&permission_context))); |
| 372 | 471 |
| 373 RespondToPermission(&permission_context, id, url, false, /* persist */ | |
| 374 CONTENT_SETTING_ASK); | |
| 375 EXPECT_EQ(1u, permission_context.decisions().size()); | 472 EXPECT_EQ(1u, permission_context.decisions().size()); |
| 376 ASSERT_EQ(expected, permission_context.decisions()[0]); | 473 ASSERT_EQ(expected, permission_context.decisions()[0]); |
| 377 EXPECT_TRUE(permission_context.tab_context_updated()); | 474 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 378 EXPECT_EQ(expected, | 475 EXPECT_EQ(expected, |
| 379 permission_context.GetContentSettingFromMap(url, url)); | 476 permission_context.GetContentSettingFromMap(url, url)); |
| 380 | 477 |
| 381 histograms.ExpectTotalCount( | 478 histograms.ExpectTotalCount( |
| 382 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i + 1); | 479 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i + 1); |
| 383 histograms.ExpectBucketCount( | 480 histograms.ExpectBucketCount( |
| 384 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i, 1); | 481 "Permissions.Prompt.Dismissed.PriorDismissCount.MidiSysEx", i, 1); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 424 ContentSetting expected_default) { | 521 ContentSetting expected_default) { |
| 425 TestPermissionContext permission_context(profile(), permission_type, | 522 TestPermissionContext permission_context(profile(), permission_type, |
| 426 content_settings_type); | 523 content_settings_type); |
| 427 GURL url("https://www.google.com"); | 524 GURL url("https://www.google.com"); |
| 428 NavigateAndCommit(url); | 525 NavigateAndCommit(url); |
| 429 | 526 |
| 430 const PermissionRequestID id( | 527 const PermissionRequestID id( |
| 431 web_contents()->GetRenderProcessHost()->GetID(), | 528 web_contents()->GetRenderProcessHost()->GetID(), |
| 432 web_contents()->GetMainFrame()->GetRoutingID(), | 529 web_contents()->GetMainFrame()->GetRoutingID(), |
| 433 -1); | 530 -1); |
| 531 permission_context.SetRespondPermissionCallback( | |
| 532 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
| 533 base::Unretained(this), &permission_context, id, url, true, | |
| 534 CONTENT_SETTING_ALLOW)); | |
| 535 | |
| 434 permission_context.RequestPermission( | 536 permission_context.RequestPermission( |
| 435 web_contents(), | 537 web_contents(), |
| 436 id, url, true /* user_gesture */, | 538 id, url, true /* user_gesture */, |
| 437 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 539 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 438 base::Unretained(&permission_context))); | 540 base::Unretained(&permission_context))); |
| 439 | 541 |
| 440 RespondToPermission(&permission_context, id, url, true, /* persist */ | |
| 441 CONTENT_SETTING_ALLOW); | |
| 442 ASSERT_EQ(1u, permission_context.decisions().size()); | 542 ASSERT_EQ(1u, permission_context.decisions().size()); |
| 443 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); | 543 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); |
| 444 EXPECT_TRUE(permission_context.tab_context_updated()); | 544 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 445 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 545 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 446 permission_context.GetContentSettingFromMap(url, url)); | 546 permission_context.GetContentSettingFromMap(url, url)); |
| 447 | 547 |
| 448 // Try to reset permission. | 548 // Try to reset permission. |
| 449 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); | 549 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); |
| 450 ContentSetting setting_after_reset = | 550 ContentSetting setting_after_reset = |
| 451 permission_context.GetContentSettingFromMap(url, url); | 551 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"); | 583 GURL url("http://www.google.com"); |
| 484 NavigateAndCommit(url); | 584 NavigateAndCommit(url); |
| 485 | 585 |
| 486 const PermissionRequestID id0( | 586 const PermissionRequestID id0( |
| 487 web_contents()->GetRenderProcessHost()->GetID(), | 587 web_contents()->GetRenderProcessHost()->GetID(), |
| 488 web_contents()->GetMainFrame()->GetRoutingID(), 0); | 588 web_contents()->GetMainFrame()->GetRoutingID(), 0); |
| 489 const PermissionRequestID id1( | 589 const PermissionRequestID id1( |
| 490 web_contents()->GetRenderProcessHost()->GetID(), | 590 web_contents()->GetRenderProcessHost()->GetID(), |
| 491 web_contents()->GetMainFrame()->GetRoutingID(), 1); | 591 web_contents()->GetMainFrame()->GetRoutingID(), 1); |
| 492 | 592 |
| 593 bool persist = (response == CONTENT_SETTING_ALLOW || | |
| 594 response == CONTENT_SETTING_BLOCK); | |
| 595 | |
| 596 // Request a permission without setting the callback to DecidePermission. | |
| 493 permission_context.RequestPermission( | 597 permission_context.RequestPermission( |
| 494 web_contents(), id0, url, true /* user_gesture */, | 598 web_contents(), id0, url, true /* user_gesture */, |
| 495 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 599 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 496 base::Unretained(&permission_context))); | 600 base::Unretained(&permission_context))); |
| 601 | |
| 602 EXPECT_EQ(0u, permission_context.decisions().size()); | |
| 603 | |
| 604 // Set the callback, and make a second permission request. | |
| 605 permission_context.SetRespondPermissionCallback( | |
| 606 base::Bind(&PermissionContextBaseTests::RespondToPermission, | |
| 607 base::Unretained(this), &permission_context, id0, url, | |
| 608 persist, response)); | |
| 497 permission_context.RequestPermission( | 609 permission_context.RequestPermission( |
| 498 web_contents(), id1, url, true /* user_gesture */, | 610 web_contents(), id1, url, true /* user_gesture */, |
| 499 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 611 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 500 base::Unretained(&permission_context))); | 612 base::Unretained(&permission_context))); |
| 501 | 613 |
| 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()); | 614 ASSERT_EQ(2u, permission_context.decisions().size()); |
| 509 EXPECT_EQ(response, permission_context.decisions()[0]); | 615 EXPECT_EQ(response, permission_context.decisions()[0]); |
| 510 EXPECT_EQ(response, permission_context.decisions()[1]); | 616 EXPECT_EQ(response, permission_context.decisions()[1]); |
| 511 EXPECT_TRUE(permission_context.tab_context_updated()); | 617 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 512 | 618 |
| 513 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); | 619 EXPECT_EQ(response, permission_context.GetContentSettingFromMap(url, url)); |
| 514 } | 620 } |
| 515 | 621 |
| 622 void TestPermissionsBlacklisting( | |
| 623 content::PermissionType permission_type, | |
| 624 ContentSettingsType content_settings_type, | |
| 625 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, | |
| 626 const GURL& url, | |
| 627 int timeout, | |
| 628 ContentSetting response) { | |
| 629 NavigateAndCommit(url); | |
| 630 base::test::ScopedFeatureList scoped_feature_list; | |
| 631 scoped_feature_list.InitAndEnableFeature(features::kPermissionsBlacklist); | |
| 632 TestPermissionContext permission_context(profile(), permission_type, | |
| 633 content_settings_type); | |
| 634 permission_context.SetSafeBrowsingDatabaseManagerAndTimeoutForTest( | |
| 635 db_manager, timeout); | |
| 636 const PermissionRequestID id( | |
| 637 web_contents()->GetRenderProcessHost()->GetID(), | |
| 638 web_contents()->GetMainFrame()->GetRoutingID(), -1); | |
| 639 permission_context.SetRespondPermissionCallback(base::Bind( | |
| 640 &PermissionContextBaseTests::RespondToPermission, | |
| 641 base::Unretained(this), &permission_context, id, url, false, response)); | |
|
raymes
2017/01/10 02:57:15
Is this needed?
meredithl
2017/01/10 03:40:46
Turns out for simplicity it is, because we also wa
| |
| 642 permission_context.RequestPermission( | |
| 643 web_contents(), id, url, true /* user_gesture */, | |
| 644 base::Bind(&TestPermissionContext::TrackPermissionDecision, | |
| 645 base::Unretained(&permission_context))); | |
| 646 | |
| 647 ASSERT_EQ(1u, permission_context.decisions().size()); | |
| 648 EXPECT_EQ(response, permission_context.decisions()[0]); | |
| 649 } | |
| 650 | |
| 516 private: | 651 private: |
| 517 // ChromeRenderViewHostTestHarness: | 652 // ChromeRenderViewHostTestHarness: |
| 518 void SetUp() override { | 653 void SetUp() override { |
| 519 ChromeRenderViewHostTestHarness::SetUp(); | 654 ChromeRenderViewHostTestHarness::SetUp(); |
| 520 #if defined(OS_ANDROID) | 655 #if defined(OS_ANDROID) |
| 521 InfoBarService::CreateForWebContents(web_contents()); | 656 InfoBarService::CreateForWebContents(web_contents()); |
| 522 #else | 657 #else |
| 523 PermissionRequestManager::CreateForWebContents(web_contents()); | 658 PermissionRequestManager::CreateForWebContents(web_contents()); |
| 524 #endif | 659 #endif |
| 525 } | 660 } |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 660 TestParallelRequests(CONTENT_SETTING_ALLOW); | 795 TestParallelRequests(CONTENT_SETTING_ALLOW); |
| 661 } | 796 } |
| 662 | 797 |
| 663 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { | 798 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { |
| 664 TestParallelRequests(CONTENT_SETTING_BLOCK); | 799 TestParallelRequests(CONTENT_SETTING_BLOCK); |
| 665 } | 800 } |
| 666 | 801 |
| 667 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { | 802 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { |
| 668 TestParallelRequests(CONTENT_SETTING_ASK); | 803 TestParallelRequests(CONTENT_SETTING_ASK); |
| 669 } | 804 } |
| 805 | |
| 806 // Tests a blacklisted (URL, permission) pair has had its permission request | |
| 807 // blocked. | |
| 808 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingBlocked) { | |
| 809 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
| 810 new MockSafeBrowsingDatabaseManager(true); | |
|
raymes
2017/01/09 06:28:18
nit: here and below, document the bool parameter t
meredithl
2017/01/10 02:24:57
Done.
| |
| 811 const GURL url("https://www.example.com"); | |
| 812 std::set<std::string> blacklisted_permissions{ | |
| 813 PermissionUtil::GetPermissionString( | |
| 814 content::PermissionType::GEOLOCATION)}; | |
| 815 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
| 816 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, | |
| 817 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, | |
| 818 url, 2000 /* timeout */, CONTENT_SETTING_BLOCK); | |
| 819 } | |
| 820 | |
| 821 // Tests that a URL with a blacklisted permission is permitted to request a | |
| 822 // non-blacklisted permission. | |
| 823 TEST_F(PermissionContextBaseTests, TestPermissionsBlacklistingAllowed) { | |
| 824 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
| 825 new MockSafeBrowsingDatabaseManager( | |
| 826 true /* run client with result method immediately */); | |
|
raymes
2017/01/09 06:28:18
nit: just put perform_callback. If you want to add
meredithl
2017/01/10 02:24:57
Done.
| |
| 827 const GURL url("https://www.example.com"); | |
| 828 std::set<std::string> blacklisted_permissions{ | |
| 829 PermissionUtil::GetPermissionString( | |
| 830 content::PermissionType::GEOLOCATION)}; | |
| 831 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
| 832 TestPermissionsBlacklisting( | |
| 833 content::PermissionType::GEOLOCATION, CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 834 db_manager, url, 2000 /* timeout in ms */, CONTENT_SETTING_BLOCK); | |
| 835 TestPermissionsBlacklisting(content::PermissionType::NOTIFICATIONS, | |
| 836 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, db_manager, | |
| 837 url, 2000 /* timeout in ms */, | |
| 838 CONTENT_SETTING_ASK); | |
| 839 } | |
| 840 | |
| 841 // Tests that a URL with a blacklisted permisison is permitted to request that | |
| 842 // permission if Safe Browsing has timed out. | |
| 843 TEST_F(PermissionContextBaseTests, TestSafeBrowsingTimeout) { | |
| 844 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | |
| 845 new MockSafeBrowsingDatabaseManager( | |
| 846 false /* don't call client with result */); | |
|
raymes
2017/01/09 06:28:18
nit: same thing here
meredithl
2017/01/10 02:24:57
Done.
| |
| 847 const GURL url("https://www.example.com"); | |
| 848 std::set<std::string> blacklisted_permissions{ | |
| 849 PermissionUtil::GetPermissionString( | |
| 850 content::PermissionType::GEOLOCATION)}; | |
| 851 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | |
| 852 TestPermissionsBlacklisting(content::PermissionType::GEOLOCATION, | |
| 853 CONTENT_SETTINGS_TYPE_GEOLOCATION, db_manager, | |
| 854 url, 0 /* timeout in ms */, CONTENT_SETTING_ASK); | |
| 855 } | |
| OLD | NEW |