OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/push_messaging/push_messaging_permission_context.h" | |
6 | |
7 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
8 #include "chrome/browser/permissions/permission_request_id.h" | |
9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
10 #include "chrome/test/base/testing_profile.h" | |
11 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
12 #include "components/content_settings/core/common/content_settings.h" | |
13 #include "components/content_settings/core/common/content_settings_types.h" | |
14 #include "content/public/browser/web_contents.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace { | |
18 | |
19 const char kOriginA[] = "https://origina.org"; | |
20 const char kOriginB[] = "https://originb.org"; | |
21 const char kInsecureOrigin[] = "http://insecureorigin.org"; | |
22 | |
23 void DoNothing(ContentSetting content_setting) {} | |
24 | |
25 class TestPushMessagingPermissionContext | |
26 : public PushMessagingPermissionContext { | |
27 public: | |
28 explicit TestPushMessagingPermissionContext(Profile* profile) | |
29 : PushMessagingPermissionContext(profile), | |
30 was_persisted_(false), | |
31 permission_granted_(false) {} | |
32 | |
33 bool was_persisted() const { return was_persisted_; } | |
34 bool was_granted() const { return permission_granted_; } | |
35 | |
36 private: | |
37 // PushMessagingPermissionContext: | |
38 void NotifyPermissionSet(const PermissionRequestID& id, | |
39 const GURL& requesting_origin, | |
40 const GURL& embedder_origin, | |
41 const BrowserPermissionCallback& callback, | |
42 bool persist, | |
43 ContentSetting content_setting) override { | |
44 was_persisted_ = persist; | |
45 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW; | |
46 PushMessagingPermissionContext::NotifyPermissionSet( | |
47 id, requesting_origin, embedder_origin, callback, persist, | |
48 content_setting); | |
49 } | |
50 | |
51 bool was_persisted_; | |
52 bool permission_granted_; | |
53 }; | |
54 | |
55 class PushMessagingPermissionContextTest | |
56 : public ChromeRenderViewHostTestHarness { | |
57 public: | |
58 PushMessagingPermissionContextTest() {} | |
59 | |
60 protected: | |
61 void SetContentSetting(Profile* profile, | |
62 ContentSettingsType setting, | |
63 ContentSetting value) { | |
64 // These urls must match those in | |
65 // PermissionContextBase::UpdateContentSetting, since the tests below use | |
66 // this method to overwrite urls set as a result of | |
67 // PushMessagingPermissionContext::NotifyPermissionSet. | |
68 GURL url_a = GURL(kOriginA); | |
69 GURL insecure_url = GURL(kInsecureOrigin); | |
70 HostContentSettingsMap* host_content_settings_map = | |
71 HostContentSettingsMapFactory::GetForProfile(profile); | |
72 | |
73 host_content_settings_map->SetContentSettingDefaultScope( | |
74 url_a, url_a, setting, std::string(), value); | |
75 host_content_settings_map->SetContentSettingDefaultScope( | |
76 insecure_url, insecure_url, setting, std::string(), value); | |
77 } | |
78 }; | |
79 | |
80 } // namespace | |
81 | |
82 TEST_F(PushMessagingPermissionContextTest, HasPermissionPrompt) { | |
83 TestingProfile profile; | |
84 PushMessagingPermissionContext context(&profile); | |
85 EXPECT_EQ(CONTENT_SETTING_ASK, | |
86 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
87 | |
88 // Just granting push messaging permission should still prompt. | |
89 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
90 CONTENT_SETTING_ALLOW); | |
91 | |
92 EXPECT_EQ(CONTENT_SETTING_ASK, | |
93 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
94 | |
95 // Just granting notifications should allow if push messaging is not blocked. | |
96 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
97 CONTENT_SETTING_ASK); | |
98 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
99 CONTENT_SETTING_ALLOW); | |
100 | |
101 EXPECT_EQ(CONTENT_SETTING_ASK, | |
102 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
103 | |
104 // Granting allow notifications but explicitly denying push messaging should | |
105 // block. | |
106 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
107 CONTENT_SETTING_ALLOW); | |
108 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
109 CONTENT_SETTING_BLOCK); | |
110 | |
111 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
112 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
113 } | |
114 | |
115 TEST_F(PushMessagingPermissionContextTest, HasPermissionDenySettingsMismatch) { | |
116 TestingProfile profile; | |
117 PushMessagingPermissionContext context(&profile); | |
118 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
119 CONTENT_SETTING_BLOCK); | |
120 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
121 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
122 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
123 CONTENT_SETTING_ASK); | |
124 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
125 CONTENT_SETTING_BLOCK); | |
126 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
127 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
128 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
129 CONTENT_SETTING_ALLOW); | |
130 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
131 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
132 | |
133 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
134 CONTENT_SETTING_ASK); | |
135 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
136 CONTENT_SETTING_BLOCK); | |
137 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
138 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
139 } | |
140 | |
141 TEST_F(PushMessagingPermissionContextTest, HasPermissionDenyDifferentOrigins) { | |
Miguel Garcia
2016/07/14 18:02:03
Can we add this test case to the notification test
raymes
2016/07/18 07:04:51
Done.
| |
142 TestingProfile profile; | |
143 PushMessagingPermissionContext context(&profile); | |
144 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
145 context.GetPermissionStatus(GURL(kOriginB), GURL(kOriginA))); | |
146 } | |
147 | |
148 TEST_F(PushMessagingPermissionContextTest, HasPermissionAccept) { | |
149 TestingProfile profile; | |
150 PushMessagingPermissionContext context(&profile); | |
151 | |
152 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
153 CONTENT_SETTING_ALLOW); | |
154 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
155 CONTENT_SETTING_ALLOW); | |
156 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
157 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
158 } | |
159 | |
160 TEST_F(PushMessagingPermissionContextTest, DecidePushPermission) { | |
161 TestingProfile profile; | |
162 TestPushMessagingPermissionContext context(&profile); | |
163 PermissionRequestID request_id(-1, -1, -1); | |
164 BrowserPermissionCallback callback = base::Bind(DoNothing); | |
165 | |
166 context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA), | |
167 callback, | |
168 blink::mojom::PermissionStatus::DENIED); | |
169 EXPECT_FALSE(context.was_persisted()); | |
170 EXPECT_FALSE(context.was_granted()); | |
171 | |
172 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
173 CONTENT_SETTING_ALLOW); | |
174 context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA), | |
175 callback, | |
176 blink::mojom::PermissionStatus::GRANTED); | |
177 EXPECT_TRUE(context.was_persisted()); | |
178 EXPECT_TRUE(context.was_granted()); | |
179 | |
180 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
181 CONTENT_SETTING_BLOCK); | |
182 context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA), | |
183 callback, | |
184 blink::mojom::PermissionStatus::GRANTED); | |
185 EXPECT_TRUE(context.was_persisted()); | |
186 EXPECT_FALSE(context.was_granted()); | |
187 | |
188 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
189 CONTENT_SETTING_ASK); | |
190 context.DecidePushPermission(request_id, GURL(kOriginA), GURL(kOriginA), | |
191 callback, | |
192 blink::mojom::PermissionStatus::GRANTED); | |
193 EXPECT_TRUE(context.was_persisted()); | |
194 EXPECT_TRUE(context.was_granted()); | |
195 } | |
196 | |
197 TEST_F(PushMessagingPermissionContextTest, DecidePermission) { | |
198 TestingProfile profile; | |
199 TestPushMessagingPermissionContext context(&profile); | |
200 PermissionRequestID request_id(-1, -1, -1); | |
201 BrowserPermissionCallback callback = base::Bind(DoNothing); | |
202 | |
203 // Requesting and embedding origin are different. | |
204 context.DecidePermission(NULL, request_id, GURL(kOriginA), GURL(kOriginB), | |
205 callback); | |
206 EXPECT_FALSE(context.was_persisted()); | |
207 EXPECT_FALSE(context.was_granted()); | |
208 | |
209 // Insecure origin | |
210 NavigateAndCommit(GURL(kInsecureOrigin)); | |
211 context.RequestPermission(web_contents(), request_id, GURL(kInsecureOrigin), | |
212 callback); | |
213 EXPECT_FALSE(context.was_persisted()); | |
214 EXPECT_FALSE(context.was_granted()); | |
215 } | |
216 | |
217 TEST_F(PushMessagingPermissionContextTest, RequestPermission) { | |
218 TestingProfile profile; | |
219 TestPushMessagingPermissionContext context(&profile); | |
220 PermissionRequestID request_id(-1, -1, -1); | |
221 BrowserPermissionCallback callback = base::Bind(DoNothing); | |
222 | |
223 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
224 CONTENT_SETTING_ALLOW); | |
225 | |
226 EXPECT_EQ( | |
227 CONTENT_SETTING_ASK, | |
228 HostContentSettingsMapFactory::GetForProfile(&profile)->GetContentSetting( | |
229 GURL(kOriginA), GURL(kOriginA), CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
230 std::string())); | |
231 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
232 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
233 | |
234 // If a website already has notifications permission, push permission is | |
235 // silently assumed to be granted. | |
236 NavigateAndCommit(GURL(kOriginA)); | |
237 context.RequestPermission(web_contents(), request_id, GURL(kOriginA), | |
238 callback); | |
239 | |
240 // Although the permission check goes through, the push message permission | |
241 // isn't actually updated. | |
242 EXPECT_TRUE(context.was_granted()); | |
243 EXPECT_EQ( | |
244 CONTENT_SETTING_ASK, | |
245 HostContentSettingsMapFactory::GetForProfile(&profile)->GetContentSetting( | |
246 GURL(kOriginA), GURL(kOriginA), CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
247 std::string())); | |
248 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
249 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
250 } | |
251 | |
252 TEST_F(PushMessagingPermissionContextTest, RequestAfterRevokingNotifications) { | |
253 TestingProfile profile; | |
254 TestPushMessagingPermissionContext context(&profile); | |
255 PermissionRequestID request_id(-1, -1, -1); | |
256 BrowserPermissionCallback callback = base::Bind(DoNothing); | |
257 | |
258 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
259 CONTENT_SETTING_ALLOW); | |
260 | |
261 NavigateAndCommit(GURL(kOriginA)); | |
262 context.RequestPermission(web_contents(), request_id, GURL(kOriginA), | |
263 callback); | |
264 EXPECT_TRUE(context.was_granted()); | |
265 | |
266 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
267 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
268 | |
269 // Revoke notifications permission. This should revoke push, and prevent | |
270 // future requests for push from succeeding. | |
271 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
272 CONTENT_SETTING_BLOCK); | |
273 | |
274 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
275 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
276 | |
277 context.RequestPermission(web_contents(), request_id, GURL(kOriginA), | |
278 callback); | |
279 EXPECT_FALSE(context.was_persisted()); | |
280 EXPECT_FALSE(context.was_granted()); | |
281 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
282 context.GetPermissionStatus(GURL(kOriginA), GURL(kOriginA))); | |
283 } | |
284 | |
285 TEST_F(PushMessagingPermissionContextTest, GetPermissionStatusInsecureOrigin) { | |
286 TestingProfile profile; | |
287 TestPushMessagingPermissionContext context(&profile); | |
288 | |
289 // The status should be blocked for an insecure origin, regardless of the | |
290 // content setting value. | |
291 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
292 context.GetPermissionStatus(GURL(kInsecureOrigin), | |
293 GURL(kInsecureOrigin))); | |
294 | |
295 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
296 CONTENT_SETTING_ALLOW); | |
297 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
298 CONTENT_SETTING_ALLOW); | |
299 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
300 context.GetPermissionStatus(GURL(kInsecureOrigin), | |
301 GURL(kInsecureOrigin))); | |
302 | |
303 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
304 CONTENT_SETTING_BLOCK); | |
305 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
306 context.GetPermissionStatus(GURL(kInsecureOrigin), | |
307 GURL(kInsecureOrigin))); | |
308 | |
309 SetContentSetting(&profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | |
310 CONTENT_SETTING_ASK); | |
311 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
312 context.GetPermissionStatus(GURL(kInsecureOrigin), | |
313 GURL(kInsecureOrigin))); | |
314 } | |
OLD | NEW |