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

Unified Diff: base/prefs/public/pref_change_registrar.cc

Issue 11368098: Draft change to use base::Closure instead of PrefObserver in PrefChangeRegistrar. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to review comments. Created 8 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/prefs/public/pref_change_registrar.h ('k') | base/prefs/public/pref_change_registrar_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..47efc9c5ecc90f23526ce9ed0f348dd2ea45de7e 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,37 @@ 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_, std::string(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 +62,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 +75,9 @@ bool PrefChangeRegistrar::IsManaged() {
}
return false;
}
+
+void PrefChangeRegistrar::OnPreferenceChanged(PrefServiceBase* service,
+ const std::string& pref) {
+ if (IsObserved(pref))
+ observers_[pref].Run();
+}
« no previous file with comments | « base/prefs/public/pref_change_registrar.h ('k') | base/prefs/public/pref_change_registrar_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698