OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sensor/sensor_permission_context.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/macros.h" |
| 9 #include "build/build_config.h" |
| 10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 11 #include "chrome/browser/permissions/permission_request_id.h" |
| 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 15 #include "components/content_settings/core/common/content_settings.h" |
| 16 #include "components/content_settings/core/common/content_settings_types.h" |
| 17 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/test/mock_render_process_host.h" |
| 19 #include "content/public/test/web_contents_tester.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 #if defined(OS_ANDROID) |
| 23 #include "chrome/browser/infobars/infobar_service.h" |
| 24 #else |
| 25 #include "chrome/browser/permissions/permission_request_manager.h" |
| 26 #endif |
| 27 |
| 28 namespace { |
| 29 |
| 30 class TestSensorPermissionContext : public SensorPermissionContext { |
| 31 public: |
| 32 explicit TestSensorPermissionContext(Profile* profile) |
| 33 : SensorPermissionContext(profile), |
| 34 permission_set_(false), |
| 35 permission_granted_(false), |
| 36 tab_context_updated_(false) {} |
| 37 |
| 38 ~TestSensorPermissionContext() override {} |
| 39 |
| 40 bool permission_granted() { return permission_granted_; } |
| 41 |
| 42 bool permission_set() { return permission_set_; } |
| 43 |
| 44 bool tab_context_updated() { return tab_context_updated_; } |
| 45 |
| 46 void TrackPermissionDecision(ContentSetting content_setting) { |
| 47 permission_set_ = true; |
| 48 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW; |
| 49 } |
| 50 |
| 51 protected: |
| 52 void UpdateTabContext(const PermissionRequestID& id, |
| 53 const GURL& requesting_origin, |
| 54 bool allowed) override { |
| 55 tab_context_updated_ = true; |
| 56 } |
| 57 |
| 58 private: |
| 59 bool permission_set_; |
| 60 bool permission_granted_; |
| 61 bool tab_context_updated_; |
| 62 }; |
| 63 |
| 64 } // anonymous namespace |
| 65 |
| 66 class SensorPermissionContextTests : public ChromeRenderViewHostTestHarness { |
| 67 protected: |
| 68 SensorPermissionContextTests() = default; |
| 69 |
| 70 private: |
| 71 // ChromeRenderViewHostTestHarness: |
| 72 void SetUp() override { |
| 73 ChromeRenderViewHostTestHarness::SetUp(); |
| 74 #if defined(OS_ANDROID) |
| 75 InfoBarService::CreateForWebContents(web_contents()); |
| 76 #else |
| 77 PermissionRequestManager::CreateForWebContents(web_contents()); |
| 78 #endif |
| 79 } |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(SensorPermissionContextTests); |
| 82 }; |
| 83 |
| 84 // Sensor permission should be denied for insecure origin. |
| 85 TEST_F(SensorPermissionContextTests, TestInsecureRequestingUrl) { |
| 86 TestSensorPermissionContext permission_context(profile()); |
| 87 GURL url("http://www.example.com"); |
| 88 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
| 89 |
| 90 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 91 web_contents()->GetMainFrame()->GetRoutingID(), |
| 92 -1); |
| 93 permission_context.RequestPermission( |
| 94 web_contents(), id, url, true, |
| 95 base::Bind(&TestSensorPermissionContext::TrackPermissionDecision, |
| 96 base::Unretained(&permission_context))); |
| 97 |
| 98 EXPECT_TRUE(permission_context.permission_set()); |
| 99 EXPECT_FALSE(permission_context.permission_granted()); |
| 100 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 101 |
| 102 ContentSetting setting = |
| 103 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 104 ->GetContentSetting(url.GetOrigin(), url.GetOrigin(), |
| 105 CONTENT_SETTINGS_TYPE_SENSORS, std::string()); |
| 106 EXPECT_EQ(CONTENT_SETTING_ASK, setting); |
| 107 } |
| 108 |
| 109 // Sensor permission status should be denied for insecure origin. |
| 110 TEST_F(SensorPermissionContextTests, TestInsecureQueryingUrl) { |
| 111 TestSensorPermissionContext permission_context(profile()); |
| 112 GURL insecure_url("http://www.example.com"); |
| 113 GURL secure_url("https://www.example.com"); |
| 114 |
| 115 // Check that there is no saved content settings. |
| 116 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 117 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 118 ->GetContentSetting( |
| 119 insecure_url.GetOrigin(), insecure_url.GetOrigin(), |
| 120 CONTENT_SETTINGS_TYPE_SENSORS, std::string())); |
| 121 EXPECT_EQ( |
| 122 CONTENT_SETTING_ASK, |
| 123 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 124 ->GetContentSetting(secure_url.GetOrigin(), insecure_url.GetOrigin(), |
| 125 CONTENT_SETTINGS_TYPE_SENSORS, std::string())); |
| 126 EXPECT_EQ( |
| 127 CONTENT_SETTING_ASK, |
| 128 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 129 ->GetContentSetting(insecure_url.GetOrigin(), secure_url.GetOrigin(), |
| 130 CONTENT_SETTINGS_TYPE_SENSORS, std::string())); |
| 131 |
| 132 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 133 permission_context.GetPermissionStatus(insecure_url, insecure_url)); |
| 134 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 135 permission_context.GetPermissionStatus(insecure_url, secure_url)); |
| 136 } |
OLD | NEW |