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