| 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" | 
 |   17 #include "device/vr/features/features.h" | 
|   16 #include "testing/gtest/include/gtest/gtest.h" |   18 #include "testing/gtest/include/gtest/gtest.h" | 
|   17  |   19  | 
 |   20 #if BUILDFLAG(ENABLE_VR) | 
 |   21 #include "chrome/browser/android/vr_shell/vr_tab_helper.h" | 
 |   22 #endif  // BUILDFLAG(ENABLE_VR) | 
 |   23  | 
|   18 using blink::mojom::PermissionStatus; |   24 using blink::mojom::PermissionStatus; | 
|   19 using content::PermissionType; |   25 using content::PermissionType; | 
|   20  |   26  | 
|   21 namespace { |   27 namespace { | 
|   22  |   28  | 
 |   29 #if BUILDFLAG(ENABLE_VR) | 
 |   30 int kNoPendingOperation = -1; | 
 |   31 #endif  // BUILDFLAG(ENABLE_VR) | 
 |   32  | 
|   23 class PermissionManagerTestingProfile final : public TestingProfile { |   33 class PermissionManagerTestingProfile final : public TestingProfile { | 
|   24  public: |   34  public: | 
|   25   PermissionManagerTestingProfile() {} |   35   PermissionManagerTestingProfile() {} | 
|   26   ~PermissionManagerTestingProfile() override {} |   36   ~PermissionManagerTestingProfile() override {} | 
|   27  |   37  | 
|   28   PermissionManager* GetPermissionManager() override { |   38   PermissionManager* GetPermissionManager() override { | 
|   29     return PermissionManagerFactory::GetForProfile(this); |   39     return PermissionManagerFactory::GetForProfile(this); | 
|   30   } |   40   } | 
|   31  |   41  | 
|   32   DISALLOW_COPY_AND_ASSIGN(PermissionManagerTestingProfile); |   42   DISALLOW_COPY_AND_ASSIGN(PermissionManagerTestingProfile); | 
|   33 }; |   43 }; | 
|   34  |   44  | 
|   35 }  // anonymous namespace |   45 }  // anonymous namespace | 
|   36  |   46  | 
|   37 class PermissionManagerTest : public testing::Test { |   47 class PermissionManagerTest : public ChromeRenderViewHostTestHarness { | 
|   38  public: |   48  public: | 
|   39   void OnPermissionChange(PermissionStatus permission) { |   49   void OnPermissionChange(PermissionStatus permission) { | 
|   40     callback_called_ = true; |   50     callback_called_ = true; | 
|   41     callback_result_ = permission; |   51     callback_result_ = permission; | 
|   42   } |   52   } | 
|   43  |   53  | 
|   44  protected: |   54  protected: | 
|   45   PermissionManagerTest() |   55   PermissionManagerTest() | 
|   46       : url_("https://example.com"), |   56       : url_("https://example.com"), | 
|   47         other_url_("https://foo.com"), |   57         other_url_("https://foo.com"), | 
|   48         callback_called_(false), |   58         callback_called_(false), | 
|   49         callback_result_(PermissionStatus::ASK) {} |   59         callback_result_(PermissionStatus::ASK) {} | 
|   50  |   60  | 
|   51   PermissionManager* GetPermissionManager() { |   61   PermissionManager* GetPermissionManager() { | 
|   52     return profile_.GetPermissionManager(); |   62     return profile_->GetPermissionManager(); | 
|   53   } |   63   } | 
|   54  |   64  | 
|   55   HostContentSettingsMap* GetHostContentSettingsMap() { |   65   HostContentSettingsMap* GetHostContentSettingsMap() { | 
|   56     return HostContentSettingsMapFactory::GetForProfile(&profile_); |   66     return HostContentSettingsMapFactory::GetForProfile(profile_.get()); | 
|   57   } |   67   } | 
|   58  |   68  | 
|   59   void CheckPermissionStatus(PermissionType type, |   69   void CheckPermissionStatus(PermissionType type, | 
|   60                              PermissionStatus expected) { |   70                              PermissionStatus expected) { | 
|   61     EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatus( |   71     EXPECT_EQ(expected, GetPermissionManager()->GetPermissionStatus( | 
|   62                             type, url_.GetOrigin(), url_.GetOrigin())); |   72                             type, url_.GetOrigin(), url_.GetOrigin())); | 
|   63   } |   73   } | 
|   64  |   74  | 
|   65   void CheckPermissionResult(ContentSettingsType type, |   75   void CheckPermissionResult(ContentSettingsType type, | 
|   66                              ContentSetting expected_status, |   76                              ContentSetting expected_status, | 
|   67                              PermissionStatusSource expected_status_source) { |   77                              PermissionStatusSource expected_status_source) { | 
|   68     PermissionResult result = GetPermissionManager()->GetPermissionStatus( |   78     PermissionResult result = GetPermissionManager()->GetPermissionStatus( | 
|   69         type, url_.GetOrigin(), url_.GetOrigin()); |   79         type, url_.GetOrigin(), url_.GetOrigin()); | 
|   70     EXPECT_EQ(expected_status, result.content_setting); |   80     EXPECT_EQ(expected_status, result.content_setting); | 
|   71     EXPECT_EQ(expected_status_source, result.source); |   81     EXPECT_EQ(expected_status_source, result.source); | 
|   72   } |   82   } | 
|   73  |   83  | 
|   74   void SetPermission(ContentSettingsType type, ContentSetting value) { |   84   void SetPermission(ContentSettingsType type, ContentSetting value) { | 
|   75     HostContentSettingsMapFactory::GetForProfile(&profile_) |   85     HostContentSettingsMapFactory::GetForProfile(profile_.get()) | 
|   76         ->SetContentSettingDefaultScope(url_, url_, type, std::string(), value); |   86         ->SetContentSettingDefaultScope(url_, url_, type, std::string(), value); | 
|   77   } |   87   } | 
|   78  |   88  | 
|   79   const GURL& url() const { |   89   const GURL& url() const { | 
|   80     return url_; |   90     return url_; | 
|   81   } |   91   } | 
|   82  |   92  | 
|   83   const GURL& other_url() const { |   93   const GURL& other_url() const { | 
|   84     return other_url_; |   94     return other_url_; | 
|   85   } |   95   } | 
|   86  |   96  | 
|   87   bool callback_called() const { |   97   bool callback_called() const { | 
|   88     return callback_called_; |   98     return callback_called_; | 
|   89   } |   99   } | 
|   90  |  100  | 
|   91   PermissionStatus callback_result() const { return callback_result_; } |  101   PermissionStatus callback_result() const { return callback_result_; } | 
|   92  |  102  | 
|   93   void Reset() { |  103   void Reset() { | 
|   94     callback_called_ = false; |  104     callback_called_ = false; | 
|   95     callback_result_ = PermissionStatus::ASK; |  105     callback_result_ = PermissionStatus::ASK; | 
|   96   } |  106   } | 
|   97  |  107  | 
|   98  private: |  108  private: | 
 |  109   void SetUp() override { | 
 |  110     ChromeRenderViewHostTestHarness::SetUp(); | 
 |  111     profile_.reset(new PermissionManagerTestingProfile()); | 
 |  112   } | 
 |  113  | 
 |  114   void TearDown() override { | 
 |  115     profile_.reset(); | 
 |  116     ChromeRenderViewHostTestHarness::TearDown(); | 
 |  117   } | 
 |  118  | 
|   99   const GURL url_; |  119   const GURL url_; | 
|  100   const GURL other_url_; |  120   const GURL other_url_; | 
|  101   bool callback_called_; |  121   bool callback_called_; | 
|  102   PermissionStatus callback_result_; |  122   PermissionStatus callback_result_; | 
|  103   content::TestBrowserThreadBundle thread_bundle_; |  123   std::unique_ptr<PermissionManagerTestingProfile> profile_; | 
|  104   PermissionManagerTestingProfile profile_; |  | 
|  105 }; |  124 }; | 
|  106  |  125  | 
|  107 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) { |  126 TEST_F(PermissionManagerTest, GetPermissionStatusDefault) { | 
|  108   CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK); |  127   CheckPermissionStatus(PermissionType::MIDI_SYSEX, PermissionStatus::ASK); | 
|  109   CheckPermissionStatus(PermissionType::PUSH_MESSAGING, PermissionStatus::ASK); |  128   CheckPermissionStatus(PermissionType::PUSH_MESSAGING, PermissionStatus::ASK); | 
|  110   CheckPermissionStatus(PermissionType::NOTIFICATIONS, PermissionStatus::ASK); |  129   CheckPermissionStatus(PermissionType::NOTIFICATIONS, PermissionStatus::ASK); | 
|  111   CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); |  130   CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); | 
|  112 #if defined(OS_ANDROID) |  131 #if defined(OS_ANDROID) | 
|  113   CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, |  132   CheckPermissionStatus(PermissionType::PROTECTED_MEDIA_IDENTIFIER, | 
|  114                         PermissionStatus::ASK); |  133                         PermissionStatus::ASK); | 
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  373   CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); |  392   CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::ASK); | 
|  374   GetHostContentSettingsMap()->SetContentSettingDefaultScope( |  393   GetHostContentSettingsMap()->SetContentSettingDefaultScope( | 
|  375       url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), |  394       url(), url(), CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), | 
|  376       CONTENT_SETTING_ALLOW); |  395       CONTENT_SETTING_ALLOW); | 
|  377   CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED); |  396   CheckPermissionStatus(PermissionType::GEOLOCATION, PermissionStatus::GRANTED); | 
|  378  |  397  | 
|  379   EXPECT_FALSE(callback_called()); |  398   EXPECT_FALSE(callback_called()); | 
|  380  |  399  | 
|  381   GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); |  400   GetPermissionManager()->UnsubscribePermissionStatusChange(subscription_id); | 
|  382 } |  401 } | 
 |  402  | 
 |  403 #if BUILDFLAG(ENABLE_VR) | 
 |  404 TEST_F(PermissionManagerTest, SuppressPermissionRequests) { | 
 |  405   content::WebContents* contents = web_contents(); | 
 |  406   vr_shell::VrTabHelper::CreateForWebContents(contents); | 
 |  407   NavigateAndCommit(url()); | 
 |  408  | 
 |  409   SetPermission(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_ALLOW); | 
 |  410   GetPermissionManager()->RequestPermission( | 
 |  411       PermissionType::NOTIFICATIONS, main_rfh(), url(), true, | 
 |  412       base::Bind(&PermissionManagerTest::OnPermissionChange, | 
 |  413                  base::Unretained(this))); | 
 |  414   EXPECT_TRUE(callback_called()); | 
 |  415   EXPECT_EQ(PermissionStatus::GRANTED, callback_result()); | 
 |  416  | 
 |  417   vr_shell::VrTabHelper* vr_tab_helper = | 
 |  418       vr_shell::VrTabHelper::FromWebContents(contents); | 
 |  419   vr_tab_helper->SetIsInVr(true); | 
 |  420   EXPECT_EQ( | 
 |  421       kNoPendingOperation, | 
 |  422       GetPermissionManager()->RequestPermission( | 
 |  423           PermissionType::NOTIFICATIONS, contents->GetMainFrame(), url(), false, | 
 |  424           base::Bind(&PermissionManagerTest::OnPermissionChange, | 
 |  425                      base::Unretained(this)))); | 
 |  426   EXPECT_TRUE(callback_called()); | 
 |  427   EXPECT_EQ(PermissionStatus::DENIED, callback_result()); | 
 |  428  | 
 |  429   vr_tab_helper->SetIsInVr(false); | 
 |  430   GetPermissionManager()->RequestPermission( | 
 |  431       PermissionType::NOTIFICATIONS, main_rfh(), url(), false, | 
 |  432       base::Bind(&PermissionManagerTest::OnPermissionChange, | 
 |  433                  base::Unretained(this))); | 
 |  434   EXPECT_TRUE(callback_called()); | 
 |  435   EXPECT_EQ(PermissionStatus::GRANTED, callback_result()); | 
 |  436 } | 
 |  437 #endif  // BUILDFLAG(ENABLE_VR) | 
| OLD | NEW |