Index: base/prefs/public/pref_change_registrar.cc |
diff --git a/base/prefs/public/pref_change_registrar.cc b/base/prefs/public/pref_change_registrar.cc |
index 3f94b044d8633e0a007bfe196287b9eb3b0b1a1f..e150dce19796589d09ff52770af6f77abd1c3dc9 100644 |
--- a/base/prefs/public/pref_change_registrar.cc |
+++ b/base/prefs/public/pref_change_registrar.cc |
@@ -4,6 +4,7 @@ |
#include "base/prefs/public/pref_change_registrar.h" |
+#include "base/bind.h" |
#include "base/logging.h" |
#include "base/prefs/public/pref_service_base.h" |
@@ -23,43 +24,36 @@ void PrefChangeRegistrar::Init(PrefServiceBase* service) { |
} |
void PrefChangeRegistrar::Add(const char* path, PrefObserver* obs) { |
- if (!service_) { |
- NOTREACHED(); |
- return; |
- } |
- ObserverRegistration registration(path, obs); |
- if (observers_.find(registration) != observers_.end()) { |
- NOTREACHED(); |
- return; |
- } |
- observers_.insert(registration); |
- service_->AddPrefObserver(path, obs); |
+ DCHECK(obs); |
+ return Add(path, base::Bind(&PrefObserver::OnPreferenceChanged, |
+ base::Unretained(obs), service_, path)); |
} |
-void PrefChangeRegistrar::Remove(const char* path, PrefObserver* obs) { |
+void PrefChangeRegistrar::Add(const char* path, const base::Closure& obs) { |
if (!service_) { |
NOTREACHED(); |
return; |
} |
- ObserverRegistration registration(path, obs); |
- std::set<ObserverRegistration>::iterator it = |
- observers_.find(registration); |
- if (it == observers_.end()) { |
- NOTREACHED(); |
- return; |
- } |
- service_->RemovePrefObserver(it->first.c_str(), it->second); |
- observers_.erase(it); |
+ DCHECK(!IsObserved(path)) << "Already had this pref registered."; |
+ |
+ service_->AddPrefObserver(path, this); |
+ observers_[path] = obs; |
+} |
+ |
+void PrefChangeRegistrar::Remove(const char* path) { |
+ DCHECK(IsObserved(path)); |
+ |
+ observers_.erase(path); |
+ service_->RemovePrefObserver(path, this); |
} |
void PrefChangeRegistrar::RemoveAll() { |
- if (service_) { |
- for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); |
- it != observers_.end(); ++it) { |
- service_->RemovePrefObserver(it->first.c_str(), it->second); |
- } |
- observers_.clear(); |
+ for (ObserverMap::const_iterator it = observers_.begin(); |
+ it != observers_.end(); ++it) { |
+ service_->RemovePrefObserver(it->first.c_str(), this); |
} |
+ |
+ observers_.clear(); |
} |
bool PrefChangeRegistrar::IsEmpty() const { |
@@ -67,16 +61,11 @@ bool PrefChangeRegistrar::IsEmpty() const { |
} |
bool PrefChangeRegistrar::IsObserved(const std::string& pref) { |
- for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); |
- it != observers_.end(); ++it) { |
- if (it->first == pref) |
- return true; |
- } |
- return false; |
+ return observers_.find(pref) != observers_.end(); |
} |
bool PrefChangeRegistrar::IsManaged() { |
- for (std::set<ObserverRegistration>::const_iterator it = observers_.begin(); |
+ for (ObserverMap::const_iterator it = observers_.begin(); |
it != observers_.end(); ++it) { |
const PrefServiceBase::Preference* pref = |
service_->FindPreference(it->first.c_str()); |
@@ -85,3 +74,10 @@ bool PrefChangeRegistrar::IsManaged() { |
} |
return false; |
} |
+ |
+void PrefChangeRegistrar::OnPreferenceChanged(PrefServiceBase* service, |
+ const std::string& pref) { |
+ if (IsObserved(pref)) { |
Mattias Nissler (ping if slow)
2012/11/07 16:02:24
nit: no need for curlies.
Jói
2012/11/08 11:03:14
Done.
|
+ observers_[pref].Run(); |
+ } |
+} |