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. |