1 // Copyright 2013 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 // A helper class that assists preferences in firing notifications when lists 6 // or dictionaries are changed. 7 8 #ifndef COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_ 9 #define COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_ 10 11 #include <string> 12 13 #include "base/memory/raw_ptr.h" 14 #include "base/sequence_checker.h" 15 #include "base/values.h" 16 #include "components/prefs/pref_service.h" 17 #include "components/prefs/prefs_export.h" 18 19 class PrefService; 20 21 namespace subtle { 22 23 // Base class for ScopedUserPrefUpdateTemplate that contains the parts 24 // that do not depend on ScopedUserPrefUpdateTemplate's template parameter. 25 // 26 // We need this base class mostly for making it a friend of PrefService 27 // and getting access to PrefService::GetMutableUserPref and 28 // PrefService::ReportUserPrefChanged. 29 class COMPONENTS_PREFS_EXPORT ScopedUserPrefUpdateBase { 30 public: 31 ScopedUserPrefUpdateBase(const ScopedUserPrefUpdateBase&) = delete; 32 ScopedUserPrefUpdateBase& operator=(const ScopedUserPrefUpdateBase&) = delete; 33 34 protected: 35 ScopedUserPrefUpdateBase(PrefService* service, const std::string& path); 36 37 // Calls Notify(). 38 ~ScopedUserPrefUpdateBase(); 39 40 // Sets |value_| to |service_|->GetMutableUserPref and returns it. 41 base::Value* GetValueOfType(base::Value::Type type); 42 43 private: 44 // If |value_| is not null, triggers a notification of PrefObservers and 45 // resets |value_|. 46 void Notify(); 47 48 // Weak pointer. 49 raw_ptr<PrefService> service_; 50 // Path of the preference being updated. 51 std::string path_; 52 // Cache of value from user pref store (set between Get() and Notify() calls). 53 raw_ptr<base::Value> value_; 54 55 SEQUENCE_CHECKER(sequence_checker_); 56 }; 57 58 } // namespace subtle 59 60 // Class to support modifications to base::Value::Dicts while guaranteeing 61 // that PrefObservers are notified of changed values. 62 // 63 // This class may only be used on the UI thread as it requires access to the 64 // PrefService. 65 class COMPONENTS_PREFS_EXPORT ScopedDictPrefUpdate 66 : public subtle::ScopedUserPrefUpdateBase { 67 public: 68 // The underlying dictionary must not be removed from `service` during 69 // the lifetime of the created ScopedDictPrefUpdate. ScopedDictPrefUpdate(PrefService * service,const std::string & path)70 ScopedDictPrefUpdate(PrefService* service, const std::string& path) 71 : ScopedUserPrefUpdateBase(service, path) {} 72 73 ScopedDictPrefUpdate(const ScopedDictPrefUpdate&) = delete; 74 ScopedDictPrefUpdate& operator=(const ScopedDictPrefUpdate&) = delete; 75 76 // Triggers an update notification if Get() was called. 77 virtual ~ScopedDictPrefUpdate() = default; 78 79 // Returns a mutable `base::Value::Dict` instance that 80 // - is already in the user pref store, or 81 // - is (silently) created and written to the user pref store if none existed 82 // before. 83 // 84 // Calling Get() will result in an update notification automatically 85 // being triggered at destruction time. 86 // 87 // The ownership of the return value remains with the user pref store. 88 base::Value::Dict& Get(); 89 90 base::Value::Dict& operator*() { return Get(); } 91 92 base::Value::Dict* operator->() { return &Get(); } 93 }; 94 95 // Class to support modifications to base::Value::Lists while guaranteeing 96 // that PrefObservers are notified of changed values. 97 // 98 // This class may only be used on the UI thread as it requires access to the 99 // PrefService. 100 class COMPONENTS_PREFS_EXPORT ScopedListPrefUpdate 101 : public subtle::ScopedUserPrefUpdateBase { 102 public: 103 // The underlying list must not be removed from `service` during 104 // the lifetime of the created ScopedListPrefUpdate. ScopedListPrefUpdate(PrefService * service,const std::string & path)105 ScopedListPrefUpdate(PrefService* service, const std::string& path) 106 : ScopedUserPrefUpdateBase(service, path) {} 107 108 ScopedListPrefUpdate(const ScopedListPrefUpdate&) = delete; 109 ScopedListPrefUpdate& operator=(const ScopedListPrefUpdate&) = delete; 110 111 // Triggers an update notification if Get() was called. 112 virtual ~ScopedListPrefUpdate() = default; 113 114 // Returns a mutable `base::Value::List` instance that 115 // - is already in the user pref store, or 116 // - is (silently) created and written to the user pref store if none existed 117 // before. 118 // 119 // Calling Get() will result in an update notification automatically 120 // being triggered at destruction time. 121 // 122 // The ownership of the return value remains with the user pref store. 123 base::Value::List& Get(); 124 125 base::Value::List& operator*() { return Get(); } 126 127 base::Value::List* operator->() { return &Get(); } 128 }; 129 130 #endif // COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_ 131