1 // Copyright 2011 The Chromium Authors 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 COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_ 6 #define COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_ 7 8 #include <list> 9 #include <memory> 10 #include <string> 11 #include <string_view> 12 #include <unordered_map> 13 14 #include "base/compiler_specific.h" 15 #include "base/functional/callback.h" 16 #include "base/memory/raw_ptr.h" 17 #include "base/observer_list.h" 18 #include "base/sequence_checker.h" 19 #include "components/prefs/pref_notifier.h" 20 #include "components/prefs/pref_observer.h" 21 #include "components/prefs/prefs_export.h" 22 #include "components/prefs/transparent_unordered_string_map.h" 23 24 class PrefService; 25 26 // The PrefNotifier implementation used by the PrefService. 27 class COMPONENTS_PREFS_EXPORT PrefNotifierImpl : public PrefNotifier { 28 public: 29 PrefNotifierImpl(); 30 explicit PrefNotifierImpl(PrefService* pref_service); 31 32 PrefNotifierImpl(const PrefNotifierImpl&) = delete; 33 PrefNotifierImpl& operator=(const PrefNotifierImpl&) = delete; 34 35 ~PrefNotifierImpl() override; 36 37 // If the pref at the given path changes, we call the observer's 38 // OnPreferenceChanged method. 39 void AddPrefObserver(std::string_view path, PrefObserver* observer); 40 void RemovePrefObserver(std::string_view path, PrefObserver* observer); 41 42 // These observers are called for any pref changes. 43 // 44 // AVOID ADDING THESE. See the long comment in the identically-named 45 // functions on PrefService for background. 46 void AddPrefObserverAllPrefs(PrefObserver* observer); 47 void RemovePrefObserverAllPrefs(PrefObserver* observer); 48 49 // We run the callback once, when initialization completes. The bool 50 // parameter will be set to true for successful initialization, 51 // false for unsuccessful. 52 void AddInitObserver(base::OnceCallback<void(bool)> observer); 53 54 void SetPrefService(PrefService* pref_service); 55 56 // PrefNotifier overrides. 57 void OnPreferenceChanged(std::string_view pref_name) override; 58 59 protected: 60 // PrefNotifier overrides. 61 void OnInitializationCompleted(bool succeeded) override; 62 63 // A map from pref names to a list of observers. Observers get fired in the 64 // order they are added. These should only be accessed externally for unit 65 // testing. 66 using PrefObserverList = base::ObserverList<PrefObserver>::Unchecked; 67 using PrefObserverMap = TransparentUnorderedStringMap<PrefObserverList>; 68 using PrefInitObserverList = std::list<base::OnceCallback<void(bool)>>; 69 pref_observers()70 const PrefObserverMap* pref_observers() const { return &pref_observers_; } 71 72 private: 73 // For the given pref_name, fire any observer of the pref. Virtual so it can 74 // be mocked for unit testing. 75 virtual void FireObservers(std::string_view path); 76 77 // Weak reference; the notifier is owned by the PrefService. 78 raw_ptr<PrefService> pref_service_; 79 80 PrefObserverMap pref_observers_; 81 PrefInitObserverList init_observers_; 82 83 // Observers for changes to any preference. 84 PrefObserverList all_prefs_pref_observers_; 85 86 SEQUENCE_CHECKER(sequence_checker_); 87 }; 88 89 #endif // COMPONENTS_PREFS_PREF_NOTIFIER_IMPL_H_ 90