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.
5 // A helper class that assists preferences in firing notifications when lists
6 // or dictionaries are changed.
8 #ifndef COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_
9 #define COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_
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"
23 // Base class for ScopedUserPrefUpdateTemplate that contains the parts
24 // that do not depend on ScopedUserPrefUpdateTemplate's template parameter.
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 {
31 ScopedUserPrefUpdateBase(const ScopedUserPrefUpdateBase&) = delete;
32 ScopedUserPrefUpdateBase& operator=(const ScopedUserPrefUpdateBase&) = delete;
35 ScopedUserPrefUpdateBase(PrefService* service, const std::string& path);
38 ~ScopedUserPrefUpdateBase();
40 // Sets |value_| to |service_|->GetMutableUserPref and returns it.
41 base::Value* GetValueOfType(base::Value::Type type);
44 // If |value_| is not null, triggers a notification of PrefObservers and
49 raw_ptr<PrefService> service_;
50 // Path of the preference being updated.
52 // Cache of value from user pref store (set between Get() and Notify() calls).
53 raw_ptr<base::Value> value_;
55 SEQUENCE_CHECKER(sequence_checker_);
60 // Class to support modifications to base::Value::Dicts while guaranteeing
61 // that PrefObservers are notified of changed values.
63 // This class may only be used on the UI thread as it requires access to the
65 class COMPONENTS_PREFS_EXPORT ScopedDictPrefUpdate
66 : public subtle::ScopedUserPrefUpdateBase {
68 // The underlying dictionary must not be removed from `service` during
69 // the lifetime of the created ScopedDictPrefUpdate.
70 ScopedDictPrefUpdate(PrefService* service, const std::string& path)
71 : ScopedUserPrefUpdateBase(service, path) {}
73 ScopedDictPrefUpdate(const ScopedDictPrefUpdate&) = delete;
74 ScopedDictPrefUpdate& operator=(const ScopedDictPrefUpdate&) = delete;
76 // Triggers an update notification if Get() was called.
77 virtual ~ScopedDictPrefUpdate() = default;
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
84 // Calling Get() will result in an update notification automatically
85 // being triggered at destruction time.
87 // The ownership of the return value remains with the user pref store.
88 base::Value::Dict& Get();
90 base::Value::Dict& operator*() { return Get(); }
92 base::Value::Dict* operator->() { return &Get(); }
95 // Class to support modifications to base::Value::Lists while guaranteeing
96 // that PrefObservers are notified of changed values.
98 // This class may only be used on the UI thread as it requires access to the
100 class COMPONENTS_PREFS_EXPORT ScopedListPrefUpdate
101 : public subtle::ScopedUserPrefUpdateBase {
103 // The underlying list must not be removed from `service` during
104 // the lifetime of the created ScopedListPrefUpdate.
105 ScopedListPrefUpdate(PrefService* service, const std::string& path)
106 : ScopedUserPrefUpdateBase(service, path) {}
108 ScopedListPrefUpdate(const ScopedListPrefUpdate&) = delete;
109 ScopedListPrefUpdate& operator=(const ScopedListPrefUpdate&) = delete;
111 // Triggers an update notification if Get() was called.
112 virtual ~ScopedListPrefUpdate() = default;
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
119 // Calling Get() will result in an update notification automatically
120 // being triggered at destruction time.
122 // The ownership of the return value remains with the user pref store.
123 base::Value::List& Get();
125 base::Value::List& operator*() { return Get(); }
127 base::Value::List* operator->() { return &Get(); }
130 #endif // COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_