blob: 70a92bc6c67a98cb449cd49d914c4898b17bd7f8 [file] [log] [blame]
brettw58cd1f12016-01-30 05:56:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettw066508682016-02-03 08:22:025#ifndef COMPONENTS_PREFS_PREF_VALUE_STORE_H_
6#define COMPONENTS_PREFS_PREF_VALUE_STORE_H_
brettw58cd1f12016-01-30 05:56:057
Lei Zhang2cdf35c2019-03-29 01:33:078#include <functional>
brettw58cd1f12016-01-30 05:56:059#include <map>
François Degrosee2f030d2017-12-04 00:29:0110#include <memory>
brettw58cd1f12016-01-30 05:56:0511#include <string>
danakj6d0446e52017-04-05 16:22:2912#include <type_traits>
brettw58cd1f12016-01-30 05:56:0513#include <vector>
14
15#include "base/callback.h"
16#include "base/macros.h"
17#include "base/memory/ref_counted.h"
brettw58cd1f12016-01-30 05:56:0518#include "base/values.h"
brettwf00b9b42016-02-01 22:11:3819#include "components/prefs/pref_store.h"
Brett Wilson5c6cf262017-09-09 02:05:5420#include "components/prefs/prefs_export.h"
brettw58cd1f12016-01-30 05:56:0521
Sam McNally538fca1e2017-07-14 03:10:4322class PersistentPrefStore;
brettw58cd1f12016-01-30 05:56:0523class PrefNotifier;
Sam McNally538fca1e2017-07-14 03:10:4324class PrefRegistry;
brettw58cd1f12016-01-30 05:56:0525class PrefStore;
26
27// The PrefValueStore manages various sources of values for Preferences
28// (e.g., configuration policies, extensions, and user settings). It returns
29// the value of a Preference from the source with the highest priority, and
30// allows setting user-defined values for preferences that are not managed.
31//
32// Unless otherwise explicitly noted, all of the methods of this class must
33// be called on the UI thread.
brettw066508682016-02-03 08:22:0234class COMPONENTS_PREFS_EXPORT PrefValueStore {
brettw58cd1f12016-01-30 05:56:0535 public:
36 typedef base::Callback<void(const std::string&)> PrefChangedCallback;
37
Johan Tibell26b550b2017-06-20 04:59:1338 // Delegate used to observe certain events in the |PrefValueStore|'s lifetime.
39 class Delegate {
40 public:
41 virtual ~Delegate() {}
42
43 // Called by the PrefValueStore constructor with the PrefStores passed to
44 // it.
45 virtual void Init(PrefStore* managed_prefs,
46 PrefStore* supervised_user_prefs,
47 PrefStore* extension_prefs,
48 PrefStore* command_line_prefs,
49 PrefStore* user_prefs,
50 PrefStore* recommended_prefs,
51 PrefStore* default_prefs,
52 PrefNotifier* pref_notifier) = 0;
53
Sam McNallyaa67d7f22017-09-04 03:34:0654 virtual void InitIncognitoUserPrefs(
55 scoped_refptr<PersistentPrefStore> incognito_user_prefs_overlay,
56 scoped_refptr<PersistentPrefStore> incognito_user_prefs_underlay,
57 const std::vector<const char*>& overlay_pref_names) = 0;
Sam McNally538fca1e2017-07-14 03:10:4358
59 virtual void InitPrefRegistry(PrefRegistry* pref_registry) = 0;
60
Johan Tibell26b550b2017-06-20 04:59:1361 // Called whenever PrefValueStore::UpdateCommandLinePrefStore is called,
62 // with the same argument.
63 virtual void UpdateCommandLinePrefStore(PrefStore* command_line_prefs) = 0;
64 };
65
tibell11141bd2017-03-10 00:16:2966 // PrefStores must be listed here in order from highest to lowest priority.
67 // MANAGED contains all managed preference values that are provided by
68 // mandatory policies (e.g. Windows Group Policy or cloud policy).
69 // SUPERVISED_USER contains preferences that are valid for supervised users.
70 // EXTENSION contains preference values set by extensions.
71 // COMMAND_LINE contains preference values set by command-line switches.
72 // USER contains all user-set preference values.
73 // RECOMMENDED contains all preferences that are provided by recommended
74 // policies.
75 // DEFAULT contains all application default preference values.
76 enum PrefStoreType {
77 // INVALID_STORE is not associated with an actual PrefStore but used as
78 // an invalid marker, e.g. as a return value.
79 INVALID_STORE = -1,
80 MANAGED_STORE = 0,
81 SUPERVISED_USER_STORE,
82 EXTENSION_STORE,
83 COMMAND_LINE_STORE,
84 USER_STORE,
85 RECOMMENDED_STORE,
86 DEFAULT_STORE,
87 PREF_STORE_TYPE_MAX = DEFAULT_STORE
88 };
89
brettw58cd1f12016-01-30 05:56:0590 // In decreasing order of precedence:
91 // |managed_prefs| contains all preferences from mandatory policies.
92 // |supervised_user_prefs| contains all preferences from supervised user
93 // settings, i.e. settings configured for a supervised user by their
94 // custodian.
95 // |extension_prefs| contains preference values set by extensions.
96 // |command_line_prefs| contains preference values set by command-line
97 // switches.
98 // |user_prefs| contains all user-set preference values.
99 // |recommended_prefs| contains all preferences from recommended policies.
100 // |default_prefs| contains application-default preference values. It must
101 // be non-null if any preferences are to be registered.
102 //
103 // |pref_notifier| facilitates broadcasting preference change notifications
104 // to the world.
105 PrefValueStore(PrefStore* managed_prefs,
106 PrefStore* supervised_user_prefs,
107 PrefStore* extension_prefs,
108 PrefStore* command_line_prefs,
109 PrefStore* user_prefs,
110 PrefStore* recommended_prefs,
111 PrefStore* default_prefs,
Johan Tibell26b550b2017-06-20 04:59:13112 PrefNotifier* pref_notifier,
113 std::unique_ptr<Delegate> delegate = nullptr);
brettw58cd1f12016-01-30 05:56:05114 virtual ~PrefValueStore();
115
116 // Creates a clone of this PrefValueStore with PrefStores overwritten
117 // by the parameters passed, if unequal NULL.
Johan Tibell26b550b2017-06-20 04:59:13118 //
119 // The new PrefValueStore is passed the |delegate| in its constructor.
François Degrosee2f030d2017-12-04 00:29:01120 std::unique_ptr<PrefValueStore> CloneAndSpecialize(
Johan Tibell26b550b2017-06-20 04:59:13121 PrefStore* managed_prefs,
122 PrefStore* supervised_user_prefs,
123 PrefStore* extension_prefs,
124 PrefStore* command_line_prefs,
125 PrefStore* user_prefs,
126 PrefStore* recommended_prefs,
127 PrefStore* default_prefs,
128 PrefNotifier* pref_notifier,
129 std::unique_ptr<Delegate> delegate = nullptr);
brettw58cd1f12016-01-30 05:56:05130
131 // A PrefValueStore can have exactly one callback that is directly
132 // notified of preferences changing in the store. This does not
133 // filter through the PrefNotifier mechanism, which may not forward
134 // certain changes (e.g. unregistered prefs).
135 void set_callback(const PrefChangedCallback& callback);
136
137 // Gets the value for the given preference name that has the specified value
138 // type. Values stored in a PrefStore that have the matching |name| but
139 // a non-matching |type| are silently skipped. Returns true if a valid value
140 // was found in any of the available PrefStores. Most callers should use
141 // Preference::GetValue() instead of calling this method directly.
142 bool GetValue(const std::string& name,
143 base::Value::Type type,
144 const base::Value** out_value) const;
145
146 // Gets the recommended value for the given preference name that has the
147 // specified value type. A value stored in the recommended PrefStore that has
148 // the matching |name| but a non-matching |type| is silently ignored. Returns
149 // true if a valid value was found. Most callers should use
150 // Preference::GetRecommendedValue() instead of calling this method directly.
151 bool GetRecommendedValue(const std::string& name,
152 base::Value::Type type,
153 const base::Value** out_value) const;
154
155 // These methods return true if a preference with the given name is in the
156 // indicated pref store, even if that value is currently being overridden by
157 // a higher-priority source.
158 bool PrefValueInManagedStore(const std::string& name) const;
159 bool PrefValueInSupervisedStore(const std::string& name) const;
160 bool PrefValueInExtensionStore(const std::string& name) const;
161 bool PrefValueInUserStore(const std::string& name) const;
162
163 // These methods return true if a preference with the given name is actually
164 // being controlled by the indicated pref store and not being overridden by
165 // a higher-priority source.
166 bool PrefValueFromExtensionStore(const std::string& name) const;
167 bool PrefValueFromUserStore(const std::string& name) const;
168 bool PrefValueFromRecommendedStore(const std::string& name) const;
169 bool PrefValueFromDefaultStore(const std::string& name) const;
170
171 // Check whether a Preference value is modifiable by the user, i.e. whether
172 // there is no higher-priority source controlling it.
173 bool PrefValueUserModifiable(const std::string& name) const;
174
175 // Check whether a Preference value is modifiable by an extension, i.e.
176 // whether there is no higher-priority source controlling it.
177 bool PrefValueExtensionModifiable(const std::string& name) const;
178
179 // Update the command line PrefStore with |command_line_prefs|.
180 void UpdateCommandLinePrefStore(PrefStore* command_line_prefs);
181
Sam McNallyf4dab612017-08-16 03:06:33182 bool IsInitializationComplete() const;
183
Min Qincaa1e2e2018-09-14 20:56:48184 // Check whether a particular type of PrefStore exists.
185 bool HasPrefStore(PrefStoreType type) const;
186
brettw58cd1f12016-01-30 05:56:05187 private:
brettw58cd1f12016-01-30 05:56:05188 // Keeps a PrefStore reference on behalf of the PrefValueStore and monitors
189 // the PrefStore for changes, forwarding notifications to PrefValueStore. This
190 // indirection is here for the sake of disambiguating notifications from the
191 // individual PrefStores.
192 class PrefStoreKeeper : public PrefStore::Observer {
193 public:
194 PrefStoreKeeper();
195 ~PrefStoreKeeper() override;
196
197 // Takes ownership of |pref_store|.
198 void Initialize(PrefValueStore* store,
199 PrefStore* pref_store,
200 PrefStoreType type);
201
202 PrefStore* store() { return pref_store_.get(); }
203 const PrefStore* store() const { return pref_store_.get(); }
204
205 private:
206 // PrefStore::Observer implementation.
207 void OnPrefValueChanged(const std::string& key) override;
208 void OnInitializationCompleted(bool succeeded) override;
209
210 // PrefValueStore this keeper is part of.
211 PrefValueStore* pref_value_store_;
212
213 // The PrefStore managed by this keeper.
214 scoped_refptr<PrefStore> pref_store_;
215
216 // Type of the pref store.
217 PrefStoreType type_;
218
219 DISALLOW_COPY_AND_ASSIGN(PrefStoreKeeper);
220 };
221
222 typedef std::map<std::string, base::Value::Type> PrefTypeMap;
223
224 // Returns true if the preference with the given name has a value in the
225 // given PrefStoreType, of the same value type as the preference was
226 // registered with.
227 bool PrefValueInStore(const std::string& name, PrefStoreType store) const;
228
229 // Returns true if a preference has an explicit value in any of the
230 // stores in the range specified by |first_checked_store| and
231 // |last_checked_store|, even if that value is currently being
232 // overridden by a higher-priority store.
233 bool PrefValueInStoreRange(const std::string& name,
234 PrefStoreType first_checked_store,
235 PrefStoreType last_checked_store) const;
236
237 // Returns the pref store type identifying the source that controls the
238 // Preference identified by |name|. If none of the sources has a value,
239 // INVALID_STORE is returned. In practice, the default PrefStore
240 // should always have a value for any registered preferencem, so INVALID_STORE
241 // indicates an error.
242 PrefStoreType ControllingPrefStoreForPref(const std::string& name) const;
243
244 // Get a value from the specified |store|.
245 bool GetValueFromStore(const std::string& name,
246 PrefStoreType store,
247 const base::Value** out_value) const;
248
249 // Get a value from the specified |store| if its |type| matches.
250 bool GetValueFromStoreWithType(const std::string& name,
251 base::Value::Type type,
252 PrefStoreType store,
253 const base::Value** out_value) const;
254
255 // Called upon changes in individual pref stores in order to determine whether
256 // the user-visible pref value has changed. Triggers the change notification
257 // if the effective value of the preference has changed, or if the store
258 // controlling the pref has changed.
259 void NotifyPrefChanged(const std::string& path, PrefStoreType new_store);
260
261 // Called from the PrefStoreKeeper implementation when a pref value for |key|
262 // changed in the pref store for |type|.
263 void OnPrefValueChanged(PrefStoreType type, const std::string& key);
264
265 // Handle the event that the store for |type| has completed initialization.
266 void OnInitializationCompleted(PrefStoreType type, bool succeeded);
267
268 // Initializes a pref store keeper. Sets up a PrefStoreKeeper that will take
269 // ownership of the passed |pref_store|.
270 void InitPrefStore(PrefStoreType type, PrefStore* pref_store);
271
272 // Checks whether initialization is completed and tells the notifier if that
273 // is the case.
274 void CheckInitializationCompleted();
275
276 // Get the PrefStore pointer for the given type. May return NULL if there is
277 // no PrefStore for that type.
278 PrefStore* GetPrefStore(PrefStoreType type) {
279 return pref_stores_[type].store();
280 }
281 const PrefStore* GetPrefStore(PrefStoreType type) const {
282 return pref_stores_[type].store();
283 }
284
285 // Keeps the PrefStore references in order of precedence.
286 PrefStoreKeeper pref_stores_[PREF_STORE_TYPE_MAX + 1];
287
288 PrefChangedCallback pref_changed_callback_;
289
290 // Used for generating notifications. This is a weak reference,
291 // since the notifier is owned by the corresponding PrefService.
292 PrefNotifier* pref_notifier_;
293
294 // A mapping of preference names to their registered types.
295 PrefTypeMap pref_types_;
296
297 // True if not all of the PrefStores were initialized successfully.
298 bool initialization_failed_;
299
Johan Tibell26b550b2017-06-20 04:59:13300 // Might be null.
301 std::unique_ptr<Delegate> delegate_;
302
brettw58cd1f12016-01-30 05:56:05303 DISALLOW_COPY_AND_ASSIGN(PrefValueStore);
304};
305
tibell11141bd2017-03-10 00:16:29306namespace std {
307
308template <>
309struct hash<PrefValueStore::PrefStoreType> {
310 size_t operator()(PrefValueStore::PrefStoreType type) const {
311 return std::hash<
danakj6d0446e52017-04-05 16:22:29312 std::underlying_type<PrefValueStore::PrefStoreType>::type>()(type);
tibell11141bd2017-03-10 00:16:29313 }
314};
315
316} // namespace std
317
brettw066508682016-02-03 08:22:02318#endif // COMPONENTS_PREFS_PREF_VALUE_STORE_H_