brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 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 | |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 5 | #ifndef COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_ |
| 6 | #define COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_ |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 7 | |
| 8 | #include <list> |
avi | a92d13b | 2016-09-21 07:48:40 | [diff] [blame] | 9 | #include <memory> |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | #include "base/callback.h" |
| 13 | #include "base/compiler_specific.h" |
| 14 | #include "base/containers/hash_tables.h" |
| 15 | #include "base/macros.h" |
| 16 | #include "base/observer_list.h" |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 17 | #include "base/threading/thread_checker.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 18 | #include "components/prefs/base_prefs_export.h" |
| 19 | #include "components/prefs/pref_notifier.h" |
| 20 | #include "components/prefs/pref_observer.h" |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 21 | |
| 22 | class PrefService; |
| 23 | |
| 24 | // The PrefNotifier implementation used by the PrefService. |
Nico Weber | 43ddd7a3 | 2017-08-15 19:19:27 | [diff] [blame^] | 25 | class COMPONENTS_PREFS_EXPORT PrefNotifierImpl : public PrefNotifier { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 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; |
avi | a92d13b | 2016-09-21 07:48:40 | [diff] [blame] | 52 | typedef base::hash_map<std::string, std::unique_ptr<PrefObserverList>> |
| 53 | PrefObserverMap; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 54 | |
| 55 | typedef std::list<base::Callback<void(bool)>> PrefInitObserverList; |
| 56 | |
| 57 | const PrefObserverMap* pref_observers() const { return &pref_observers_; } |
| 58 | |
| 59 | private: |
| 60 | // For the given pref_name, fire any observer of the pref. Virtual so it can |
| 61 | // be mocked for unit testing. |
| 62 | virtual void FireObservers(const std::string& path); |
| 63 | |
| 64 | // Weak reference; the notifier is owned by the PrefService. |
| 65 | PrefService* pref_service_; |
| 66 | |
| 67 | PrefObserverMap pref_observers_; |
| 68 | PrefInitObserverList init_observers_; |
| 69 | |
| 70 | base::ThreadChecker thread_checker_; |
| 71 | |
| 72 | DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl); |
| 73 | }; |
| 74 | |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 75 | #endif // COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_ |