OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ | 5 #ifndef CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ |
6 #define CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ | 6 #define CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/hash_tables.h" | 12 #include "base/hash_tables.h" |
13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
14 #include "base/prefs/pref_notifier.h" | 14 #include "base/prefs/pref_notifier.h" |
15 #include "base/prefs/public/pref_observer.h" | |
16 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
17 | 16 |
18 class PrefService; | 17 class PrefService; |
19 | 18 |
20 // The PrefNotifier implementation used by the PrefService. | 19 // The PrefNotifier implementation used by the PrefService. |
21 class PrefNotifierImpl : public PrefNotifier, | 20 class PrefNotifierImpl : public PrefNotifier, |
22 public base::NonThreadSafe { | 21 public base::NonThreadSafe { |
23 public: | 22 public: |
24 PrefNotifierImpl(); | 23 PrefNotifierImpl(); |
25 explicit PrefNotifierImpl(PrefService* pref_service); | 24 explicit PrefNotifierImpl(PrefService* pref_service); |
26 virtual ~PrefNotifierImpl(); | 25 virtual ~PrefNotifierImpl(); |
27 | 26 |
28 // If the pref at the given path changes, we call the observer's | 27 // If the pref at the given path changes, we call the observer's |
29 // OnPreferenceChanged method. | 28 // OnPreferenceChanged method. |
30 void AddPrefObserver(const char* path, PrefObserver* observer); | 29 void AddPrefObserver(const char* path, const base::Closure& observer); |
31 void RemovePrefObserver(const char* path, PrefObserver* observer); | 30 |
| 31 // This removes an observer. Note that when using the Closure |
| 32 // version of the method, you must provide a closure whose Equal() |
| 33 // method will evaluate to true when compared to the closure you |
| 34 // registered using AddPrefObserver. |
| 35 void RemovePrefObserver(const char* path, const base::Closure& observer); |
32 | 36 |
33 // We run the callback once, when initialization completes. The bool | 37 // We run the callback once, when initialization completes. The bool |
34 // parameter will be set to true for successful initialization, | 38 // parameter will be set to true for successful initialization, |
35 // false for unsuccessful. | 39 // false for unsuccessful. |
36 void AddInitObserver(base::Callback<void(bool)> observer); | 40 void AddInitObserver(base::Callback<void(bool)> observer); |
37 | 41 |
38 void SetPrefService(PrefService* pref_service); | 42 void SetPrefService(PrefService* pref_service); |
39 | 43 |
40 protected: | 44 protected: |
41 // PrefNotifier overrides. | 45 // PrefNotifier overrides. |
42 virtual void OnPreferenceChanged(const std::string& pref_name) OVERRIDE; | 46 virtual void OnPreferenceChanged(const std::string& pref_name) OVERRIDE; |
43 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; | 47 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; |
44 | 48 |
45 // A map from pref names to a list of observers. Observers get fired in the | 49 // A map from pref names to a list of observers. Observers get fired in the |
46 // order they are added. These should only be accessed externally for unit | 50 // order they are added. These should only be accessed externally for unit |
47 // testing. | 51 // testing. |
48 typedef ObserverList<PrefObserver> PrefObserverList; | 52 typedef std::list<base::Closure> PrefObserverList; |
49 typedef base::hash_map<std::string, PrefObserverList*> PrefObserverMap; | 53 typedef base::hash_map<std::string, PrefObserverList*> PrefObserverMap; |
50 | 54 |
51 typedef std::list<base::Callback<void(bool)> > PrefInitObserverList; | 55 typedef std::list<base::Callback<void(bool)> > PrefInitObserverList; |
52 | 56 |
53 const PrefObserverMap* pref_observers() const { return &pref_observers_; } | 57 const PrefObserverMap* pref_observers() const { return &pref_observers_; } |
54 | 58 |
55 private: | 59 private: |
56 // For the given pref_name, fire any observer of the pref. Virtual so it can | 60 // For the given pref_name, fire any observer of the pref. Virtual so it can |
57 // be mocked for unit testing. | 61 // be mocked for unit testing. |
58 virtual void FireObservers(const std::string& path); | 62 virtual void FireObservers(const std::string& path); |
59 | 63 |
60 // Weak reference; the notifier is owned by the PrefService. | 64 // Weak reference; the notifier is owned by the PrefService. |
61 PrefService* pref_service_; | 65 PrefService* pref_service_; |
62 | 66 |
63 PrefObserverMap pref_observers_; | 67 PrefObserverMap pref_observers_; |
64 PrefInitObserverList init_observers_; | 68 PrefInitObserverList init_observers_; |
65 | 69 |
66 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl); | 70 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl); |
67 }; | 71 }; |
68 | 72 |
69 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ | 73 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ |
OLD | NEW |