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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/prefs/pref_set_observer.h"
6
7 #include "chrome/common/pref_names.h"
8 #include "content/common/notification_type.h"
9
PrefSetObserver(PrefService * pref_service,NotificationObserver * observer)10 PrefSetObserver::PrefSetObserver(PrefService* pref_service,
11 NotificationObserver* observer)
12 : pref_service_(pref_service),
13 observer_(observer) {
14 registrar_.Init(pref_service);
15 }
16
~PrefSetObserver()17 PrefSetObserver::~PrefSetObserver() {}
18
AddPref(const std::string & pref)19 void PrefSetObserver::AddPref(const std::string& pref) {
20 if (!prefs_.count(pref) && pref_service_->FindPreference(pref.c_str())) {
21 prefs_.insert(pref);
22 registrar_.Add(pref.c_str(), this);
23 }
24 }
25
RemovePref(const std::string & pref)26 void PrefSetObserver::RemovePref(const std::string& pref) {
27 if (prefs_.erase(pref))
28 registrar_.Remove(pref.c_str(), this);
29 }
30
IsObserved(const std::string & pref)31 bool PrefSetObserver::IsObserved(const std::string& pref) {
32 return prefs_.count(pref) > 0;
33 }
34
IsManaged()35 bool PrefSetObserver::IsManaged() {
36 for (PrefSet::const_iterator i(prefs_.begin()); i != prefs_.end(); ++i) {
37 const PrefService::Preference* pref =
38 pref_service_->FindPreference(i->c_str());
39 if (pref && pref->IsManaged())
40 return true;
41 }
42 return false;
43 }
44
45 // static
CreateProxyPrefSetObserver(PrefService * pref_service,NotificationObserver * observer)46 PrefSetObserver* PrefSetObserver::CreateProxyPrefSetObserver(
47 PrefService* pref_service,
48 NotificationObserver* observer) {
49 PrefSetObserver* pref_set = new PrefSetObserver(pref_service, observer);
50 pref_set->AddPref(prefs::kProxy);
51
52 return pref_set;
53 }
54
55 // static
CreateDefaultSearchPrefSetObserver(PrefService * pref_service,NotificationObserver * observer)56 PrefSetObserver* PrefSetObserver::CreateDefaultSearchPrefSetObserver(
57 PrefService* pref_service,
58 NotificationObserver* observer) {
59 PrefSetObserver* pref_set = new PrefSetObserver(pref_service, observer);
60 pref_set->AddPref(prefs::kDefaultSearchProviderEnabled);
61 pref_set->AddPref(prefs::kDefaultSearchProviderName);
62 pref_set->AddPref(prefs::kDefaultSearchProviderKeyword);
63 pref_set->AddPref(prefs::kDefaultSearchProviderSearchURL);
64 pref_set->AddPref(prefs::kDefaultSearchProviderSuggestURL);
65 pref_set->AddPref(prefs::kDefaultSearchProviderIconURL);
66 pref_set->AddPref(prefs::kDefaultSearchProviderInstantURL);
67 pref_set->AddPref(prefs::kDefaultSearchProviderEncodings);
68
69 return pref_set;
70 }
71
Observe(NotificationType type,const NotificationSource & source,const NotificationDetails & details)72 void PrefSetObserver::Observe(NotificationType type,
73 const NotificationSource& source,
74 const NotificationDetails& details) {
75 if (observer_)
76 observer_->Observe(type, source, details);
77 }
78