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

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

Issue 7344008: Make the hcsm and its providers communicate via an observer interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: "remove commented code" 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/auto_reset.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
15 #include "chrome/browser/content_settings/content_settings_details.h" 15 #include "chrome/browser/content_settings/content_settings_details.h"
16 #include "chrome/browser/content_settings/content_settings_pattern.h" 16 #include "chrome/browser/content_settings/content_settings_pattern.h"
17 #include "chrome/browser/content_settings/content_settings_utils.h" 17 #include "chrome/browser/content_settings/content_settings_utils.h"
18 #include "chrome/browser/prefs/pref_service.h" 18 #include "chrome/browser/prefs/pref_service.h"
19 #include "chrome/browser/prefs/scoped_user_pref_update.h" 19 #include "chrome/browser/prefs/scoped_user_pref_update.h"
20 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/common/chrome_notification_types.h" 21 #include "chrome/common/chrome_notification_types.h"
22 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/content_settings.h" 23 #include "chrome/common/content_settings.h"
24 #include "chrome/common/pref_names.h" 24 #include "chrome/common/pref_names.h"
25 #include "content/browser/browser_thread.h" 25 #include "content/browser/browser_thread.h"
26 #include "content/browser/user_metrics.h" 26 #include "content/browser/user_metrics.h"
27 #include "content/common/notification_details.h"
28 #include "content/common/notification_service.h" 27 #include "content/common/notification_service.h"
29 #include "content/common/notification_source.h" 28 #include "content/common/notification_source.h"
30 #include "googleurl/src/gurl.h" 29 #include "googleurl/src/gurl.h"
31 #include "net/base/net_util.h" 30 #include "net/base/net_util.h"
32 31
33 namespace { 32 namespace {
34 33
35 // The preference keys where resource identifiers are stored for 34 // The preference keys where resource identifiers are stored for
36 // ContentSettingsType values that support resource identifiers. 35 // ContentSettingsType values that support resource identifiers.
37 const char* kResourceTypeNames[] = { 36 const char* kResourceTypeNames[] = {
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 376
378 // Obsolete prefs, for migration: 377 // Obsolete prefs, for migration:
379 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns, 378 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns,
380 PrefService::SYNCABLE_PREF); 379 PrefService::SYNCABLE_PREF);
381 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, 380 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts,
382 PrefService::UNSYNCABLE_PREF); 381 PrefService::UNSYNCABLE_PREF);
383 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings, 382 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings,
384 PrefService::UNSYNCABLE_PREF); 383 PrefService::UNSYNCABLE_PREF);
385 } 384 }
386 385
387 PrefProvider::PrefProvider(HostContentSettingsMap* map, 386 PrefProvider::PrefProvider(Observer* observer,
388 PrefService* prefs, 387 PrefService* prefs,
389 bool incognito) 388 bool incognito)
390 : prefs_(prefs), 389 : prefs_(prefs),
391 host_content_settings_map_(map),
392 is_incognito_(incognito), 390 is_incognito_(incognito),
393 updating_preferences_(false) { 391 updating_preferences_(false) {
394 DCHECK(prefs_); 392 DCHECK(prefs_);
393 AddObserver(observer);
395 if (!is_incognito_) { 394 if (!is_incognito_) {
396 // Migrate obsolete preferences. 395 // Migrate obsolete preferences.
397 MigrateObsoletePerhostPref(); 396 MigrateObsoletePerhostPref();
398 MigrateObsoletePopupsPref(); 397 MigrateObsoletePopupsPref();
399 MigrateObsoleteContentSettingsPatternPref(); 398 MigrateObsoleteContentSettingsPatternPref();
400 } 399 }
401 400
402 // Verify preferences version. 401 // Verify preferences version.
403 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { 402 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) {
404 prefs_->SetInteger(prefs::kContentSettingsVersion, 403 prefs_->SetInteger(prefs::kContentSettingsVersion,
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 512
514 // Update the content settings preference. 513 // Update the content settings preference.
515 if (!is_incognito_) { 514 if (!is_incognito_) {
516 UpdatePref(primary_pattern, 515 UpdatePref(primary_pattern,
517 secondary_pattern, 516 secondary_pattern,
518 content_type, 517 content_type,
519 resource_identifier, 518 resource_identifier,
520 setting); 519 setting);
521 } 520 }
522 521
523 ContentSettingsDetails details( 522 NotifyObservers(
524 primary_pattern, secondary_pattern, content_type, resource_identifier); 523 primary_pattern, secondary_pattern, content_type, resource_identifier);
525 NotifyObservers(details);
526 } 524 }
527 525
528 void PrefProvider::ClearAllContentSettingsRules( 526 void PrefProvider::ClearAllContentSettingsRules(
529 ContentSettingsType content_type) { 527 ContentSettingsType content_type) {
530 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 528 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
531 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 529 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
532 DCHECK(prefs_); 530 DCHECK(prefs_);
533 531
534 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; 532 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_;
535 if (!is_incognito_) 533 if (!is_incognito_)
(...skipping 11 matching lines...) Expand all
547 entry->identifier, 545 entry->identifier,
548 CONTENT_SETTING_DEFAULT); 546 CONTENT_SETTING_DEFAULT);
549 // Delete current |entry| and set |entry| to the next value map entry. 547 // Delete current |entry| and set |entry| to the next value map entry.
550 entry = map_to_modify->erase(entry); 548 entry = map_to_modify->erase(entry);
551 } else { 549 } else {
552 ++entry; 550 ++entry;
553 } 551 }
554 } 552 }
555 } 553 }
556 554
557 ContentSettingsDetails details( 555 NotifyObservers(ContentSettingsPattern(),
558 ContentSettingsPattern(), 556 ContentSettingsPattern(),
559 ContentSettingsPattern(), 557 content_type,
560 content_type, 558 std::string());
561 std::string());
562 NotifyObservers(details);
563 } 559 }
564 560
565 void PrefProvider::Observe( 561 void PrefProvider::Observe(
566 int type, 562 int type,
567 const NotificationSource& source, 563 const NotificationSource& source,
568 const NotificationDetails& details) { 564 const NotificationDetails& details) {
569 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 565 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
570 566
571 if (type == chrome::NOTIFICATION_PREF_CHANGED) { 567 if (type == chrome::NOTIFICATION_PREF_CHANGED) {
572 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); 568 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr());
573 if (updating_preferences_) 569 if (updating_preferences_)
574 return; 570 return;
575 571
576 std::string* name = Details<std::string>(details).ptr(); 572 std::string* name = Details<std::string>(details).ptr();
577 if (*name == prefs::kContentSettingsPatternPairs) { 573 if (*name == prefs::kContentSettingsPatternPairs) {
578 SyncObsoletePref(); 574 SyncObsoletePref();
579 ReadContentSettingsFromPref(true); 575 ReadContentSettingsFromPref(true);
580 } else if (*name == prefs::kContentSettingsPatterns) { 576 } else if (*name == prefs::kContentSettingsPatterns) {
581 AutoReset<bool> auto_reset(&updating_preferences_, true); 577 AutoReset<bool> auto_reset(&updating_preferences_, true);
582 MigrateObsoleteContentSettingsPatternPref(); 578 MigrateObsoleteContentSettingsPatternPref();
583 ReadContentSettingsFromPref(true); 579 ReadContentSettingsFromPref(true);
584 } else { 580 } else {
585 NOTREACHED() << "Unexpected preference observed"; 581 NOTREACHED() << "Unexpected preference observed";
586 return; 582 return;
587 } 583 }
588 584
589 ContentSettingsDetails details(ContentSettingsPattern(), 585 NotifyObservers(ContentSettingsPattern(),
590 ContentSettingsPattern(), 586 ContentSettingsPattern(),
591 CONTENT_SETTINGS_TYPE_DEFAULT, 587 CONTENT_SETTINGS_TYPE_DEFAULT,
592 std::string()); 588 std::string());
593 NotifyObservers(details);
594 } else { 589 } else {
595 NOTREACHED() << "Unexpected notification"; 590 NOTREACHED() << "Unexpected notification";
596 } 591 }
597 } 592 }
598 593
599 PrefProvider::~PrefProvider() { 594 PrefProvider::~PrefProvider() {
600 DCHECK(!prefs_); 595 DCHECK(!prefs_);
601 } 596 }
602 597
603 // //////////////////////////////////////////////////////////////////////////// 598 // ////////////////////////////////////////////////////////////////////////////
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 885
891 for (size_t i = 0; i < move_items.size(); ++i) { 886 for (size_t i = 0; i < move_items.size(); ++i) {
892 Value* pattern_settings_dictionary = NULL; 887 Value* pattern_settings_dictionary = NULL;
893 all_settings_dictionary->RemoveWithoutPathExpansion( 888 all_settings_dictionary->RemoveWithoutPathExpansion(
894 move_items[i].first, &pattern_settings_dictionary); 889 move_items[i].first, &pattern_settings_dictionary);
895 all_settings_dictionary->SetWithoutPathExpansion( 890 all_settings_dictionary->SetWithoutPathExpansion(
896 move_items[i].second, pattern_settings_dictionary); 891 move_items[i].second, pattern_settings_dictionary);
897 } 892 }
898 } 893 }
899 894
900 void PrefProvider::NotifyObservers(
901 const ContentSettingsDetails& details) {
902 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
903 DCHECK(host_content_settings_map_);
904 NotificationService::current()->Notify(
905 chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED,
906 Source<HostContentSettingsMap>(host_content_settings_map_),
907 Details<const ContentSettingsDetails>(&details));
908 }
909
910 void PrefProvider::ShutdownOnUIThread() { 895 void PrefProvider::ShutdownOnUIThread() {
911 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 896 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
912 DCHECK(prefs_); 897 DCHECK(prefs_);
898 RemoveAllObservers();
913 pref_change_registrar_.RemoveAll(); 899 pref_change_registrar_.RemoveAll();
914 prefs_ = NULL; 900 prefs_ = NULL;
915 host_content_settings_map_ = NULL;
916 } 901 }
917 902
918 void PrefProvider::MigrateObsoletePerhostPref() { 903 void PrefProvider::MigrateObsoletePerhostPref() {
919 if (prefs_->HasPrefPath(prefs::kPerHostContentSettings)) { 904 if (prefs_->HasPrefPath(prefs::kPerHostContentSettings)) {
920 const DictionaryValue* all_settings_dictionary = 905 const DictionaryValue* all_settings_dictionary =
921 prefs_->GetDictionary(prefs::kPerHostContentSettings); 906 prefs_->GetDictionary(prefs::kPerHostContentSettings);
922 DCHECK(all_settings_dictionary); 907 DCHECK(all_settings_dictionary);
923 for (DictionaryValue::key_iterator 908 for (DictionaryValue::key_iterator
924 i(all_settings_dictionary->begin_keys()); 909 i(all_settings_dictionary->begin_keys());
925 i != all_settings_dictionary->end_keys(); ++i) { 910 i != all_settings_dictionary->end_keys(); ++i) {
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
1058 DCHECK(found); 1043 DCHECK(found);
1059 std::string new_key = pattern_pair.first.ToString(); 1044 std::string new_key = pattern_pair.first.ToString();
1060 // Existing values are overwritten. 1045 // Existing values are overwritten.
1061 obsolete_settings_dictionary->SetWithoutPathExpansion( 1046 obsolete_settings_dictionary->SetWithoutPathExpansion(
1062 new_key, dictionary->DeepCopy()); 1047 new_key, dictionary->DeepCopy());
1063 } 1048 }
1064 } 1049 }
1065 } 1050 }
1066 1051
1067 } // namespace content_settings 1052 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698