| 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" |
| 11 #include "chrome/browser/permissions/permission_queue_controller.h" | 14 #include "chrome/browser/permissions/permission_queue_controller.h" |
| 12 #include "chrome/browser/permissions/permission_request_id.h" | 15 #include "chrome/browser/permissions/permission_request_id.h" |
| 13 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 16 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
| 14 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 20 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "components/content_settings/core/common/content_settings.h" | 21 #include "components/content_settings/core/common/content_settings.h" |
| 19 #include "components/content_settings/core/common/content_settings_types.h" | 22 #include "components/content_settings/core/common/content_settings_types.h" |
| 23 #include "components/variations/variations_associated_data.h" |
| 20 #include "content/public/browser/render_frame_host.h" | 24 #include "content/public/browser/render_frame_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/test/mock_render_process_host.h" | 26 #include "content/public/test/mock_render_process_host.h" |
| 23 #include "content/public/test/web_contents_tester.h" | 27 #include "content/public/test/web_contents_tester.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 29 |
| 30 static const char* kApiKillSwitchFieldStudy = |
| 31 PermissionContextBase::kApiKillSwitchFieldStudy; |
| 32 static const char* kApiKillSwitchFieldParamEnabledValue = |
| 33 PermissionContextBase::kApiKillSwitchFieldParamEnabledValue; |
| 34 static const char kApiKillSwitchTestGroup[] = "TestGroup"; |
| 35 |
| 26 class TestPermissionContext : public PermissionContextBase { | 36 class TestPermissionContext : public PermissionContextBase { |
| 27 public: | 37 public: |
| 28 TestPermissionContext(Profile* profile, | 38 TestPermissionContext(Profile* profile, |
| 29 const ContentSettingsType permission_type) | 39 const ContentSettingsType permission_type) |
| 30 : PermissionContextBase(profile, permission_type), | 40 : PermissionContextBase(profile, permission_type), |
| 31 permission_set_(false), | 41 permission_set_(false), |
| 32 permission_granted_(false), | 42 permission_granted_(false), |
| 33 tab_context_updated_(false) {} | 43 tab_context_updated_(false), |
| 44 field_trial_list_(new base::FieldTrialList(new base::MockEntropyProvider)) |
| 45 {} |
| 34 | 46 |
| 35 ~TestPermissionContext() override {} | 47 ~TestPermissionContext() override {} |
| 36 | 48 |
| 37 PermissionQueueController* GetInfoBarController() { | 49 PermissionQueueController* GetInfoBarController() { |
| 38 return GetQueueController(); | 50 return GetQueueController(); |
| 39 } | 51 } |
| 40 | 52 |
| 41 bool permission_granted() { | 53 bool permission_granted() { |
| 42 return permission_granted_; | 54 return permission_granted_; |
| 43 } | 55 } |
| 44 | 56 |
| 45 bool permission_set() { | 57 bool permission_set() { |
| 46 return permission_set_; | 58 return permission_set_; |
| 47 } | 59 } |
| 48 | 60 |
| 49 bool tab_context_updated() { | 61 bool tab_context_updated() { |
| 50 return tab_context_updated_; | 62 return tab_context_updated_; |
| 51 } | 63 } |
| 52 | 64 |
| 53 void TrackPermissionDecision(ContentSetting content_setting) { | 65 void TrackPermissionDecision(ContentSetting content_setting) { |
| 54 permission_set_ = true; | 66 permission_set_ = true; |
| 55 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW; | 67 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW; |
| 56 } | 68 } |
| 57 | 69 |
| 70 void ResetFieldTrialList() { |
| 71 // Destroy the existing FieldTrialList before creating a new one to avoid |
| 72 // a DCHECK. |
| 73 field_trial_list_.reset(); |
| 74 field_trial_list_.reset(new base::FieldTrialList( |
| 75 new base::MockEntropyProvider)); |
| 76 variations::testing::ClearAllVariationParams(); |
| 77 } |
| 78 |
| 58 protected: | 79 protected: |
| 59 void UpdateTabContext(const PermissionRequestID& id, | 80 void UpdateTabContext(const PermissionRequestID& id, |
| 60 const GURL& requesting_origin, | 81 const GURL& requesting_origin, |
| 61 bool allowed) override { | 82 bool allowed) override { |
| 62 tab_context_updated_ = true; | 83 tab_context_updated_ = true; |
| 63 } | 84 } |
| 64 | 85 |
| 65 bool IsRestrictedToSecureOrigins() const override { | 86 bool IsRestrictedToSecureOrigins() const override { |
| 66 return false; | 87 return false; |
| 67 } | 88 } |
| 68 | 89 |
| 69 private: | 90 private: |
| 70 bool permission_set_; | 91 bool permission_set_; |
| 71 bool permission_granted_; | 92 bool permission_granted_; |
| 72 bool tab_context_updated_; | 93 bool tab_context_updated_; |
| 94 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 73 }; | 95 }; |
| 74 | 96 |
| 75 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { | 97 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { |
| 76 protected: | 98 protected: |
| 77 PermissionContextBaseTests() {} | 99 PermissionContextBaseTests() {} |
| 78 | 100 |
| 79 // Accept or dismiss the permission bubble or infobar. | 101 // Accept or dismiss the permission bubble or infobar. |
| 80 void RespondToPermission(TestPermissionContext* context, | 102 void RespondToPermission(TestPermissionContext* context, |
| 81 const PermissionRequestID& id, | 103 const PermissionRequestID& id, |
| 82 const GURL& url, | 104 const GURL& url, |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 ->GetContentSetting(url.GetOrigin(), | 242 ->GetContentSetting(url.GetOrigin(), |
| 221 url.GetOrigin(), | 243 url.GetOrigin(), |
| 222 type, | 244 type, |
| 223 std::string()); | 245 std::string()); |
| 224 ContentSetting default_setting = | 246 ContentSetting default_setting = |
| 225 HostContentSettingsMapFactory::GetForProfile(profile()) | 247 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 226 ->GetDefaultContentSetting(type, nullptr); | 248 ->GetDefaultContentSetting(type, nullptr); |
| 227 EXPECT_EQ(default_setting, setting_after_reset); | 249 EXPECT_EQ(default_setting, setting_after_reset); |
| 228 } | 250 } |
| 229 | 251 |
| 252 void TestGlobalApiKillSwitch(ContentSettingsType type) { |
| 253 TestPermissionContext permission_context( |
| 254 profile(), type); |
| 255 permission_context.ResetFieldTrialList(); |
| 256 |
| 257 EXPECT_FALSE(permission_context.IsApiKillSwitchOn()); |
| 258 std::map<std::string, std::string> params; |
| 259 params[PermissionContextUmaUtil::GetPermissionString(type)] = |
| 260 kApiKillSwitchFieldParamEnabledValue; |
| 261 variations::AssociateVariationParams( |
| 262 kApiKillSwitchFieldStudy, kApiKillSwitchTestGroup, params); |
| 263 base::FieldTrialList::CreateFieldTrial(kApiKillSwitchFieldStudy, |
| 264 kApiKillSwitchTestGroup); |
| 265 EXPECT_TRUE(permission_context.IsApiKillSwitchOn()); |
| 266 } |
| 267 |
| 230 private: | 268 private: |
| 231 // ChromeRenderViewHostTestHarness: | 269 // ChromeRenderViewHostTestHarness: |
| 232 void SetUp() override { | 270 void SetUp() override { |
| 233 ChromeRenderViewHostTestHarness::SetUp(); | 271 ChromeRenderViewHostTestHarness::SetUp(); |
| 234 InfoBarService::CreateForWebContents(web_contents()); | 272 InfoBarService::CreateForWebContents(web_contents()); |
| 235 PermissionBubbleManager::CreateForWebContents(web_contents()); | 273 PermissionBubbleManager::CreateForWebContents(web_contents()); |
| 236 } | 274 } |
| 237 | 275 |
| 238 DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests); | 276 DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests); |
| 239 }; | 277 }; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 #if defined(ENABLE_NOTIFICATIONS) | 328 #if defined(ENABLE_NOTIFICATIONS) |
| 291 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 329 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 292 CONTENT_SETTING_ASK); | 330 CONTENT_SETTING_ASK); |
| 293 #endif | 331 #endif |
| 294 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | 332 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, |
| 295 CONTENT_SETTING_ASK); | 333 CONTENT_SETTING_ASK); |
| 296 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | 334 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, |
| 297 CONTENT_SETTING_ASK); | 335 CONTENT_SETTING_ASK); |
| 298 } | 336 } |
| 299 #endif | 337 #endif |
| 338 |
| 339 // Tests the global kill switch by enabling/disabling the Field Trials. |
| 340 TEST_F(PermissionContextBaseTests, TestGlobalKillSwitch) { |
| 341 TestGlobalApiKillSwitch(CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 342 TestGlobalApiKillSwitch(CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 343 TestGlobalApiKillSwitch(CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
| 344 TestGlobalApiKillSwitch(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); |
| 345 TestGlobalApiKillSwitch(CONTENT_SETTINGS_TYPE_DURABLE_STORAGE); |
| 346 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 347 TestGlobalApiKillSwitch(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER); |
| 348 #endif |
| 349 TestGlobalApiKillSwitch(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); |
| 350 TestGlobalApiKillSwitch(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); |
| 351 } |
| OLD | NEW |