1 // Copyright 2010 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_CHANGE_REGISTRAR_H_ 6 #define COMPONENTS_PREFS_PREF_CHANGE_REGISTRAR_H_ 7 8 #include <functional> 9 #include <map> 10 #include <string> 11 #include <string_view> 12 13 #include "base/functional/callback.h" 14 #include "base/memory/raw_ptr.h" 15 #include "components/prefs/pref_observer.h" 16 #include "components/prefs/prefs_export.h" 17 18 class PrefService; 19 20 // Automatically manages the registration of one or more pref change observers 21 // with a PrefStore. When the Registrar is destroyed, all registered observers 22 // are automatically unregistered with the PrefStore. 23 class COMPONENTS_PREFS_EXPORT PrefChangeRegistrar final : public PrefObserver { 24 public: 25 // You can register this type of callback if you need to know the 26 // path of the preference that is changing. 27 using NamedChangeCallback = base::RepeatingCallback<void(const std::string&)>; 28 29 PrefChangeRegistrar(); 30 31 PrefChangeRegistrar(const PrefChangeRegistrar&) = delete; 32 PrefChangeRegistrar& operator=(const PrefChangeRegistrar&) = delete; 33 34 ~PrefChangeRegistrar(); 35 36 // Must be called before adding or removing observers. Can be called more 37 // than once as long as the value of |service| doesn't change. 38 void Init(PrefService* service); 39 40 // Removes all observers and clears the reference to `PrefService`. 41 // `Init` must be called before adding or removing any observers. 42 void Reset(); 43 44 // Adds a pref observer for the specified pref |path| and |obs| observer 45 // object. All registered observers will be automatically unregistered 46 // when the registrar's destructor is called. 47 // 48 // The second version binds a callback that will receive the path of 49 // the preference that is changing as its parameter. 50 // 51 // Only one observer may be registered per path. 52 void Add(std::string_view path, const base::RepeatingClosure& obs); 53 void Add(std::string_view path, const NamedChangeCallback& obs); 54 55 // Removes the pref observer registered for |path|. 56 void Remove(std::string_view path); 57 58 // Removes all observers that have been previously added with a call to Add. 59 void RemoveAll(); 60 61 // Returns true if no pref observers are registered. 62 bool IsEmpty() const; 63 64 // Check whether |pref| is in the set of preferences being observed. 65 bool IsObserved(std::string_view pref); 66 67 // Return the PrefService for this registrar. 68 PrefService* prefs(); 69 const PrefService* prefs() const; 70 71 private: 72 // PrefObserver: 73 void OnPreferenceChanged(PrefService* service, 74 std::string_view pref_name) override; 75 76 static void InvokeUnnamedCallback(const base::RepeatingClosure& callback, 77 const std::string& pref_name); 78 79 using ObserverMap = std::map<std::string, NamedChangeCallback, std::less<>>; 80 81 ObserverMap observers_; 82 raw_ptr<PrefService, AcrossTasksDanglingUntriaged> service_; 83 }; 84 85 #endif // COMPONENTS_PREFS_PREF_CHANGE_REGISTRAR_H_ 86