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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
// Copyright (c) 2011 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/content_settings/content_settings_pref_provider.h"
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "chrome/browser/content_settings/stub_settings_observer.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/prefs/default_pref_store.h"
#include "chrome/browser/prefs/overlay_persistent_pref_store.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/testing_pref_store.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/testing_browser_process_test.h"
#include "chrome/test/testing_pref_service.h"
#include "chrome/test/testing_profile.h"
#include "content/browser/browser_thread.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class ContentSettingsPrefService : public PrefService {
public:
ContentSettingsPrefService(PrefStore* managed_platform_prefs,
PrefStore* managed_cloud_prefs,
PrefStore* extension_prefs,
PrefStore* command_line_prefs,
PersistentPrefStore* user_prefs,
PrefStore* recommended_platform_prefs,
PrefStore* recommended_cloud_prefs,
DefaultPrefStore* default_store)
: PrefService(
managed_platform_prefs, managed_cloud_prefs, extension_prefs,
command_line_prefs, user_prefs, recommended_platform_prefs,
recommended_cloud_prefs, default_store, NULL) {}
virtual ~ContentSettingsPrefService() {}
};
}
namespace content_settings {
class PrefDefaultProviderTest : public TestingBrowserProcessTest {
public:
PrefDefaultProviderTest()
: ui_thread_(BrowserThread::UI, &message_loop_) {
}
protected:
MessageLoop message_loop_;
BrowserThread ui_thread_;
};
TEST_F(PrefDefaultProviderTest, DefaultValues) {
TestingProfile profile;
content_settings::PrefDefaultProvider provider(&profile);
ASSERT_FALSE(
provider.DefaultSettingIsManaged(CONTENT_SETTINGS_TYPE_COOKIES));
// Check setting defaults.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
provider.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES,
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
provider.ResetToDefaults();
EXPECT_EQ(CONTENT_SETTING_ALLOW,
provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
}
TEST_F(PrefDefaultProviderTest, Observer) {
TestingProfile profile;
PrefDefaultProvider provider(&profile);
StubSettingsObserver observer;
provider.UpdateDefaultSetting(
CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(profile.GetHostContentSettingsMap(), observer.last_notifier);
EXPECT_TRUE(observer.last_update_all);
EXPECT_FALSE(observer.last_update_all_types);
EXPECT_EQ(1, observer.counter);
}
TEST_F(PrefDefaultProviderTest, ObserveDefaultPref) {
TestingProfile profile;
PrefDefaultProvider provider(&profile);
PrefService* prefs = profile.GetPrefs();
// Make a copy of the default pref value so we can reset it later.
scoped_ptr<Value> default_value(prefs->FindPreference(
prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
provider.UpdateDefaultSetting(
CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
// Make a copy of the pref's new value so we can reset it later.
scoped_ptr<Value> new_value(prefs->FindPreference(
prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
// Clearing the backing pref should also clear the internal cache.
prefs->Set(prefs::kDefaultContentSettings, *default_value);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
// Reseting the pref to its previous value should update the cache.
prefs->Set(prefs::kDefaultContentSettings, *new_value);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
}
TEST_F(PrefDefaultProviderTest, OffTheRecord) {
TestingProfile profile;
PrefDefaultProvider provider(&profile);
profile.set_incognito(true);
PrefDefaultProvider otr_provider(&profile);
profile.set_incognito(false);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
// Changing content settings on the main provider should also affect the
// incognito map.
provider.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES,
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
// Changing content settings on the incognito provider should be ignored.
otr_provider.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES,
CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
}
// ////////////////////////////////////////////////////////////////////////////
// PrefProviderTest
//
bool SettingsEqual(const ContentSettings& settings1,
const ContentSettings& settings2) {
for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
if (settings1.settings[i] != settings2.settings[i])
return false;
}
return true;
}
class PrefProviderTest : public TestingBrowserProcessTest {
public:
PrefProviderTest() : ui_thread_(
BrowserThread::UI, &message_loop_) {
}
protected:
MessageLoop message_loop_;
BrowserThread ui_thread_;
};
TEST_F(PrefProviderTest, Observer) {
TestingProfile profile;
profile.GetHostContentSettingsMap();
Profile* p = &profile;
PrefProvider pref_content_settings_provider(p);
StubSettingsObserver observer;
ContentSettingsPattern pattern("[*.]example.com");
pref_content_settings_provider.SetContentSetting(
pattern,
pattern,
CONTENT_SETTINGS_TYPE_IMAGES,
"",
CONTENT_SETTING_ALLOW);
EXPECT_EQ(profile.GetHostContentSettingsMap(), observer.last_notifier);
EXPECT_EQ(pattern, observer.last_pattern);
EXPECT_FALSE(observer.last_update_all);
EXPECT_FALSE(observer.last_update_all_types);
// Expect 2 calls: One from the update and one from canonicalization.
EXPECT_EQ(2, observer.counter);
}
// Test for regression in which the PrefProvider modified the user pref store
// of the OTR unintentionally: http://crbug.com/74466.
TEST_F(PrefProviderTest, Incognito) {
DefaultPrefStore* default_prefs = new DefaultPrefStore();
PersistentPrefStore* user_prefs = new TestingPrefStore();
OverlayPersistentPrefStore* otr_user_prefs =
new OverlayPersistentPrefStore(user_prefs);
PrefService* regular_prefs = new ContentSettingsPrefService(
NULL, // managed_platform_prefs
NULL, // managed_cloud_prefs
NULL, // extension_prefs
NULL, // command_line_prefs
user_prefs,
NULL, // recommended_platform_prefs,
NULL, // recommended_cloud_prefs,
default_prefs);
Profile::RegisterUserPrefs(regular_prefs);
browser::RegisterUserPrefs(regular_prefs);
PrefService* otr_prefs = new ContentSettingsPrefService(
NULL, // managed_platform_prefs
NULL, // managed_cloud_prefs
NULL, // extension_prefs
NULL, // command_line_prefs
otr_user_prefs,
NULL, // recommended_platform_prefs,
NULL, // recommended_cloud_prefs,
default_prefs);
TestingProfile profile;
TestingProfile* otr_profile = new TestingProfile;
profile.SetOffTheRecordProfile(otr_profile);
profile.SetPrefService(regular_prefs);
otr_profile->set_incognito(true);
otr_profile->SetPrefService(otr_prefs);
profile.GetHostContentSettingsMap();
PrefProvider pref_content_settings_provider(&profile);
PrefProvider pref_content_settings_provider_incognito(otr_profile);
ContentSettingsPattern pattern("[*.]example.com");
pref_content_settings_provider.SetContentSetting(
pattern,
pattern,
CONTENT_SETTINGS_TYPE_IMAGES,
"",
CONTENT_SETTING_ALLOW);
GURL host("http://example.com/");
// The value should of course be visible in the regular PrefProvider.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
pref_content_settings_provider.GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
// And also in the OTR version.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
pref_content_settings_provider_incognito.GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
// But the value should not be overridden in the OTR user prefs accidentally.
EXPECT_FALSE(otr_user_prefs->IsSetInOverlay(prefs::kContentSettingsPatterns));
}
TEST_F(PrefProviderTest, Patterns) {
TestingProfile testing_profile;
testing_profile.GetHostContentSettingsMap();
PrefProvider pref_content_settings_provider(
testing_profile.GetOriginalProfile());
GURL host1("http://example.com/");
GURL host2("http://www.example.com/");
GURL host3("http://example.org/");
GURL host4("file:///tmp/test.html");
ContentSettingsPattern pattern1("[*.]example.com");
ContentSettingsPattern pattern2("example.org");
ContentSettingsPattern pattern3("file:///tmp/test.html");
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
pref_content_settings_provider.GetContentSetting(
host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, ""));
pref_content_settings_provider.SetContentSetting(
pattern1,
pattern1,
CONTENT_SETTINGS_TYPE_IMAGES,
"",
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
pref_content_settings_provider.GetContentSetting(
host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, ""));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
pref_content_settings_provider.GetContentSetting(
host2, host2, CONTENT_SETTINGS_TYPE_IMAGES, ""));
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
pref_content_settings_provider.GetContentSetting(
host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, ""));
pref_content_settings_provider.SetContentSetting(
pattern2,
pattern2,
CONTENT_SETTINGS_TYPE_IMAGES,
"",
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
pref_content_settings_provider.GetContentSetting(
host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, ""));
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
pref_content_settings_provider.GetContentSetting(
host4, host4, CONTENT_SETTINGS_TYPE_IMAGES, ""));
pref_content_settings_provider.SetContentSetting(
pattern3,
pattern3,
CONTENT_SETTINGS_TYPE_IMAGES,
"",
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
pref_content_settings_provider.GetContentSetting(
host4, host4, CONTENT_SETTINGS_TYPE_IMAGES, ""));
}
TEST_F(PrefProviderTest, ResourceIdentifier) {
// This feature is currently behind a flag.
CommandLine* cmd = CommandLine::ForCurrentProcess();
AutoReset<CommandLine> auto_reset(cmd, *cmd);
cmd->AppendSwitch(switches::kEnableResourceContentSettings);
TestingProfile testing_profile;
testing_profile.GetHostContentSettingsMap();
PrefProvider pref_content_settings_provider(
testing_profile.GetOriginalProfile());
GURL host("http://example.com/");
ContentSettingsPattern pattern("[*.]example.com");
std::string resource1("someplugin");
std::string resource2("otherplugin");
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
pref_content_settings_provider.GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1));
pref_content_settings_provider.SetContentSetting(
pattern,
pattern,
CONTENT_SETTINGS_TYPE_PLUGINS,
resource1,
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
pref_content_settings_provider.GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1));
EXPECT_EQ(CONTENT_SETTING_DEFAULT,
pref_content_settings_provider.GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource2));
}
} // namespace content_settings
|