OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/prefs/pref_set_observer.h" | 5 #include "chrome/browser/prefs/pref_set_observer.h" |
6 | 6 |
7 #include "chrome/common/notification_type.h" | 7 #include "chrome/common/notification_type.h" |
8 #include "chrome/common/pref_names.h" | 8 #include "chrome/common/pref_names.h" |
9 | 9 |
10 PrefSetObserver::PrefSetObserver(PrefService* pref_service, | 10 PrefSetObserver::PrefSetObserver(PrefService* pref_service, |
11 NotificationObserver* observer) | 11 NotificationObserver* observer) |
12 : pref_service_(pref_service), | 12 : pref_service_(pref_service), |
13 observer_(observer) { | 13 observer_(observer) { |
14 } | 14 registrar_.Init(pref_service); |
15 | |
16 PrefSetObserver::~PrefSetObserver() { | |
17 for (PrefSet::const_iterator i(prefs_.begin()); i != prefs_.end(); ++i) | |
18 pref_service_->RemovePrefObserver(i->c_str(), this); | |
19 } | 15 } |
20 | 16 |
21 void PrefSetObserver::AddPref(const std::string& pref) { | 17 void PrefSetObserver::AddPref(const std::string& pref) { |
22 if (!prefs_.count(pref) && pref_service_->FindPreference(pref.c_str())) { | 18 if (!prefs_.count(pref) && pref_service_->FindPreference(pref.c_str())) { |
23 prefs_.insert(pref); | 19 prefs_.insert(pref); |
24 pref_service_->AddPrefObserver(pref.c_str(), this); | 20 registrar_.Add(pref.c_str(), this); |
25 } | 21 } |
26 } | 22 } |
27 | 23 |
28 void PrefSetObserver::RemovePref(const std::string& pref) { | 24 void PrefSetObserver::RemovePref(const std::string& pref) { |
29 if (prefs_.erase(pref)) | 25 if (prefs_.erase(pref)) |
30 pref_service_->RemovePrefObserver(pref.c_str(), this); | 26 registrar_.Remove(pref.c_str(), this); |
31 } | 27 } |
32 | 28 |
33 bool PrefSetObserver::IsObserved(const std::string& pref) { | 29 bool PrefSetObserver::IsObserved(const std::string& pref) { |
34 return prefs_.count(pref) > 0; | 30 return prefs_.count(pref) > 0; |
35 } | 31 } |
36 | 32 |
37 bool PrefSetObserver::IsManaged() { | 33 bool PrefSetObserver::IsManaged() { |
38 for (PrefSet::const_iterator i(prefs_.begin()); i != prefs_.end(); ++i) { | 34 for (PrefSet::const_iterator i(prefs_.begin()); i != prefs_.end(); ++i) { |
39 const PrefService::Preference* pref = | 35 const PrefService::Preference* pref = |
40 pref_service_->FindPreference(i->c_str()); | 36 pref_service_->FindPreference(i->c_str()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 68 |
73 return pref_set; | 69 return pref_set; |
74 } | 70 } |
75 | 71 |
76 void PrefSetObserver::Observe(NotificationType type, | 72 void PrefSetObserver::Observe(NotificationType type, |
77 const NotificationSource& source, | 73 const NotificationSource& source, |
78 const NotificationDetails& details) { | 74 const NotificationDetails& details) { |
79 if (observer_) | 75 if (observer_) |
80 observer_->Observe(type, source, details); | 76 observer_->Observe(type, source, details); |
81 } | 77 } |
OLD | NEW |