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