OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ | |
6 #define CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ | |
7 | |
8 #include <list> | |
9 #include <string> | |
10 | |
11 #include "base/callback.h" | |
12 #include "base/hash_tables.h" | |
13 #include "base/observer_list.h" | |
14 #include "base/prefs/pref_notifier.h" | |
15 #include "base/prefs/pref_observer.h" | |
16 #include "base/threading/non_thread_safe.h" | |
17 | |
18 class PrefService; | |
19 | |
20 // The PrefNotifier implementation used by the PrefService. | |
21 class PrefNotifierImpl : public PrefNotifier, | |
22 public base::NonThreadSafe { | |
23 public: | |
24 PrefNotifierImpl(); | |
25 explicit PrefNotifierImpl(PrefService* pref_service); | |
26 virtual ~PrefNotifierImpl(); | |
27 | |
28 // If the pref at the given path changes, we call the observer's | |
29 // OnPreferenceChanged method. | |
30 void AddPrefObserver(const char* path, PrefObserver* observer); | |
31 void RemovePrefObserver(const char* path, PrefObserver* observer); | |
32 | |
33 // We run the callback once, when initialization completes. The bool | |
34 // parameter will be set to true for successful initialization, | |
35 // false for unsuccessful. | |
36 void AddInitObserver(base::Callback<void(bool)> observer); | |
37 | |
38 void SetPrefService(PrefService* pref_service); | |
39 | |
40 protected: | |
41 // PrefNotifier overrides. | |
42 virtual void OnPreferenceChanged(const std::string& pref_name) OVERRIDE; | |
43 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; | |
44 | |
45 // 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 | |
47 // testing. | |
48 typedef ObserverList<PrefObserver> PrefObserverList; | |
49 typedef base::hash_map<std::string, PrefObserverList*> PrefObserverMap; | |
50 | |
51 typedef std::list<base::Callback<void(bool)> > PrefInitObserverList; | |
52 | |
53 const PrefObserverMap* pref_observers() const { return &pref_observers_; } | |
54 | |
55 private: | |
56 // For the given pref_name, fire any observer of the pref. Virtual so it can | |
57 // be mocked for unit testing. | |
58 virtual void FireObservers(const std::string& path); | |
59 | |
60 // Weak reference; the notifier is owned by the PrefService. | |
61 PrefService* pref_service_; | |
62 | |
63 PrefObserverMap pref_observers_; | |
64 PrefInitObserverList init_observers_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl); | |
67 }; | |
68 | |
69 #endif // CHROME_BROWSER_PREFS_PREF_NOTIFIER_IMPL_H_ | |
OLD | NEW |