| 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/notifications/notification_permission_context.h" | 5 #include "chrome/browser/notifications/notification_permission_context.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/test/test_mock_time_task_runner.h" | 9 #include "base/test/test_mock_time_task_runner.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 ContentSetting GetContentSettingFromMap(const GURL& url_a, | 49 ContentSetting GetContentSettingFromMap(const GURL& url_a, |
| 50 const GURL& url_b) { | 50 const GURL& url_b) { |
| 51 return HostContentSettingsMapFactory::GetForProfile(profile()) | 51 return HostContentSettingsMapFactory::GetForProfile(profile()) |
| 52 ->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), | 52 ->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), |
| 53 content_settings_type(), std::string()); | 53 content_settings_type(), std::string()); |
| 54 } | 54 } |
| 55 | 55 |
| 56 private: | 56 private: |
| 57 // NotificationPermissionContext: | 57 // NotificationPermissionContext: |
| 58 void NotifyPermissionSet(const PermissionRequestID& id, | 58 void NotifyPermissionSet(const PermissionRequestID& id, |
| 59 const GURL& requesting_origin, | 59 const url::Origin& requesting_origin, |
| 60 const GURL& embedder_origin, | 60 const url::Origin& embedder_origin, |
| 61 const BrowserPermissionCallback& callback, | 61 const BrowserPermissionCallback& callback, |
| 62 bool persist, | 62 bool persist, |
| 63 ContentSetting content_setting) override { | 63 ContentSetting content_setting) override { |
| 64 permission_set_count_++; | 64 permission_set_count_++; |
| 65 last_permission_set_persisted_ = persist; | 65 last_permission_set_persisted_ = persist; |
| 66 last_permission_set_setting_ = content_setting; | 66 last_permission_set_setting_ = content_setting; |
| 67 NotificationPermissionContext::NotifyPermissionSet( | 67 NotificationPermissionContext::NotifyPermissionSet( |
| 68 id, requesting_origin, embedder_origin, callback, persist, | 68 id, requesting_origin, embedder_origin, callback, persist, |
| 69 content_setting); | 69 content_setting); |
| 70 } | 70 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 96 | 96 |
| 97 private: | 97 private: |
| 98 scoped_refptr<base::SingleThreadTaskRunner> old_task_runner_; | 98 scoped_refptr<base::SingleThreadTaskRunner> old_task_runner_; |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 } // namespace | 101 } // namespace |
| 102 | 102 |
| 103 // Web Notification permission requests will completely ignore the embedder | 103 // Web Notification permission requests will completely ignore the embedder |
| 104 // origin. See https://crbug.com/416894. | 104 // origin. See https://crbug.com/416894. |
| 105 TEST_F(NotificationPermissionContextTest, IgnoresEmbedderOrigin) { | 105 TEST_F(NotificationPermissionContextTest, IgnoresEmbedderOrigin) { |
| 106 GURL requesting_origin("https://example.com"); | 106 const GURL requesting_url("https://example.com"); |
| 107 GURL embedding_origin("https://chrome.com"); | 107 const url::Origin requesting_origin(requesting_url); |
| 108 GURL different_origin("https://foobar.com"); | 108 const GURL embedding_url("https://chrome.com"); |
| 109 const url::Origin embedding_origin(embedding_url); |
| 110 const GURL different_url("https://foobar.com"); |
| 111 const url::Origin different_origin(different_url); |
| 109 | 112 |
| 110 NotificationPermissionContext context(profile()); | 113 NotificationPermissionContext context(profile()); |
| 111 context.UpdateContentSetting(requesting_origin, | 114 context.UpdateContentSetting(requesting_origin, |
| 112 embedding_origin, | 115 embedding_origin, |
| 113 CONTENT_SETTING_ALLOW); | 116 CONTENT_SETTING_ALLOW); |
| 114 | 117 |
| 115 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 118 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 116 context.GetPermissionStatus(requesting_origin, embedding_origin)); | 119 context.GetPermissionStatus(requesting_origin, embedding_origin)); |
| 117 | 120 |
| 118 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 121 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 119 context.GetPermissionStatus(requesting_origin, different_origin)); | 122 context.GetPermissionStatus(requesting_origin, different_origin)); |
| 120 | 123 |
| 121 context.ResetPermission(requesting_origin, embedding_origin); | 124 context.ResetPermission(requesting_origin, embedding_origin); |
| 122 | 125 |
| 123 EXPECT_EQ(CONTENT_SETTING_ASK, | 126 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 124 context.GetPermissionStatus(requesting_origin, embedding_origin)); | 127 context.GetPermissionStatus(requesting_origin, embedding_origin)); |
| 125 | 128 |
| 126 EXPECT_EQ(CONTENT_SETTING_ASK, | 129 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 127 context.GetPermissionStatus(requesting_origin, different_origin)); | 130 context.GetPermissionStatus(requesting_origin, different_origin)); |
| 128 } | 131 } |
| 129 | 132 |
| 130 // Web Notifications do not require a secure origin when requesting permission. | 133 // Web Notifications do not require a secure origin when requesting permission. |
| 131 // See https://crbug.com/404095. | 134 // See https://crbug.com/404095. |
| 132 TEST_F(NotificationPermissionContextTest, NoSecureOriginRequirement) { | 135 TEST_F(NotificationPermissionContextTest, NoSecureOriginRequirement) { |
| 133 GURL origin("http://example.com"); | 136 const GURL url("http://example.com"); |
| 137 const url::Origin origin("http://example.com"); |
| 134 | 138 |
| 135 NotificationPermissionContext context(profile()); | 139 NotificationPermissionContext context(profile()); |
| 136 EXPECT_EQ(CONTENT_SETTING_ASK, | 140 EXPECT_EQ(CONTENT_SETTING_ASK, context.GetPermissionStatus(origin, origin)); |
| 137 context.GetPermissionStatus(origin, origin)); | |
| 138 | 141 |
| 139 context.UpdateContentSetting(origin, origin, CONTENT_SETTING_ALLOW); | 142 context.UpdateContentSetting(origin, origin, CONTENT_SETTING_ALLOW); |
| 140 | 143 |
| 141 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 144 EXPECT_EQ(CONTENT_SETTING_ALLOW, context.GetPermissionStatus(origin, origin)); |
| 142 context.GetPermissionStatus(origin, origin)); | |
| 143 } | 145 } |
| 144 | 146 |
| 145 // Tests auto-denial after a time delay in incognito. | 147 // Tests auto-denial after a time delay in incognito. |
| 146 TEST_F(NotificationPermissionContextTest, TestDenyInIncognitoAfterDelay) { | 148 TEST_F(NotificationPermissionContextTest, TestDenyInIncognitoAfterDelay) { |
| 147 TestNotificationPermissionContext permission_context( | 149 TestNotificationPermissionContext permission_context( |
| 148 profile()->GetOffTheRecordProfile()); | 150 profile()->GetOffTheRecordProfile()); |
| 149 GURL url("https://www.example.com"); | 151 const GURL url("https://www.example.com"); |
| 150 NavigateAndCommit(url); | 152 NavigateAndCommit(url); |
| 151 | 153 |
| 152 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 154 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 153 web_contents()->GetMainFrame()->GetRoutingID(), | 155 web_contents()->GetMainFrame()->GetRoutingID(), |
| 154 -1); | 156 -1); |
| 155 | 157 |
| 156 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(SwitchToMockTime()); | 158 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(SwitchToMockTime()); |
| 157 | 159 |
| 158 ASSERT_EQ(0, permission_context.permission_set_count()); | 160 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 159 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | 161 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 160 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | 162 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 161 permission_context.last_permission_set_setting()); | 163 permission_context.last_permission_set_setting()); |
| 162 | 164 |
| 163 permission_context.RequestPermission( | 165 const url::Origin origin(url); |
| 164 web_contents(), id, url, base::Bind(&DoNothing)); | 166 permission_context.RequestPermission(web_contents(), id, origin, |
| 167 base::Bind(&DoNothing)); |
| 165 | 168 |
| 166 // Should be blocked after 1-2 seconds, but the timer is reset whenever the | 169 // Should be blocked after 1-2 seconds, but the timer is reset whenever the |
| 167 // tab is not visible, so these 500ms never add up to >= 1 second. | 170 // tab is not visible, so these 500ms never add up to >= 1 second. |
| 168 for (int n = 0; n < 10; n++) { | 171 for (int n = 0; n < 10; n++) { |
| 169 web_contents()->WasShown(); | 172 web_contents()->WasShown(); |
| 170 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); | 173 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
| 171 web_contents()->WasHidden(); | 174 web_contents()->WasHidden(); |
| 172 } | 175 } |
| 173 | 176 |
| 174 EXPECT_EQ(0, permission_context.permission_set_count()); | 177 EXPECT_EQ(0, permission_context.permission_set_count()); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 205 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 208 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 206 permission_context.last_permission_set_setting()); | 209 permission_context.last_permission_set_setting()); |
| 207 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 210 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 208 permission_context.GetContentSettingFromMap(url, url)); | 211 permission_context.GetContentSettingFromMap(url, url)); |
| 209 } | 212 } |
| 210 | 213 |
| 211 // Tests that navigating cancels incognito permission requests without crashing. | 214 // Tests that navigating cancels incognito permission requests without crashing. |
| 212 TEST_F(NotificationPermissionContextTest, TestCancelledIncognitoRequest) { | 215 TEST_F(NotificationPermissionContextTest, TestCancelledIncognitoRequest) { |
| 213 TestNotificationPermissionContext permission_context( | 216 TestNotificationPermissionContext permission_context( |
| 214 profile()->GetOffTheRecordProfile()); | 217 profile()->GetOffTheRecordProfile()); |
| 215 GURL url("https://www.example.com"); | 218 const GURL url("https://www.example.com"); |
| 216 NavigateAndCommit(url); | 219 NavigateAndCommit(url); |
| 217 | 220 |
| 218 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 221 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 219 web_contents()->GetMainFrame()->GetRoutingID(), | 222 web_contents()->GetMainFrame()->GetRoutingID(), |
| 220 -1); | 223 -1); |
| 221 | 224 |
| 222 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(SwitchToMockTime()); | 225 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(SwitchToMockTime()); |
| 223 | 226 |
| 224 content::PermissionManager* permission_manager = | 227 content::PermissionManager* permission_manager = |
| 225 PermissionManagerFactory::GetForProfile( | 228 PermissionManagerFactory::GetForProfile( |
| 226 profile()->GetOffTheRecordProfile()); | 229 profile()->GetOffTheRecordProfile()); |
| 227 | 230 |
| 228 // Request and cancel the permission via PermissionManager. That way if | 231 // Request and cancel the permission via PermissionManager. That way if |
| 229 // https://crbug.com/586944 regresses, then as well as the EXPECT_EQs below | 232 // https://crbug.com/586944 regresses, then as well as the EXPECT_EQs below |
| 230 // failing, PermissionManager::OnPermissionsRequestResponseStatus will crash. | 233 // failing, PermissionManager::OnPermissionsRequestResponseStatus will crash. |
| 234 const url::Origin origin(url); |
| 231 int request_id = permission_manager->RequestPermission( | 235 int request_id = permission_manager->RequestPermission( |
| 232 content::PermissionType::NOTIFICATIONS, web_contents()->GetMainFrame(), | 236 content::PermissionType::NOTIFICATIONS, web_contents()->GetMainFrame(), |
| 233 url.GetOrigin(), base::Bind(&DoNothing2)); | 237 origin, base::Bind(&DoNothing2)); |
| 234 | 238 |
| 235 permission_manager->CancelPermissionRequest(request_id); | 239 permission_manager->CancelPermissionRequest(request_id); |
| 236 | 240 |
| 237 task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); | 241 task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); |
| 238 | 242 |
| 239 EXPECT_EQ(0, permission_context.permission_set_count()); | 243 EXPECT_EQ(0, permission_context.permission_set_count()); |
| 240 EXPECT_EQ(CONTENT_SETTING_ASK, | 244 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 241 permission_context.GetContentSettingFromMap(url, url)); | 245 permission_context.GetContentSettingFromMap(url, url)); |
| 242 } | 246 } |
| 243 | 247 |
| 244 // Tests how multiple parallel permission requests get auto-denied in incognito. | 248 // Tests how multiple parallel permission requests get auto-denied in incognito. |
| 245 TEST_F(NotificationPermissionContextTest, TestParallelDenyInIncognito) { | 249 TEST_F(NotificationPermissionContextTest, TestParallelDenyInIncognito) { |
| 246 TestNotificationPermissionContext permission_context( | 250 TestNotificationPermissionContext permission_context( |
| 247 profile()->GetOffTheRecordProfile()); | 251 profile()->GetOffTheRecordProfile()); |
| 248 GURL url("https://www.example.com"); | 252 const GURL url("https://www.example.com"); |
| 249 NavigateAndCommit(url); | 253 NavigateAndCommit(url); |
| 250 web_contents()->WasShown(); | 254 web_contents()->WasShown(); |
| 251 | 255 |
| 252 const PermissionRequestID id0(web_contents()->GetRenderProcessHost()->GetID(), | 256 const PermissionRequestID id0(web_contents()->GetRenderProcessHost()->GetID(), |
| 253 web_contents()->GetMainFrame()->GetRoutingID(), | 257 web_contents()->GetMainFrame()->GetRoutingID(), |
| 254 0); | 258 0); |
| 255 const PermissionRequestID id1(web_contents()->GetRenderProcessHost()->GetID(), | 259 const PermissionRequestID id1(web_contents()->GetRenderProcessHost()->GetID(), |
| 256 web_contents()->GetMainFrame()->GetRoutingID(), | 260 web_contents()->GetMainFrame()->GetRoutingID(), |
| 257 1); | 261 1); |
| 258 | 262 |
| 259 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(SwitchToMockTime()); | 263 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(SwitchToMockTime()); |
| 260 | 264 |
| 261 ASSERT_EQ(0, permission_context.permission_set_count()); | 265 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 262 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | 266 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 263 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | 267 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 264 permission_context.last_permission_set_setting()); | 268 permission_context.last_permission_set_setting()); |
| 265 | 269 |
| 266 permission_context.RequestPermission(web_contents(), id0, url, | 270 const url::Origin origin(url); |
| 271 permission_context.RequestPermission(web_contents(), id0, origin, |
| 267 base::Bind(&DoNothing)); | 272 base::Bind(&DoNothing)); |
| 268 permission_context.RequestPermission(web_contents(), id1, url, | 273 permission_context.RequestPermission(web_contents(), id1, origin, |
| 269 base::Bind(&DoNothing)); | 274 base::Bind(&DoNothing)); |
| 270 | 275 |
| 271 EXPECT_EQ(0, permission_context.permission_set_count()); | 276 EXPECT_EQ(0, permission_context.permission_set_count()); |
| 272 EXPECT_EQ(CONTENT_SETTING_ASK, | 277 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 273 permission_context.GetContentSettingFromMap(url, url)); | 278 permission_context.GetContentSettingFromMap(url, url)); |
| 274 | 279 |
| 275 // Fast forward up to 2.5 seconds. Stop as soon as the first permission | 280 // Fast forward up to 2.5 seconds. Stop as soon as the first permission |
| 276 // request is auto-denied. | 281 // request is auto-denied. |
| 277 for (int n = 0; n < 5; n++) { | 282 for (int n = 0; n < 5; n++) { |
| 278 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); | 283 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 291 // After another 2.5 seconds, the second permission request should also have | 296 // After another 2.5 seconds, the second permission request should also have |
| 292 // received a response. | 297 // received a response. |
| 293 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); | 298 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); |
| 294 EXPECT_EQ(2, permission_context.permission_set_count()); | 299 EXPECT_EQ(2, permission_context.permission_set_count()); |
| 295 EXPECT_TRUE(permission_context.last_permission_set_persisted()); | 300 EXPECT_TRUE(permission_context.last_permission_set_persisted()); |
| 296 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 301 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 297 permission_context.last_permission_set_setting()); | 302 permission_context.last_permission_set_setting()); |
| 298 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 303 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 299 permission_context.GetContentSettingFromMap(url, url)); | 304 permission_context.GetContentSettingFromMap(url, url)); |
| 300 } | 305 } |
| OLD | NEW |