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

Side by Side Diff: chrome/browser/content_settings/content_settings_pref_provider.cc

Issue 7218073: Explicitly ShutdownOnUIThread the HostContentSettingsMap when destroying the Profile. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync Created 9 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/content_settings/content_settings_pref_provider.h" 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h"
6 6
7 #include <list> 7 #include <list>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/auto_reset.h"
12 #include "base/command_line.h" 13 #include "base/command_line.h"
13 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
14 #include "chrome/browser/content_settings/content_settings_details.h" 15 #include "chrome/browser/content_settings/content_settings_details.h"
15 #include "chrome/browser/content_settings/content_settings_pattern.h" 16 #include "chrome/browser/content_settings/content_settings_pattern.h"
16 #include "chrome/browser/content_settings/content_settings_utils.h" 17 #include "chrome/browser/content_settings/content_settings_utils.h"
17 #include "chrome/browser/prefs/pref_service.h" 18 #include "chrome/browser/prefs/pref_service.h"
18 #include "chrome/browser/prefs/scoped_user_pref_update.h" 19 #include "chrome/browser/prefs/scoped_user_pref_update.h"
19 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/content_settings.h" 22 #include "chrome/common/content_settings.h"
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 setting == CONTENT_SETTING_ASK) { 142 setting == CONTENT_SETTING_ASK) {
142 return CONTENT_SETTING_BLOCK; 143 return CONTENT_SETTING_BLOCK;
143 } 144 }
144 return setting; 145 return setting;
145 } 146 }
146 147
147 } // namespace 148 } // namespace
148 149
149 namespace content_settings { 150 namespace content_settings {
150 151
151 PrefDefaultProvider::PrefDefaultProvider(Profile* profile) 152 PrefDefaultProvider::PrefDefaultProvider(HostContentSettingsMap* map,
152 : profile_(profile), 153 PrefService* prefs,
153 is_incognito_(profile_->IsOffTheRecord()), 154 bool incognito)
155 : map_(map),
156 prefs_(prefs),
157 is_incognito_(incognito),
154 updating_preferences_(false) { 158 updating_preferences_(false) {
155 initializing_ = true; 159 MigrateObsoleteNotificationPref();
156 PrefService* prefs = profile->GetPrefs();
157
158 MigrateObsoleteNotificationPref(prefs);
159 160
160 // Read global defaults. 161 // Read global defaults.
161 DCHECK_EQ(arraysize(kTypeNames), 162 DCHECK_EQ(arraysize(kTypeNames),
162 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); 163 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES));
163 ReadDefaultSettings(true); 164 ReadDefaultSettings(true);
164 if (default_content_settings_.settings[CONTENT_SETTINGS_TYPE_COOKIES] == 165 if (default_content_settings_.settings[CONTENT_SETTINGS_TYPE_COOKIES] ==
165 CONTENT_SETTING_BLOCK) { 166 CONTENT_SETTING_BLOCK) {
166 UserMetrics::RecordAction( 167 UserMetrics::RecordAction(
167 UserMetricsAction("CookieBlockingEnabledPerDefault")); 168 UserMetricsAction("CookieBlockingEnabledPerDefault"));
168 } else { 169 } else {
169 UserMetrics::RecordAction( 170 UserMetrics::RecordAction(
170 UserMetricsAction("CookieBlockingDisabledPerDefault")); 171 UserMetricsAction("CookieBlockingDisabledPerDefault"));
171 } 172 }
172 173
173 pref_change_registrar_.Init(prefs); 174 pref_change_registrar_.Init(prefs_);
174 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this); 175 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this);
175 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
176 Source<Profile>(profile_));
177 initializing_ = false;
178 } 176 }
179 177
180 PrefDefaultProvider::~PrefDefaultProvider() { 178 PrefDefaultProvider::~PrefDefaultProvider() {
181 UnregisterObservers(); 179 DCHECK(!prefs_);
182 } 180 }
183 181
184 ContentSetting PrefDefaultProvider::ProvideDefaultSetting( 182 ContentSetting PrefDefaultProvider::ProvideDefaultSetting(
185 ContentSettingsType content_type) const { 183 ContentSettingsType content_type) const {
186 base::AutoLock lock(lock_); 184 base::AutoLock lock(lock_);
187 return default_content_settings_.settings[content_type]; 185 return default_content_settings_.settings[content_type];
188 } 186 }
189 187
190 void PrefDefaultProvider::UpdateDefaultSetting( 188 void PrefDefaultProvider::UpdateDefaultSetting(
191 ContentSettingsType content_type, 189 ContentSettingsType content_type,
192 ContentSetting setting) { 190 ContentSetting setting) {
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
194 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 192 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
195 193
196 // The default settings may not be directly modified for OTR sessions. 194 // The default settings may not be directly modified for OTR sessions.
197 // Instead, they are synced to the main profile's setting. 195 // Instead, they are synced to the main profile's setting.
198 if (is_incognito_) 196 if (is_incognito_)
199 return; 197 return;
200 198
201 PrefService* prefs = profile_->GetPrefs();
202
203 std::string dictionary_path(kTypeNames[content_type]); 199 std::string dictionary_path(kTypeNames[content_type]);
204 updating_preferences_ = true; 200 updating_preferences_ = true;
205 { 201 {
206 base::AutoLock lock(lock_); 202 base::AutoLock lock(lock_);
207 DictionaryPrefUpdate update(prefs, prefs::kDefaultContentSettings); 203 DictionaryPrefUpdate update(prefs_, prefs::kDefaultContentSettings);
208 DictionaryValue* default_settings_dictionary = update.Get(); 204 DictionaryValue* default_settings_dictionary = update.Get();
209 if ((setting == CONTENT_SETTING_DEFAULT) || 205 if ((setting == CONTENT_SETTING_DEFAULT) ||
210 (setting == kDefaultSettings[content_type])) { 206 (setting == kDefaultSettings[content_type])) {
211 default_content_settings_.settings[content_type] = 207 default_content_settings_.settings[content_type] =
212 kDefaultSettings[content_type]; 208 kDefaultSettings[content_type];
213 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, 209 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path,
214 NULL); 210 NULL);
215 } else { 211 } else {
216 default_content_settings_.settings[content_type] = setting; 212 default_content_settings_.settings[content_type] = setting;
217 default_settings_dictionary->SetWithoutPathExpansion( 213 default_settings_dictionary->SetWithoutPathExpansion(
(...skipping 15 matching lines...) Expand all
233 return false; 229 return false;
234 } 230 }
235 231
236 void PrefDefaultProvider::ResetToDefaults() { 232 void PrefDefaultProvider::ResetToDefaults() {
237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
238 base::AutoLock lock(lock_); 234 base::AutoLock lock(lock_);
239 default_content_settings_ = ContentSettings(); 235 default_content_settings_ = ContentSettings();
240 ForceDefaultsToBeExplicit(); 236 ForceDefaultsToBeExplicit();
241 237
242 if (!is_incognito_) { 238 if (!is_incognito_) {
243 PrefService* prefs = profile_->GetPrefs();
244 updating_preferences_ = true; 239 updating_preferences_ = true;
245 prefs->ClearPref(prefs::kDefaultContentSettings); 240 prefs_->ClearPref(prefs::kDefaultContentSettings);
246 updating_preferences_ = false; 241 updating_preferences_ = false;
247 } 242 }
248 } 243 }
249 244
250 void PrefDefaultProvider::Observe(NotificationType type, 245 void PrefDefaultProvider::Observe(NotificationType type,
251 const NotificationSource& source, 246 const NotificationSource& source,
252 const NotificationDetails& details) { 247 const NotificationDetails& details) {
253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
254 249
255 if (type == NotificationType::PREF_CHANGED) { 250 if (type == NotificationType::PREF_CHANGED) {
256 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); 251 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr());
257 if (updating_preferences_) 252 if (updating_preferences_)
258 return; 253 return;
259 254
260 std::string* name = Details<std::string>(details).ptr(); 255 std::string* name = Details<std::string>(details).ptr();
261 if (*name == prefs::kDefaultContentSettings) { 256 if (*name == prefs::kDefaultContentSettings) {
262 ReadDefaultSettings(true); 257 ReadDefaultSettings(true);
263 } else { 258 } else {
264 NOTREACHED() << "Unexpected preference observed"; 259 NOTREACHED() << "Unexpected preference observed";
265 return; 260 return;
266 } 261 }
267 262
268 if (!is_incognito_) { 263 ContentSettingsDetails details(ContentSettingsPattern(),
269 ContentSettingsDetails details(ContentSettingsPattern(), 264 ContentSettingsPattern(),
270 ContentSettingsPattern(), 265 CONTENT_SETTINGS_TYPE_DEFAULT,
271 CONTENT_SETTINGS_TYPE_DEFAULT, 266 std::string());
272 std::string()); 267 NotifyObservers(details);
273 NotifyObservers(details);
274 }
275 } else if (type == NotificationType::PROFILE_DESTROYED) {
276 DCHECK_EQ(profile_, Source<Profile>(source).ptr());
277 UnregisterObservers();
278 } else { 268 } else {
279 NOTREACHED() << "Unexpected notification"; 269 NOTREACHED() << "Unexpected notification";
280 } 270 }
281 } 271 }
282 272
283 void PrefDefaultProvider::UnregisterObservers() { 273 void PrefDefaultProvider::ShutdownOnUIThread() {
274 if (!prefs_)
275 return;
284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
285 if (!profile_)
286 return;
287 pref_change_registrar_.RemoveAll(); 277 pref_change_registrar_.RemoveAll();
288 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, 278 prefs_ = NULL;
289 Source<Profile>(profile_)); 279 map_ = NULL;
290 profile_ = NULL;
291 } 280 }
292 281
293 void PrefDefaultProvider::ReadDefaultSettings(bool overwrite) { 282 void PrefDefaultProvider::ReadDefaultSettings(bool overwrite) {
294 PrefService* prefs = profile_->GetPrefs();
295 const DictionaryValue* default_settings_dictionary = 283 const DictionaryValue* default_settings_dictionary =
296 prefs->GetDictionary(prefs::kDefaultContentSettings); 284 prefs_->GetDictionary(prefs::kDefaultContentSettings);
297 285
298 base::AutoLock lock(lock_); 286 base::AutoLock lock(lock_);
299 287
300 if (overwrite) 288 if (overwrite)
301 default_content_settings_ = ContentSettings(); 289 default_content_settings_ = ContentSettings();
302 290
303 // Careful: The returned value could be NULL if the pref has never been set. 291 // Careful: The returned value could be NULL if the pref has never been set.
304 if (default_settings_dictionary != NULL) { 292 if (default_settings_dictionary != NULL) {
305 GetSettingsFromDictionary(default_settings_dictionary, 293 GetSettingsFromDictionary(default_settings_dictionary,
306 &default_content_settings_); 294 &default_content_settings_);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; 329 settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK;
342 330
343 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] = 331 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] =
344 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, 332 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS,
345 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]); 333 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]);
346 } 334 }
347 335
348 void PrefDefaultProvider::NotifyObservers( 336 void PrefDefaultProvider::NotifyObservers(
349 const ContentSettingsDetails& details) { 337 const ContentSettingsDetails& details) {
350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
351 if (initializing_ || profile_ == NULL) 339 if (map_ == NULL)
352 return; 340 return;
353 NotificationService::current()->Notify( 341 NotificationService::current()->Notify(
354 NotificationType::CONTENT_SETTINGS_CHANGED, 342 NotificationType::CONTENT_SETTINGS_CHANGED,
355 Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()), 343 Source<HostContentSettingsMap>(map_),
356 Details<const ContentSettingsDetails>(&details)); 344 Details<const ContentSettingsDetails>(&details));
357 } 345 }
358 346
359 void PrefDefaultProvider::MigrateObsoleteNotificationPref(PrefService* prefs) { 347 void PrefDefaultProvider::MigrateObsoleteNotificationPref() {
360 if (prefs->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) { 348 if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) {
361 ContentSetting setting = IntToContentSetting( 349 ContentSetting setting = IntToContentSetting(
362 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); 350 prefs_->GetInteger(prefs::kDesktopNotificationDefaultContentSetting));
363 UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting); 351 UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting);
364 prefs->ClearPref(prefs::kDesktopNotificationDefaultContentSetting); 352 prefs_->ClearPref(prefs::kDesktopNotificationDefaultContentSetting);
365 } 353 }
366 } 354 }
367 355
368 // static 356 // static
369 void PrefDefaultProvider::RegisterUserPrefs(PrefService* prefs) { 357 void PrefDefaultProvider::RegisterUserPrefs(PrefService* prefs) {
370 // The registration of the preference prefs::kDefaultContentSettings should 358 // The registration of the preference prefs::kDefaultContentSettings should
371 // also include the default values for default content settings. This allows 359 // also include the default values for default content settings. This allows
372 // functional tests to get default content settings by reading the preference 360 // functional tests to get default content settings by reading the preference
373 // prefs::kDefaultContentSettings via pyauto. 361 // prefs::kDefaultContentSettings via pyauto.
374 // TODO(markusheintz): Write pyauto hooks for the content settings map as 362 // TODO(markusheintz): Write pyauto hooks for the content settings map as
(...skipping 26 matching lines...) Expand all
401 389
402 // Obsolete prefs, for migration: 390 // Obsolete prefs, for migration:
403 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns, 391 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns,
404 PrefService::SYNCABLE_PREF); 392 PrefService::SYNCABLE_PREF);
405 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, 393 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts,
406 PrefService::UNSYNCABLE_PREF); 394 PrefService::UNSYNCABLE_PREF);
407 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings, 395 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings,
408 PrefService::UNSYNCABLE_PREF); 396 PrefService::UNSYNCABLE_PREF);
409 } 397 }
410 398
411 PrefProvider::PrefProvider(Profile* profile) 399 PrefProvider::PrefProvider(HostContentSettingsMap* map,
412 : profile_(profile), 400 PrefService* prefs,
413 is_incognito_(profile_->IsOffTheRecord()), 401 bool incognito)
402 : prefs_(prefs),
403 map_(map),
404 is_incognito_(incognito),
414 updating_preferences_(false) { 405 updating_preferences_(false) {
415 Init(); 406 if (!is_incognito_) {
416 } 407 // Migrate obsolete preferences.
417 408 MigrateObsoletePerhostPref();
418 void PrefProvider::Init() { 409 MigrateObsoletePopupsPref();
419 initializing_ = true; 410 MigrateObsoleteContentSettingsPatternPref();
420 PrefService* prefs = profile_->GetPrefs(); 411 }
421
422 // Migrate obsolete preferences.
423 MigrateObsoletePerhostPref(prefs);
424 MigrateObsoletePopupsPref(prefs);
425 MigrateObsoleteContentSettingsPatternPref(prefs);
426 412
427 // Verify preferences version. 413 // Verify preferences version.
428 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { 414 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) {
429 prefs->SetInteger(prefs::kContentSettingsVersion, 415 prefs_->SetInteger(prefs::kContentSettingsVersion,
430 ContentSettingsPattern::kContentSettingsPatternVersion); 416 ContentSettingsPattern::kContentSettingsPatternVersion);
431 } 417 }
432 if (prefs->GetInteger(prefs::kContentSettingsVersion) > 418 if (prefs_->GetInteger(prefs::kContentSettingsVersion) >
433 ContentSettingsPattern::kContentSettingsPatternVersion) { 419 ContentSettingsPattern::kContentSettingsPatternVersion) {
434 LOG(ERROR) << "Unknown content settings version in preferences."; 420 LOG(ERROR) << "Unknown content settings version in preferences.";
435 return; 421 return;
436 } 422 }
437 423
438 // Read content settings exceptions. 424 // Read content settings exceptions.
439 ReadContentSettingsFromPref(false); 425 ReadContentSettingsFromPref(false);
440 426
441 if (!is_incognito_) { 427 if (!is_incognito_) {
442 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions", 428 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions",
443 value_map_.size()); 429 value_map_.size());
444 } 430 }
445 431
446 pref_change_registrar_.Init(prefs); 432 pref_change_registrar_.Init(prefs_);
447 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); 433 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this);
448 pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this); 434 pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this);
449
450 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
451 Source<Profile>(profile_));
452 initializing_ = false;
453 } 435 }
454 436
455 ContentSetting PrefProvider::GetContentSetting( 437 ContentSetting PrefProvider::GetContentSetting(
456 const GURL& primary_url, 438 const GURL& primary_url,
457 const GURL& secondary_url, 439 const GURL& secondary_url,
458 ContentSettingsType content_type, 440 ContentSettingsType content_type,
459 const ResourceIdentifier& resource_identifier) const { 441 const ResourceIdentifier& resource_identifier) const {
460 // For a |PrefProvider| used in a |HostContentSettingsMap| of a non incognito 442 // For a |PrefProvider| used in a |HostContentSettingsMap| of a non incognito
461 // profile, this will always return NULL. 443 // profile, this will always return NULL.
462 // TODO(markusheintz): I don't like this. I'd like to have an 444 // TODO(markusheintz): I don't like this. I'd like to have an
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 map_to_modify->SetValue( 515 map_to_modify->SetValue(
534 primary_pattern, 516 primary_pattern,
535 secondary_pattern, 517 secondary_pattern,
536 content_type, 518 content_type,
537 resource_identifier, 519 resource_identifier,
538 Value::CreateIntegerValue(setting)); 520 Value::CreateIntegerValue(setting));
539 } 521 }
540 } 522 }
541 523
542 // Update the content settings preference. 524 // Update the content settings preference.
543 if (!is_incognito_ && profile_) { 525 if (!is_incognito_) {
544 UpdatePref(primary_pattern, 526 UpdatePref(primary_pattern,
545 secondary_pattern, 527 secondary_pattern,
546 content_type, 528 content_type,
547 resource_identifier, 529 resource_identifier,
548 setting); 530 setting);
549 } 531 }
550 532
551 ContentSettingsDetails details( 533 ContentSettingsDetails details(
552 primary_pattern, secondary_pattern, content_type, std::string()); 534 primary_pattern, secondary_pattern, content_type, std::string());
553 NotifyObservers(details); 535 NotifyObservers(details);
554 } 536 }
555 537
556 void PrefProvider::ResetToDefaults() { 538 void PrefProvider::ResetToDefaults() {
557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 539 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
540 DCHECK(prefs_);
558 541
559 { 542 {
560 base::AutoLock auto_lock(lock_); 543 base::AutoLock auto_lock(lock_);
561 value_map_.clear(); 544 value_map_.clear();
562 incognito_value_map_.clear(); 545 incognito_value_map_.clear();
563 } 546 }
564 547
565 if (!is_incognito_) { 548 if (!is_incognito_) {
566 PrefService* prefs = profile_->GetPrefs();
567 updating_preferences_ = true; 549 updating_preferences_ = true;
568 prefs->ClearPref(prefs::kContentSettingsPatternPairs); 550 prefs_->ClearPref(prefs::kContentSettingsPatternPairs);
569 prefs->ClearPref(prefs::kContentSettingsPatterns); 551 prefs_->ClearPref(prefs::kContentSettingsPatterns);
570 updating_preferences_ = false; 552 updating_preferences_ = false;
571 } 553 }
572 } 554 }
573 555
574 void PrefProvider::ClearAllContentSettingsRules( 556 void PrefProvider::ClearAllContentSettingsRules(
575 ContentSettingsType content_type) { 557 ContentSettingsType content_type) {
576 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 558 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
559 DCHECK(prefs_);
577 560
578 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; 561 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_;
579 if (!is_incognito_) 562 if (!is_incognito_)
580 map_to_modify = &value_map_; 563 map_to_modify = &value_map_;
581 564
582 { 565 {
583 base::AutoLock auto_lock(lock_); 566 base::AutoLock auto_lock(lock_);
584 OriginIdentifierValueMap::iterator entry(map_to_modify->begin()); 567 OriginIdentifierValueMap::iterator entry(map_to_modify->begin());
585 while (entry != map_to_modify->end()) { 568 while (entry != map_to_modify->end()) {
586 if (entry->content_type == content_type) { 569 if (entry->content_type == content_type) {
(...skipping 19 matching lines...) Expand all
606 NotifyObservers(details); 589 NotifyObservers(details);
607 } 590 }
608 591
609 void PrefProvider::Observe( 592 void PrefProvider::Observe(
610 NotificationType type, 593 NotificationType type,
611 const NotificationSource& source, 594 const NotificationSource& source,
612 const NotificationDetails& details) { 595 const NotificationDetails& details) {
613 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 596 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
614 597
615 if (type == NotificationType::PREF_CHANGED) { 598 if (type == NotificationType::PREF_CHANGED) {
616 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); 599 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr());
617 if (updating_preferences_) 600 if (updating_preferences_)
618 return; 601 return;
619 602
620 std::string* name = Details<std::string>(details).ptr(); 603 std::string* name = Details<std::string>(details).ptr();
621 if (*name == prefs::kContentSettingsPatternPairs) { 604 if (*name == prefs::kContentSettingsPatternPairs) {
622 SyncObsoletePref(profile_->GetPrefs()); 605 SyncObsoletePref();
623 ReadContentSettingsFromPref(true); 606 ReadContentSettingsFromPref(true);
624 } else if (*name == prefs::kContentSettingsPatterns) { 607 } else if (*name == prefs::kContentSettingsPatterns) {
625 updating_preferences_ = true; 608 AutoReset<bool> tmp(&updating_preferences_, true);
626 MigrateObsoleteContentSettingsPatternPref(profile_->GetPrefs()); 609 MigrateObsoleteContentSettingsPatternPref();
627 updating_preferences_ = false;
628 ReadContentSettingsFromPref(true); 610 ReadContentSettingsFromPref(true);
629 } else { 611 } else {
630 NOTREACHED() << "Unexpected preference observed"; 612 NOTREACHED() << "Unexpected preference observed";
631 return; 613 return;
632 } 614 }
633 615
634 if (!is_incognito_) { 616 ContentSettingsDetails details(ContentSettingsPattern(),
635 ContentSettingsDetails details(ContentSettingsPattern(), 617 ContentSettingsPattern(),
636 ContentSettingsPattern(), 618 CONTENT_SETTINGS_TYPE_DEFAULT,
637 CONTENT_SETTINGS_TYPE_DEFAULT, 619 std::string());
638 std::string()); 620 NotifyObservers(details);
639 NotifyObservers(details);
640 }
641 } else if (type == NotificationType::PROFILE_DESTROYED) {
642 DCHECK_EQ(profile_, Source<Profile>(source).ptr());
643 UnregisterObservers();
644 } else { 621 } else {
645 NOTREACHED() << "Unexpected notification"; 622 NOTREACHED() << "Unexpected notification";
646 } 623 }
647 } 624 }
648 625
649 PrefProvider::~PrefProvider() { 626 PrefProvider::~PrefProvider() {
650 UnregisterObservers(); 627 DCHECK(!prefs_);
651 } 628 }
652 629
653 // //////////////////////////////////////////////////////////////////////////// 630 // ////////////////////////////////////////////////////////////////////////////
654 // Private 631 // Private
655 632
656 void PrefProvider::UpdatePref( 633 void PrefProvider::UpdatePref(
657 const ContentSettingsPattern& primary_pattern, 634 const ContentSettingsPattern& primary_pattern,
658 const ContentSettingsPattern& secondary_pattern, 635 const ContentSettingsPattern& secondary_pattern,
659 ContentSettingsType content_type, 636 ContentSettingsType content_type,
660 const ResourceIdentifier& resource_identifier, 637 const ResourceIdentifier& resource_identifier,
661 ContentSetting setting) { 638 ContentSetting setting) {
662 updating_preferences_ = true; 639 DCHECK(prefs_);
640 AutoReset<bool> tmp(&updating_preferences_, true);
663 UpdatePatternPairsPref(primary_pattern, 641 UpdatePatternPairsPref(primary_pattern,
664 secondary_pattern, 642 secondary_pattern,
665 content_type, 643 content_type,
666 resource_identifier, 644 resource_identifier,
667 setting); 645 setting);
668 UpdatePatternsPref(primary_pattern, 646 UpdatePatternsPref(primary_pattern,
669 secondary_pattern, 647 secondary_pattern,
670 content_type, 648 content_type,
671 resource_identifier, 649 resource_identifier,
672 setting); 650 setting);
673 updating_preferences_ = false;
674 } 651 }
675 652
676 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) { 653 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) {
654 DCHECK(prefs_);
677 base::AutoLock auto_lock(lock_); 655 base::AutoLock auto_lock(lock_);
678 656
679 PrefService* prefs = profile_->GetPrefs();
680 const DictionaryValue* all_settings_dictionary = 657 const DictionaryValue* all_settings_dictionary =
681 prefs->GetDictionary(prefs::kContentSettingsPatternPairs); 658 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
682 659
683 if (overwrite) 660 if (overwrite)
684 value_map_.clear(); 661 value_map_.clear();
685 662
686 updating_preferences_ = true; 663 AutoReset<bool> tmp(&updating_preferences_, true);
687 // Careful: The returned value could be NULL if the pref has never been set. 664 // Careful: The returned value could be NULL if the pref has never been set.
688 if (all_settings_dictionary != NULL) { 665 if (all_settings_dictionary != NULL) {
689 DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatternPairs); 666 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
690 DictionaryValue* mutable_settings; 667 DictionaryValue* mutable_settings;
691 scoped_ptr<DictionaryValue> mutable_settings_scope; 668 scoped_ptr<DictionaryValue> mutable_settings_scope;
692 669
693 if (!is_incognito_) { 670 if (!is_incognito_) {
694 mutable_settings = update.Get(); 671 mutable_settings = update.Get();
695 } else { 672 } else {
696 // Create copy as we do not want to persist anything in OTR prefs. 673 // Create copy as we do not want to persist anything in OTR prefs.
697 mutable_settings = all_settings_dictionary->DeepCopy(); 674 mutable_settings = all_settings_dictionary->DeepCopy();
698 mutable_settings_scope.reset(mutable_settings); 675 mutable_settings_scope.reset(mutable_settings);
699 } 676 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
766 } 743 }
767 updating_preferences_ = false; 744 updating_preferences_ = false;
768 } 745 }
769 746
770 void PrefProvider::UpdatePatternsPref( 747 void PrefProvider::UpdatePatternsPref(
771 const ContentSettingsPattern& primary_pattern, 748 const ContentSettingsPattern& primary_pattern,
772 const ContentSettingsPattern& secondary_pattern, 749 const ContentSettingsPattern& secondary_pattern,
773 ContentSettingsType content_type, 750 ContentSettingsType content_type,
774 const ResourceIdentifier& resource_identifier, 751 const ResourceIdentifier& resource_identifier,
775 ContentSetting setting) { 752 ContentSetting setting) {
776 DictionaryPrefUpdate update(profile_->GetPrefs(), 753 DCHECK(prefs_);
754 DictionaryPrefUpdate update(prefs_,
777 prefs::kContentSettingsPatterns); 755 prefs::kContentSettingsPatterns);
778 DictionaryValue* all_settings_dictionary = update.Get(); 756 DictionaryValue* all_settings_dictionary = update.Get();
779 757
780 // Get settings dictionary for |primary_pattern|. 758 // Get settings dictionary for |primary_pattern|.
781 std::string pattern_str(primary_pattern.ToString()); 759 std::string pattern_str(primary_pattern.ToString());
782 DictionaryValue* settings_dictionary = NULL; 760 DictionaryValue* settings_dictionary = NULL;
783 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( 761 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
784 pattern_str, &settings_dictionary); 762 pattern_str, &settings_dictionary);
785 763
786 if (!found && (setting != CONTENT_SETTING_DEFAULT)) { 764 if (!found && (setting != CONTENT_SETTING_DEFAULT)) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
832 } 810 }
833 } 811 }
834 } 812 }
835 813
836 void PrefProvider::UpdatePatternPairsPref( 814 void PrefProvider::UpdatePatternPairsPref(
837 const ContentSettingsPattern& primary_pattern, 815 const ContentSettingsPattern& primary_pattern,
838 const ContentSettingsPattern& secondary_pattern, 816 const ContentSettingsPattern& secondary_pattern,
839 ContentSettingsType content_type, 817 ContentSettingsType content_type,
840 const ResourceIdentifier& resource_identifier, 818 const ResourceIdentifier& resource_identifier,
841 ContentSetting setting) { 819 ContentSetting setting) {
842 DictionaryPrefUpdate update(profile_->GetPrefs(), 820 DCHECK(prefs_);
821 DictionaryPrefUpdate update(prefs_,
843 prefs::kContentSettingsPatternPairs); 822 prefs::kContentSettingsPatternPairs);
844 DictionaryValue* all_settings_dictionary = update.Get(); 823 DictionaryValue* all_settings_dictionary = update.Get();
845 824
846 // Get settings dictionary for the given patterns. 825 // Get settings dictionary for the given patterns.
847 std::string pattern_str(CreatePatternString(primary_pattern, 826 std::string pattern_str(CreatePatternString(primary_pattern,
848 secondary_pattern)); 827 secondary_pattern));
849 DictionaryValue* settings_dictionary = NULL; 828 DictionaryValue* settings_dictionary = NULL;
850 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( 829 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
851 pattern_str, &settings_dictionary); 830 pattern_str, &settings_dictionary);
852 831
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
945 all_settings_dictionary->RemoveWithoutPathExpansion( 924 all_settings_dictionary->RemoveWithoutPathExpansion(
946 move_items[i].first, &pattern_settings_dictionary); 925 move_items[i].first, &pattern_settings_dictionary);
947 all_settings_dictionary->SetWithoutPathExpansion( 926 all_settings_dictionary->SetWithoutPathExpansion(
948 move_items[i].second, pattern_settings_dictionary); 927 move_items[i].second, pattern_settings_dictionary);
949 } 928 }
950 } 929 }
951 930
952 void PrefProvider::NotifyObservers( 931 void PrefProvider::NotifyObservers(
953 const ContentSettingsDetails& details) { 932 const ContentSettingsDetails& details) {
954 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 933 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
955 if (initializing_ || profile_ == NULL) 934 if (map_ == NULL)
956 return; 935 return;
957 NotificationService::current()->Notify( 936 NotificationService::current()->Notify(
958 NotificationType::CONTENT_SETTINGS_CHANGED, 937 NotificationType::CONTENT_SETTINGS_CHANGED,
959 Source<HostContentSettingsMap>( 938 Source<HostContentSettingsMap>(map_),
960 profile_->GetHostContentSettingsMap()),
961 Details<const ContentSettingsDetails>(&details)); 939 Details<const ContentSettingsDetails>(&details));
962 } 940 }
963 941
964 void PrefProvider::UnregisterObservers() { 942 void PrefProvider::ShutdownOnUIThread() {
965 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 943 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
966 if (!profile_) 944 if (!prefs_)
967 return; 945 return;
968 pref_change_registrar_.RemoveAll(); 946 pref_change_registrar_.RemoveAll();
969 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, 947 prefs_ = NULL;
970 Source<Profile>(profile_)); 948 map_ = NULL;
971 profile_ = NULL;
972 } 949 }
973 950
974 void PrefProvider::MigrateObsoletePerhostPref(PrefService* prefs) { 951 void PrefProvider::MigrateObsoletePerhostPref() {
975 if (prefs->HasPrefPath(prefs::kPerHostContentSettings)) { 952 if (prefs_->HasPrefPath(prefs::kPerHostContentSettings)) {
976 const DictionaryValue* all_settings_dictionary = 953 const DictionaryValue* all_settings_dictionary =
977 prefs->GetDictionary(prefs::kPerHostContentSettings); 954 prefs_->GetDictionary(prefs::kPerHostContentSettings);
978 DCHECK(all_settings_dictionary); 955 DCHECK(all_settings_dictionary);
979 for (DictionaryValue::key_iterator 956 for (DictionaryValue::key_iterator
980 i(all_settings_dictionary->begin_keys()); 957 i(all_settings_dictionary->begin_keys());
981 i != all_settings_dictionary->end_keys(); ++i) { 958 i != all_settings_dictionary->end_keys(); ++i) {
982 const std::string& host(*i); 959 const std::string& host(*i);
983 ContentSettingsPattern pattern = 960 ContentSettingsPattern pattern =
984 ContentSettingsPattern::FromString( 961 ContentSettingsPattern::FromString(
985 std::string(ContentSettingsPattern::kDomainWildcard) + host); 962 std::string(ContentSettingsPattern::kDomainWildcard) + host);
986 DictionaryValue* host_settings_dictionary = NULL; 963 DictionaryValue* host_settings_dictionary = NULL;
987 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( 964 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1019 SetContentSetting( 996 SetContentSetting(
1020 pattern, 997 pattern,
1021 pattern, 998 pattern,
1022 content_type, 999 content_type,
1023 "", 1000 "",
1024 setting); 1001 setting);
1025 } 1002 }
1026 } 1003 }
1027 } 1004 }
1028 } 1005 }
1029 prefs->ClearPref(prefs::kPerHostContentSettings); 1006 prefs_->ClearPref(prefs::kPerHostContentSettings);
1030 } 1007 }
1031 } 1008 }
1032 1009
1033 void PrefProvider::MigrateObsoletePopupsPref(PrefService* prefs) { 1010 void PrefProvider::MigrateObsoletePopupsPref() {
1034 if (prefs->HasPrefPath(prefs::kPopupWhitelistedHosts)) { 1011 if (prefs_->HasPrefPath(prefs::kPopupWhitelistedHosts)) {
1035 const ListValue* whitelist_pref = 1012 const ListValue* whitelist_pref =
1036 prefs->GetList(prefs::kPopupWhitelistedHosts); 1013 prefs_->GetList(prefs::kPopupWhitelistedHosts);
1037 for (ListValue::const_iterator i(whitelist_pref->begin()); 1014 for (ListValue::const_iterator i(whitelist_pref->begin());
1038 i != whitelist_pref->end(); ++i) { 1015 i != whitelist_pref->end(); ++i) {
1039 std::string host; 1016 std::string host;
1040 (*i)->GetAsString(&host); 1017 (*i)->GetAsString(&host);
1041 SetContentSetting(ContentSettingsPattern::FromString(host), 1018 SetContentSetting(ContentSettingsPattern::FromString(host),
1042 ContentSettingsPattern::FromString(host), 1019 ContentSettingsPattern::FromString(host),
1043 CONTENT_SETTINGS_TYPE_POPUPS, 1020 CONTENT_SETTINGS_TYPE_POPUPS,
1044 "", 1021 "",
1045 CONTENT_SETTING_ALLOW); 1022 CONTENT_SETTING_ALLOW);
1046 } 1023 }
1047 prefs->ClearPref(prefs::kPopupWhitelistedHosts); 1024 prefs_->ClearPref(prefs::kPopupWhitelistedHosts);
1048 } 1025 }
1049 } 1026 }
1050 1027
1051 void PrefProvider::MigrateObsoleteContentSettingsPatternPref( 1028 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() {
1052 PrefService* prefs) { 1029 DCHECK(prefs_);
1053 if (prefs->HasPrefPath(prefs::kContentSettingsPatterns) && 1030 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns)) {
1054 !is_incognito_) {
1055 const DictionaryValue* all_settings_dictionary = 1031 const DictionaryValue* all_settings_dictionary =
1056 prefs->GetDictionary(prefs::kContentSettingsPatterns); 1032 prefs_->GetDictionary(prefs::kContentSettingsPatterns);
1057 1033
1058 DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatternPairs); 1034 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
1059 DictionaryValue* exceptions_dictionary; 1035 DictionaryValue* exceptions_dictionary;
1060 exceptions_dictionary = update.Get(); 1036 exceptions_dictionary = update.Get();
1061 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); 1037 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
1062 i != all_settings_dictionary->end_keys(); 1038 i != all_settings_dictionary->end_keys();
1063 ++i) { 1039 ++i) {
1064 const std::string& key(*i); 1040 const std::string& key(*i);
1065 1041
1066 // Validate pattern string and skip it if it is invalid. 1042 // Validate pattern string and skip it if it is invalid.
1067 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = 1043 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
1068 ParsePatternString(key); 1044 ParsePatternString(key);
(...skipping 12 matching lines...) Expand all
1081 std::string new_pattern_str = CreatePatternString( 1057 std::string new_pattern_str = CreatePatternString(
1082 primary_pattern, ContentSettingsPattern::Wildcard()); 1058 primary_pattern, ContentSettingsPattern::Wildcard());
1083 1059
1084 // Existing values are overwritten. 1060 // Existing values are overwritten.
1085 exceptions_dictionary->SetWithoutPathExpansion( 1061 exceptions_dictionary->SetWithoutPathExpansion(
1086 new_pattern_str, dictionary->DeepCopy()); 1062 new_pattern_str, dictionary->DeepCopy());
1087 } 1063 }
1088 } 1064 }
1089 } 1065 }
1090 1066
1091 void PrefProvider::SyncObsoletePref(PrefService* prefs) { 1067 void PrefProvider::SyncObsoletePref() {
1092 updating_preferences_ = true; 1068 DCHECK(prefs_);
1093 if (prefs->HasPrefPath(prefs::kContentSettingsPatternPairs) && 1069 AutoReset<bool> tmp(&updating_preferences_, true);
markusheintz_ 2011/07/06 15:56:24 This is up to you: Could we use a more descriptive
1070 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) &&
1094 !is_incognito_) { 1071 !is_incognito_) {
1095 const DictionaryValue* pattern_pairs_dictionary = 1072 const DictionaryValue* pattern_pairs_dictionary =
1096 prefs->GetDictionary(prefs::kContentSettingsPatternPairs); 1073 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
1097 1074
1098 DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatterns); 1075 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns);
1099 DictionaryValue* obsolete_settings_dictionary = update.Get(); 1076 DictionaryValue* obsolete_settings_dictionary = update.Get();
1100 1077
1101 for (DictionaryValue::key_iterator i = 1078 for (DictionaryValue::key_iterator i =
1102 pattern_pairs_dictionary->begin_keys(); 1079 pattern_pairs_dictionary->begin_keys();
1103 i != pattern_pairs_dictionary->end_keys(); 1080 i != pattern_pairs_dictionary->end_keys();
1104 ++i) { 1081 ++i) {
1105 const std::string& key(*i); 1082 const std::string& key(*i);
1106 // Validate pattern string and skip it if it is invalid. 1083 // Validate pattern string and skip it if it is invalid.
1107 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = 1084 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
1108 ParsePatternString(key); 1085 ParsePatternString(key);
1109 if (!pattern_pair.first.IsValid() || !pattern_pair.second.IsValid()) 1086 if (!pattern_pair.first.IsValid() || !pattern_pair.second.IsValid())
1110 continue; 1087 continue;
1111 1088
1112 // Copy dictionary 1089 // Copy dictionary
1113 DictionaryValue* dictionary = NULL; 1090 DictionaryValue* dictionary = NULL;
1114 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion( 1091 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion(
1115 key, &dictionary); 1092 key, &dictionary);
1116 DCHECK(found); 1093 DCHECK(found);
1117 std::string new_key = pattern_pair.first.ToString(); 1094 std::string new_key = pattern_pair.first.ToString();
1118 // Existing values are overwritten. 1095 // Existing values are overwritten.
1119 obsolete_settings_dictionary->SetWithoutPathExpansion( 1096 obsolete_settings_dictionary->SetWithoutPathExpansion(
1120 new_key, dictionary->DeepCopy()); 1097 new_key, dictionary->DeepCopy());
1121 } 1098 }
1122 } 1099 }
1123 updating_preferences_ = false;
1124 } 1100 }
1125 1101
1126 } // namespace content_settings 1102 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698