| 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/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/test/scoped_mock_time_message_loop_task_runner.h" | 9 #include "base/test/scoped_mock_time_message_loop_task_runner.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat
us.mojom.h" | 25 #include "third_party/WebKit/public/platform/modules/permissions/permission_stat
us.mojom.h" |
| 26 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 void DoNothing(ContentSetting content_setting) {} | 30 void DoNothing(ContentSetting content_setting) {} |
| 31 void DoNothing2(blink::mojom::PermissionStatus status) {} | 31 void DoNothing2(blink::mojom::PermissionStatus content_setting) {} |
| 32 | 32 |
| 33 class TestNotificationPermissionContext : public NotificationPermissionContext { | 33 class TestNotificationPermissionContext : public NotificationPermissionContext { |
| 34 public: | 34 public: |
| 35 explicit TestNotificationPermissionContext(Profile* profile) | 35 explicit TestNotificationPermissionContext(Profile* profile) |
| 36 : NotificationPermissionContext(profile, | 36 : NotificationPermissionContext(profile, |
| 37 content::PermissionType::NOTIFICATIONS), | 37 CONTENT_SETTINGS_TYPE_NOTIFICATIONS), |
| 38 permission_set_count_(0), | 38 permission_set_count_(0), |
| 39 last_permission_set_persisted_(false), | 39 last_permission_set_persisted_(false), |
| 40 last_permission_set_setting_(CONTENT_SETTING_DEFAULT) {} | 40 last_permission_set_setting_(CONTENT_SETTING_DEFAULT) {} |
| 41 | 41 |
| 42 int permission_set_count() const { return permission_set_count_; } | 42 int permission_set_count() const { return permission_set_count_; } |
| 43 bool last_permission_set_persisted() const { | 43 bool last_permission_set_persisted() const { |
| 44 return last_permission_set_persisted_; | 44 return last_permission_set_persisted_; |
| 45 } | 45 } |
| 46 ContentSetting last_permission_set_setting() const { | 46 ContentSetting last_permission_set_setting() const { |
| 47 return last_permission_set_setting_; | 47 return last_permission_set_setting_; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 // Web Notification permission requests will completely ignore the embedder | 108 // Web Notification permission requests will completely ignore the embedder |
| 109 // origin. See https://crbug.com/416894. | 109 // origin. See https://crbug.com/416894. |
| 110 TEST_F(NotificationPermissionContextTest, IgnoresEmbedderOrigin) { | 110 TEST_F(NotificationPermissionContextTest, IgnoresEmbedderOrigin) { |
| 111 GURL requesting_origin("https://example.com"); | 111 GURL requesting_origin("https://example.com"); |
| 112 GURL embedding_origin("https://chrome.com"); | 112 GURL embedding_origin("https://chrome.com"); |
| 113 GURL different_origin("https://foobar.com"); | 113 GURL different_origin("https://foobar.com"); |
| 114 | 114 |
| 115 NotificationPermissionContext context(profile(), | 115 NotificationPermissionContext context(profile(), |
| 116 content::PermissionType::NOTIFICATIONS); | 116 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 117 UpdateContentSetting(&context, requesting_origin, embedding_origin, | 117 UpdateContentSetting(&context, requesting_origin, embedding_origin, |
| 118 CONTENT_SETTING_ALLOW); | 118 CONTENT_SETTING_ALLOW); |
| 119 | 119 |
| 120 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 120 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 121 context.GetPermissionStatus(requesting_origin, embedding_origin)); | 121 context.GetPermissionStatus(requesting_origin, embedding_origin)); |
| 122 | 122 |
| 123 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 123 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 124 context.GetPermissionStatus(requesting_origin, different_origin)); | 124 context.GetPermissionStatus(requesting_origin, different_origin)); |
| 125 | 125 |
| 126 context.ResetPermission(requesting_origin, embedding_origin); | 126 context.ResetPermission(requesting_origin, embedding_origin); |
| 127 | 127 |
| 128 EXPECT_EQ(CONTENT_SETTING_ASK, | 128 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 129 context.GetPermissionStatus(requesting_origin, embedding_origin)); | 129 context.GetPermissionStatus(requesting_origin, embedding_origin)); |
| 130 | 130 |
| 131 EXPECT_EQ(CONTENT_SETTING_ASK, | 131 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 132 context.GetPermissionStatus(requesting_origin, different_origin)); | 132 context.GetPermissionStatus(requesting_origin, different_origin)); |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Push messaging permission requests should only succeed for top level origins | 135 // Push messaging permission requests should only succeed for top level origins |
| 136 // (embedding origin == requesting origin). | 136 // (embedding origin == requesting origin). |
| 137 TEST_F(NotificationPermissionContextTest, PushTopLevelOriginOnly) { | 137 TEST_F(NotificationPermissionContextTest, PushTopLevelOriginOnly) { |
| 138 GURL requesting_origin("https://example.com"); | 138 GURL requesting_origin("https://example.com"); |
| 139 GURL embedding_origin("https://chrome.com"); | 139 GURL embedding_origin("https://chrome.com"); |
| 140 | 140 |
| 141 NotificationPermissionContext context( | 141 NotificationPermissionContext context(profile(), |
| 142 profile(), content::PermissionType::PUSH_MESSAGING); | 142 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); |
| 143 UpdateContentSetting(&context, requesting_origin, embedding_origin, | 143 UpdateContentSetting(&context, requesting_origin, embedding_origin, |
| 144 CONTENT_SETTING_ALLOW); | 144 CONTENT_SETTING_ALLOW); |
| 145 | 145 |
| 146 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 146 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 147 context.GetPermissionStatus(requesting_origin, embedding_origin)); | 147 context.GetPermissionStatus(requesting_origin, embedding_origin)); |
| 148 | 148 |
| 149 context.ResetPermission(requesting_origin, embedding_origin); | 149 context.ResetPermission(requesting_origin, embedding_origin); |
| 150 | 150 |
| 151 UpdateContentSetting(&context, embedding_origin, embedding_origin, | 151 UpdateContentSetting(&context, embedding_origin, embedding_origin, |
| 152 CONTENT_SETTING_ALLOW); | 152 CONTENT_SETTING_ALLOW); |
| 153 | 153 |
| 154 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 154 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 155 context.GetPermissionStatus(embedding_origin, embedding_origin)); | 155 context.GetPermissionStatus(embedding_origin, embedding_origin)); |
| 156 | 156 |
| 157 context.ResetPermission(embedding_origin, embedding_origin); | 157 context.ResetPermission(embedding_origin, embedding_origin); |
| 158 | 158 |
| 159 EXPECT_EQ(CONTENT_SETTING_ASK, | 159 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 160 context.GetPermissionStatus(embedding_origin, embedding_origin)); | 160 context.GetPermissionStatus(embedding_origin, embedding_origin)); |
| 161 } | 161 } |
| 162 | 162 |
| 163 // Web Notifications do not require a secure origin when requesting permission. | 163 // Web Notifications do not require a secure origin when requesting permission. |
| 164 // See https://crbug.com/404095. | 164 // See https://crbug.com/404095. |
| 165 TEST_F(NotificationPermissionContextTest, NoSecureOriginRequirement) { | 165 TEST_F(NotificationPermissionContextTest, NoSecureOriginRequirement) { |
| 166 GURL origin("http://example.com"); | 166 GURL origin("http://example.com"); |
| 167 | 167 |
| 168 NotificationPermissionContext context(profile(), | 168 NotificationPermissionContext context(profile(), |
| 169 content::PermissionType::NOTIFICATIONS); | 169 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 170 EXPECT_EQ(CONTENT_SETTING_ASK, | 170 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 171 context.GetPermissionStatus(origin, origin)); | 171 context.GetPermissionStatus(origin, origin)); |
| 172 | 172 |
| 173 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); | 173 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); |
| 174 | 174 |
| 175 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 175 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 176 context.GetPermissionStatus(origin, origin)); | 176 context.GetPermissionStatus(origin, origin)); |
| 177 } | 177 } |
| 178 | 178 |
| 179 // Push notifications requires a secure origin to acquire permission. | 179 // Push notifications requires a secure origin to acquire permission. |
| 180 TEST_F(NotificationPermissionContextTest, PushSecureOriginRequirement) { | 180 TEST_F(NotificationPermissionContextTest, PushSecureOriginRequirement) { |
| 181 GURL origin("http://example.com"); | 181 GURL origin("http://example.com"); |
| 182 GURL secure_origin("https://example.com"); | 182 GURL secure_origin("https://example.com"); |
| 183 | 183 |
| 184 NotificationPermissionContext context( | 184 NotificationPermissionContext context(profile(), |
| 185 profile(), content::PermissionType::PUSH_MESSAGING); | 185 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); |
| 186 EXPECT_EQ(CONTENT_SETTING_BLOCK, context.GetPermissionStatus(origin, origin)); | 186 EXPECT_EQ(CONTENT_SETTING_BLOCK, context.GetPermissionStatus(origin, origin)); |
| 187 | 187 |
| 188 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); | 188 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); |
| 189 | 189 |
| 190 EXPECT_EQ(CONTENT_SETTING_BLOCK, context.GetPermissionStatus(origin, origin)); | 190 EXPECT_EQ(CONTENT_SETTING_BLOCK, context.GetPermissionStatus(origin, origin)); |
| 191 | 191 |
| 192 EXPECT_EQ(CONTENT_SETTING_ASK, | 192 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 193 context.GetPermissionStatus(secure_origin, secure_origin)); | 193 context.GetPermissionStatus(secure_origin, secure_origin)); |
| 194 | 194 |
| 195 UpdateContentSetting(&context, secure_origin, secure_origin, | 195 UpdateContentSetting(&context, secure_origin, secure_origin, |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 profile()->GetOffTheRecordProfile()); | 271 profile()->GetOffTheRecordProfile()); |
| 272 GURL url("https://www.example.com"); | 272 GURL url("https://www.example.com"); |
| 273 NavigateAndCommit(url); | 273 NavigateAndCommit(url); |
| 274 | 274 |
| 275 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 275 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 276 web_contents()->GetMainFrame()->GetRoutingID(), | 276 web_contents()->GetMainFrame()->GetRoutingID(), |
| 277 -1); | 277 -1); |
| 278 | 278 |
| 279 base::TestMockTimeTaskRunner* task_runner = SwitchToMockTime(); | 279 base::TestMockTimeTaskRunner* task_runner = SwitchToMockTime(); |
| 280 | 280 |
| 281 content::PermissionManager* permission_manager = | 281 PermissionManager* permission_manager = |
| 282 PermissionManagerFactory::GetForProfile( | 282 PermissionManagerFactory::GetForProfile( |
| 283 profile()->GetOffTheRecordProfile()); | 283 profile()->GetOffTheRecordProfile()); |
| 284 | 284 |
| 285 // Request and cancel the permission via PermissionManager. That way if | 285 // Request and cancel the permission via PermissionManager. That way if |
| 286 // https://crbug.com/586944 regresses, then as well as the EXPECT_EQs below | 286 // https://crbug.com/586944 regresses, then as well as the EXPECT_EQs below |
| 287 // failing, PermissionManager::OnPermissionsRequestResponseStatus will crash. | 287 // failing, PermissionManager::OnPermissionsRequestResponseStatus will crash. |
| 288 int request_id = permission_manager->RequestPermission( | 288 int request_id = permission_manager->RequestPermission( |
| 289 content::PermissionType::NOTIFICATIONS, web_contents()->GetMainFrame(), | 289 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, web_contents()->GetMainFrame(), |
| 290 url.GetOrigin(), true /* user_gesture */, base::Bind(&DoNothing2)); | 290 url.GetOrigin(), true /* user_gesture */, base::Bind(&DoNothing2)); |
| 291 | 291 |
| 292 permission_manager->CancelPermissionRequest(request_id); | 292 permission_manager->CancelPermissionRequest(request_id); |
| 293 | 293 |
| 294 task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); | 294 task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); |
| 295 | 295 |
| 296 EXPECT_EQ(0, permission_context.permission_set_count()); | 296 EXPECT_EQ(0, permission_context.permission_set_count()); |
| 297 EXPECT_EQ(CONTENT_SETTING_ASK, | 297 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 298 permission_context.GetContentSettingFromMap(url, url)); | 298 permission_context.GetContentSettingFromMap(url, url)); |
| 299 } | 299 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 // After another 2.5 seconds, the second permission request should also have | 350 // After another 2.5 seconds, the second permission request should also have |
| 351 // received a response. | 351 // received a response. |
| 352 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); | 352 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); |
| 353 EXPECT_EQ(2, permission_context.permission_set_count()); | 353 EXPECT_EQ(2, permission_context.permission_set_count()); |
| 354 EXPECT_TRUE(permission_context.last_permission_set_persisted()); | 354 EXPECT_TRUE(permission_context.last_permission_set_persisted()); |
| 355 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 355 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 356 permission_context.last_permission_set_setting()); | 356 permission_context.last_permission_set_setting()); |
| 357 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 357 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 358 permission_context.GetContentSettingFromMap(url, url)); | 358 permission_context.GetContentSettingFromMap(url, url)); |
| 359 } | 359 } |
| OLD | NEW |