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