| 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 "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/infobars/infobar_service.h" | 10 #include "chrome/browser/infobars/infobar_service.h" |
| 11 #include "chrome/browser/permissions/permission_infobar_manager.h" |
| 12 #include "chrome/browser/permissions/permission_infobar_request.h" |
| 11 #include "chrome/browser/permissions/permission_request_id.h" | 13 #include "chrome/browser/permissions/permission_request_id.h" |
| 12 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 16 #include "components/content_settings/core/common/content_settings.h" | 18 #include "components/content_settings/core/common/content_settings.h" |
| 17 #include "components/content_settings/core/common/content_settings_types.h" | 19 #include "components/content_settings/core/common/content_settings_types.h" |
| 18 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
| 19 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/test/mock_render_process_host.h" | 22 #include "content/public/test/mock_render_process_host.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 31 public: | 33 public: |
| 32 TestPermissionContext(Profile* profile, | 34 TestPermissionContext(Profile* profile, |
| 33 const ContentSettingsType permission_type) | 35 const ContentSettingsType permission_type) |
| 34 : PermissionContextBase(profile, permission_type), | 36 : PermissionContextBase(profile, permission_type), |
| 35 permission_set_(false), | 37 permission_set_(false), |
| 36 permission_granted_(false), | 38 permission_granted_(false), |
| 37 tab_context_updated_(false) {} | 39 tab_context_updated_(false) {} |
| 38 | 40 |
| 39 ~TestPermissionContext() override {} | 41 ~TestPermissionContext() override {} |
| 40 | 42 |
| 41 #if defined(OS_ANDROID) | |
| 42 PermissionQueueController* GetInfoBarController() { | |
| 43 return GetQueueController(); | |
| 44 } | |
| 45 #endif | |
| 46 | |
| 47 bool permission_granted() { | 43 bool permission_granted() { |
| 48 return permission_granted_; | 44 return permission_granted_; |
| 49 } | 45 } |
| 50 | 46 |
| 51 bool permission_set() { | 47 bool permission_set() { |
| 52 return permission_set_; | 48 return permission_set_; |
| 53 } | 49 } |
| 54 | 50 |
| 55 bool tab_context_updated() { | 51 bool tab_context_updated() { |
| 56 return tab_context_updated_; | 52 return tab_context_updated_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 75 private: | 71 private: |
| 76 bool permission_set_; | 72 bool permission_set_; |
| 77 bool permission_granted_; | 73 bool permission_granted_; |
| 78 bool tab_context_updated_; | 74 bool tab_context_updated_; |
| 79 }; | 75 }; |
| 80 | 76 |
| 81 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { | 77 class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness { |
| 82 protected: | 78 protected: |
| 83 PermissionContextBaseTests() {} | 79 PermissionContextBaseTests() {} |
| 84 | 80 |
| 81 void ShowUI() { |
| 82 #if defined(OS_ANDROID) |
| 83 PermissionInfoBarManager* manager = |
| 84 PermissionInfoBarManager::FromWebContents(web_contents()); |
| 85 manager->ShowNextQueuedRequest(); |
| 86 #endif |
| 87 } |
| 88 |
| 85 // Accept or dismiss the permission bubble or infobar. | 89 // Accept or dismiss the permission bubble or infobar. |
| 86 void RespondToPermission(TestPermissionContext* context, | 90 void RespondToPermission(bool accept) { |
| 87 const PermissionRequestID& id, | |
| 88 const GURL& url, | |
| 89 bool accept) { | |
| 90 #if defined(OS_ANDROID) | 91 #if defined(OS_ANDROID) |
| 91 context->GetInfoBarController()->OnPermissionSet(id, url, url, accept, | |
| 92 accept); | |
| 93 #else | |
| 94 PermissionBubbleManager* manager = | 92 PermissionBubbleManager* manager = |
| 95 PermissionBubbleManager::FromWebContents(web_contents()); | 93 PermissionBubbleManager::FromWebContents(web_contents()); |
| 96 if (accept) | 94 if (accept) |
| 97 manager->Accept(); | 95 manager->Accept(); |
| 98 else | 96 else |
| 99 manager->Closing(); | 97 manager->Closing(); |
| 98 #else |
| 99 PermissionInfoBarManager* manager = |
| 100 PermissionInfoBarManager::FromWebContents(web_contents()); |
| 101 if (accept) |
| 102 manager->current_request_->AcceptForTests(); |
| 103 else |
| 104 manager->current_request_->ClosingForTests(); |
| 105 } |
| 100 #endif | 106 #endif |
| 101 } | 107 } |
| 102 | 108 |
| 103 void TestAskAndGrant_TestContent() { | 109 void TestAskAndGrant_TestContent() { |
| 104 TestPermissionContext permission_context( | 110 TestPermissionContext permission_context( |
| 105 profile(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 111 profile(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 106 GURL url("http://www.google.com"); | 112 GURL url("http://www.google.com"); |
| 107 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 113 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
| 108 | 114 |
| 109 const PermissionRequestID id( | 115 const PermissionRequestID id( |
| 110 web_contents()->GetRenderProcessHost()->GetID(), | 116 web_contents()->GetRenderProcessHost()->GetID(), |
| 111 web_contents()->GetMainFrame()->GetRoutingID(), | 117 web_contents()->GetMainFrame()->GetRoutingID(), |
| 112 -1); | 118 0); |
| 113 permission_context.RequestPermission( | 119 permission_context.RequestPermission( |
| 114 web_contents(), | 120 web_contents(), |
| 115 id, url, true, | 121 id, url, true, |
| 116 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 122 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 117 base::Unretained(&permission_context))); | 123 base::Unretained(&permission_context))); |
| 124 ShowUI(); |
| 118 | 125 |
| 119 RespondToPermission(&permission_context, id, url, true); | 126 RespondToPermission(true); |
| 120 EXPECT_TRUE(permission_context.permission_set()); | 127 EXPECT_TRUE(permission_context.permission_set()); |
| 121 EXPECT_TRUE(permission_context.permission_granted()); | 128 EXPECT_TRUE(permission_context.permission_granted()); |
| 122 EXPECT_TRUE(permission_context.tab_context_updated()); | 129 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 123 | 130 |
| 124 ContentSetting setting = | 131 ContentSetting setting = |
| 125 HostContentSettingsMapFactory::GetForProfile(profile()) | 132 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 126 ->GetContentSetting(url.GetOrigin(), | 133 ->GetContentSetting(url.GetOrigin(), |
| 127 url.GetOrigin(), | 134 url.GetOrigin(), |
| 128 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 135 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 129 std::string()); | 136 std::string()); |
| 130 EXPECT_EQ(CONTENT_SETTING_ALLOW, setting); | 137 EXPECT_EQ(CONTENT_SETTING_ALLOW, setting); |
| 131 } | 138 } |
| 132 | 139 |
| 133 void TestAskAndDismiss_TestContent() { | 140 void TestAskAndDismiss_TestContent() { |
| 134 TestPermissionContext permission_context( | 141 TestPermissionContext permission_context( |
| 135 profile(), CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | 142 profile(), CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
| 136 GURL url("http://www.google.es"); | 143 GURL url("http://www.google.es"); |
| 137 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 144 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
| 138 | 145 |
| 139 const PermissionRequestID id( | 146 const PermissionRequestID id( |
| 140 web_contents()->GetRenderProcessHost()->GetID(), | 147 web_contents()->GetRenderProcessHost()->GetID(), |
| 141 web_contents()->GetMainFrame()->GetRoutingID(), | 148 web_contents()->GetMainFrame()->GetRoutingID(), |
| 142 -1); | 149 0); |
| 143 permission_context.RequestPermission( | 150 permission_context.RequestPermission( |
| 144 web_contents(), | 151 web_contents(), |
| 145 id, url, true, | 152 id, url, true, |
| 146 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 153 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 147 base::Unretained(&permission_context))); | 154 base::Unretained(&permission_context))); |
| 155 ShowUI(); |
| 148 | 156 |
| 149 RespondToPermission(&permission_context, id, url, false); | 157 RespondToPermission(false); |
| 150 EXPECT_TRUE(permission_context.permission_set()); | 158 EXPECT_TRUE(permission_context.permission_set()); |
| 151 EXPECT_FALSE(permission_context.permission_granted()); | 159 EXPECT_FALSE(permission_context.permission_granted()); |
| 152 EXPECT_TRUE(permission_context.tab_context_updated()); | 160 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 153 | 161 |
| 154 ContentSetting setting = | 162 ContentSetting setting = |
| 155 HostContentSettingsMapFactory::GetForProfile(profile()) | 163 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 156 ->GetContentSetting(url.GetOrigin(), | 164 ->GetContentSetting(url.GetOrigin(), |
| 157 url.GetOrigin(), | 165 url.GetOrigin(), |
| 158 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | 166 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, |
| 159 std::string()); | 167 std::string()); |
| 160 EXPECT_EQ(CONTENT_SETTING_ASK, setting); | 168 EXPECT_EQ(CONTENT_SETTING_ASK, setting); |
| 161 } | 169 } |
| 162 | 170 |
| 163 void TestRequestPermissionInvalidUrl(ContentSettingsType type) { | 171 void TestRequestPermissionInvalidUrl(ContentSettingsType type) { |
| 164 TestPermissionContext permission_context(profile(), type); | 172 TestPermissionContext permission_context(profile(), type); |
| 165 GURL url; | 173 GURL url; |
| 166 ASSERT_FALSE(url.is_valid()); | 174 ASSERT_FALSE(url.is_valid()); |
| 167 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 175 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
| 168 | 176 |
| 169 const PermissionRequestID id( | 177 const PermissionRequestID id( |
| 170 web_contents()->GetRenderProcessHost()->GetID(), | 178 web_contents()->GetRenderProcessHost()->GetID(), |
| 171 web_contents()->GetMainFrame()->GetRoutingID(), | 179 web_contents()->GetMainFrame()->GetRoutingID(), |
| 172 -1); | 180 0); |
| 173 permission_context.RequestPermission( | 181 permission_context.RequestPermission( |
| 174 web_contents(), | 182 web_contents(), |
| 175 id, url, true, | 183 id, url, true, |
| 176 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 184 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 177 base::Unretained(&permission_context))); | 185 base::Unretained(&permission_context))); |
| 186 ShowUI(); |
| 178 | 187 |
| 179 EXPECT_TRUE(permission_context.permission_set()); | 188 EXPECT_TRUE(permission_context.permission_set()); |
| 180 EXPECT_FALSE(permission_context.permission_granted()); | 189 EXPECT_FALSE(permission_context.permission_granted()); |
| 181 EXPECT_TRUE(permission_context.tab_context_updated()); | 190 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 182 | 191 |
| 183 ContentSetting setting = | 192 ContentSetting setting = |
| 184 HostContentSettingsMapFactory::GetForProfile(profile()) | 193 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 185 ->GetContentSetting(url.GetOrigin(), | 194 ->GetContentSetting(url.GetOrigin(), |
| 186 url.GetOrigin(), | 195 url.GetOrigin(), |
| 187 type, | 196 type, |
| 188 std::string()); | 197 std::string()); |
| 189 EXPECT_EQ(CONTENT_SETTING_ASK, setting); | 198 EXPECT_EQ(CONTENT_SETTING_ASK, setting); |
| 190 } | 199 } |
| 191 | 200 |
| 192 void TestGrantAndRevoke_TestContent(ContentSettingsType type, | 201 void TestGrantAndRevoke_TestContent(ContentSettingsType type, |
| 193 ContentSetting expected_default) { | 202 ContentSetting expected_default) { |
| 194 TestPermissionContext permission_context(profile(), type); | 203 TestPermissionContext permission_context(profile(), type); |
| 195 GURL url("https://www.google.com"); | 204 GURL url("https://www.google.com"); |
| 196 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 205 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
| 197 | 206 |
| 198 const PermissionRequestID id( | 207 const PermissionRequestID id( |
| 199 web_contents()->GetRenderProcessHost()->GetID(), | 208 web_contents()->GetRenderProcessHost()->GetID(), |
| 200 web_contents()->GetMainFrame()->GetRoutingID(), | 209 web_contents()->GetMainFrame()->GetRoutingID(), |
| 201 -1); | 210 0); |
| 202 permission_context.RequestPermission( | 211 permission_context.RequestPermission( |
| 203 web_contents(), | 212 web_contents(), |
| 204 id, url, true, | 213 id, url, true, |
| 205 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 214 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 206 base::Unretained(&permission_context))); | 215 base::Unretained(&permission_context))); |
| 216 ShowUI(); |
| 207 | 217 |
| 208 RespondToPermission(&permission_context, id, url, true); | 218 RespondToPermission(true); |
| 209 EXPECT_TRUE(permission_context.permission_set()); | 219 EXPECT_TRUE(permission_context.permission_set()); |
| 210 EXPECT_TRUE(permission_context.permission_granted()); | 220 EXPECT_TRUE(permission_context.permission_granted()); |
| 211 EXPECT_TRUE(permission_context.tab_context_updated()); | 221 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 212 | 222 |
| 213 ContentSetting setting = | 223 ContentSetting setting = |
| 214 HostContentSettingsMapFactory::GetForProfile(profile()) | 224 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 215 ->GetContentSetting(url.GetOrigin(), | 225 ->GetContentSetting(url.GetOrigin(), |
| 216 url.GetOrigin(), | 226 url.GetOrigin(), |
| 217 type, | 227 type, |
| 218 std::string()); | 228 std::string()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 231 ->GetDefaultContentSetting(type, nullptr); | 241 ->GetDefaultContentSetting(type, nullptr); |
| 232 EXPECT_EQ(default_setting, setting_after_reset); | 242 EXPECT_EQ(default_setting, setting_after_reset); |
| 233 } | 243 } |
| 234 | 244 |
| 235 private: | 245 private: |
| 236 // ChromeRenderViewHostTestHarness: | 246 // ChromeRenderViewHostTestHarness: |
| 237 void SetUp() override { | 247 void SetUp() override { |
| 238 ChromeRenderViewHostTestHarness::SetUp(); | 248 ChromeRenderViewHostTestHarness::SetUp(); |
| 239 #if defined(OS_ANDROID) | 249 #if defined(OS_ANDROID) |
| 240 InfoBarService::CreateForWebContents(web_contents()); | 250 InfoBarService::CreateForWebContents(web_contents()); |
| 251 PermissionInfoBarManager::CreateForWebContents(web_contents()); |
| 241 #else | 252 #else |
| 242 PermissionBubbleManager::CreateForWebContents(web_contents()); | 253 PermissionBubbleManager::CreateForWebContents(web_contents()); |
| 243 #endif | 254 #endif |
| 244 } | 255 } |
| 245 | 256 |
| 246 DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests); | 257 DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests); |
| 247 }; | 258 }; |
| 248 | 259 |
| 249 // Simulates clicking Accept. The permission should be granted and | 260 // Simulates clicking Accept. The permission should be granted and |
| 250 // saved for future use. | 261 // saved for future use. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 #if defined(ENABLE_NOTIFICATIONS) | 309 #if defined(ENABLE_NOTIFICATIONS) |
| 299 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 310 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 300 CONTENT_SETTING_ASK); | 311 CONTENT_SETTING_ASK); |
| 301 #endif | 312 #endif |
| 302 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | 313 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, |
| 303 CONTENT_SETTING_ASK); | 314 CONTENT_SETTING_ASK); |
| 304 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | 315 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, |
| 305 CONTENT_SETTING_ASK); | 316 CONTENT_SETTING_ASK); |
| 306 } | 317 } |
| 307 #endif | 318 #endif |
| OLD | NEW |