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