Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: components/content_settings/core/browser/content_settings_pref_provider.cc

Issue 1005303003: Split the aggregate dictionary of content settings exceptions into per-type dictionaries (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Memory leak in last test. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "components/content_settings/core/browser/content_settings_pref_provide r.h" 5 #include "components/content_settings/core/browser/content_settings_pref_provide r.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/auto_reset.h"
11 #include "base/bind.h" 12 #include "base/bind.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
13 #include "base/metrics/histogram.h" 15 #include "base/metrics/histogram.h"
14 #include "base/prefs/pref_service.h" 16 #include "base/prefs/pref_service.h"
17 #include "base/prefs/scoped_user_pref_update.h"
15 #include "base/strings/string_split.h" 18 #include "base/strings/string_split.h"
16 #include "base/time/clock.h" 19 #include "base/time/clock.h"
17 #include "base/time/default_clock.h" 20 #include "base/time/default_clock.h"
18 #include "components/content_settings/core/browser/content_settings_pref.h" 21 #include "components/content_settings/core/browser/content_settings_pref.h"
19 #include "components/content_settings/core/browser/content_settings_rule.h" 22 #include "components/content_settings/core/browser/content_settings_rule.h"
20 #include "components/content_settings/core/browser/content_settings_utils.h" 23 #include "components/content_settings/core/browser/content_settings_utils.h"
21 #include "components/content_settings/core/browser/host_content_settings_map.h" 24 #include "components/content_settings/core/browser/host_content_settings_map.h"
22 #include "components/content_settings/core/common/content_settings.h" 25 #include "components/content_settings/core/common/content_settings.h"
23 #include "components/content_settings/core/common/content_settings_pattern.h" 26 #include "components/content_settings/core/common/content_settings_pattern.h"
24 #include "components/content_settings/core/common/pref_names.h" 27 #include "components/content_settings/core/common/pref_names.h"
25 #include "components/pref_registry/pref_registry_syncable.h" 28 #include "components/pref_registry/pref_registry_syncable.h"
26 29
27 namespace { 30 namespace {
28 31
32 const char kPerPluginPrefName[] = "per_plugin";
33
34 // Returns true and sets |pref_key| to the key in the content settings
35 // dictionary under which per-resource content settings are stored,
36 // if the given content type supports resource identifiers in user preferences.
37 bool GetResourceTypeName(ContentSettingsType content_type,
38 std::string* pref_key) {
39 if (content_type == CONTENT_SETTINGS_TYPE_PLUGINS) {
40 *pref_key = kPerPluginPrefName;
41 return true;
42 }
43 return false;
44 }
45
46 // TODO(msramek): Check if we still need this migration code. If not, remove it.
47 ContentSetting FixObsoleteCookiePromptMode(ContentSettingsType content_type,
48 ContentSetting setting) {
49 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES &&
50 setting == CONTENT_SETTING_ASK) {
51 return CONTENT_SETTING_BLOCK;
52 }
53 return setting;
54 }
55
56 // A helper function to duplicate |ContentSettingsPattern|, so that
57 // |ReadContentSettingsFromOldPref| can export them in a vector. We cannot pass
58 // them by pointer, because the original values will go out of scope when
59 // the vector is used in |WriteSettingsToNewPreferences|.
60 ContentSettingsPattern CopyPattern(const ContentSettingsPattern& pattern) {
61 return ContentSettingsPattern::FromString(pattern.ToString());
62 }
63
29 const char kAudioKey[] = "audio"; 64 const char kAudioKey[] = "audio";
30 const char kVideoKey[] = "video"; 65 const char kVideoKey[] = "video";
31 66
67 // A list of exception preferences corresponding to individual content settings
68 // types. Must be kept in sync with the enum |ContentSettingsType|.
69 const char* kContentSettingsExceptionsPrefs[] = {
70 prefs::kContentSettingsCookiesPatternPairs,
71 prefs::kContentSettingsImagesPatternPairs,
72 prefs::kContentSettingsJavaScriptPatternPairs,
73 prefs::kContentSettingsPluginsPatternPairs,
74 prefs::kContentSettingsPopupsPatternPairs,
75 prefs::kContentSettingsGeolocationPatternPairs,
76 prefs::kContentSettingsNotificationsPatternPairs,
77 prefs::kContentSettingsAutoSelectCertificatePatternPairs,
78 prefs::kContentSettingsFullScreenPatternPairs,
79 prefs::kContentSettingsMouseLockPatternPairs,
80 prefs::kContentSettingsMixedScriptPatternPairs,
81 prefs::kContentSettingsMediaStreamPatternPairs,
82 prefs::kContentSettingsMediaStreamMicPatternPairs,
83 prefs::kContentSettingsMediaStreamCameraPatternPairs,
84 prefs::kContentSettingsProtocolHandlersPatternPairs,
85 prefs::kContentSettingsPpapiBrokerPatternPairs,
86 prefs::kContentSettingsAutomaticDownloadsPatternPairs,
87 prefs::kContentSettingsMidiSysexPatternPairs,
88 prefs::kContentSettingsPushMessagingPatternPairs,
89 prefs::kContentSettingsSSLCertDecisionsPatternPairs,
90 #if defined(OS_WIN)
91 prefs::kContentSettingsMetroSwitchToDesktopPatternPairs,
92 #elif defined(OS_ANDROID) || defined(OS_CHROMEOS)
93 prefs::kContentSettingsProtectedMediaIdentifierPatternPairs,
94 #endif
95 prefs::kContentSettingsAppBannerPatternPairs
96 };
97 static_assert(arraysize(kContentSettingsExceptionsPrefs)
98 == CONTENT_SETTINGS_NUM_TYPES,
99 "kContentSettingsExceptionsPrefs should have "
100 "CONTENT_SETTINGS_NUM_TYPES elements");
101
32 } // namespace 102 } // namespace
33 103
34 namespace content_settings { 104 namespace content_settings {
35 105
36 // //////////////////////////////////////////////////////////////////////////// 106 // ////////////////////////////////////////////////////////////////////////////
37 // PrefProvider: 107 // PrefProvider:
38 // 108 //
39 109
40 // static 110 // static
41 void PrefProvider::RegisterProfilePrefs( 111 void PrefProvider::RegisterProfilePrefs(
42 user_prefs::PrefRegistrySyncable* registry) { 112 user_prefs::PrefRegistrySyncable* registry) {
43 registry->RegisterIntegerPref( 113 registry->RegisterIntegerPref(
44 prefs::kContentSettingsVersion, 114 prefs::kContentSettingsVersion,
45 ContentSettingsPattern::kContentSettingsPatternVersion, 115 ContentSettingsPattern::kContentSettingsPatternVersion,
46 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 116 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
47 registry->RegisterDictionaryPref( 117 registry->RegisterDictionaryPref(
48 prefs::kContentSettingsPatternPairs, 118 prefs::kContentSettingsPatternPairs,
49 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 119 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
120 registry->RegisterBooleanPref(
121 prefs::kMigratedContentSettingsPatternPairs,
122 false,
123 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
124
125 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
126 registry->RegisterDictionaryPref(
127 kContentSettingsExceptionsPrefs[i],
128 IsContentSettingsTypeSyncable(ContentSettingsType(i))
129 ? user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
130 : user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
131 );
132 }
50 } 133 }
51 134
52 PrefProvider::PrefProvider(PrefService* prefs, bool incognito) 135 PrefProvider::PrefProvider(PrefService* prefs, bool incognito)
53 : prefs_(prefs), 136 : prefs_(prefs),
54 clock_(new base::DefaultClock()) { 137 clock_(new base::DefaultClock()),
138 is_incognito_(incognito),
139 updating_old_preferences_(false) {
55 DCHECK(prefs_); 140 DCHECK(prefs_);
56 // Verify preferences version. 141 // Verify preferences version.
57 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { 142 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) {
58 prefs_->SetInteger(prefs::kContentSettingsVersion, 143 prefs_->SetInteger(prefs::kContentSettingsVersion,
59 ContentSettingsPattern::kContentSettingsPatternVersion); 144 ContentSettingsPattern::kContentSettingsPatternVersion);
60 } 145 }
61 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > 146 if (prefs_->GetInteger(prefs::kContentSettingsVersion) >
62 ContentSettingsPattern::kContentSettingsPatternVersion) { 147 ContentSettingsPattern::kContentSettingsPatternVersion) {
63 return; 148 return;
64 } 149 }
65 150
66 pref_change_registrar_.Init(prefs_); 151 pref_change_registrar_.Init(prefs_);
67 content_settings_pref_.reset(new ContentSettingsPref(
68 prefs_, &pref_change_registrar_, clock_.get(), incognito,
69 base::Bind(&PrefProvider::Notify,
70 base::Unretained(this))));
71 152
72 if (!incognito) { 153 pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, base::Bind(
154 &PrefProvider::OnOldContentSettingsPatternPairsChanged,
155 base::Unretained(this)));
156
157 for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
158 content_settings_prefs_.push_back(new ContentSettingsPref(
159 ContentSettingsType(i), prefs_, &pref_change_registrar_,
160 kContentSettingsExceptionsPrefs[i], is_incognito_,
161 &updating_old_preferences_, base::Bind(&PrefProvider::Notify,
162 base::Unretained(this))));
163 }
164
165 // Migrate all the exceptions from the aggregate dictionary preference
166 // to the separate dictionaries, if this hasn't been done before.
167 if (!prefs_->GetBoolean(prefs::kMigratedContentSettingsPatternPairs)) {
168 WriteSettingsToNewPreferences(false);
169 prefs_->SetBoolean(prefs::kMigratedContentSettingsPatternPairs, true);
170 } else {
171 // Trigger the update of old preference, and as a result,
172 // the new preferences as well.
173 OnOldContentSettingsPatternPairsChanged();
174 }
175
176 if (!is_incognito_) {
177 size_t num_exceptions = 0;
178 for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i)
179 num_exceptions += content_settings_prefs_[i]->GetNumExceptions();
180
73 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions", 181 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions",
74 content_settings_pref_->GetNumExceptions()); 182 num_exceptions);
183
75 // Migrate the obsolete media content setting exceptions to the new 184 // Migrate the obsolete media content setting exceptions to the new
76 // settings. This needs to be done after ReadContentSettingsFromPref(). 185 // settings.
77 MigrateObsoleteMediaContentSetting(); 186 MigrateObsoleteMediaContentSetting();
78 } 187 }
188
raymes 2015/04/07 01:55:56 nit: unneeded extra line
79 } 189 }
80 190
81 PrefProvider::~PrefProvider() { 191 PrefProvider::~PrefProvider() {
82 DCHECK(!prefs_); 192 DCHECK(!prefs_);
83 } 193 }
84 194
85 RuleIterator* PrefProvider::GetRuleIterator( 195 RuleIterator* PrefProvider::GetRuleIterator(
86 ContentSettingsType content_type, 196 ContentSettingsType content_type,
87 const ResourceIdentifier& resource_identifier, 197 const ResourceIdentifier& resource_identifier,
88 bool incognito) const { 198 bool incognito) const {
89 return content_settings_pref_->GetRuleIterator(content_type, 199 return content_settings_prefs_[content_type]->GetRuleIterator(
90 resource_identifier, 200 resource_identifier,
91 incognito); 201 incognito);
92 } 202 }
93 203
94 bool PrefProvider::SetWebsiteSetting( 204 bool PrefProvider::SetWebsiteSetting(
95 const ContentSettingsPattern& primary_pattern, 205 const ContentSettingsPattern& primary_pattern,
96 const ContentSettingsPattern& secondary_pattern, 206 const ContentSettingsPattern& secondary_pattern,
97 ContentSettingsType content_type, 207 ContentSettingsType content_type,
98 const ResourceIdentifier& resource_identifier, 208 const ResourceIdentifier& resource_identifier,
99 base::Value* in_value) { 209 base::Value* in_value) {
100 DCHECK(CalledOnValidThread()); 210 DCHECK(CalledOnValidThread());
101 DCHECK(prefs_); 211 DCHECK(prefs_);
102 212
103 return content_settings_pref_->SetWebsiteSetting(primary_pattern, 213 // Default settings are set using a wildcard pattern for both
104 secondary_pattern, 214 // |primary_pattern| and |secondary_pattern|. Don't store default settings in
105 content_type, 215 // the |PrefProvider|. The |PrefProvider| handles settings for specific
106 resource_identifier, 216 // sites/origins defined by the |primary_pattern| and the |secondary_pattern|.
107 in_value); 217 // Default settings are handled by the |DefaultProvider|.
218 if (primary_pattern == ContentSettingsPattern::Wildcard() &&
219 secondary_pattern == ContentSettingsPattern::Wildcard() &&
220 resource_identifier.empty()) {
221 return false;
222 }
223
224 return content_settings_prefs_[content_type]->SetWebsiteSetting(
225 primary_pattern,
226 secondary_pattern,
227 resource_identifier,
228 in_value);
108 } 229 }
109 230
110 void PrefProvider::ClearAllContentSettingsRules( 231 void PrefProvider::ClearAllContentSettingsRules(
111 ContentSettingsType content_type) { 232 ContentSettingsType content_type) {
112 DCHECK(CalledOnValidThread()); 233 DCHECK(CalledOnValidThread());
113 DCHECK(prefs_); 234 DCHECK(prefs_);
114 235
115 content_settings_pref_->ClearAllContentSettingsRules(content_type); 236 content_settings_prefs_[content_type]->ClearAllContentSettingsRules();
116 } 237 }
117 238
118 void PrefProvider::ShutdownOnUIThread() { 239 void PrefProvider::ShutdownOnUIThread() {
119 DCHECK(CalledOnValidThread()); 240 DCHECK(CalledOnValidThread());
120 DCHECK(prefs_); 241 DCHECK(prefs_);
121 RemoveAllObservers(); 242 RemoveAllObservers();
122 pref_change_registrar_.RemoveAll(); 243 pref_change_registrar_.RemoveAll();
123 prefs_ = NULL; 244 prefs_ = NULL;
124 } 245 }
125 246
126 void PrefProvider::UpdateLastUsage( 247 void PrefProvider::UpdateLastUsage(
127 const ContentSettingsPattern& primary_pattern, 248 const ContentSettingsPattern& primary_pattern,
128 const ContentSettingsPattern& secondary_pattern, 249 const ContentSettingsPattern& secondary_pattern,
129 ContentSettingsType content_type) { 250 ContentSettingsType content_type) {
130 content_settings_pref_->UpdateLastUsage(primary_pattern, 251 content_settings_prefs_[content_type]->UpdateLastUsage(primary_pattern,
131 secondary_pattern, 252 secondary_pattern,
132 content_type); 253 clock_.get());
133 } 254 }
134 255
135 base::Time PrefProvider::GetLastUsage( 256 base::Time PrefProvider::GetLastUsage(
136 const ContentSettingsPattern& primary_pattern, 257 const ContentSettingsPattern& primary_pattern,
137 const ContentSettingsPattern& secondary_pattern, 258 const ContentSettingsPattern& secondary_pattern,
138 ContentSettingsType content_type) { 259 ContentSettingsType content_type) {
139 return content_settings_pref_->GetLastUsage(primary_pattern, 260 return content_settings_prefs_[content_type]->GetLastUsage(primary_pattern,
140 secondary_pattern, 261 secondary_pattern);
141 content_type);
142 } 262 }
143 263
144 // //////////////////////////////////////////////////////////////////////////// 264 // ////////////////////////////////////////////////////////////////////////////
145 // Private 265 // Private
146 266
267 PrefProvider::ContentSettingsPrefEntry::ContentSettingsPrefEntry(
268 const ContentSettingsPattern primary_pattern,
269 const ContentSettingsPattern secondary_pattern,
270 const ResourceIdentifier resource_identifier,
271 base::Value* value)
272 : primary_pattern(CopyPattern(primary_pattern)),
273 secondary_pattern(CopyPattern(secondary_pattern)),
274 resource_identifier(resource_identifier),
275 value(value) {
276 }
277
278 PrefProvider::ContentSettingsPrefEntry::ContentSettingsPrefEntry(
279 const ContentSettingsPrefEntry& entry)
280 : primary_pattern(CopyPattern(entry.primary_pattern)),
281 secondary_pattern(CopyPattern(entry.secondary_pattern)),
282 resource_identifier(entry.resource_identifier),
283 value(entry.value->DeepCopy()) {
284 }
285
286 PrefProvider::ContentSettingsPrefEntry&
287 PrefProvider::ContentSettingsPrefEntry::operator=(
288 const ContentSettingsPrefEntry& entry) {
289 this->primary_pattern = CopyPattern(entry.primary_pattern);
290 this->secondary_pattern = CopyPattern(entry.secondary_pattern);
291 this->resource_identifier = entry.resource_identifier;
292 this->value.reset(entry.value->DeepCopy());
293
294 return *this;
295 }
296
297 PrefProvider::ContentSettingsPrefEntry::~ContentSettingsPrefEntry() {}
298
147 void PrefProvider::MigrateObsoleteMediaContentSetting() { 299 void PrefProvider::MigrateObsoleteMediaContentSetting() {
148 std::vector<Rule> rules_to_delete; 300 std::vector<Rule> rules_to_delete;
149 { 301 {
150 scoped_ptr<RuleIterator> rule_iterator(GetRuleIterator( 302 scoped_ptr<RuleIterator> rule_iterator(GetRuleIterator(
151 CONTENT_SETTINGS_TYPE_MEDIASTREAM, std::string(), false)); 303 CONTENT_SETTINGS_TYPE_MEDIASTREAM, std::string(), false));
152 while (rule_iterator->HasNext()) { 304 while (rule_iterator->HasNext()) {
153 // Skip default setting and rules without a value. 305 // Skip default setting and rules without a value.
154 const content_settings::Rule& rule = rule_iterator->Next(); 306 const content_settings::Rule& rule = rule_iterator->Next();
155 DCHECK(rule.primary_pattern != ContentSettingsPattern::Wildcard()); 307 DCHECK(rule.primary_pattern != ContentSettingsPattern::Wildcard());
156 if (!rule.value.get()) 308 if (!rule.value.get())
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 const ContentSettingsPattern& primary_pattern, 350 const ContentSettingsPattern& primary_pattern,
199 const ContentSettingsPattern& secondary_pattern, 351 const ContentSettingsPattern& secondary_pattern,
200 ContentSettingsType content_type, 352 ContentSettingsType content_type,
201 const std::string& resource_identifier) { 353 const std::string& resource_identifier) {
202 NotifyObservers(primary_pattern, 354 NotifyObservers(primary_pattern,
203 secondary_pattern, 355 secondary_pattern,
204 content_type, 356 content_type,
205 resource_identifier); 357 resource_identifier);
206 } 358 }
207 359
360 void PrefProvider::ReadContentSettingsFromOldPref() {
361 // |DictionaryPrefUpdate| sends out notifications when destructed. This
362 // construction order ensures |AutoLock| gets destroyed first and |old_lock_|
363 // is not held when the notifications are sent. Also, |auto_reset| must be
364 // still valid when the notifications are sent, so that |Observe| skips the
365 // notification.
366 base::AutoReset<bool> auto_reset(&updating_old_preferences_, true);
367 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
368
369 ClearPrefEntryMap();
370
371 const base::DictionaryValue* all_settings_dictionary =
372 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
373
374 // Careful: The returned value could be NULL if the pref has never been set.
375 if (!all_settings_dictionary)
376 return;
377
378 base::DictionaryValue* mutable_settings;
379 scoped_ptr<base::DictionaryValue> mutable_settings_scope;
380
381 if (!is_incognito_) {
382 mutable_settings = update.Get();
383 } else {
384 // Create copy as we do not want to persist anything in OTR prefs.
385 mutable_settings = all_settings_dictionary->DeepCopy();
386 mutable_settings_scope.reset(mutable_settings);
387 }
388 // Convert all Unicode patterns into punycode form, then read.
389 ContentSettingsPref::CanonicalizeContentSettingsExceptions(mutable_settings);
390
391 for (base::DictionaryValue::Iterator i(*mutable_settings); !i.IsAtEnd();
392 i.Advance()) {
393 const std::string& pattern_str(i.key());
394 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
395 ParsePatternString(pattern_str);
396 if (!pattern_pair.first.IsValid() ||
397 !pattern_pair.second.IsValid()) {
398 // TODO: Change this to DFATAL when crbug.com/132659 is fixed.
399 LOG(ERROR) << "Invalid pattern strings: " << pattern_str;
400 continue;
401 }
402
403 // Get settings dictionary for the current pattern string, and read
404 // settings from the dictionary.
405 const base::DictionaryValue* settings_dictionary = NULL;
406 bool is_dictionary = i.value().GetAsDictionary(&settings_dictionary);
407 DCHECK(is_dictionary);
408
409 for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
brucedawson 2015/04/03 19:55:52 The VC++ /analyze builder pointed out that this lo
msramek 2015/04/03 22:13:48 Thanks a lot for pointing this out! Apart from sma
410 ContentSettingsType content_type = static_cast<ContentSettingsType>(i);
411
412 std::string res_dictionary_path;
413 if (GetResourceTypeName(content_type, &res_dictionary_path)) {
414 const base::DictionaryValue* resource_dictionary = NULL;
415 if (settings_dictionary->GetDictionary(
416 res_dictionary_path, &resource_dictionary)) {
417 for (base::DictionaryValue::Iterator j(*resource_dictionary);
418 !j.IsAtEnd();
419 j.Advance()) {
420 const std::string& resource_identifier(j.key());
421 int setting = CONTENT_SETTING_DEFAULT;
422 bool is_integer = j.value().GetAsInteger(&setting);
423 DCHECK(is_integer);
424 DCHECK_NE(CONTENT_SETTING_DEFAULT, setting);
425
426 pref_entry_map_[content_type].push_back(
427 new ContentSettingsPrefEntry(
428 pattern_pair.first,
429 pattern_pair.second,
430 resource_identifier,
431 new base::FundamentalValue(setting)));
432 }
433 }
434 }
435 base::Value* value = NULL;
436 if (HostContentSettingsMap::ContentTypeHasCompoundValue(content_type)) {
437 const base::DictionaryValue* setting = NULL;
438 // TODO(xians): Handle the non-dictionary types.
439 if (settings_dictionary->GetDictionaryWithoutPathExpansion(
440 GetTypeName(ContentSettingsType(i)), &setting)) {
441 DCHECK(!setting->empty());
442 value = setting->DeepCopy();
443 }
444 } else {
445 int setting = CONTENT_SETTING_DEFAULT;
446 if (settings_dictionary->GetIntegerWithoutPathExpansion(
447 GetTypeName(ContentSettingsType(i)), &setting)) {
448 DCHECK_NE(CONTENT_SETTING_DEFAULT, setting);
449 setting = FixObsoleteCookiePromptMode(content_type,
450 ContentSetting(setting));
451 value = new base::FundamentalValue(setting);
452 }
453 }
454
455 // |pref_entry_map_| will take the ownership of |value|.
456 if (value != NULL) {
457 pref_entry_map_[content_type].push_back(
458 new ContentSettingsPrefEntry(
459 pattern_pair.first,
460 pattern_pair.second,
461 ResourceIdentifier(),
462 value));
463 }
464 }
465 }
466 }
467
468 void PrefProvider::WriteSettingsToNewPreferences(bool syncable_only) {
469 // The incognito provider cannot write the settings to avoid echo effect:
470 // New preference -> PrefProvider -> Old preference ->
471 // -> Incognito PrefProvider -> New preference -> etc.
472 if (is_incognito_)
473 return;
474
475 if (updating_old_preferences_)
476 return;
477
478 base::AutoReset<bool> auto_reset(&updating_old_preferences_, true);
479 base::AutoLock auto_lock(old_lock_);
480
481 ReadContentSettingsFromOldPref();
482
483 for (int k = 0; k < CONTENT_SETTINGS_NUM_TYPES; ++k) {
484 ContentSettingsType content_type = ContentSettingsType(k);
485
486 if (syncable_only && !IsContentSettingsTypeSyncable(content_type))
487 continue;
488
489 content_settings_prefs_[content_type]->ClearAllContentSettingsRules();
490
491 for (size_t i = 0; i < pref_entry_map_[content_type].size(); ++i) {
492 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
493 // Protected Media Identifier "Allow" exceptions can not be migrated.
494 const base::FundamentalValue allow_value(CONTENT_SETTING_ALLOW);
495 if (content_type == CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER &&
496 pref_entry_map_[content_type][i]->value->Equals(&allow_value)) {
497 continue;
498 }
499 #endif
500
501 content_settings_prefs_[content_type]->SetWebsiteSetting(
502 pref_entry_map_[content_type][i]->primary_pattern,
503 pref_entry_map_[content_type][i]->secondary_pattern,
504 pref_entry_map_[content_type][i]->resource_identifier,
505 pref_entry_map_[content_type][i]->value.release());
506 }
507 }
508
509 ClearPrefEntryMap();
510 }
511
512 void PrefProvider::ClearPrefEntryMap() {
513 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i)
514 pref_entry_map_[i].clear();
515 }
516
517 void PrefProvider::OnOldContentSettingsPatternPairsChanged() {
518 DCHECK(thread_checker_.CalledOnValidThread());
519
520 WriteSettingsToNewPreferences(true);
521 }
522
208 void PrefProvider::SetClockForTesting(scoped_ptr<base::Clock> clock) { 523 void PrefProvider::SetClockForTesting(scoped_ptr<base::Clock> clock) {
209 clock_ = clock.Pass(); 524 clock_ = clock.Pass();
210 content_settings_pref_->SetClockForTesting(clock_.get()); 525 }
526
527 bool PrefProvider::TestAllLocks() const {
528 for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
529 if (!content_settings_prefs_[i]->lock_.Try())
530 return false;
531 content_settings_prefs_[i]->lock_.Release();
532 }
533 return true;
211 } 534 }
212 535
213 } // namespace content_settings 536 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698