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: |
23 PrefChangeRegistrar(); | 24 PrefChangeRegistrar(); |
24 virtual ~PrefChangeRegistrar(); | 25 virtual ~PrefChangeRegistrar(); |
25 | 26 |
26 // Must be called before adding or removing observers. Can be called more | 27 // 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. | 28 // than once as long as the value of |service| doesn't change. |
28 void Init(PrefServiceBase* service); | 29 void Init(PrefServiceBase* service); |
29 | 30 |
30 // Adds an pref observer for the specified pref |path| and |obs| observer | 31 // Adds an pref observer for the specified pref |path| and |obs| observer |
31 // object. All registered observers will be automatically unregistered | 32 // object. All registered observers will be automatically unregistered |
32 // when the registrar's destructor is called unless the observer has been | 33 // when the registrar's destructor is called. |
33 // explicitly removed by a call to Remove beforehand. | 34 // |
| 35 // Only one observer may be registered per path. |
| 36 void Add(const char* path, const base::Closure& obs); |
| 37 |
| 38 // Deprecated version of Add, soon to be removed. |
34 void Add(const char* path, PrefObserver* obs); | 39 void Add(const char* path, PrefObserver* obs); |
35 | 40 |
36 // Removes a preference observer that has previously been added with a call to | 41 // Removes the pref observer registered for |path|. |
37 // Add. | 42 void Remove(const char* path); |
38 void Remove(const char* path, PrefObserver* obs); | |
39 | 43 |
40 // Removes all observers that have been previously added with a call to Add. | 44 // Removes all observers that have been previously added with a call to Add. |
41 void RemoveAll(); | 45 void RemoveAll(); |
42 | 46 |
43 // Returns true if no pref observers are registered. | 47 // Returns true if no pref observers are registered. |
44 bool IsEmpty() const; | 48 bool IsEmpty() const; |
45 | 49 |
46 // Check whether |pref| is in the set of preferences being observed. | 50 // Check whether |pref| is in the set of preferences being observed. |
47 bool IsObserved(const std::string& pref); | 51 bool IsObserved(const std::string& pref); |
48 | 52 |
49 // Check whether any of the observed preferences has the managed bit set. | 53 // Check whether any of the observed preferences has the managed bit set. |
50 bool IsManaged(); | 54 bool IsManaged(); |
51 | 55 |
52 private: | 56 private: |
53 typedef std::pair<std::string, PrefObserver*> ObserverRegistration; | 57 // PrefObserver: |
| 58 virtual void OnPreferenceChanged(PrefServiceBase* service, |
| 59 const std::string& pref_name) OVERRIDE; |
54 | 60 |
55 std::set<ObserverRegistration> observers_; | 61 typedef std::map<std::string, base::Closure> ObserverMap; |
| 62 |
| 63 ObserverMap observers_; |
56 PrefServiceBase* service_; | 64 PrefServiceBase* service_; |
57 | 65 |
58 DISALLOW_COPY_AND_ASSIGN(PrefChangeRegistrar); | 66 DISALLOW_COPY_AND_ASSIGN(PrefChangeRegistrar); |
59 }; | 67 }; |
60 | 68 |
61 #endif // BASE_PREFS_PUBLIC_PREF_CHANGE_REGISTRAR_H_ | 69 #endif // BASE_PREFS_PUBLIC_PREF_CHANGE_REGISTRAR_H_ |
OLD | NEW |