| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_PREFS_PUBLIC_PREF_CHANGE_REGISTRAR_H_ | 5 #ifndef BASE_PREFS_PUBLIC_PREF_CHANGE_REGISTRAR_H_ |
| 6 #define BASE_PREFS_PUBLIC_PREF_CHANGE_REGISTRAR_H_ | 6 #define BASE_PREFS_PUBLIC_PREF_CHANGE_REGISTRAR_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" |
| 12 #include "base/prefs/base_prefs_export.h" | 13 #include "base/prefs/base_prefs_export.h" |
| 14 #include "base/prefs/public/pref_observer.h" |
| 13 | 15 |
| 14 class PrefObserver; | |
| 15 class PrefServiceBase; | 16 class PrefServiceBase; |
| 16 | 17 |
| 17 // Automatically manages the registration of one or more pref change observers | 18 // Automatically manages the registration of one or more pref change observers |
| 18 // with a PrefStore. Functions much like NotificationRegistrar, but specifically | 19 // with a PrefStore. Functions much like NotificationRegistrar, but specifically |
| 19 // manages observers of preference changes. When the Registrar is destroyed, | 20 // manages observers of preference changes. When the Registrar is destroyed, |
| 20 // all registered observers are automatically unregistered with the PrefStore. | 21 // all registered observers are automatically unregistered with the PrefStore. |
| 21 class BASE_PREFS_EXPORT PrefChangeRegistrar { | 22 class BASE_PREFS_EXPORT PrefChangeRegistrar : public PrefObserver { |
| 22 public: | 23 public: |
| 24 // You can register this type of callback if you need to know the |
| 25 // path of the preference that is changing. |
| 26 typedef base::Callback<void(const std::string&)> NamedChangeCallback; |
| 27 |
| 23 PrefChangeRegistrar(); | 28 PrefChangeRegistrar(); |
| 24 virtual ~PrefChangeRegistrar(); | 29 virtual ~PrefChangeRegistrar(); |
| 25 | 30 |
| 26 // Must be called before adding or removing observers. Can be called more | 31 // Must be called before adding or removing observers. Can be called more |
| 27 // than once as long as the value of |service| doesn't change. | 32 // than once as long as the value of |service| doesn't change. |
| 28 void Init(PrefServiceBase* service); | 33 void Init(PrefServiceBase* service); |
| 29 | 34 |
| 30 // Adds an pref observer for the specified pref |path| and |obs| observer | 35 // Adds a pref observer for the specified pref |path| and |obs| observer |
| 31 // object. All registered observers will be automatically unregistered | 36 // object. All registered observers will be automatically unregistered |
| 32 // when the registrar's destructor is called unless the observer has been | 37 // when the registrar's destructor is called. |
| 33 // explicitly removed by a call to Remove beforehand. | 38 // |
| 39 // Only one observer may be registered per path. |
| 40 void Add(const char* path, const base::Closure& obs); |
| 41 void Add(const char* path, const NamedChangeCallback& obs); |
| 42 |
| 43 // Deprecated version of Add, soon to be removed. |
| 34 void Add(const char* path, PrefObserver* obs); | 44 void Add(const char* path, PrefObserver* obs); |
| 35 | 45 |
| 36 // Removes a preference observer that has previously been added with a call to | 46 // Removes the pref observer registered for |path|. |
| 37 // Add. | 47 void Remove(const char* path); |
| 38 void Remove(const char* path, PrefObserver* obs); | |
| 39 | 48 |
| 40 // Removes all observers that have been previously added with a call to Add. | 49 // Removes all observers that have been previously added with a call to Add. |
| 41 void RemoveAll(); | 50 void RemoveAll(); |
| 42 | 51 |
| 43 // Returns true if no pref observers are registered. | 52 // Returns true if no pref observers are registered. |
| 44 bool IsEmpty() const; | 53 bool IsEmpty() const; |
| 45 | 54 |
| 46 // Check whether |pref| is in the set of preferences being observed. | 55 // Check whether |pref| is in the set of preferences being observed. |
| 47 bool IsObserved(const std::string& pref); | 56 bool IsObserved(const std::string& pref); |
| 48 | 57 |
| 49 // Check whether any of the observed preferences has the managed bit set. | 58 // Check whether any of the observed preferences has the managed bit set. |
| 50 bool IsManaged(); | 59 bool IsManaged(); |
| 51 | 60 |
| 52 private: | 61 private: |
| 53 typedef std::pair<std::string, PrefObserver*> ObserverRegistration; | 62 // PrefObserver: |
| 63 virtual void OnPreferenceChanged(PrefServiceBase* service, |
| 64 const std::string& pref_name) OVERRIDE; |
| 54 | 65 |
| 55 std::set<ObserverRegistration> observers_; | 66 static void InvokeUnnamedCallback(const base::Closure& callback, |
| 67 const std::string& pref_name); |
| 68 |
| 69 typedef std::map<std::string, NamedChangeCallback> ObserverMap; |
| 70 |
| 71 ObserverMap observers_; |
| 56 PrefServiceBase* service_; | 72 PrefServiceBase* service_; |
| 57 | 73 |
| 58 DISALLOW_COPY_AND_ASSIGN(PrefChangeRegistrar); | 74 DISALLOW_COPY_AND_ASSIGN(PrefChangeRegistrar); |
| 59 }; | 75 }; |
| 60 | 76 |
| 61 #endif // BASE_PREFS_PUBLIC_PREF_CHANGE_REGISTRAR_H_ | 77 #endif // BASE_PREFS_PUBLIC_PREF_CHANGE_REGISTRAR_H_ |
| OLD | NEW |