1 // 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.
5 #ifndef BASE_PREFS_PREF_VALUE_STORE_H_
6 #define BASE_PREFS_PREF_VALUE_STORE_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/prefs/base_prefs_export.h"
17 #include "base/prefs/pref_store.h"
18 #include "base/values.h"
23 // The PrefValueStore manages various sources of values for Preferences
24 // (e.g., configuration policies, extensions, and user settings). It returns
25 // the value of a Preference from the source with the highest priority, and
26 // allows setting user-defined values for preferences that are not managed.
28 // Unless otherwise explicitly noted, all of the methods of this class must
29 // be called on the UI thread.
30 class BASE_PREFS_EXPORT PrefValueStore
{
32 typedef base::Callback
<void(const std::string
&)> PrefChangedCallback
;
34 // In decreasing order of precedence:
35 // |managed_prefs| contains all preferences from mandatory policies.
36 // |supervised_user_prefs| contains all preferences from supervised user
37 // settings, i.e. settings configured for a supervised user by their
39 // |extension_prefs| contains preference values set by extensions.
40 // |command_line_prefs| contains preference values set by command-line
42 // |user_prefs| contains all user-set preference values.
43 // |recommended_prefs| contains all preferences from recommended policies.
44 // |default_prefs| contains application-default preference values. It must
45 // be non-null if any preferences are to be registered.
47 // |pref_notifier| facilitates broadcasting preference change notifications
49 PrefValueStore(PrefStore
* managed_prefs
,
50 PrefStore
* supervised_user_prefs
,
51 PrefStore
* extension_prefs
,
52 PrefStore
* command_line_prefs
,
53 PrefStore
* user_prefs
,
54 PrefStore
* recommended_prefs
,
55 PrefStore
* default_prefs
,
56 PrefNotifier
* pref_notifier
);
57 virtual ~PrefValueStore();
59 // Creates a clone of this PrefValueStore with PrefStores overwritten
60 // by the parameters passed, if unequal NULL.
61 PrefValueStore
* CloneAndSpecialize(PrefStore
* managed_prefs
,
62 PrefStore
* supervised_user_prefs
,
63 PrefStore
* extension_prefs
,
64 PrefStore
* command_line_prefs
,
65 PrefStore
* user_prefs
,
66 PrefStore
* recommended_prefs
,
67 PrefStore
* default_prefs
,
68 PrefNotifier
* pref_notifier
);
70 // A PrefValueStore can have exactly one callback that is directly
71 // notified of preferences changing in the store. This does not
72 // filter through the PrefNotifier mechanism, which may not forward
73 // certain changes (e.g. unregistered prefs).
74 void set_callback(const PrefChangedCallback
& callback
);
76 // Gets the value for the given preference name that has the specified value
77 // type. Values stored in a PrefStore that have the matching |name| but
78 // a non-matching |type| are silently skipped. Returns true if a valid value
79 // was found in any of the available PrefStores. Most callers should use
80 // Preference::GetValue() instead of calling this method directly.
81 bool GetValue(const std::string
& name
,
82 base::Value::Type type
,
83 const base::Value
** out_value
) const;
85 // Gets the recommended value for the given preference name that has the
86 // specified value type. A value stored in the recommended PrefStore that has
87 // the matching |name| but a non-matching |type| is silently ignored. Returns
88 // true if a valid value was found. Most callers should use
89 // Preference::GetRecommendedValue() instead of calling this method directly.
90 bool GetRecommendedValue(const std::string
& name
,
91 base::Value::Type type
,
92 const base::Value
** out_value
) const;
94 // These methods return true if a preference with the given name is in the
95 // indicated pref store, even if that value is currently being overridden by
96 // a higher-priority source.
97 bool PrefValueInManagedStore(const std::string
& name
) const;
98 bool PrefValueInSupervisedStore(const std::string
& name
) const;
99 bool PrefValueInExtensionStore(const std::string
& name
) const;
100 bool PrefValueInUserStore(const std::string
& name
) const;
102 // These methods return true if a preference with the given name is actually
103 // being controlled by the indicated pref store and not being overridden by
104 // a higher-priority source.
105 bool PrefValueFromExtensionStore(const std::string
& name
) const;
106 bool PrefValueFromUserStore(const std::string
& name
) const;
107 bool PrefValueFromRecommendedStore(const std::string
& name
) const;
108 bool PrefValueFromDefaultStore(const std::string
& name
) const;
110 // Check whether a Preference value is modifiable by the user, i.e. whether
111 // there is no higher-priority source controlling it.
112 bool PrefValueUserModifiable(const std::string
& name
) const;
114 // Check whether a Preference value is modifiable by an extension, i.e.
115 // whether there is no higher-priority source controlling it.
116 bool PrefValueExtensionModifiable(const std::string
& name
) const;
118 // Update the command line PrefStore with |command_line_prefs|.
119 void UpdateCommandLinePrefStore(PrefStore
* command_line_prefs
);
122 // PrefStores must be listed here in order from highest to lowest priority.
123 // MANAGED contains all managed preference values that are provided by
124 // mandatory policies (e.g. Windows Group Policy or cloud policy).
125 // SUPERVISED_USER contains preferences that are valid for supervised users.
126 // EXTENSION contains preference values set by extensions.
127 // COMMAND_LINE contains preference values set by command-line switches.
128 // USER contains all user-set preference values.
129 // RECOMMENDED contains all preferences that are provided by recommended
131 // DEFAULT contains all application default preference values.
133 // INVALID_STORE is not associated with an actual PrefStore but used as
134 // an invalid marker, e.g. as a return value.
137 SUPERVISED_USER_STORE
,
143 PREF_STORE_TYPE_MAX
= DEFAULT_STORE
146 // Keeps a PrefStore reference on behalf of the PrefValueStore and monitors
147 // the PrefStore for changes, forwarding notifications to PrefValueStore. This
148 // indirection is here for the sake of disambiguating notifications from the
149 // individual PrefStores.
150 class PrefStoreKeeper
: public PrefStore::Observer
{
153 ~PrefStoreKeeper() override
;
155 // Takes ownership of |pref_store|.
156 void Initialize(PrefValueStore
* store
,
157 PrefStore
* pref_store
,
160 PrefStore
* store() { return pref_store_
.get(); }
161 const PrefStore
* store() const { return pref_store_
.get(); }
164 // PrefStore::Observer implementation.
165 void OnPrefValueChanged(const std::string
& key
) override
;
166 void OnInitializationCompleted(bool succeeded
) override
;
168 // PrefValueStore this keeper is part of.
169 PrefValueStore
* pref_value_store_
;
171 // The PrefStore managed by this keeper.
172 scoped_refptr
<PrefStore
> pref_store_
;
174 // Type of the pref store.
177 DISALLOW_COPY_AND_ASSIGN(PrefStoreKeeper
);
180 typedef std::map
<std::string
, base::Value::Type
> PrefTypeMap
;
182 friend class PrefValueStorePolicyRefreshTest
;
183 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest
, TestPolicyRefresh
);
184 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest
,
185 TestRefreshPolicyPrefsCompletion
);
186 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest
,
187 TestConcurrentPolicyRefresh
);
189 // Returns true if the preference with the given name has a value in the
190 // given PrefStoreType, of the same value type as the preference was
192 bool PrefValueInStore(const std::string
& name
, PrefStoreType store
) const;
194 // Returns true if a preference has an explicit value in any of the
195 // stores in the range specified by |first_checked_store| and
196 // |last_checked_store|, even if that value is currently being
197 // overridden by a higher-priority store.
198 bool PrefValueInStoreRange(const std::string
& name
,
199 PrefStoreType first_checked_store
,
200 PrefStoreType last_checked_store
) const;
202 // Returns the pref store type identifying the source that controls the
203 // Preference identified by |name|. If none of the sources has a value,
204 // INVALID_STORE is returned. In practice, the default PrefStore
205 // should always have a value for any registered preferencem, so INVALID_STORE
206 // indicates an error.
207 PrefStoreType
ControllingPrefStoreForPref(const std::string
& name
) const;
209 // Get a value from the specified |store|.
210 bool GetValueFromStore(const std::string
& name
,
212 const base::Value
** out_value
) const;
214 // Get a value from the specified |store| if its |type| matches.
215 bool GetValueFromStoreWithType(const std::string
& name
,
216 base::Value::Type type
,
218 const base::Value
** out_value
) const;
220 // Called upon changes in individual pref stores in order to determine whether
221 // the user-visible pref value has changed. Triggers the change notification
222 // if the effective value of the preference has changed, or if the store
223 // controlling the pref has changed.
224 void NotifyPrefChanged(const std::string
& path
, PrefStoreType new_store
);
226 // Called from the PrefStoreKeeper implementation when a pref value for |key|
227 // changed in the pref store for |type|.
228 void OnPrefValueChanged(PrefStoreType type
, const std::string
& key
);
230 // Handle the event that the store for |type| has completed initialization.
231 void OnInitializationCompleted(PrefStoreType type
, bool succeeded
);
233 // Initializes a pref store keeper. Sets up a PrefStoreKeeper that will take
234 // ownership of the passed |pref_store|.
235 void InitPrefStore(PrefStoreType type
, PrefStore
* pref_store
);
237 // Checks whether initialization is completed and tells the notifier if that
239 void CheckInitializationCompleted();
241 // Get the PrefStore pointer for the given type. May return NULL if there is
242 // no PrefStore for that type.
243 PrefStore
* GetPrefStore(PrefStoreType type
) {
244 return pref_stores_
[type
].store();
246 const PrefStore
* GetPrefStore(PrefStoreType type
) const {
247 return pref_stores_
[type
].store();
250 // Keeps the PrefStore references in order of precedence.
251 PrefStoreKeeper pref_stores_
[PREF_STORE_TYPE_MAX
+ 1];
253 PrefChangedCallback pref_changed_callback_
;
255 // Used for generating notifications. This is a weak reference,
256 // since the notifier is owned by the corresponding PrefService.
257 PrefNotifier
* pref_notifier_
;
259 // A mapping of preference names to their registered types.
260 PrefTypeMap pref_types_
;
262 // True if not all of the PrefStores were initialized successfully.
263 bool initialization_failed_
;
265 DISALLOW_COPY_AND_ASSIGN(PrefValueStore
);
268 #endif // BASE_PREFS_PREF_VALUE_STORE_H_