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