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