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 .content_setting); | 122 .content_setting); |
123 | 123 |
124 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 124 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
125 context.GetPermissionStatus(requesting_origin, different_origin) | 125 context.GetPermissionStatus(requesting_origin, different_origin) |
126 .content_setting); | 126 .content_setting); |
127 | 127 |
128 context.ResetPermission(requesting_origin, embedding_origin); | 128 context.ResetPermission(requesting_origin, embedding_origin); |
129 | 129 |
130 EXPECT_EQ(CONTENT_SETTING_ASK, | 130 EXPECT_EQ(CONTENT_SETTING_ASK, |
131 context.GetPermissionStatus(requesting_origin, embedding_origin) | 131 context.GetPermissionStatus(requesting_origin, embedding_origin) |
132 .content_setting); | 132 .content_setting); |
133 | 133 |
134 EXPECT_EQ(CONTENT_SETTING_ASK, | 134 EXPECT_EQ(CONTENT_SETTING_ASK, |
135 context.GetPermissionStatus(requesting_origin, different_origin) | 135 context.GetPermissionStatus(requesting_origin, different_origin) |
136 .content_setting); | 136 .content_setting); |
137 } | 137 } |
138 | 138 |
139 // Push messaging permission requests should only succeed for top level origins | 139 // Push messaging permission requests should only succeed for top level origins |
140 // (embedding origin == requesting origin). | 140 // (embedding origin == requesting origin). |
141 TEST_F(NotificationPermissionContextTest, PushTopLevelOriginOnly) { | 141 TEST_F(NotificationPermissionContextTest, PushTopLevelOriginOnly) { |
142 GURL requesting_origin("https://example.com"); | 142 GURL requesting_origin("https://example.com"); |
143 GURL embedding_origin("https://chrome.com"); | 143 GURL embedding_origin("https://chrome.com"); |
144 | 144 |
145 NotificationPermissionContext context( | 145 NotificationPermissionContext context(profile(), |
146 profile(), content::PermissionType::PUSH_MESSAGING); | 146 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); |
147 UpdateContentSetting(&context, requesting_origin, embedding_origin, | 147 UpdateContentSetting(&context, requesting_origin, embedding_origin, |
148 CONTENT_SETTING_ALLOW); | 148 CONTENT_SETTING_ALLOW); |
149 | 149 |
150 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 150 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
151 context.GetPermissionStatus(requesting_origin, embedding_origin) | 151 context.GetPermissionStatus(requesting_origin, embedding_origin) |
152 .content_setting); | 152 .content_setting); |
153 | 153 |
154 context.ResetPermission(requesting_origin, embedding_origin); | 154 context.ResetPermission(requesting_origin, embedding_origin); |
155 | 155 |
156 UpdateContentSetting(&context, embedding_origin, embedding_origin, | 156 UpdateContentSetting(&context, embedding_origin, embedding_origin, |
157 CONTENT_SETTING_ALLOW); | 157 CONTENT_SETTING_ALLOW); |
158 | 158 |
159 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 159 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
160 context.GetPermissionStatus(embedding_origin, embedding_origin) | 160 context.GetPermissionStatus(embedding_origin, embedding_origin) |
161 .content_setting); | 161 .content_setting); |
162 | 162 |
163 context.ResetPermission(embedding_origin, embedding_origin); | 163 context.ResetPermission(embedding_origin, embedding_origin); |
164 | 164 |
165 EXPECT_EQ(CONTENT_SETTING_ASK, | 165 EXPECT_EQ(CONTENT_SETTING_ASK, |
166 context.GetPermissionStatus(embedding_origin, embedding_origin) | 166 context.GetPermissionStatus(embedding_origin, embedding_origin) |
167 .content_setting); | 167 .content_setting); |
168 } | 168 } |
169 | 169 |
170 // Web Notifications do not require a secure origin when requesting permission. | 170 // Web Notifications do not require a secure origin when requesting permission. |
171 // See https://crbug.com/404095. | 171 // See https://crbug.com/404095. |
172 TEST_F(NotificationPermissionContextTest, NoSecureOriginRequirement) { | 172 TEST_F(NotificationPermissionContextTest, NoSecureOriginRequirement) { |
173 GURL origin("http://example.com"); | 173 GURL origin("http://example.com"); |
174 | 174 |
175 NotificationPermissionContext context(profile(), | 175 NotificationPermissionContext context(profile(), |
176 content::PermissionType::NOTIFICATIONS); | 176 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
177 EXPECT_EQ(CONTENT_SETTING_ASK, | 177 EXPECT_EQ(CONTENT_SETTING_ASK, |
178 context.GetPermissionStatus(origin, origin).content_setting); | 178 context.GetPermissionStatus(origin, origin).content_setting); |
179 | 179 |
180 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); | 180 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); |
181 | 181 |
182 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 182 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
183 context.GetPermissionStatus(origin, origin).content_setting); | 183 context.GetPermissionStatus(origin, origin).content_setting); |
184 } | 184 } |
185 | 185 |
186 // Push notifications requires a secure origin to acquire permission. | 186 // Push notifications requires a secure origin to acquire permission. |
187 TEST_F(NotificationPermissionContextTest, PushSecureOriginRequirement) { | 187 TEST_F(NotificationPermissionContextTest, PushSecureOriginRequirement) { |
188 GURL origin("http://example.com"); | 188 GURL origin("http://example.com"); |
189 GURL secure_origin("https://example.com"); | 189 GURL secure_origin("https://example.com"); |
190 | 190 |
191 NotificationPermissionContext context( | 191 NotificationPermissionContext context( |
192 profile(), content::PermissionType::PUSH_MESSAGING); | 192 profile(), CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); |
193 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 193 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
194 context.GetPermissionStatus(origin, origin).content_setting); | 194 context.GetPermissionStatus(origin, origin).content_setting); |
195 | 195 |
196 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); | 196 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); |
197 | 197 |
198 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 198 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
199 context.GetPermissionStatus(origin, origin).content_setting); | 199 context.GetPermissionStatus(origin, origin).content_setting); |
200 | 200 |
201 EXPECT_EQ(CONTENT_SETTING_ASK, | 201 EXPECT_EQ(CONTENT_SETTING_ASK, |
202 context.GetPermissionStatus(secure_origin, secure_origin) | 202 context.GetPermissionStatus(secure_origin, secure_origin) |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 profile()->GetOffTheRecordProfile()); | 282 profile()->GetOffTheRecordProfile()); |
283 GURL url("https://www.example.com"); | 283 GURL url("https://www.example.com"); |
284 NavigateAndCommit(url); | 284 NavigateAndCommit(url); |
285 | 285 |
286 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 286 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
287 web_contents()->GetMainFrame()->GetRoutingID(), | 287 web_contents()->GetMainFrame()->GetRoutingID(), |
288 -1); | 288 -1); |
289 | 289 |
290 base::TestMockTimeTaskRunner* task_runner = SwitchToMockTime(); | 290 base::TestMockTimeTaskRunner* task_runner = SwitchToMockTime(); |
291 | 291 |
292 content::PermissionManager* permission_manager = | 292 PermissionManager* permission_manager = |
293 PermissionManagerFactory::GetForProfile( | 293 PermissionManagerFactory::GetForProfile( |
294 profile()->GetOffTheRecordProfile()); | 294 profile()->GetOffTheRecordProfile()); |
295 | 295 |
296 // Request and cancel the permission via PermissionManager. That way if | 296 // Request and cancel the permission via PermissionManager. That way if |
297 // https://crbug.com/586944 regresses, then as well as the EXPECT_EQs below | 297 // https://crbug.com/586944 regresses, then as well as the EXPECT_EQs below |
298 // failing, PermissionManager::OnPermissionsRequestResponseStatus will crash. | 298 // failing, PermissionManager::OnPermissionsRequestResponseStatus will crash. |
299 int request_id = permission_manager->RequestPermission( | 299 int request_id = permission_manager->RequestPermission( |
300 content::PermissionType::NOTIFICATIONS, web_contents()->GetMainFrame(), | 300 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, web_contents()->GetMainFrame(), |
301 url.GetOrigin(), true /* user_gesture */, base::Bind(&DoNothing2)); | 301 url.GetOrigin(), true /* user_gesture */, base::Bind(&DoNothing2)); |
302 | 302 |
303 permission_manager->CancelPermissionRequest(request_id); | 303 permission_manager->CancelPermissionRequest(request_id); |
304 | 304 |
305 task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); | 305 task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); |
306 | 306 |
307 EXPECT_EQ(0, permission_context.permission_set_count()); | 307 EXPECT_EQ(0, permission_context.permission_set_count()); |
308 EXPECT_EQ(CONTENT_SETTING_ASK, | 308 EXPECT_EQ(CONTENT_SETTING_ASK, |
309 permission_context.GetContentSettingFromMap(url, url)); | 309 permission_context.GetContentSettingFromMap(url, url)); |
310 } | 310 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 // After another 2.5 seconds, the second permission request should also have | 361 // After another 2.5 seconds, the second permission request should also have |
362 // received a response. | 362 // received a response. |
363 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); | 363 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); |
364 EXPECT_EQ(2, permission_context.permission_set_count()); | 364 EXPECT_EQ(2, permission_context.permission_set_count()); |
365 EXPECT_TRUE(permission_context.last_permission_set_persisted()); | 365 EXPECT_TRUE(permission_context.last_permission_set_persisted()); |
366 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 366 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
367 permission_context.last_permission_set_setting()); | 367 permission_context.last_permission_set_setting()); |
368 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 368 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
369 permission_context.GetContentSettingFromMap(url, url)); | 369 permission_context.GetContentSettingFromMap(url, url)); |
370 } | 370 } |
OLD | NEW |