| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/test/mock_entropy_provider.h" |
| 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 10 #include "chrome/browser/infobars/infobar_service.h" | 12 #include "chrome/browser/infobars/infobar_service.h" |
| 13 #include "chrome/browser/permissions/permission_context_uma_util.h" |
| 14 #include "chrome/browser/permissions/permission_queue_controller.h" |
| 11 #include "chrome/browser/permissions/permission_request_id.h" | 15 #include "chrome/browser/permissions/permission_request_id.h" |
| 16 #include "chrome/browser/permissions/permission_util.h" |
| 17 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
| 12 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 19 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 21 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 16 #include "components/content_settings/core/common/content_settings.h" | 22 #include "components/content_settings/core/common/content_settings.h" |
| 17 #include "components/content_settings/core/common/content_settings_types.h" | 23 #include "components/content_settings/core/common/content_settings_types.h" |
| 24 #include "components/variations/variations_associated_data.h" |
| 18 #include "content/public/browser/render_frame_host.h" | 25 #include "content/public/browser/render_frame_host.h" |
| 19 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/test/mock_render_process_host.h" | 27 #include "content/public/test/mock_render_process_host.h" |
| 21 #include "content/public/test/web_contents_tester.h" | 28 #include "content/public/test/web_contents_tester.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 30 |
| 24 #if defined(OS_ANDROID) | 31 #if defined(OS_ANDROID) |
| 25 #include "chrome/browser/permissions/permission_queue_controller.h" | 32 #include "chrome/browser/permissions/permission_queue_controller.h" |
| 26 #else | 33 #else |
| 27 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 34 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
| 28 #endif | 35 #endif |
| 29 | 36 |
| 37 const char* kPermissionsKillSwitchFieldStudy = |
| 38 PermissionContextBase::kPermissionsKillSwitchFieldStudy; |
| 39 const char* kPermissionsKillSwitchBlockedValue = |
| 40 PermissionContextBase::kPermissionsKillSwitchBlockedValue; |
| 41 const char kPermissionsKillSwitchTestGroup[] = "TestGroup"; |
| 42 |
| 30 class TestPermissionContext : public PermissionContextBase { | 43 class TestPermissionContext : public PermissionContextBase { |
| 31 public: | 44 public: |
| 32 TestPermissionContext(Profile* profile, | 45 TestPermissionContext(Profile* profile, |
| 33 const ContentSettingsType permission_type) | 46 const ContentSettingsType permission_type) |
| 34 : PermissionContextBase(profile, permission_type), | 47 : PermissionContextBase(profile, permission_type), |
| 35 permission_set_(false), | 48 permission_set_(false), |
| 36 permission_granted_(false), | 49 permission_granted_(false), |
| 37 tab_context_updated_(false) {} | 50 tab_context_updated_(false), |
| 51 field_trial_list_(new base::FieldTrialList(new base::MockEntropyProvider)) |
| 52 {} |
| 38 | 53 |
| 39 ~TestPermissionContext() override {} | 54 ~TestPermissionContext() override {} |
| 40 | 55 |
| 41 #if defined(OS_ANDROID) | 56 #if defined(OS_ANDROID) |
| 42 PermissionQueueController* GetInfoBarController() { | 57 PermissionQueueController* GetInfoBarController() { |
| 43 return GetQueueController(); | 58 return GetQueueController(); |
| 44 } | 59 } |
| 45 #endif | 60 #endif |
| 46 | 61 |
| 47 bool permission_granted() { | 62 bool permission_granted() { |
| 48 return permission_granted_; | 63 return permission_granted_; |
| 49 } | 64 } |
| 50 | 65 |
| 51 bool permission_set() { | 66 bool permission_set() { |
| 52 return permission_set_; | 67 return permission_set_; |
| 53 } | 68 } |
| 54 | 69 |
| 55 bool tab_context_updated() { | 70 bool tab_context_updated() { |
| 56 return tab_context_updated_; | 71 return tab_context_updated_; |
| 57 } | 72 } |
| 58 | 73 |
| 59 void TrackPermissionDecision(ContentSetting content_setting) { | 74 void TrackPermissionDecision(ContentSetting content_setting) { |
| 60 permission_set_ = true; | 75 permission_set_ = true; |
| 61 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW; | 76 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW; |
| 62 } | 77 } |
| 63 | 78 |
| 79 void ResetFieldTrialList() { |
| 80 // Destroy the existing FieldTrialList before creating a new one to avoid |
| 81 // a DCHECK. |
| 82 field_trial_list_.reset(); |
| 83 field_trial_list_.reset(new base::FieldTrialList( |
| 84 new base::MockEntropyProvider)); |
| 85 variations::testing::ClearAllVariationParams(); |
| 86 } |
| 87 |
| 64 protected: | 88 protected: |
| 65 void UpdateTabContext(const PermissionRequestID& id, | 89 void UpdateTabContext(const PermissionRequestID& id, |
| 66 const GURL& requesting_origin, | 90 const GURL& requesting_origin, |
| 67 bool allowed) override { | 91 bool allowed) override { |
| 68 tab_context_updated_ = true; | 92 tab_context_updated_ = true; |
| 69 } | 93 } |
| 70 | 94 |
| 71 bool IsRestrictedToSecureOrigins() const override { | 95 bool IsRestrictedToSecureOrigins() const override { |
| 72 return false; | 96 return false; |
| 73 } | 97 } |
| 74 | 98 |
| 75 private: | 99 private: |
| 76 bool permission_set_; | 100 bool permission_set_; |
| 77 bool permission_granted_; | 101 bool permission_granted_; |
| 78 bool tab_context_updated_; | 102 bool tab_context_updated_; |
| 103 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 79 }; | 104 }; |
| 80 | 105 |
| 81 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { | 106 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { |
| 82 protected: | 107 protected: |
| 83 PermissionContextBaseTests() {} | 108 PermissionContextBaseTests() {} |
| 84 | 109 |
| 85 // Accept or dismiss the permission bubble or infobar. | 110 // Accept or dismiss the permission bubble or infobar. |
| 86 void RespondToPermission(TestPermissionContext* context, | 111 void RespondToPermission(TestPermissionContext* context, |
| 87 const PermissionRequestID& id, | 112 const PermissionRequestID& id, |
| 88 const GURL& url, | 113 const GURL& url, |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 ->GetContentSetting(url.GetOrigin(), | 250 ->GetContentSetting(url.GetOrigin(), |
| 226 url.GetOrigin(), | 251 url.GetOrigin(), |
| 227 type, | 252 type, |
| 228 std::string()); | 253 std::string()); |
| 229 ContentSetting default_setting = | 254 ContentSetting default_setting = |
| 230 HostContentSettingsMapFactory::GetForProfile(profile()) | 255 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 231 ->GetDefaultContentSetting(type, nullptr); | 256 ->GetDefaultContentSetting(type, nullptr); |
| 232 EXPECT_EQ(default_setting, setting_after_reset); | 257 EXPECT_EQ(default_setting, setting_after_reset); |
| 233 } | 258 } |
| 234 | 259 |
| 260 void TestGlobalPermissionsKillSwitch(ContentSettingsType type) { |
| 261 TestPermissionContext permission_context(profile(), type); |
| 262 permission_context.ResetFieldTrialList(); |
| 263 |
| 264 EXPECT_FALSE(permission_context.IsPermissionKillSwitchOn()); |
| 265 std::map<std::string, std::string> params; |
| 266 params[PermissionUtil::GetPermissionString(type)] = |
| 267 kPermissionsKillSwitchBlockedValue; |
| 268 variations::AssociateVariationParams( |
| 269 kPermissionsKillSwitchFieldStudy, kPermissionsKillSwitchTestGroup, |
| 270 params); |
| 271 base::FieldTrialList::CreateFieldTrial(kPermissionsKillSwitchFieldStudy, |
| 272 kPermissionsKillSwitchTestGroup); |
| 273 EXPECT_TRUE(permission_context.IsPermissionKillSwitchOn()); |
| 274 } |
| 275 |
| 235 private: | 276 private: |
| 236 // ChromeRenderViewHostTestHarness: | 277 // ChromeRenderViewHostTestHarness: |
| 237 void SetUp() override { | 278 void SetUp() override { |
| 238 ChromeRenderViewHostTestHarness::SetUp(); | 279 ChromeRenderViewHostTestHarness::SetUp(); |
| 239 #if defined(OS_ANDROID) | 280 #if defined(OS_ANDROID) |
| 240 InfoBarService::CreateForWebContents(web_contents()); | 281 InfoBarService::CreateForWebContents(web_contents()); |
| 241 #else | 282 #else |
| 242 PermissionBubbleManager::CreateForWebContents(web_contents()); | 283 PermissionBubbleManager::CreateForWebContents(web_contents()); |
| 243 #endif | 284 #endif |
| 244 } | 285 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 #if defined(ENABLE_NOTIFICATIONS) | 339 #if defined(ENABLE_NOTIFICATIONS) |
| 299 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 340 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 300 CONTENT_SETTING_ASK); | 341 CONTENT_SETTING_ASK); |
| 301 #endif | 342 #endif |
| 302 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | 343 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, |
| 303 CONTENT_SETTING_ASK); | 344 CONTENT_SETTING_ASK); |
| 304 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | 345 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, |
| 305 CONTENT_SETTING_ASK); | 346 CONTENT_SETTING_ASK); |
| 306 } | 347 } |
| 307 #endif | 348 #endif |
| 349 |
| 350 // Tests the global kill switch by enabling/disabling the Field Trials. |
| 351 TEST_F(PermissionContextBaseTests, TestGlobalKillSwitch) { |
| 352 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 353 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 354 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
| 355 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); |
| 356 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_DURABLE_STORAGE); |
| 357 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 358 TestGlobalPermissionsKillSwitch( |
| 359 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER); |
| 360 #endif |
| 361 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); |
| 362 TestGlobalPermissionsKillSwitch(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); |
| 363 } |
| OLD | NEW |