| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_manager.h" | 5 #include "chrome/browser/permissions/permission_manager.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 10 #include "chrome/browser/permissions/permission_manager_factory.h" | 10 #include "chrome/browser/permissions/permission_manager_factory.h" |
| 11 #include "chrome/browser/permissions/permission_result.h" | 11 #include "chrome/browser/permissions/permission_result.h" |
| 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 12 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
| 13 #include "components/content_settings/core/browser/host_content_settings_map.h" | 14 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 14 #include "content/public/browser/permission_type.h" | 15 #include "content/public/browser/permission_type.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 using blink::mojom::PermissionStatus; | 19 using blink::mojom::PermissionStatus; |
| 19 using content::PermissionType; | 20 using content::PermissionType; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 class PermissionManagerTestingProfile final : public TestingProfile { | 24 class PermissionManagerTestingProfile final : public TestingProfile { |
| 24 public: | 25 public: |
| 25 PermissionManagerTestingProfile() {} | 26 PermissionManagerTestingProfile() {} |
| 26 ~PermissionManagerTestingProfile() override {} | 27 ~PermissionManagerTestingProfile() override {} |
| 27 | 28 |
| 28 PermissionManager* GetPermissionManager() override { | 29 PermissionManager* GetPermissionManager() override { |
| 29 return PermissionManagerFactory::GetForProfile(this); | 30 return PermissionManagerFactory::GetForProfile(this); |
| 30 } | 31 } |
| 31 | 32 |
| 32 DISALLOW_COPY_AND_ASSIGN(PermissionManagerTestingProfile); | 33 DISALLOW_COPY_AND_ASSIGN(PermissionManagerTestingProfile); |
| 33 }; | 34 }; |
| 34 | 35 |
| 35 } // anonymous namespace | 36 } // anonymous namespace |
| 36 | 37 |
| 37 class PermissionManagerTest : public testing::Test { | 38 class PermissionManagerTest : public ChromeRenderViewHostTestHarness { |
| 38 public: | 39 public: |
| 39 void OnPermissionChange(PermissionStatus permission) { | 40 void OnPermissionChange(PermissionStatus permission) { |
| 40 callback_called_ = true; | 41 callback_called_ = true; |
| 41 callback_result_ = permission; | 42 callback_result_ = permission; |
| 42 } | 43 } |
| 43 | 44 |
| 44 protected: | 45 protected: |
| 45 PermissionManagerTest() | 46 PermissionManagerTest() |
| 46 : url_("https://example.com"), | 47 : url_("https://example.com"), |
| 47 other_url_("https://foo.com"), | 48 other_url_("https://foo.com"), |
| 48 callback_called_(false), | 49 callback_called_(false), |
| 49 callback_result_(PermissionStatus::ASK) {} | 50 callback_result_(PermissionStatus::ASK) {} |
| 50 | 51 |
| 52 void SetUp() override { |
| 53 ChromeRenderViewHostTestHarness::SetUp(); |
| 54 profile_.reset(new PermissionManagerTestingProfile); |
| 55 NavigateAndCommit(url_); |
| 56 } |
| 57 |
| 58 void TearDown() override { |
| 59 profile_.reset(); |
| 60 ChromeRenderViewHostTestHarness::TearDown(); |
| 61 } |
| 62 |
| 51 PermissionManager* GetPermissionManager() { | 63 PermissionManager* GetPermissionManager() { |
| 52 return profile_.GetPermissionManager(); | 64 return profile_->GetPermissionManager(); |
| 53 } | 65 } |
| 54 | 66 |
| 55 HostContentSettingsMap* GetHostContentSettingsMap() { | 67 HostContentSettingsMap* GetHostContentSettingsMap() { |
| 56 return HostContentSettingsMapFactory::GetForProfile(&profile_); | 68 return HostContentSettingsMapFactory::GetForProfile(profile_.get()); |
| 57 } | 69 } |
| 58 | 70 |
| 59 void CheckPermissionStatus(PermissionType type, | 71 void CheckPermissionStatus(PermissionType type, |
| 60 PermissionStatus expected) { | 72 PermissionStatus expected) { |
| 61 EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatus( | 73 EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatusForFrame( |
| 62 type, url_.GetOrigin(), url_.GetOrigin())); | 74 type, main_rfh(), url_.GetOrigin())); |
| 63 } | 75 } |
| 64 | 76 |
| 65 void CheckPermissionResult(ContentSettingsType type, | 77 void CheckPermissionResult(ContentSettingsType type, |
| 66 ContentSetting expected_status, | 78 ContentSetting expected_status, |
| 67 PermissionStatusSource expected_status_source) { | 79 PermissionStatusSource expected_status_source) { |
| 68 PermissionResult result = GetPermissionManager()->GetPermissionStatus( | 80 PermissionResult result = |
| 69 type, url_.GetOrigin(), url_.GetOrigin()); | 81 GetPermissionManager()->GetPermissionStatusForFrame(type, main_rfh(), |
| 82 url_.GetOrigin()); |
| 70 EXPECT_EQ(expected_status, result.content_setting); | 83 EXPECT_EQ(expected_status, result.content_setting); |
| 71 EXPECT_EQ(expected_status_source, result.source); | 84 EXPECT_EQ(expected_status_source, result.source); |
| 72 } | 85 } |
| 73 | 86 |
| 74 void SetPermission(ContentSettingsType type, ContentSetting value) { | 87 void SetPermission(ContentSettingsType type, ContentSetting value) { |
| 75 HostContentSettingsMapFactory::GetForProfile(&profile_) | 88 HostContentSettingsMapFactory::GetForProfile(profile_.get()) |
| 76 ->SetContentSettingDefaultScope(url_, url_, type, std::string(), value); | 89 ->SetContentSettingDefaultScope(url_, url_, type, std::string(), value); |
| 77 } | 90 } |
| 78 | 91 |
| 79 const GURL& url() const { | 92 const GURL& url() const { |
| 80 return url_; | 93 return url_; |
| 81 } | 94 } |
| 82 | 95 |
| 83 const GURL& other_url() const { | 96 const GURL& other_url() const { |
| 84 return other_url_; | 97 return other_url_; |
| 85 } | 98 } |
| 86 | 99 |
| 87 bool callback_called() const { | 100 bool callback_called() const { |
| 88 return callback_called_; | 101 return callback_called_; |
| 89 } | 102 } |
| 90 | 103 |
| 91 PermissionStatus callback_result() const { return callback_result_; } | 104 PermissionStatus callback_result() const { return callback_result_; } |
| 92 | 105 |
| 93 void Reset() { | 106 void Reset() { |
| 94 callback_called_ = false; | 107 callback_called_ = false; |
| 95 callback_result_ = PermissionStatus::ASK; | 108 callback_result_ = PermissionStatus::ASK; |
| 96 } | 109 } |
| 97 | 110 |
| 98 private: | 111 private: |
| 99 const GURL url_; | 112 const GURL url_; |
| 100 const GURL other_url_; | 113 const GURL other_url_; |
| 101 bool callback_called_; | 114 bool callback_called_; |
| 102 PermissionStatus callback_result_; | 115 PermissionStatus callback_result_; |
| 103 content::TestBrowserThreadBundle thread_bundle_; | 116 std::unique_ptr<PermissionManagerTestingProfile> profile_; |
| 104 PermissionManagerTestingProfile profile_; | |
| 105 }; | 117 }; |
| 106 | 118 |
| 107 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) { | 119 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) { |
| 108 CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK); | 120 CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK); |
| 109 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, PermissionStatus::ASK); | 121 CheckPermissionStatus(PermissionType::PUSH_MESSAGING, PermissionStatus::ASK); |
| 110 CheckPermissionStatus(PermissionType::NOTIFICATIONS, PermissionStatus::ASK); | 122 CheckPermissionStatus(PermissionType::NOTIFICATIONS, PermissionStatus::ASK); |
| 111 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); | 123 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); |
| 112 #if defined(OS_ANDROID) | 124 #if defined(OS_ANDROID) |
| 113 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, | 125 CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, |
| 114 PermissionStatus::ASK); | 126 PermissionStatus::ASK); |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); | 385 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); |
| 374 GetHostContentSettingsMap()->SetContentSettingDefaultScope( | 386 GetHostContentSettingsMap()->SetContentSettingDefaultScope( |
| 375 url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), | 387 url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), |
| 376 CONTENT_SETTING_ALLOW); | 388 CONTENT_SETTING_ALLOW); |
| 377 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED); | 389 CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED); |
| 378 | 390 |
| 379 EXPECT_FALSE(callback_called()); | 391 EXPECT_FALSE(callback_called()); |
| 380 | 392 |
| 381 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); | 393 GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |
| 382 } | 394 } |
| 395 |
| 396 // Test that permissions that are allowed in workers are queried correctly. |
| 397 TEST_F(PermissionManagerTest, AllowedInWorker) { |
| 398 // Notifications is allowed from workers but should ASK by default. This |
| 399 // should return DENIED in a worker because it can't prompt. |
| 400 EXPECT_EQ(PermissionStatus::DENIED, |
| 401 GetPermissionManager()->GetPermissionStatusForWorker( |
| 402 PermissionType::NOTIFICATIONS, url())); |
| 403 |
| 404 // If it is allowed, it should return GRANTED. |
| 405 GetHostContentSettingsMap()->SetContentSettingDefaultScope( |
| 406 url(), url(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), |
| 407 CONTENT_SETTING_ALLOW); |
| 408 EXPECT_EQ(PermissionStatus::GRANTED, |
| 409 GetPermissionManager()->GetPermissionStatusForWorker( |
| 410 PermissionType::NOTIFICATIONS, url())); |
| 411 |
| 412 // Geolocation isn't allowed from workers so should return DENIED even if the |
| 413 // user has allowed it. |
| 414 GetHostContentSettingsMap()->SetContentSettingDefaultScope( |
| 415 url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), |
| 416 CONTENT_SETTING_ALLOW); |
| 417 EXPECT_EQ(PermissionStatus::DENIED, |
| 418 GetPermissionManager()->GetPermissionStatusForWorker( |
| 419 PermissionType::GEOLOCATION, url())); |
| 420 } |
| OLD | NEW |