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