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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
// Copyright (c) 2010 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/host_content_settings_map.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "net/base/dns_util.h"
#include "net/base/static_cookie_policy.h"
// static
const wchar_t*
HostContentSettingsMap::kTypeNames[CONTENT_SETTINGS_NUM_TYPES] = {
L"cookies",
L"images",
L"javascript",
L"plugins",
L"popups",
NULL, // Not used for Geolocation
};
// static
const ContentSetting
HostContentSettingsMap::kDefaultSettings[CONTENT_SETTINGS_NUM_TYPES] = {
CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_COOKIES
CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_IMAGES
CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_JAVASCRIPT
CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_PLUGINS
CONTENT_SETTING_BLOCK, // CONTENT_SETTINGS_TYPE_POPUPS
CONTENT_SETTING_ASK, // Not used for Geolocation
};
HostContentSettingsMap::HostContentSettingsMap(Profile* profile)
: profile_(profile),
block_third_party_cookies_(false) {
PrefService* prefs = profile_->GetPrefs();
// Migrate obsolete cookie pref.
if (prefs->HasPrefPath(prefs::kCookieBehavior)) {
int cookie_behavior = prefs->GetInteger(prefs::kCookieBehavior);
prefs->ClearPref(prefs::kCookieBehavior);
if (!prefs->HasPrefPath(prefs::kDefaultContentSettings)) {
SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES,
(cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ?
CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW);
}
if (!prefs->HasPrefPath(prefs::kBlockThirdPartyCookies)) {
SetBlockThirdPartyCookies(cookie_behavior ==
net::StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES);
}
}
// Migrate obsolete popups pref.
if (prefs->HasPrefPath(prefs::kPopupWhitelistedHosts)) {
const ListValue* whitelist_pref =
prefs->GetList(prefs::kPopupWhitelistedHosts);
for (ListValue::const_iterator i(whitelist_pref->begin());
i != whitelist_pref->end(); ++i) {
std::string host;
(*i)->GetAsString(&host);
SetContentSetting(host, CONTENT_SETTINGS_TYPE_POPUPS,
CONTENT_SETTING_ALLOW);
}
prefs->ClearPref(prefs::kPopupWhitelistedHosts);
}
// Read global defaults.
DCHECK_EQ(arraysize(kTypeNames),
static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES));
const DictionaryValue* default_settings_dictionary =
prefs->GetDictionary(prefs::kDefaultContentSettings);
// Careful: The returned value could be NULL if the pref has never been set.
if (default_settings_dictionary != NULL) {
GetSettingsFromDictionary(default_settings_dictionary,
&default_content_settings_);
}
ForceDefaultsToBeExplicit();
// Read host-specific exceptions.
const DictionaryValue* all_settings_dictionary =
prefs->GetDictionary(prefs::kPerHostContentSettings);
// Careful: The returned value could be NULL if the pref has never been set.
if (all_settings_dictionary != NULL) {
for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
i != all_settings_dictionary->end_keys(); ++i) {
std::wstring wide_host(*i);
DictionaryValue* host_settings_dictionary = NULL;
bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
wide_host, &host_settings_dictionary);
DCHECK(found);
ContentSettings settings;
GetSettingsFromDictionary(host_settings_dictionary, &settings);
host_content_settings_[WideToUTF8(wide_host)] = settings;
}
}
// Read misc. global settings.
block_third_party_cookies_ =
prefs->GetBoolean(prefs::kBlockThirdPartyCookies);
}
// static
void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings);
prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings);
prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false);
prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0);
// Obsolete prefs, for migration:
prefs->RegisterIntegerPref(prefs::kCookieBehavior,
net::StaticCookiePolicy::ALLOW_ALL_COOKIES);
prefs->RegisterListPref(prefs::kPopupWhitelistedHosts);
}
ContentSetting HostContentSettingsMap::GetDefaultContentSetting(
ContentSettingsType content_type) const {
AutoLock auto_lock(lock_);
return default_content_settings_.settings[content_type];
}
ContentSetting HostContentSettingsMap::GetContentSetting(
const std::string& host,
ContentSettingsType content_type) const {
AutoLock auto_lock(lock_);
HostContentSettings::const_iterator i(host_content_settings_.find(
net::TrimEndingDot(host)));
if (i != host_content_settings_.end()) {
ContentSetting setting = i->second.settings[content_type];
if (setting != CONTENT_SETTING_DEFAULT)
return setting;
}
return default_content_settings_.settings[content_type];
}
ContentSetting HostContentSettingsMap::GetContentSetting(
const GURL& url,
ContentSettingsType content_type) const {
return ShouldAllowAllContent(url) ?
CONTENT_SETTING_ALLOW : GetContentSetting(url.host(), content_type);
}
ContentSettings HostContentSettingsMap::GetContentSettings(
const std::string& host) const {
AutoLock auto_lock(lock_);
HostContentSettings::const_iterator i(host_content_settings_.find(
net::TrimEndingDot(host)));
if (i == host_content_settings_.end())
return default_content_settings_;
ContentSettings output = i->second;
for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) {
if (output.settings[j] == CONTENT_SETTING_DEFAULT)
output.settings[j] = default_content_settings_.settings[j];
}
return output;
}
ContentSettings HostContentSettingsMap::GetContentSettings(
const GURL& url) const {
return ShouldAllowAllContent(url) ?
ContentSettings(CONTENT_SETTING_ALLOW) : GetContentSettings(url.host());
}
void HostContentSettingsMap::GetSettingsForOneType(
ContentSettingsType content_type,
SettingsForOneType* settings) const {
DCHECK(settings);
settings->clear();
AutoLock auto_lock(lock_);
for (HostContentSettings::const_iterator i(host_content_settings_.begin());
i != host_content_settings_.end(); ++i) {
ContentSetting setting = i->second.settings[content_type];
if (setting != CONTENT_SETTING_DEFAULT) {
// Use of push_back() relies on the map iterator traversing in order of
// ascending keys.
settings->push_back(std::make_pair(i->first, setting));
}
}
}
void HostContentSettingsMap::SetDefaultContentSetting(
ContentSettingsType content_type,
ContentSetting setting) {
DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
DictionaryValue* default_settings_dictionary =
profile_->GetPrefs()->GetMutableDictionary(
prefs::kDefaultContentSettings);
std::wstring dictionary_path(kTypeNames[content_type]);
{
AutoLock auto_lock(lock_);
if ((setting == CONTENT_SETTING_DEFAULT) ||
(setting == kDefaultSettings[content_type])) {
default_content_settings_.settings[content_type] =
kDefaultSettings[content_type];
default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path,
NULL);
} else {
default_content_settings_.settings[content_type] = setting;
default_settings_dictionary->SetWithoutPathExpansion(
dictionary_path, Value::CreateIntegerValue(setting));
}
}
NotifyObservers(std::string());
}
void HostContentSettingsMap::SetContentSetting(const std::string& host,
ContentSettingsType content_type,
ContentSetting setting) {
DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
bool early_exit = false;
std::wstring wide_host(UTF8ToWide(host));
DictionaryValue* all_settings_dictionary =
profile_->GetPrefs()->GetMutableDictionary(
prefs::kPerHostContentSettings);
{
AutoLock auto_lock(lock_);
if (!host_content_settings_.count(host))
host_content_settings_[host] = ContentSettings();
HostContentSettings::iterator i(host_content_settings_.find(host));
ContentSettings& settings = i->second;
settings.settings[content_type] = setting;
if (AllDefault(settings)) {
host_content_settings_.erase(i);
all_settings_dictionary->RemoveWithoutPathExpansion(wide_host, NULL);
// We can't just return because |NotifyObservers()| needs to be called,
// without |lock_| being held.
early_exit = true;
}
}
if (!early_exit) {
DictionaryValue* host_settings_dictionary;
bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
wide_host, &host_settings_dictionary);
if (!found) {
host_settings_dictionary = new DictionaryValue;
all_settings_dictionary->SetWithoutPathExpansion(
wide_host, host_settings_dictionary);
DCHECK_NE(setting, CONTENT_SETTING_DEFAULT);
}
std::wstring dictionary_path(kTypeNames[content_type]);
if (setting == CONTENT_SETTING_DEFAULT) {
host_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path,
NULL);
} else {
host_settings_dictionary->SetWithoutPathExpansion(
dictionary_path, Value::CreateIntegerValue(setting));
}
}
NotifyObservers(host);
}
void HostContentSettingsMap::ClearSettingsForOneType(
ContentSettingsType content_type) {
DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
{
AutoLock auto_lock(lock_);
for (HostContentSettings::iterator i(host_content_settings_.begin());
i != host_content_settings_.end(); ) {
if (i->second.settings[content_type] != CONTENT_SETTING_DEFAULT) {
i->second.settings[content_type] = CONTENT_SETTING_DEFAULT;
std::wstring wide_host(UTF8ToWide(i->first));
DictionaryValue* all_settings_dictionary =
profile_->GetPrefs()->GetMutableDictionary(
prefs::kPerHostContentSettings);
if (AllDefault(i->second)) {
all_settings_dictionary->RemoveWithoutPathExpansion(wide_host, NULL);
host_content_settings_.erase(i++);
} else {
DictionaryValue* host_settings_dictionary;
bool found =
all_settings_dictionary->GetDictionaryWithoutPathExpansion(
wide_host, &host_settings_dictionary);
DCHECK(found);
host_settings_dictionary->RemoveWithoutPathExpansion(
kTypeNames[content_type], NULL);
++i;
}
} else {
++i;
}
}
}
NotifyObservers(std::string());
}
void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
{
AutoLock auto_lock(lock_);
block_third_party_cookies_ = block;
}
PrefService* prefs = profile_->GetPrefs();
if (block)
prefs->SetBoolean(prefs::kBlockThirdPartyCookies, true);
else
prefs->ClearPref(prefs::kBlockThirdPartyCookies);
}
void HostContentSettingsMap::ResetToDefaults() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
{
AutoLock auto_lock(lock_);
default_content_settings_ = ContentSettings();
ForceDefaultsToBeExplicit();
host_content_settings_.clear();
block_third_party_cookies_ = false;
}
PrefService* prefs = profile_->GetPrefs();
prefs->ClearPref(prefs::kDefaultContentSettings);
prefs->ClearPref(prefs::kPerHostContentSettings);
prefs->ClearPref(prefs::kBlockThirdPartyCookies);
NotifyObservers(std::string());
}
HostContentSettingsMap::~HostContentSettingsMap() {
}
// static
bool HostContentSettingsMap::ShouldAllowAllContent(const GURL& url) {
return url.SchemeIs(chrome::kChromeInternalScheme) ||
url.SchemeIs(chrome::kChromeUIScheme) ||
url.SchemeIs(chrome::kExtensionScheme) ||
url.SchemeIs(chrome::kGearsScheme) ||
url.SchemeIs(chrome::kUserScriptScheme);
}
void HostContentSettingsMap::GetSettingsFromDictionary(
const DictionaryValue* dictionary,
ContentSettings* settings) {
for (DictionaryValue::key_iterator i(dictionary->begin_keys());
i != dictionary->end_keys(); ++i) {
std::wstring content_type(*i);
int setting = CONTENT_SETTING_DEFAULT;
bool found = dictionary->GetIntegerWithoutPathExpansion(content_type,
&setting);
DCHECK(found);
for (size_t type = 0; type < arraysize(kTypeNames); ++type) {
if ((kTypeNames[type] != NULL) &&
(std::wstring(kTypeNames[type]) == content_type)) {
settings->settings[type] = IntToContentSetting(setting);
break;
}
}
}
}
void HostContentSettingsMap::ForceDefaultsToBeExplicit() {
DCHECK_EQ(arraysize(kDefaultSettings),
static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES));
for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
if (default_content_settings_.settings[i] == CONTENT_SETTING_DEFAULT)
default_content_settings_.settings[i] = kDefaultSettings[i];
}
}
bool HostContentSettingsMap::AllDefault(const ContentSettings& settings) const {
for (size_t i = 0; i < arraysize(settings.settings); ++i) {
if (settings.settings[i] != CONTENT_SETTING_DEFAULT)
return false;
}
return true;
}
void HostContentSettingsMap::NotifyObservers(const std::string& host) {
ContentSettingsDetails details(host);
NotificationService::current()->Notify(
NotificationType::CONTENT_SETTINGS_CHANGED,
Source<HostContentSettingsMap>(this),
Details<ContentSettingsDetails>(&details));
}
|