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/pref_notifier.h" |
| 19 | #include "components/prefs/pref_observer.h" |
Brett Wilson | 5c6cf26 | 2017-09-09 02:05:54 | [diff] [blame] | 20 | #include "components/prefs/prefs_export.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 | |
Brett Wilson | 21cf626a | 2017-09-07 00:30:20 | [diff] [blame] | 36 | // These observers are called for any pref changes. |
| 37 | // |
| 38 | // AVOID ADDING THESE. See the long comment in the identically-named |
| 39 | // functions on PrefService for background. |
| 40 | void AddPrefObserverAllPrefs(PrefObserver* observer); |
| 41 | void RemovePrefObserverAllPrefs(PrefObserver* observer); |
| 42 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 43 | // We run the callback once, when initialization completes. The bool |
| 44 | // parameter will be set to true for successful initialization, |
| 45 | // false for unsuccessful. |
| 46 | void AddInitObserver(base::Callback<void(bool)> observer); |
| 47 | |
| 48 | void SetPrefService(PrefService* pref_service); |
| 49 | |
| 50 | protected: |
| 51 | // PrefNotifier overrides. |
| 52 | void OnPreferenceChanged(const std::string& pref_name) override; |
| 53 | void OnInitializationCompleted(bool succeeded) override; |
| 54 | |
| 55 | // A map from pref names to a list of observers. Observers get fired in the |
| 56 | // order they are added. These should only be accessed externally for unit |
| 57 | // testing. |
| 58 | typedef base::ObserverList<PrefObserver> PrefObserverList; |
avi | a92d13b | 2016-09-21 07:48:40 | [diff] [blame] | 59 | typedef base::hash_map<std::string, std::unique_ptr<PrefObserverList>> |
| 60 | PrefObserverMap; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 61 | |
| 62 | typedef std::list<base::Callback<void(bool)>> PrefInitObserverList; |
| 63 | |
| 64 | const PrefObserverMap* pref_observers() const { return &pref_observers_; } |
| 65 | |
| 66 | private: |
| 67 | // For the given pref_name, fire any observer of the pref. Virtual so it can |
| 68 | // be mocked for unit testing. |
| 69 | virtual void FireObservers(const std::string& path); |
| 70 | |
| 71 | // Weak reference; the notifier is owned by the PrefService. |
| 72 | PrefService* pref_service_; |
| 73 | |
| 74 | PrefObserverMap pref_observers_; |
| 75 | PrefInitObserverList init_observers_; |
| 76 | |
Brett Wilson | 21cf626a | 2017-09-07 00:30:20 | [diff] [blame] | 77 | // Observers for changes to any preference. |
| 78 | PrefObserverList all_prefs_pref_observers_; |
| 79 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 80 | base::ThreadChecker thread_checker_; |
| 81 | |
| 82 | DISALLOW_COPY_AND_ASSIGN(PrefNotifierImpl); |
| 83 | }; |
| 84 | |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 85 | #endif // COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_ |