Add third_party/khronos to GN configs in ozone gbm
[chromium-blink-merge.git] / base / prefs / pref_service.h
blob1fc6c127c050f6780fb31b38c6ffef226218fa4e
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 // This provides a way to access the application's current preferences.
7 // Chromium settings and storage represent user-selected preferences and
8 // information and MUST not be extracted, overwritten or modified except
9 // through Chromium defined APIs.
11 #ifndef BASE_PREFS_PREF_SERVICE_H_
12 #define BASE_PREFS_PREF_SERVICE_H_
14 #include <set>
15 #include <string>
17 #include "base/callback.h"
18 #include "base/compiler_specific.h"
19 #include "base/containers/hash_tables.h"
20 #include "base/memory/ref_counted.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/observer_list.h"
23 #include "base/prefs/base_prefs_export.h"
24 #include "base/prefs/persistent_pref_store.h"
25 #include "base/threading/non_thread_safe.h"
26 #include "base/values.h"
28 class PrefNotifier;
29 class PrefNotifierImpl;
30 class PrefObserver;
31 class PrefRegistry;
32 class PrefValueStore;
33 class PrefStore;
35 namespace base {
36 class FilePath;
39 namespace subtle {
40 class PrefMemberBase;
41 class ScopedUserPrefUpdateBase;
44 // Base class for PrefServices. You can use the base class to read and
45 // interact with preferences, but not to register new preferences; for
46 // that see e.g. PrefRegistrySimple.
48 // Settings and storage accessed through this class represent
49 // user-selected preferences and information and MUST not be
50 // extracted, overwritten or modified except through the defined APIs.
51 class BASE_PREFS_EXPORT PrefService : public base::NonThreadSafe {
52 public:
53 enum PrefInitializationStatus {
54 INITIALIZATION_STATUS_WAITING,
55 INITIALIZATION_STATUS_SUCCESS,
56 INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE,
57 INITIALIZATION_STATUS_ERROR
60 // A helper class to store all the information associated with a preference.
61 class BASE_PREFS_EXPORT Preference {
62 public:
63 // The type of the preference is determined by the type with which it is
64 // registered. This type needs to be a boolean, integer, double, string,
65 // dictionary (a branch), or list. You shouldn't need to construct this on
66 // your own; use the PrefService::Register*Pref methods instead.
67 Preference(const PrefService* service,
68 const std::string& name,
69 base::Value::Type type);
70 ~Preference() {}
72 // Returns the name of the Preference (i.e., the key, e.g.,
73 // browser.window_placement).
74 const std::string name() const;
76 // Returns the registered type of the preference.
77 base::Value::Type GetType() const;
79 // Returns the value of the Preference, falling back to the registered
80 // default value if no other has been set.
81 const base::Value* GetValue() const;
83 // Returns the value recommended by the admin, if any.
84 const base::Value* GetRecommendedValue() const;
86 // Returns true if the Preference is managed, i.e. set by an admin policy.
87 // Since managed prefs have the highest priority, this also indicates
88 // whether the pref is actually being controlled by the policy setting.
89 bool IsManaged() const;
91 // Returns true if the Preference is controlled by the custodian of the
92 // supervised user. Since a supervised user is not expected to have an admin
93 // policy, this is the controlling pref if set.
94 bool IsManagedByCustodian() const;
96 // Returns true if the Preference is recommended, i.e. set by an admin
97 // policy but the user is allowed to change it.
98 bool IsRecommended() const;
100 // Returns true if the Preference has a value set by an extension, even if
101 // that value is being overridden by a higher-priority source.
102 bool HasExtensionSetting() const;
104 // Returns true if the Preference has a user setting, even if that value is
105 // being overridden by a higher-priority source.
106 bool HasUserSetting() const;
108 // Returns true if the Preference value is currently being controlled by an
109 // extension, and not by any higher-priority source.
110 bool IsExtensionControlled() const;
112 // Returns true if the Preference value is currently being controlled by a
113 // user setting, and not by any higher-priority source.
114 bool IsUserControlled() const;
116 // Returns true if the Preference is currently using its default value,
117 // and has not been set by any higher-priority source (even with the same
118 // value).
119 bool IsDefaultValue() const;
121 // Returns true if the user can change the Preference value, which is the
122 // case if no higher-priority source than the user store controls the
123 // Preference.
124 bool IsUserModifiable() const;
126 // Returns true if an extension can change the Preference value, which is
127 // the case if no higher-priority source than the extension store controls
128 // the Preference.
129 bool IsExtensionModifiable() const;
131 // Return the registration flags for this pref as a bitmask of
132 // PrefRegistry::PrefRegistrationFlags.
133 uint32 registration_flags() const { return registration_flags_; }
135 private:
136 friend class PrefService;
138 PrefValueStore* pref_value_store() const {
139 return pref_service_->pref_value_store_.get();
142 const std::string name_;
144 const base::Value::Type type_;
146 uint32 registration_flags_;
148 // Reference to the PrefService in which this pref was created.
149 const PrefService* pref_service_;
152 // You may wish to use PrefServiceFactory or one of its subclasses
153 // for simplified construction.
154 PrefService(
155 PrefNotifierImpl* pref_notifier,
156 PrefValueStore* pref_value_store,
157 PersistentPrefStore* user_prefs,
158 PrefRegistry* pref_registry,
159 base::Callback<void(PersistentPrefStore::PrefReadError)>
160 read_error_callback,
161 bool async);
162 virtual ~PrefService();
164 // Lands pending writes to disk. This should only be used if we need to save
165 // immediately (basically, during shutdown).
166 void CommitPendingWrite();
168 // Returns true if the preference for the given preference name is available
169 // and is managed.
170 bool IsManagedPreference(const std::string& pref_name) const;
172 // Returns true if the preference for the given preference name is available
173 // and is controlled by the parent/guardian of the child Account.
174 bool IsPreferenceManagedByCustodian(const std::string& pref_name) const;
176 // Returns |true| if a preference with the given name is available and its
177 // value can be changed by the user.
178 bool IsUserModifiablePreference(const std::string& pref_name) const;
180 // Look up a preference. Returns NULL if the preference is not
181 // registered.
182 const PrefService::Preference* FindPreference(const std::string& path) const;
184 // If the path is valid and the value at the end of the path matches the type
185 // specified, it will return the specified value. Otherwise, the default
186 // value (set when the pref was registered) will be returned.
187 bool GetBoolean(const std::string& path) const;
188 int GetInteger(const std::string& path) const;
189 double GetDouble(const std::string& path) const;
190 std::string GetString(const std::string& path) const;
191 base::FilePath GetFilePath(const std::string& path) const;
193 // Returns the branch if it exists, or the registered default value otherwise.
194 // Note that |path| must point to a registered preference. In that case, these
195 // functions will never return NULL.
196 const base::DictionaryValue* GetDictionary(const std::string& path) const;
197 const base::ListValue* GetList(const std::string& path) const;
199 // Removes a user pref and restores the pref to its default value.
200 void ClearPref(const std::string& path);
202 // If the path is valid (i.e., registered), update the pref value in the user
203 // prefs.
204 // To set the value of dictionary or list values in the pref tree use
205 // Set(), but to modify the value of a dictionary or list use either
206 // ListPrefUpdate or DictionaryPrefUpdate from scoped_user_pref_update.h.
207 void Set(const std::string& path, const base::Value& value);
208 void SetBoolean(const std::string& path, bool value);
209 void SetInteger(const std::string& path, int value);
210 void SetDouble(const std::string& path, double value);
211 void SetString(const std::string& path, const std::string& value);
212 void SetFilePath(const std::string& path, const base::FilePath& value);
214 // Int64 helper methods that actually store the given value as a string.
215 // Note that if obtaining the named value via GetDictionary or GetList, the
216 // Value type will be TYPE_STRING.
217 void SetInt64(const std::string& path, int64 value);
218 int64 GetInt64(const std::string& path) const;
220 // As above, but for unsigned values.
221 void SetUint64(const std::string& path, uint64 value);
222 uint64 GetUint64(const std::string& path) const;
224 // Returns the value of the given preference, from the user pref store. If
225 // the preference is not set in the user pref store, returns NULL.
226 const base::Value* GetUserPrefValue(const std::string& path) const;
228 // Changes the default value for a preference. Takes ownership of |value|.
230 // Will cause a pref change notification to be fired if this causes
231 // the effective value to change.
232 void SetDefaultPrefValue(const std::string& path, base::Value* value);
234 // Returns the default value of the given preference. |path| must point to a
235 // registered preference. In that case, will never return NULL.
236 const base::Value* GetDefaultPrefValue(const std::string& path) const;
238 // Returns true if a value has been set for the specified path.
239 // NOTE: this is NOT the same as FindPreference. In particular
240 // FindPreference returns whether RegisterXXX has been invoked, where as
241 // this checks if a value exists for the path.
242 bool HasPrefPath(const std::string& path) const;
244 // Returns a dictionary with effective preference values.
245 scoped_ptr<base::DictionaryValue> GetPreferenceValues() const;
247 // Returns a dictionary with effective preference values, omitting prefs that
248 // are at their default values.
249 scoped_ptr<base::DictionaryValue> GetPreferenceValuesOmitDefaults() const;
251 // Returns a dictionary with effective preference values. Contrary to
252 // GetPreferenceValues(), the paths of registered preferences are not split on
253 // '.' characters. If a registered preference stores a dictionary, however,
254 // the hierarchical structure inside the preference will be preserved.
255 // For example, if "foo.bar" is a registered preference, the result could look
256 // like this:
257 // {"foo.bar": {"a": {"b": true}}}.
258 scoped_ptr<base::DictionaryValue> GetPreferenceValuesWithoutPathExpansion()
259 const;
261 bool ReadOnly() const;
263 PrefInitializationStatus GetInitializationStatus() const;
265 // Tell our PrefValueStore to update itself to |command_line_store|.
266 // Takes ownership of the store.
267 virtual void UpdateCommandLinePrefStore(PrefStore* command_line_store);
269 // We run the callback once, when initialization completes. The bool
270 // parameter will be set to true for successful initialization,
271 // false for unsuccessful.
272 void AddPrefInitObserver(base::Callback<void(bool)> callback);
274 // Returns the PrefRegistry object for this service. You should not
275 // use this; the intent is for no registrations to take place after
276 // PrefService has been constructed.
278 // Instead of using this method, the recommended approach is to
279 // register all preferences for a class Xyz up front in a static
280 // Xyz::RegisterPrefs function, which gets invoked early in the
281 // application's start-up, before a PrefService is created.
283 // As an example, prefs registration in Chrome is triggered by the
284 // functions chrome::RegisterPrefs (for global preferences) and
285 // chrome::RegisterProfilePrefs (for user-specific preferences)
286 // implemented in chrome/browser/prefs/browser_prefs.cc.
287 PrefRegistry* DeprecatedGetPrefRegistry();
289 protected:
290 // The PrefNotifier handles registering and notifying preference observers.
291 // It is created and owned by this PrefService. Subclasses may access it for
292 // unit testing.
293 scoped_ptr<PrefNotifierImpl> pref_notifier_;
295 // The PrefValueStore provides prioritized preference values. It is owned by
296 // this PrefService. Subclasses may access it for unit testing.
297 scoped_ptr<PrefValueStore> pref_value_store_;
299 scoped_refptr<PrefRegistry> pref_registry_;
301 // Pref Stores and profile that we passed to the PrefValueStore.
302 scoped_refptr<PersistentPrefStore> user_pref_store_;
304 // Callback to call when a read error occurs.
305 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_;
307 private:
308 // Hash map expected to be fastest here since it minimises expensive
309 // string comparisons. Order is unimportant, and deletions are rare.
310 // Confirmed on Android where this speeded Chrome startup by roughly 50ms
311 // vs. std::map, and by roughly 180ms vs. std::set of Preference pointers.
312 typedef base::hash_map<std::string, Preference> PreferenceMap;
314 // Give access to ReportUserPrefChanged() and GetMutableUserPref().
315 friend class subtle::ScopedUserPrefUpdateBase;
316 friend class PrefServiceTest_WriteablePrefStoreFlags_Test;
318 // Registration of pref change observers must be done using the
319 // PrefChangeRegistrar, which is declared as a friend here to grant it
320 // access to the otherwise protected members Add/RemovePrefObserver.
321 // PrefMember registers for preferences changes notification directly to
322 // avoid the storage overhead of the registrar, so its base class must be
323 // declared as a friend, too.
324 friend class PrefChangeRegistrar;
325 friend class subtle::PrefMemberBase;
327 // These are protected so they can only be accessed by the friend
328 // classes listed above.
330 // If the pref at the given path changes, we call the observer's
331 // OnPreferenceChanged method. Note that observers should not call
332 // these methods directly but rather use a PrefChangeRegistrar to
333 // make sure the observer gets cleaned up properly.
335 // Virtual for testing.
336 virtual void AddPrefObserver(const std::string& path, PrefObserver* obs);
337 virtual void RemovePrefObserver(const std::string& path, PrefObserver* obs);
339 // Sends notification of a changed preference. This needs to be called by
340 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed.
341 void ReportUserPrefChanged(const std::string& key);
343 // Sets the value for this pref path in the user pref store and informs the
344 // PrefNotifier of the change.
345 void SetUserPrefValue(const std::string& path, base::Value* new_value);
347 // Load preferences from storage, attempting to diagnose and handle errors.
348 // This should only be called from the constructor.
349 void InitFromStorage(bool async);
351 // Used to set the value of dictionary or list values in the user pref store.
352 // This will create a dictionary or list if one does not exist in the user
353 // pref store. This method returns NULL only if you're requesting an
354 // unregistered pref or a non-dict/non-list pref.
355 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and
356 // |path| must point to a registered preference of type |type|.
357 // Ownership of the returned value remains at the user pref store.
358 base::Value* GetMutableUserPref(const std::string& path,
359 base::Value::Type type);
361 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(),
362 // it has been added for performance. If is faster because it does
363 // not need to find or create a Preference object to get the
364 // value (GetValue() calls back though the preference service to
365 // actually get the value.).
366 const base::Value* GetPreferenceValue(const std::string& path) const;
368 // Local cache of registered Preference objects. The pref_registry_
369 // is authoritative with respect to what the types and default values
370 // of registered preferences are.
371 mutable PreferenceMap prefs_map_;
373 DISALLOW_COPY_AND_ASSIGN(PrefService);
376 #endif // BASE_PREFS_PREF_SERVICE_H_