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

Unified Diff: chrome/browser/api/prefs/pref_member.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 | « base/prefs/public/pref_change_registrar_unittest.cc ('k') | chrome/browser/api/prefs/pref_member.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/api/prefs/pref_member.h
diff --git a/chrome/browser/api/prefs/pref_member.h b/chrome/browser/api/prefs/pref_member.h
index 78436936149008c0f37080326aecc12312c8556c..5f3791cf19659585c7663c202e26f5d8ad278596 100644
--- a/chrome/browser/api/prefs/pref_member.h
+++ b/chrome/browser/api/prefs/pref_member.h
@@ -28,6 +28,8 @@
#include <vector>
#include "base/basictypes.h"
+#include "base/bind.h"
+#include "base/callback_forward.h"
#include "base/file_path.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
@@ -40,6 +42,11 @@ class PrefServiceBase;
namespace subtle {
class PrefMemberBase : public PrefObserver {
+ public:
+ // Type of callback you can register if you need to know the name of
+ // the pref that is changing.
+ typedef base::Callback<void(const std::string&)> NamedChangeCallback;
+
protected:
class Internal : public base::RefCountedThreadSafe<Internal> {
public:
@@ -91,7 +98,7 @@ class PrefMemberBase : public PrefObserver {
// See PrefMember<> for description.
void Init(const char* pref_name, PrefServiceBase* prefs,
- PrefObserver* observer);
+ const NamedChangeCallback& observer);
virtual void CreateInternal() const = 0;
@@ -123,10 +130,14 @@ class PrefMemberBase : public PrefObserver {
virtual Internal* internal() const = 0;
+ // Used to allow registering plain base::Closure callbacks.
+ static void InvokeUnnamedCallback(const base::Closure& callback,
+ const std::string& pref_name);
+
private:
// Ordered the members to compact the class instance.
std::string pref_name_;
- PrefObserver* observer_;
+ NamedChangeCallback observer_;
PrefServiceBase* prefs_;
protected:
@@ -148,13 +159,33 @@ class PrefMember : public subtle::PrefMemberBase {
PrefMember() {}
virtual ~PrefMember() {}
- // Do the actual initialization of the class. |observer| may be null if you
- // don't want any notifications of changes.
- // This method should only be called on the UI thread.
+ // Do the actual initialization of the class. Use the two-parameter
+ // version if you don't want any notifications of changes. This
+ // method should only be called on the UI thread.
void Init(const char* pref_name, PrefServiceBase* prefs,
- PrefObserver* observer) {
+ const NamedChangeCallback& observer) {
subtle::PrefMemberBase::Init(pref_name, prefs, observer);
}
+ void Init(const char* pref_name, PrefServiceBase* prefs,
+ const base::Closure& observer) {
+ subtle::PrefMemberBase::Init(
+ pref_name, prefs,
+ base::Bind(&PrefMemberBase::InvokeUnnamedCallback, observer));
+ }
+ void Init(const char* pref_name, PrefServiceBase* prefs) {
+ subtle::PrefMemberBase::Init(pref_name, prefs);
+ }
+
+ // Deprecated version of Init.
+ void Init(const char* pref_name, PrefServiceBase* prefs,
+ PrefObserver* observer) {
+ if (observer) {
+ Init(pref_name, prefs, base::Bind(&PrefObserver::OnPreferenceChanged,
+ base::Unretained(observer), prefs));
+ } else {
+ Init(pref_name, prefs, NamedChangeCallback());
+ }
+ }
// Unsubscribes the PrefMember from the PrefService. After calling this
// function, the PrefMember may not be used any more on the UI thread.
« no previous file with comments | « base/prefs/public/pref_change_registrar_unittest.cc ('k') | chrome/browser/api/prefs/pref_member.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698