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