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

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

Issue 11369153: Reland: Closure-based API to PrefChangeObserver and PrefMember. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . 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 | « no previous file | base/prefs/public/pref_change_registrar.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.h
diff --git a/base/prefs/public/pref_change_registrar.h b/base/prefs/public/pref_change_registrar.h
index f3abd9869191056aedecb8a731f499c94414f521..a21cce02a6f19a4f6f183c2fc62de6cf3f1c6c0a 100644
--- a/base/prefs/public/pref_change_registrar.h
+++ b/base/prefs/public/pref_change_registrar.h
@@ -5,21 +5,26 @@
#ifndef BASE_PREFS_PUBLIC_PREF_CHANGE_REGISTRAR_H_
#define BASE_PREFS_PUBLIC_PREF_CHANGE_REGISTRAR_H_
-#include <set>
+#include <map>
#include <string>
#include "base/basictypes.h"
+#include "base/callback.h"
#include "base/prefs/base_prefs_export.h"
+#include "base/prefs/public/pref_observer.h"
-class PrefObserver;
class PrefServiceBase;
// Automatically manages the registration of one or more pref change observers
// with a PrefStore. Functions much like NotificationRegistrar, but specifically
// manages observers of preference changes. When the Registrar is destroyed,
// all registered observers are automatically unregistered with the PrefStore.
-class BASE_PREFS_EXPORT PrefChangeRegistrar {
+class BASE_PREFS_EXPORT PrefChangeRegistrar : public PrefObserver {
public:
+ // You can register this type of callback if you need to know the
+ // path of the preference that is changing.
+ typedef base::Callback<void(const std::string&)> NamedChangeCallback;
+
PrefChangeRegistrar();
virtual ~PrefChangeRegistrar();
@@ -27,15 +32,19 @@ class BASE_PREFS_EXPORT PrefChangeRegistrar {
// than once as long as the value of |service| doesn't change.
void Init(PrefServiceBase* service);
- // Adds an pref observer for the specified pref |path| and |obs| observer
+ // Adds a pref observer for the specified pref |path| and |obs| observer
// object. All registered observers will be automatically unregistered
- // when the registrar's destructor is called unless the observer has been
- // explicitly removed by a call to Remove beforehand.
+ // when the registrar's destructor is called.
+ //
+ // Only one observer may be registered per path.
+ void Add(const char* path, const base::Closure& obs);
+ void Add(const char* path, const NamedChangeCallback& obs);
+
+ // Deprecated version of Add, soon to be removed.
void Add(const char* path, PrefObserver* obs);
- // Removes a preference observer that has previously been added with a call to
- // Add.
- void Remove(const char* path, PrefObserver* obs);
+ // Removes the pref observer registered for |path|.
+ void Remove(const char* path);
// Removes all observers that have been previously added with a call to Add.
void RemoveAll();
@@ -50,9 +59,16 @@ class BASE_PREFS_EXPORT PrefChangeRegistrar {
bool IsManaged();
private:
- typedef std::pair<std::string, PrefObserver*> ObserverRegistration;
+ // PrefObserver:
+ virtual void OnPreferenceChanged(PrefServiceBase* service,
+ const std::string& pref_name) OVERRIDE;
+
+ static void InvokeUnnamedCallback(const base::Closure& callback,
+ const std::string& pref_name);
+
+ typedef std::map<std::string, NamedChangeCallback> ObserverMap;
- std::set<ObserverRegistration> observers_;
+ ObserverMap observers_;
PrefServiceBase* service_;
DISALLOW_COPY_AND_ASSIGN(PrefChangeRegistrar);
« no previous file with comments | « no previous file | base/prefs/public/pref_change_registrar.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698