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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 GURL requesting_origin("https://example.com"); | 110 GURL requesting_origin("https://example.com"); |
111 GURL embedding_origin("https://chrome.com"); | 111 GURL embedding_origin("https://chrome.com"); |
112 GURL different_origin("https://foobar.com"); | 112 GURL different_origin("https://foobar.com"); |
113 | 113 |
114 NotificationPermissionContext context(profile(), | 114 NotificationPermissionContext context(profile(), |
115 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 115 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
116 UpdateContentSetting(&context, requesting_origin, embedding_origin, | 116 UpdateContentSetting(&context, requesting_origin, embedding_origin, |
117 CONTENT_SETTING_ALLOW); | 117 CONTENT_SETTING_ALLOW); |
118 | 118 |
119 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 119 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
120 context.GetPermissionStatus(requesting_origin, embedding_origin) | 120 context |
| 121 .GetPermissionStatus(nullptr /* render_frame_host */, |
| 122 requesting_origin, embedding_origin) |
121 .content_setting); | 123 .content_setting); |
122 | 124 |
123 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 125 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
124 context.GetPermissionStatus(requesting_origin, different_origin) | 126 context |
| 127 .GetPermissionStatus(nullptr /* render_frame_host */, |
| 128 requesting_origin, different_origin) |
125 .content_setting); | 129 .content_setting); |
126 | 130 |
127 context.ResetPermission(requesting_origin, embedding_origin); | 131 context.ResetPermission(requesting_origin, embedding_origin); |
128 | 132 |
129 EXPECT_EQ(CONTENT_SETTING_ASK, | 133 EXPECT_EQ(CONTENT_SETTING_ASK, |
130 context.GetPermissionStatus(requesting_origin, embedding_origin) | 134 context |
| 135 .GetPermissionStatus(nullptr /* render_frame_host */, |
| 136 requesting_origin, embedding_origin) |
131 .content_setting); | 137 .content_setting); |
132 | 138 |
133 EXPECT_EQ(CONTENT_SETTING_ASK, | 139 EXPECT_EQ(CONTENT_SETTING_ASK, |
134 context.GetPermissionStatus(requesting_origin, different_origin) | 140 context |
| 141 .GetPermissionStatus(nullptr /* render_frame_host */, |
| 142 requesting_origin, different_origin) |
135 .content_setting); | 143 .content_setting); |
136 } | 144 } |
137 | 145 |
138 // Push messaging permission requests should only succeed for top level origins | 146 // Push messaging permission requests should only succeed for top level origins |
139 // (embedding origin == requesting origin). | 147 // (embedding origin == requesting origin). |
140 TEST_F(NotificationPermissionContextTest, PushTopLevelOriginOnly) { | 148 TEST_F(NotificationPermissionContextTest, PushTopLevelOriginOnly) { |
141 GURL requesting_origin("https://example.com"); | 149 GURL requesting_origin("https://example.com"); |
142 GURL embedding_origin("https://chrome.com"); | 150 GURL embedding_origin("https://chrome.com"); |
143 | 151 |
144 NotificationPermissionContext context(profile(), | 152 NotificationPermissionContext context(profile(), |
145 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); | 153 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); |
146 UpdateContentSetting(&context, requesting_origin, embedding_origin, | 154 UpdateContentSetting(&context, requesting_origin, embedding_origin, |
147 CONTENT_SETTING_ALLOW); | 155 CONTENT_SETTING_ALLOW); |
148 | 156 |
149 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 157 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
150 context.GetPermissionStatus(requesting_origin, embedding_origin) | 158 context |
| 159 .GetPermissionStatus(nullptr /* render_frame_host */, |
| 160 requesting_origin, embedding_origin) |
151 .content_setting); | 161 .content_setting); |
152 | 162 |
153 context.ResetPermission(requesting_origin, embedding_origin); | 163 context.ResetPermission(requesting_origin, embedding_origin); |
154 | 164 |
155 UpdateContentSetting(&context, embedding_origin, embedding_origin, | 165 UpdateContentSetting(&context, embedding_origin, embedding_origin, |
156 CONTENT_SETTING_ALLOW); | 166 CONTENT_SETTING_ALLOW); |
157 | 167 |
158 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 168 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
159 context.GetPermissionStatus(embedding_origin, embedding_origin) | 169 context |
| 170 .GetPermissionStatus(nullptr /* render_frame_host */, |
| 171 embedding_origin, embedding_origin) |
160 .content_setting); | 172 .content_setting); |
161 | 173 |
162 context.ResetPermission(embedding_origin, embedding_origin); | 174 context.ResetPermission(embedding_origin, embedding_origin); |
163 | 175 |
164 EXPECT_EQ(CONTENT_SETTING_ASK, | 176 EXPECT_EQ(CONTENT_SETTING_ASK, |
165 context.GetPermissionStatus(embedding_origin, embedding_origin) | 177 context |
| 178 .GetPermissionStatus(nullptr /* render_frame_host */, |
| 179 embedding_origin, embedding_origin) |
166 .content_setting); | 180 .content_setting); |
167 } | 181 } |
168 | 182 |
169 // Web Notifications do not require a secure origin when requesting permission. | 183 // Web Notifications do not require a secure origin when requesting permission. |
170 // See https://crbug.com/404095. | 184 // See https://crbug.com/404095. |
171 TEST_F(NotificationPermissionContextTest, NoSecureOriginRequirement) { | 185 TEST_F(NotificationPermissionContextTest, NoSecureOriginRequirement) { |
172 GURL origin("http://example.com"); | 186 GURL origin("http://example.com"); |
173 | 187 |
174 NotificationPermissionContext context(profile(), | 188 NotificationPermissionContext context(profile(), |
175 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 189 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
176 EXPECT_EQ(CONTENT_SETTING_ASK, | 190 EXPECT_EQ( |
177 context.GetPermissionStatus(origin, origin).content_setting); | 191 CONTENT_SETTING_ASK, |
| 192 context |
| 193 .GetPermissionStatus(nullptr /* render_frame_host */, origin, origin) |
| 194 .content_setting); |
178 | 195 |
179 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); | 196 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); |
180 | 197 |
181 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 198 EXPECT_EQ( |
182 context.GetPermissionStatus(origin, origin).content_setting); | 199 CONTENT_SETTING_ALLOW, |
| 200 context |
| 201 .GetPermissionStatus(nullptr /* render_frame_host */, origin, origin) |
| 202 .content_setting); |
183 } | 203 } |
184 | 204 |
185 // Push notifications requires a secure origin to acquire permission. | 205 // Push notifications requires a secure origin to acquire permission. |
186 TEST_F(NotificationPermissionContextTest, PushSecureOriginRequirement) { | 206 TEST_F(NotificationPermissionContextTest, PushSecureOriginRequirement) { |
187 GURL origin("http://example.com"); | 207 GURL origin("http://example.com"); |
188 GURL secure_origin("https://example.com"); | 208 GURL secure_origin("https://example.com"); |
189 | 209 |
190 NotificationPermissionContext context( | 210 NotificationPermissionContext context( |
191 profile(), CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); | 211 profile(), CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); |
192 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 212 EXPECT_EQ( |
193 context.GetPermissionStatus(origin, origin).content_setting); | 213 CONTENT_SETTING_BLOCK, |
| 214 context |
| 215 .GetPermissionStatus(nullptr /* render_frame_host */, origin, origin) |
| 216 .content_setting); |
194 | 217 |
195 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); | 218 UpdateContentSetting(&context, origin, origin, CONTENT_SETTING_ALLOW); |
196 | 219 |
197 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 220 EXPECT_EQ( |
198 context.GetPermissionStatus(origin, origin).content_setting); | 221 CONTENT_SETTING_BLOCK, |
| 222 context |
| 223 .GetPermissionStatus(nullptr /* render_frame_host */, origin, origin) |
| 224 .content_setting); |
199 | 225 |
200 EXPECT_EQ(CONTENT_SETTING_ASK, | 226 EXPECT_EQ(CONTENT_SETTING_ASK, |
201 context.GetPermissionStatus(secure_origin, secure_origin) | 227 context |
| 228 .GetPermissionStatus(nullptr /* render_frame_host */, |
| 229 secure_origin, secure_origin) |
202 .content_setting); | 230 .content_setting); |
203 | 231 |
204 UpdateContentSetting(&context, secure_origin, secure_origin, | 232 UpdateContentSetting(&context, secure_origin, secure_origin, |
205 CONTENT_SETTING_ALLOW); | 233 CONTENT_SETTING_ALLOW); |
206 | 234 |
207 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 235 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
208 context.GetPermissionStatus(secure_origin, secure_origin) | 236 context |
| 237 .GetPermissionStatus(nullptr /* render_frame_host */, |
| 238 secure_origin, secure_origin) |
209 .content_setting); | 239 .content_setting); |
210 } | 240 } |
211 | 241 |
212 // Tests auto-denial after a time delay in incognito. | 242 // Tests auto-denial after a time delay in incognito. |
213 TEST_F(NotificationPermissionContextTest, TestDenyInIncognitoAfterDelay) { | 243 TEST_F(NotificationPermissionContextTest, TestDenyInIncognitoAfterDelay) { |
214 TestNotificationPermissionContext permission_context( | 244 TestNotificationPermissionContext permission_context( |
215 profile()->GetOffTheRecordProfile()); | 245 profile()->GetOffTheRecordProfile()); |
216 GURL url("https://www.example.com"); | 246 GURL url("https://www.example.com"); |
217 NavigateAndCommit(url); | 247 NavigateAndCommit(url); |
218 | 248 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 // After another 2.5 seconds, the second permission request should also have | 390 // After another 2.5 seconds, the second permission request should also have |
361 // received a response. | 391 // received a response. |
362 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); | 392 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); |
363 EXPECT_EQ(2, permission_context.permission_set_count()); | 393 EXPECT_EQ(2, permission_context.permission_set_count()); |
364 EXPECT_TRUE(permission_context.last_permission_set_persisted()); | 394 EXPECT_TRUE(permission_context.last_permission_set_persisted()); |
365 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 395 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
366 permission_context.last_permission_set_setting()); | 396 permission_context.last_permission_set_setting()); |
367 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 397 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
368 permission_context.GetContentSettingFromMap(url, url)); | 398 permission_context.GetContentSettingFromMap(url, url)); |
369 } | 399 } |
OLD | NEW |