xref: /aosp_15_r20/external/cronet/components/prefs/pref_value_store.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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 #include "components/prefs/pref_value_store.h"
6 
7 #include <stddef.h>
8 #include <string>
9 
10 #include "base/logging.h"
11 #include "base/strings/string_piece.h"
12 #include "components/prefs/pref_notifier.h"
13 #include "components/prefs/pref_observer.h"
14 
PrefStoreKeeper()15 PrefValueStore::PrefStoreKeeper::PrefStoreKeeper()
16     : pref_value_store_(nullptr), type_(PrefValueStore::INVALID_STORE) {}
17 
~PrefStoreKeeper()18 PrefValueStore::PrefStoreKeeper::~PrefStoreKeeper() {
19   if (pref_store_) {
20     pref_store_->RemoveObserver(this);
21     pref_store_ = nullptr;
22   }
23   pref_value_store_ = nullptr;
24 }
25 
Initialize(PrefValueStore * store,PrefStore * pref_store,PrefValueStore::PrefStoreType type)26 void PrefValueStore::PrefStoreKeeper::Initialize(
27     PrefValueStore* store,
28     PrefStore* pref_store,
29     PrefValueStore::PrefStoreType type) {
30   if (pref_store_) {
31     pref_store_->RemoveObserver(this);
32     DCHECK(!pref_store_->HasObservers());
33   }
34   type_ = type;
35   pref_value_store_ = store;
36   pref_store_ = pref_store;
37   if (pref_store_)
38     pref_store_->AddObserver(this);
39 }
40 
OnPrefValueChanged(const std::string & key)41 void PrefValueStore::PrefStoreKeeper::OnPrefValueChanged(
42     const std::string& key) {
43   pref_value_store_->OnPrefValueChanged(type_, key);
44 }
45 
OnInitializationCompleted(bool succeeded)46 void PrefValueStore::PrefStoreKeeper::OnInitializationCompleted(
47     bool succeeded) {
48   pref_value_store_->OnInitializationCompleted(type_, succeeded);
49 }
50 
PrefValueStore(PrefStore * managed_prefs,PrefStore * supervised_user_prefs,PrefStore * extension_prefs,PrefStore * standalone_browser_prefs,PrefStore * command_line_prefs,PrefStore * user_prefs,PrefStore * recommended_prefs,PrefStore * default_prefs,PrefNotifier * pref_notifier)51 PrefValueStore::PrefValueStore(PrefStore* managed_prefs,
52                                PrefStore* supervised_user_prefs,
53                                PrefStore* extension_prefs,
54                                PrefStore* standalone_browser_prefs,
55                                PrefStore* command_line_prefs,
56                                PrefStore* user_prefs,
57                                PrefStore* recommended_prefs,
58                                PrefStore* default_prefs,
59                                PrefNotifier* pref_notifier)
60     : pref_notifier_(pref_notifier), initialization_failed_(false) {
61   InitPrefStore(MANAGED_STORE, managed_prefs);
62   InitPrefStore(SUPERVISED_USER_STORE, supervised_user_prefs);
63   InitPrefStore(EXTENSION_STORE, extension_prefs);
64   InitPrefStore(STANDALONE_BROWSER_STORE, standalone_browser_prefs);
65   InitPrefStore(COMMAND_LINE_STORE, command_line_prefs);
66   InitPrefStore(USER_STORE, user_prefs);
67   InitPrefStore(RECOMMENDED_STORE, recommended_prefs);
68   InitPrefStore(DEFAULT_STORE, default_prefs);
69 
70   CheckInitializationCompleted();
71 }
72 
~PrefValueStore()73 PrefValueStore::~PrefValueStore() {}
74 
CloneAndSpecialize(PrefStore * managed_prefs,PrefStore * supervised_user_prefs,PrefStore * extension_prefs,PrefStore * standalone_browser_prefs,PrefStore * command_line_prefs,PrefStore * user_prefs,PrefStore * recommended_prefs,PrefStore * default_prefs,PrefNotifier * pref_notifier)75 std::unique_ptr<PrefValueStore> PrefValueStore::CloneAndSpecialize(
76     PrefStore* managed_prefs,
77     PrefStore* supervised_user_prefs,
78     PrefStore* extension_prefs,
79     PrefStore* standalone_browser_prefs,
80     PrefStore* command_line_prefs,
81     PrefStore* user_prefs,
82     PrefStore* recommended_prefs,
83     PrefStore* default_prefs,
84     PrefNotifier* pref_notifier) {
85   DCHECK(pref_notifier);
86   if (!managed_prefs)
87     managed_prefs = GetPrefStore(MANAGED_STORE);
88   if (!supervised_user_prefs)
89     supervised_user_prefs = GetPrefStore(SUPERVISED_USER_STORE);
90   if (!extension_prefs)
91     extension_prefs = GetPrefStore(EXTENSION_STORE);
92   if (!standalone_browser_prefs)
93     standalone_browser_prefs = GetPrefStore(STANDALONE_BROWSER_STORE);
94   if (!command_line_prefs)
95     command_line_prefs = GetPrefStore(COMMAND_LINE_STORE);
96   if (!user_prefs)
97     user_prefs = GetPrefStore(USER_STORE);
98   if (!recommended_prefs)
99     recommended_prefs = GetPrefStore(RECOMMENDED_STORE);
100   if (!default_prefs)
101     default_prefs = GetPrefStore(DEFAULT_STORE);
102 
103   return std::make_unique<PrefValueStore>(
104       managed_prefs, supervised_user_prefs, extension_prefs,
105       standalone_browser_prefs, command_line_prefs, user_prefs,
106       recommended_prefs, default_prefs, pref_notifier);
107 }
108 
ControllingPrefStoreForPref(const std::string & name) const109 PrefValueStore::PrefStoreType PrefValueStore::ControllingPrefStoreForPref(
110     const std::string& name) const {
111   for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) {
112     if (PrefValueInStore(name, static_cast<PrefStoreType>(i))) {
113       return static_cast<PrefStoreType>(i);
114     }
115   }
116   return INVALID_STORE;
117 }
118 
GetValue(base::StringPiece name,base::Value::Type type,const base::Value ** out_value) const119 bool PrefValueStore::GetValue(base::StringPiece name,
120                               base::Value::Type type,
121                               const base::Value** out_value) const {
122   // Check the |PrefStore|s in order of their priority from highest to lowest,
123   // looking for the first preference value with the given |name| and |type|.
124   for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) {
125     if (GetValueFromStoreWithType(name, type, static_cast<PrefStoreType>(i),
126                                   out_value))
127       return true;
128   }
129   return false;
130 }
131 
GetRecommendedValue(const std::string & name,base::Value::Type type,const base::Value ** out_value) const132 bool PrefValueStore::GetRecommendedValue(const std::string& name,
133                                          base::Value::Type type,
134                                          const base::Value** out_value) const {
135   return GetValueFromStoreWithType(name, type, RECOMMENDED_STORE, out_value);
136 }
137 
NotifyPrefChanged(const std::string & path,PrefValueStore::PrefStoreType new_store)138 void PrefValueStore::NotifyPrefChanged(
139     const std::string& path,
140     PrefValueStore::PrefStoreType new_store) {
141   DCHECK(new_store != INVALID_STORE);
142   // A notification is sent when the pref value in any store changes. If this
143   // store is currently being overridden by a higher-priority store, the
144   // effective value of the pref will not have changed.
145   pref_notifier_->OnPreferenceChanged(path);
146 }
147 
PrefValueInManagedStore(const std::string & name) const148 bool PrefValueStore::PrefValueInManagedStore(const std::string& name) const {
149   return PrefValueInStore(name, MANAGED_STORE);
150 }
151 
PrefValueInSupervisedStore(const std::string & name) const152 bool PrefValueStore::PrefValueInSupervisedStore(const std::string& name) const {
153   return PrefValueInStore(name, SUPERVISED_USER_STORE);
154 }
155 
PrefValueInExtensionStore(const std::string & name) const156 bool PrefValueStore::PrefValueInExtensionStore(const std::string& name) const {
157   return PrefValueInStore(name, EXTENSION_STORE);
158 }
159 
PrefValueInUserStore(const std::string & name) const160 bool PrefValueStore::PrefValueInUserStore(const std::string& name) const {
161   return PrefValueInStore(name, USER_STORE);
162 }
163 
PrefValueFromExtensionStore(const std::string & name) const164 bool PrefValueStore::PrefValueFromExtensionStore(
165     const std::string& name) const {
166   return ControllingPrefStoreForPref(name) == EXTENSION_STORE;
167 }
168 
PrefValueFromUserStore(const std::string & name) const169 bool PrefValueStore::PrefValueFromUserStore(const std::string& name) const {
170   return ControllingPrefStoreForPref(name) == USER_STORE;
171 }
172 
PrefValueFromRecommendedStore(const std::string & name) const173 bool PrefValueStore::PrefValueFromRecommendedStore(
174     const std::string& name) const {
175   return ControllingPrefStoreForPref(name) == RECOMMENDED_STORE;
176 }
177 
PrefValueFromStandaloneBrowserStore(const std::string & name) const178 bool PrefValueStore::PrefValueFromStandaloneBrowserStore(
179     const std::string& name) const {
180   return ControllingPrefStoreForPref(name) == STANDALONE_BROWSER_STORE;
181 }
182 
PrefValueFromDefaultStore(const std::string & name) const183 bool PrefValueStore::PrefValueFromDefaultStore(const std::string& name) const {
184   return ControllingPrefStoreForPref(name) == DEFAULT_STORE;
185 }
186 
PrefValueUserModifiable(const std::string & name) const187 bool PrefValueStore::PrefValueUserModifiable(const std::string& name) const {
188   PrefStoreType effective_store = ControllingPrefStoreForPref(name);
189   return effective_store >= USER_STORE ||
190          effective_store == INVALID_STORE;
191 }
192 
PrefValueExtensionModifiable(const std::string & name) const193 bool PrefValueStore::PrefValueExtensionModifiable(
194     const std::string& name) const {
195   PrefStoreType effective_store = ControllingPrefStoreForPref(name);
196   return effective_store >= EXTENSION_STORE ||
197          effective_store == INVALID_STORE;
198 }
199 
PrefValueStandaloneBrowserModifiable(const std::string & name) const200 bool PrefValueStore::PrefValueStandaloneBrowserModifiable(
201     const std::string& name) const {
202   PrefStoreType effective_store = ControllingPrefStoreForPref(name);
203   return effective_store >= STANDALONE_BROWSER_STORE ||
204          effective_store == INVALID_STORE;
205 }
206 
UpdateCommandLinePrefStore(PrefStore * command_line_prefs)207 void PrefValueStore::UpdateCommandLinePrefStore(PrefStore* command_line_prefs) {
208   InitPrefStore(COMMAND_LINE_STORE, command_line_prefs);
209 }
210 
IsInitializationComplete() const211 bool PrefValueStore::IsInitializationComplete() const {
212   for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) {
213     const PrefStore* pref_store = GetPrefStore(static_cast<PrefStoreType>(i));
214     if (pref_store && !pref_store->IsInitializationComplete()) {
215       return false;
216     }
217   }
218   return true;
219 }
220 
PrefValueInStore(const std::string & name,PrefValueStore::PrefStoreType store) const221 bool PrefValueStore::PrefValueInStore(
222     const std::string& name,
223     PrefValueStore::PrefStoreType store) const {
224   // Declare a temp Value* and call GetValueFromStore,
225   // ignoring the output value.
226   const base::Value* tmp_value = nullptr;
227   return GetValueFromStore(name, store, &tmp_value);
228 }
229 
PrefValueInStoreRange(const std::string & name,PrefValueStore::PrefStoreType first_checked_store,PrefValueStore::PrefStoreType last_checked_store) const230 bool PrefValueStore::PrefValueInStoreRange(
231     const std::string& name,
232     PrefValueStore::PrefStoreType first_checked_store,
233     PrefValueStore::PrefStoreType last_checked_store) const {
234   if (first_checked_store > last_checked_store) {
235     NOTREACHED();
236     return false;
237   }
238 
239   for (size_t i = first_checked_store;
240        i <= static_cast<size_t>(last_checked_store); ++i) {
241     if (PrefValueInStore(name, static_cast<PrefStoreType>(i)))
242       return true;
243   }
244   return false;
245 }
246 
GetValueFromStore(base::StringPiece name,PrefValueStore::PrefStoreType store_type,const base::Value ** out_value) const247 bool PrefValueStore::GetValueFromStore(base::StringPiece name,
248                                        PrefValueStore::PrefStoreType store_type,
249                                        const base::Value** out_value) const {
250   // Only return true if we find a value and it is the correct type, so stale
251   // values with the incorrect type will be ignored.
252   const PrefStore* store = GetPrefStore(static_cast<PrefStoreType>(store_type));
253   if (store && store->GetValue(name, out_value))
254     return true;
255 
256   // No valid value found for the given preference name: set the return value
257   // to false.
258   *out_value = nullptr;
259   return false;
260 }
261 
GetValueFromStoreWithType(base::StringPiece name,base::Value::Type type,PrefStoreType store,const base::Value ** out_value) const262 bool PrefValueStore::GetValueFromStoreWithType(
263     base::StringPiece name,
264     base::Value::Type type,
265     PrefStoreType store,
266     const base::Value** out_value) const {
267   if (GetValueFromStore(name, store, out_value)) {
268     if ((*out_value)->type() == type)
269       return true;
270 
271     LOG(WARNING) << "Expected type for " << name << " is " << type
272                  << " but got " << (*out_value)->type() << " in store "
273                  << store;
274   }
275 
276   *out_value = nullptr;
277   return false;
278 }
279 
OnPrefValueChanged(PrefValueStore::PrefStoreType type,const std::string & key)280 void PrefValueStore::OnPrefValueChanged(PrefValueStore::PrefStoreType type,
281                                         const std::string& key) {
282   NotifyPrefChanged(key, type);
283 }
284 
OnInitializationCompleted(PrefValueStore::PrefStoreType type,bool succeeded)285 void PrefValueStore::OnInitializationCompleted(
286     PrefValueStore::PrefStoreType type, bool succeeded) {
287   if (initialization_failed_)
288     return;
289   if (!succeeded) {
290     initialization_failed_ = true;
291     pref_notifier_->OnInitializationCompleted(false);
292     return;
293   }
294   CheckInitializationCompleted();
295 }
296 
InitPrefStore(PrefValueStore::PrefStoreType type,PrefStore * pref_store)297 void PrefValueStore::InitPrefStore(PrefValueStore::PrefStoreType type,
298                                    PrefStore* pref_store) {
299   pref_stores_[type].Initialize(this, pref_store, type);
300 }
301 
CheckInitializationCompleted()302 void PrefValueStore::CheckInitializationCompleted() {
303   if (initialization_failed_)
304     return;
305   for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) {
306     scoped_refptr<PrefStore> store =
307         GetPrefStore(static_cast<PrefStoreType>(i));
308     if (store.get() && !store->IsInitializationComplete())
309       return;
310   }
311   pref_notifier_->OnInitializationCompleted(true);
312 }
313