OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This provides a way to access the application's current preferences. | 5 // This provides a way to access the application's current preferences. |
6 | 6 |
7 // Chromium settings and storage represent user-selected preferences and | 7 // Chromium settings and storage represent user-selected preferences and |
8 // information and MUST not be extracted, overwritten or modified except | 8 // information and MUST not be extracted, overwritten or modified except |
9 // through Chromium defined APIs. | 9 // through Chromium defined APIs. |
10 | 10 |
11 #ifndef CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 11 #ifndef CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
12 #define CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 12 #define CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
13 | 13 |
14 #include <set> | 14 #include <set> |
15 #include <string> | 15 #include <string> |
16 | 16 |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
19 #include "base/threading/non_thread_safe.h" | 19 #include "base/threading/non_thread_safe.h" |
20 #include "base/values.h" | 20 #include "chrome/browser/api/prefs/pref_service_base.h" |
21 | 21 |
22 class CommandLine; | 22 class CommandLine; |
23 class DefaultPrefStore; | 23 class DefaultPrefStore; |
24 class FilePath; | |
25 class PersistentPrefStore; | 24 class PersistentPrefStore; |
26 class PrefModelAssociator; | 25 class PrefModelAssociator; |
27 class PrefNotifier; | 26 class PrefNotifier; |
28 class PrefNotifierImpl; | 27 class PrefNotifierImpl; |
29 class PrefStore; | 28 class PrefStore; |
30 class PrefValueStore; | 29 class PrefValueStore; |
31 | 30 |
32 namespace content { | 31 namespace content { |
33 class NotificationObserver; | 32 class NotificationObserver; |
erikwright (departed)
2012/08/16 15:12:40
I think this can be removed since it is part of th
Jói
2012/08/16 15:35:57
Done.
| |
34 } | 33 } |
35 | 34 |
36 namespace syncer { | 35 namespace syncer { |
37 class SyncableService; | 36 class SyncableService; |
38 } | 37 } |
39 | 38 |
40 namespace policy { | 39 namespace policy { |
41 class PolicyService; | 40 class PolicyService; |
42 } | 41 } |
43 | 42 |
44 namespace subtle { | 43 namespace subtle { |
45 class PrefMemberBase; | |
46 class ScopedUserPrefUpdateBase; | 44 class ScopedUserPrefUpdateBase; |
47 }; | 45 }; |
48 | 46 |
49 class PrefService : public base::NonThreadSafe { | 47 class PrefService : public PrefServiceBase, public base::NonThreadSafe { |
50 public: | 48 public: |
51 // Enum used when registering preferences to determine if it should be synced | |
52 // or not. This is only used for profile prefs, not local state prefs. | |
53 // See the Register*Pref methods for profile prefs below. | |
54 enum PrefSyncStatus { | |
55 UNSYNCABLE_PREF, | |
56 SYNCABLE_PREF | |
57 }; | |
58 | |
59 enum PrefInitializationStatus { | 49 enum PrefInitializationStatus { |
60 INITIALIZATION_STATUS_WAITING, | 50 INITIALIZATION_STATUS_WAITING, |
61 INITIALIZATION_STATUS_SUCCESS, | 51 INITIALIZATION_STATUS_SUCCESS, |
62 INITIALIZATION_STATUS_CREATED_NEW_PROFILE, | 52 INITIALIZATION_STATUS_CREATED_NEW_PROFILE, |
63 INITIALIZATION_STATUS_ERROR | 53 INITIALIZATION_STATUS_ERROR |
64 }; | 54 }; |
65 | 55 |
66 // A helper class to store all the information associated with a preference. | 56 // A helper class to store all the information associated with a preference. |
67 class Preference { | 57 class Preference { |
68 public: | 58 public: |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 // incognito windows). | 160 // incognito windows). |
171 PrefService* CreateIncognitoPrefService(PrefStore* incognito_extension_prefs); | 161 PrefService* CreateIncognitoPrefService(PrefStore* incognito_extension_prefs); |
172 | 162 |
173 virtual ~PrefService(); | 163 virtual ~PrefService(); |
174 | 164 |
175 // Reloads the data from file. This should only be called when the importer | 165 // Reloads the data from file. This should only be called when the importer |
176 // is running during first run, and the main process may not change pref | 166 // is running during first run, and the main process may not change pref |
177 // values while the importer process is running. Returns true on success. | 167 // values while the importer process is running. Returns true on success. |
178 bool ReloadPersistentPrefs(); | 168 bool ReloadPersistentPrefs(); |
179 | 169 |
180 // Returns true if the preference for the given preference name is available | |
181 // and is managed. | |
182 bool IsManagedPreference(const char* pref_name) const; | |
183 | |
184 // Returns |true| if a preference with the given name is available and its | |
185 // value can be changed by the user. | |
186 bool IsUserModifiablePreference(const char* pref_name) const; | |
187 | |
188 // Lands pending writes to disk. This should only be used if we need to save | 170 // Lands pending writes to disk. This should only be used if we need to save |
189 // immediately (basically, during shutdown). | 171 // immediately (basically, during shutdown). |
190 void CommitPendingWrite(); | 172 void CommitPendingWrite(); |
191 | 173 |
192 // Make the PrefService aware of a pref. | 174 // PrefServiceBase implementation. |
193 // TODO(zea): split local state and profile prefs into their own subclasses. | 175 virtual bool IsManagedPreference(const char* pref_name) const OVERRIDE; |
194 // ---------- Local state prefs ---------- | 176 virtual bool IsUserModifiablePreference(const char* pref_name) const OVERRIDE; |
195 void RegisterBooleanPref(const char* path, bool default_value); | 177 virtual void RegisterBooleanPref(const char* path, |
196 void RegisterIntegerPref(const char* path, int default_value); | 178 bool default_value) OVERRIDE; |
197 void RegisterDoublePref(const char* path, double default_value); | 179 virtual void RegisterIntegerPref(const char* path, |
198 void RegisterStringPref(const char* path, const std::string& default_value); | 180 int default_value) OVERRIDE; |
199 void RegisterFilePathPref(const char* path, const FilePath& default_value); | 181 virtual void RegisterDoublePref(const char* path, |
200 void RegisterListPref(const char* path); | 182 double default_value) OVERRIDE; |
201 void RegisterDictionaryPref(const char* path); | 183 virtual void RegisterStringPref(const char* path, |
202 // These take ownership of the default_value: | 184 const std::string& default_value) OVERRIDE; |
203 void RegisterListPref(const char* path, | 185 virtual void RegisterFilePathPref(const char* path, |
204 base::ListValue* default_value); | 186 const FilePath& default_value) OVERRIDE; |
205 void RegisterDictionaryPref(const char* path, | 187 virtual void RegisterListPref(const char* path) OVERRIDE; |
206 base::DictionaryValue* default_value); | 188 virtual void RegisterDictionaryPref(const char* path) OVERRIDE; |
207 // These variants use a default value from the locale dll instead. | 189 virtual void RegisterListPref(const char* path, |
208 void RegisterLocalizedBooleanPref(const char* path, | 190 base::ListValue* default_value) OVERRIDE; |
209 int locale_default_message_id); | 191 virtual void RegisterDictionaryPref( |
210 void RegisterLocalizedIntegerPref(const char* path, | 192 const char* path, base::DictionaryValue* default_value) OVERRIDE; |
211 int locale_default_message_id); | 193 virtual void RegisterLocalizedBooleanPref( |
212 void RegisterLocalizedDoublePref(const char* path, | 194 const char* path, int locale_default_message_id) OVERRIDE; |
213 int locale_default_message_id); | 195 virtual void RegisterLocalizedIntegerPref( |
214 void RegisterLocalizedStringPref(const char* path, | 196 const char* path, int locale_default_message_id) OVERRIDE; |
215 int locale_default_message_id); | 197 virtual void RegisterLocalizedDoublePref( |
216 void RegisterInt64Pref(const char* path, int64 default_value); | 198 const char* path, int locale_default_message_id) OVERRIDE; |
217 | 199 virtual void RegisterLocalizedStringPref( |
218 // ---------- Profile prefs ---------- | 200 const char* path, int locale_default_message_id) OVERRIDE; |
219 // Profile prefs must specify whether the pref should be synchronized across | 201 virtual void RegisterInt64Pref(const char* path, |
220 // machines or not (see PrefSyncStatus enum above). | 202 int64 default_value) OVERRIDE; |
221 void RegisterBooleanPref(const char* path, | 203 virtual void RegisterBooleanPref(const char* path, |
222 bool default_value, | 204 bool default_value, |
223 PrefSyncStatus sync_status); | 205 PrefSyncStatus sync_status) OVERRIDE; |
224 void RegisterIntegerPref(const char* path, | 206 virtual void RegisterIntegerPref(const char* path, |
225 int default_value, | 207 int default_value, |
226 PrefSyncStatus sync_status); | 208 PrefSyncStatus sync_status) OVERRIDE; |
227 void RegisterDoublePref(const char* path, | 209 virtual void RegisterDoublePref(const char* path, |
228 double default_value, | 210 double default_value, |
229 PrefSyncStatus sync_status); | 211 PrefSyncStatus sync_status) OVERRIDE; |
230 void RegisterStringPref(const char* path, | 212 virtual void RegisterStringPref(const char* path, |
231 const std::string& default_value, | 213 const std::string& default_value, |
232 PrefSyncStatus sync_status); | 214 PrefSyncStatus sync_status) OVERRIDE; |
233 void RegisterFilePathPref(const char* path, | 215 virtual void RegisterFilePathPref(const char* path, |
234 const FilePath& default_value, | 216 const FilePath& default_value, |
235 PrefSyncStatus sync_status); | 217 PrefSyncStatus sync_status) OVERRIDE; |
236 void RegisterListPref(const char* path, PrefSyncStatus sync_status); | 218 virtual void RegisterListPref(const char* path, |
237 void RegisterDictionaryPref(const char* path, PrefSyncStatus sync_status); | 219 PrefSyncStatus sync_status) OVERRIDE; |
238 // These take ownership of the default_value: | 220 virtual void RegisterDictionaryPref(const char* path, |
239 void RegisterListPref(const char* path, | 221 PrefSyncStatus sync_status) OVERRIDE; |
240 base::ListValue* default_value, | 222 virtual void RegisterListPref(const char* path, |
241 PrefSyncStatus sync_status); | 223 base::ListValue* default_value, |
242 void RegisterDictionaryPref(const char* path, | 224 PrefSyncStatus sync_status) OVERRIDE; |
243 base::DictionaryValue* default_value, | 225 virtual void RegisterDictionaryPref(const char* path, |
244 PrefSyncStatus sync_status); | 226 base::DictionaryValue* default_value, |
245 // These variants use a default value from the locale dll instead. | 227 PrefSyncStatus sync_status) OVERRIDE; |
246 void RegisterLocalizedBooleanPref(const char* path, | 228 virtual void RegisterLocalizedBooleanPref( |
247 int locale_default_message_id, | 229 const char* path, |
248 PrefSyncStatus sync_status); | 230 int locale_default_message_id, |
249 void RegisterLocalizedIntegerPref(const char* path, | 231 PrefSyncStatus sync_status) OVERRIDE; |
250 int locale_default_message_id, | 232 virtual void RegisterLocalizedIntegerPref( |
251 PrefSyncStatus sync_status); | 233 const char* path, |
252 void RegisterLocalizedDoublePref(const char* path, | 234 int locale_default_message_id, |
253 int locale_default_message_id, | 235 PrefSyncStatus sync_status) OVERRIDE; |
254 PrefSyncStatus sync_status); | 236 virtual void RegisterLocalizedDoublePref( |
255 void RegisterLocalizedStringPref(const char* path, | 237 const char* path, |
256 int locale_default_message_id, | 238 int locale_default_message_id, |
257 PrefSyncStatus sync_status); | 239 PrefSyncStatus sync_status) OVERRIDE; |
258 void RegisterInt64Pref(const char* path, | 240 virtual void RegisterLocalizedStringPref( |
259 int64 default_value, | 241 const char* path, |
260 PrefSyncStatus sync_status); | 242 int locale_default_message_id, |
261 void RegisterUint64Pref(const char* path, | 243 PrefSyncStatus sync_status) OVERRIDE; |
262 uint64 default_value, | 244 virtual void RegisterInt64Pref(const char* path, |
263 PrefSyncStatus sync_status); | 245 int64 default_value, |
264 // Unregisters a preference. | 246 PrefSyncStatus sync_status) OVERRIDE; |
265 void UnregisterPreference(const char* path); | 247 virtual void RegisterUint64Pref(const char* path, |
266 | 248 uint64 default_value, |
267 // If the path is valid and the value at the end of the path matches the type | 249 PrefSyncStatus sync_status) OVERRIDE; |
268 // specified, it will return the specified value. Otherwise, the default | 250 virtual void UnregisterPreference(const char* path) OVERRIDE; |
269 // value (set when the pref was registered) will be returned. | 251 virtual bool GetBoolean(const char* path) const OVERRIDE; |
270 bool GetBoolean(const char* path) const; | 252 virtual int GetInteger(const char* path) const OVERRIDE; |
271 int GetInteger(const char* path) const; | 253 virtual double GetDouble(const char* path) const OVERRIDE; |
272 double GetDouble(const char* path) const; | 254 virtual std::string GetString(const char* path) const OVERRIDE; |
273 std::string GetString(const char* path) const; | 255 virtual FilePath GetFilePath(const char* path) const OVERRIDE; |
274 FilePath GetFilePath(const char* path) const; | 256 virtual const base::DictionaryValue* GetDictionary( |
275 | 257 const char* path) const OVERRIDE; |
276 // Returns the branch if it exists, or the registered default value otherwise. | 258 virtual const base::ListValue* GetList(const char* path) const OVERRIDE; |
277 // Note that |path| must point to a registered preference. In that case, these | 259 virtual const base::Value* GetUserPrefValue(const char* path) const OVERRIDE; |
278 // functions will never return NULL. | 260 virtual const base::Value* GetDefaultPrefValue( |
279 const base::DictionaryValue* GetDictionary(const char* path) const; | 261 const char* path) const OVERRIDE; |
280 const base::ListValue* GetList(const char* path) const; | 262 virtual void ClearPref(const char* path) OVERRIDE; |
281 | 263 virtual void Set(const char* path, const base::Value& value) OVERRIDE; |
282 // Returns the value of the given preference, from the user pref store. If | 264 virtual void SetBoolean(const char* path, bool value) OVERRIDE; |
283 // the preference is not set in the user pref store, returns NULL. | 265 virtual void SetInteger(const char* path, int value) OVERRIDE; |
284 const base::Value* GetUserPrefValue(const char* path) const; | 266 virtual void SetDouble(const char* path, double value) OVERRIDE; |
285 | 267 virtual void SetString(const char* path, const std::string& value) OVERRIDE; |
286 // Returns the default value of the given preference. |path| must point to a | 268 virtual void SetFilePath(const char* path, const FilePath& value) OVERRIDE; |
287 // registered preference. In that case, will never return NULL. | 269 virtual void SetInt64(const char* path, int64 value) OVERRIDE; |
288 const base::Value* GetDefaultPrefValue(const char* path) const; | 270 virtual int64 GetInt64(const char* path) const OVERRIDE; |
289 | 271 virtual void SetUint64(const char* path, uint64 value) OVERRIDE; |
290 // Removes a user pref and restores the pref to its default value. | 272 virtual uint64 GetUint64(const char* path) const OVERRIDE; |
291 void ClearPref(const char* path); | |
292 | |
293 // If the path is valid (i.e., registered), update the pref value in the user | |
294 // prefs. | |
295 // To set the value of dictionary or list values in the pref tree use | |
296 // Set(), but to modify the value of a dictionary or list use either | |
297 // ListPrefUpdate or DictionaryPrefUpdate from scoped_user_pref_update.h. | |
298 void Set(const char* path, const base::Value& value); | |
299 void SetBoolean(const char* path, bool value); | |
300 void SetInteger(const char* path, int value); | |
301 void SetDouble(const char* path, double value); | |
302 void SetString(const char* path, const std::string& value); | |
303 void SetFilePath(const char* path, const FilePath& value); | |
304 | |
305 // Int64 helper methods that actually store the given value as a string. | |
306 // Note that if obtaining the named value via GetDictionary or GetList, the | |
307 // Value type will be TYPE_STRING. | |
308 void SetInt64(const char* path, int64 value); | |
309 int64 GetInt64(const char* path) const; | |
310 | |
311 // As above, but for unsigned values. | |
312 void SetUint64(const char* path, uint64 value); | |
313 uint64 GetUint64(const char* path) const; | |
314 | 273 |
315 // Returns true if a value has been set for the specified path. | 274 // Returns true if a value has been set for the specified path. |
316 // NOTE: this is NOT the same as FindPreference. In particular | 275 // NOTE: this is NOT the same as FindPreference. In particular |
317 // FindPreference returns whether RegisterXXX has been invoked, where as | 276 // FindPreference returns whether RegisterXXX has been invoked, where as |
318 // this checks if a value exists for the path. | 277 // this checks if a value exists for the path. |
319 bool HasPrefPath(const char* path) const; | 278 bool HasPrefPath(const char* path) const; |
320 | 279 |
321 // Returns a dictionary with effective preference values. The ownership | 280 // Returns a dictionary with effective preference values. The ownership |
322 // is passed to the caller. | 281 // is passed to the caller. |
323 base::DictionaryValue* GetPreferenceValues() const; | 282 base::DictionaryValue* GetPreferenceValues() const; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
357 class PreferencePathComparator { | 316 class PreferencePathComparator { |
358 public: | 317 public: |
359 bool operator() (Preference* lhs, Preference* rhs) const { | 318 bool operator() (Preference* lhs, Preference* rhs) const { |
360 return lhs->name() < rhs->name(); | 319 return lhs->name() < rhs->name(); |
361 } | 320 } |
362 }; | 321 }; |
363 typedef std::set<Preference*, PreferencePathComparator> PreferenceSet; | 322 typedef std::set<Preference*, PreferencePathComparator> PreferenceSet; |
364 | 323 |
365 friend class PrefServiceMockBuilder; | 324 friend class PrefServiceMockBuilder; |
366 | 325 |
367 // Registration of pref change observers must be done using the | |
368 // PrefChangeRegistrar, which is declared as a friend here to grant it | |
369 // access to the otherwise protected members Add/RemovePrefObserver. | |
370 // PrefMember registers for preferences changes notification directly to | |
371 // avoid the storage overhead of the registrar, so its base class must be | |
372 // declared as a friend, too. | |
373 friend class PrefChangeRegistrar; | |
374 friend class subtle::PrefMemberBase; | |
375 | |
376 // Give access to ReportUserPrefChanged() and GetMutableUserPref(). | 326 // Give access to ReportUserPrefChanged() and GetMutableUserPref(). |
377 friend class subtle::ScopedUserPrefUpdateBase; | 327 friend class subtle::ScopedUserPrefUpdateBase; |
378 | 328 |
329 // PrefServiceBase implementation (protected in base, private here). | |
330 virtual void AddPrefObserver(const char* path, | |
331 content::NotificationObserver* obs) OVERRIDE; | |
332 virtual void RemovePrefObserver(const char* path, | |
333 content::NotificationObserver* obs) OVERRIDE; | |
334 | |
379 // Sends notification of a changed preference. This needs to be called by | 335 // Sends notification of a changed preference. This needs to be called by |
380 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed. | 336 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed. |
381 void ReportUserPrefChanged(const std::string& key); | 337 void ReportUserPrefChanged(const std::string& key); |
382 | 338 |
383 // If the pref at the given path changes, we call the observer's Observe | |
384 // method with PREF_CHANGED. Note that observers should not call these methods | |
385 // directly but rather use a PrefChangeRegistrar to make sure the observer | |
386 // gets cleaned up properly. | |
387 virtual void AddPrefObserver(const char* path, | |
388 content::NotificationObserver* obs); | |
389 virtual void RemovePrefObserver(const char* path, | |
390 content::NotificationObserver* obs); | |
391 | |
392 // Registers a new preference at |path|. The |default_value| must not be | 339 // Registers a new preference at |path|. The |default_value| must not be |
393 // NULL as it determines the preference value's type. | 340 // NULL as it determines the preference value's type. |
394 // RegisterPreference must not be called twice for the same path. | 341 // RegisterPreference must not be called twice for the same path. |
395 // This method takes ownership of |default_value|. | 342 // This method takes ownership of |default_value|. |
396 void RegisterPreference(const char* path, | 343 void RegisterPreference(const char* path, |
397 base::Value* default_value, | 344 base::Value* default_value, |
398 PrefSyncStatus sync_status); | 345 PrefSyncStatus sync_status); |
399 | 346 |
400 // Sets the value for this pref path in the user pref store and informs the | 347 // Sets the value for this pref path in the user pref store and informs the |
401 // PrefNotifier of the change. | 348 // PrefNotifier of the change. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
433 | 380 |
434 // Whether CreateIncognitoPrefService() or | 381 // Whether CreateIncognitoPrefService() or |
435 // CreatePrefServiceWithPerTabPrefStore() have been called to create a | 382 // CreatePrefServiceWithPerTabPrefStore() have been called to create a |
436 // "forked" PrefService. | 383 // "forked" PrefService. |
437 bool pref_service_forked_; | 384 bool pref_service_forked_; |
438 | 385 |
439 DISALLOW_COPY_AND_ASSIGN(PrefService); | 386 DISALLOW_COPY_AND_ASSIGN(PrefService); |
440 }; | 387 }; |
441 | 388 |
442 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 389 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
OLD | NEW |