OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 7 #ifndef CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
8 #define CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 8 #define CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
9 #pragma once | 9 #pragma once |
10 | 10 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 // A helper class to store all the information associated with a preference. | 49 // A helper class to store all the information associated with a preference. |
50 class Preference { | 50 class Preference { |
51 public: | 51 public: |
52 | 52 |
53 // The type of the preference is determined by the type with which it is | 53 // The type of the preference is determined by the type with which it is |
54 // registered. This type needs to be a boolean, integer, double, string, | 54 // registered. This type needs to be a boolean, integer, double, string, |
55 // dictionary (a branch), or list. You shouldn't need to construct this on | 55 // dictionary (a branch), or list. You shouldn't need to construct this on |
56 // your own; use the PrefService::Register*Pref methods instead. | 56 // your own; use the PrefService::Register*Pref methods instead. |
57 Preference(const PrefService* service, | 57 Preference(const PrefService* service, |
58 const char* name, | 58 const char* name, |
59 Value::ValueType type); | 59 base::Value::ValueType type); |
60 ~Preference() {} | 60 ~Preference() {} |
61 | 61 |
62 // Returns the name of the Preference (i.e., the key, e.g., | 62 // Returns the name of the Preference (i.e., the key, e.g., |
63 // browser.window_placement). | 63 // browser.window_placement). |
64 const std::string name() const { return name_; } | 64 const std::string name() const { return name_; } |
65 | 65 |
66 // Returns the registered type of the preference. | 66 // Returns the registered type of the preference. |
67 Value::ValueType GetType() const; | 67 base::Value::ValueType GetType() const; |
68 | 68 |
69 // Returns the value of the Preference, falling back to the registered | 69 // Returns the value of the Preference, falling back to the registered |
70 // default value if no other has been set. | 70 // default value if no other has been set. |
71 const Value* GetValue() const; | 71 const base::Value* GetValue() const; |
72 | 72 |
73 // Returns true if the Preference is managed, i.e. set by an admin policy. | 73 // Returns true if the Preference is managed, i.e. set by an admin policy. |
74 // Since managed prefs have the highest priority, this also indicates | 74 // Since managed prefs have the highest priority, this also indicates |
75 // whether the pref is actually being controlled by the policy setting. | 75 // whether the pref is actually being controlled by the policy setting. |
76 bool IsManaged() const; | 76 bool IsManaged() const; |
77 | 77 |
78 // Returns true if the Preference has a value set by an extension, even if | 78 // Returns true if the Preference has a value set by an extension, even if |
79 // that value is being overridden by a higher-priority source. | 79 // that value is being overridden by a higher-priority source. |
80 bool HasExtensionSetting() const; | 80 bool HasExtensionSetting() const; |
81 | 81 |
(...skipping 26 matching lines...) Expand all Loading... |
108 | 108 |
109 private: | 109 private: |
110 friend class PrefService; | 110 friend class PrefService; |
111 | 111 |
112 PrefValueStore* pref_value_store() const { | 112 PrefValueStore* pref_value_store() const { |
113 return pref_service_->pref_value_store_.get(); | 113 return pref_service_->pref_value_store_.get(); |
114 } | 114 } |
115 | 115 |
116 std::string name_; | 116 std::string name_; |
117 | 117 |
118 Value::ValueType type_; | 118 base::Value::ValueType type_; |
119 | 119 |
120 // Reference to the PrefService in which this pref was created. | 120 // Reference to the PrefService in which this pref was created. |
121 const PrefService* pref_service_; | 121 const PrefService* pref_service_; |
122 | 122 |
123 DISALLOW_COPY_AND_ASSIGN(Preference); | 123 DISALLOW_COPY_AND_ASSIGN(Preference); |
124 }; | 124 }; |
125 | 125 |
126 // Factory method that creates a new instance of a PrefService with the | 126 // Factory method that creates a new instance of a PrefService with the |
127 // applicable PrefStores. The |pref_filename| points to the user preference | 127 // applicable PrefStores. The |pref_filename| points to the user preference |
128 // file. This is the usual way to create a new PrefService. | 128 // file. This is the usual way to create a new PrefService. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 // TODO(zea): split local state and profile prefs into their own subclasses. | 171 // TODO(zea): split local state and profile prefs into their own subclasses. |
172 // ---------- Local state prefs ---------- | 172 // ---------- Local state prefs ---------- |
173 void RegisterBooleanPref(const char* path, bool default_value); | 173 void RegisterBooleanPref(const char* path, bool default_value); |
174 void RegisterIntegerPref(const char* path, int default_value); | 174 void RegisterIntegerPref(const char* path, int default_value); |
175 void RegisterDoublePref(const char* path, double default_value); | 175 void RegisterDoublePref(const char* path, double default_value); |
176 void RegisterStringPref(const char* path, const std::string& default_value); | 176 void RegisterStringPref(const char* path, const std::string& default_value); |
177 void RegisterFilePathPref(const char* path, const FilePath& default_value); | 177 void RegisterFilePathPref(const char* path, const FilePath& default_value); |
178 void RegisterListPref(const char* path); | 178 void RegisterListPref(const char* path); |
179 void RegisterDictionaryPref(const char* path); | 179 void RegisterDictionaryPref(const char* path); |
180 // These take ownership of the default_value: | 180 // These take ownership of the default_value: |
181 void RegisterListPref(const char* path, ListValue* default_value); | 181 void RegisterListPref(const char* path, |
182 void RegisterDictionaryPref(const char* path, DictionaryValue* default_value); | 182 base::ListValue* default_value); |
| 183 void RegisterDictionaryPref(const char* path, |
| 184 base::DictionaryValue* default_value); |
183 // These variants use a default value from the locale dll instead. | 185 // These variants use a default value from the locale dll instead. |
184 void RegisterLocalizedBooleanPref(const char* path, | 186 void RegisterLocalizedBooleanPref(const char* path, |
185 int locale_default_message_id); | 187 int locale_default_message_id); |
186 void RegisterLocalizedIntegerPref(const char* path, | 188 void RegisterLocalizedIntegerPref(const char* path, |
187 int locale_default_message_id); | 189 int locale_default_message_id); |
188 void RegisterLocalizedDoublePref(const char* path, | 190 void RegisterLocalizedDoublePref(const char* path, |
189 int locale_default_message_id); | 191 int locale_default_message_id); |
190 void RegisterLocalizedStringPref(const char* path, | 192 void RegisterLocalizedStringPref(const char* path, |
191 int locale_default_message_id); | 193 int locale_default_message_id); |
192 void RegisterInt64Pref(const char* path, int64 default_value); | 194 void RegisterInt64Pref(const char* path, int64 default_value); |
(...skipping 13 matching lines...) Expand all Loading... |
206 void RegisterStringPref(const char* path, | 208 void RegisterStringPref(const char* path, |
207 const std::string& default_value, | 209 const std::string& default_value, |
208 PrefSyncStatus sync_status); | 210 PrefSyncStatus sync_status); |
209 void RegisterFilePathPref(const char* path, | 211 void RegisterFilePathPref(const char* path, |
210 const FilePath& default_value, | 212 const FilePath& default_value, |
211 PrefSyncStatus sync_status); | 213 PrefSyncStatus sync_status); |
212 void RegisterListPref(const char* path, PrefSyncStatus sync_status); | 214 void RegisterListPref(const char* path, PrefSyncStatus sync_status); |
213 void RegisterDictionaryPref(const char* path, PrefSyncStatus sync_status); | 215 void RegisterDictionaryPref(const char* path, PrefSyncStatus sync_status); |
214 // These take ownership of the default_value: | 216 // These take ownership of the default_value: |
215 void RegisterListPref(const char* path, | 217 void RegisterListPref(const char* path, |
216 ListValue* default_value, | 218 base::ListValue* default_value, |
217 PrefSyncStatus sync_status); | 219 PrefSyncStatus sync_status); |
218 void RegisterDictionaryPref(const char* path, | 220 void RegisterDictionaryPref(const char* path, |
219 DictionaryValue* default_value, | 221 base::DictionaryValue* default_value, |
220 PrefSyncStatus sync_status); | 222 PrefSyncStatus sync_status); |
221 // These variants use a default value from the locale dll instead. | 223 // These variants use a default value from the locale dll instead. |
222 void RegisterLocalizedBooleanPref(const char* path, | 224 void RegisterLocalizedBooleanPref(const char* path, |
223 int locale_default_message_id, | 225 int locale_default_message_id, |
224 PrefSyncStatus sync_status); | 226 PrefSyncStatus sync_status); |
225 void RegisterLocalizedIntegerPref(const char* path, | 227 void RegisterLocalizedIntegerPref(const char* path, |
226 int locale_default_message_id, | 228 int locale_default_message_id, |
227 PrefSyncStatus sync_status); | 229 PrefSyncStatus sync_status); |
228 void RegisterLocalizedDoublePref(const char* path, | 230 void RegisterLocalizedDoublePref(const char* path, |
229 int locale_default_message_id, | 231 int locale_default_message_id, |
(...skipping 10 matching lines...) Expand all Loading... |
240 // value (set when the pref was registered) will be returned. | 242 // value (set when the pref was registered) will be returned. |
241 bool GetBoolean(const char* path) const; | 243 bool GetBoolean(const char* path) const; |
242 int GetInteger(const char* path) const; | 244 int GetInteger(const char* path) const; |
243 double GetDouble(const char* path) const; | 245 double GetDouble(const char* path) const; |
244 std::string GetString(const char* path) const; | 246 std::string GetString(const char* path) const; |
245 FilePath GetFilePath(const char* path) const; | 247 FilePath GetFilePath(const char* path) const; |
246 | 248 |
247 // Returns the branch if it exists, or the registered default value otherwise. | 249 // Returns the branch if it exists, or the registered default value otherwise. |
248 // Note that |path| must point to a registered preference. In that case, these | 250 // Note that |path| must point to a registered preference. In that case, these |
249 // functions will never return NULL. | 251 // functions will never return NULL. |
250 const DictionaryValue* GetDictionary(const char* path) const; | 252 const base::DictionaryValue* GetDictionary(const char* path) const; |
251 const ListValue* GetList(const char* path) const; | 253 const base::ListValue* GetList(const char* path) const; |
252 | 254 |
253 // Removes a user pref and restores the pref to its default value. | 255 // Removes a user pref and restores the pref to its default value. |
254 void ClearPref(const char* path); | 256 void ClearPref(const char* path); |
255 | 257 |
256 // If the path is valid (i.e., registered), update the pref value in the user | 258 // If the path is valid (i.e., registered), update the pref value in the user |
257 // prefs. | 259 // prefs. |
258 // To set the value of dictionary or list values in the pref tree use | 260 // To set the value of dictionary or list values in the pref tree use |
259 // Set(), but to modify the value of a dictionary or list use either | 261 // Set(), but to modify the value of a dictionary or list use either |
260 // ListPrefUpdate or DictionaryPrefUpdate from scoped_user_pref_update.h. | 262 // ListPrefUpdate or DictionaryPrefUpdate from scoped_user_pref_update.h. |
261 void Set(const char* path, const Value& value); | 263 void Set(const char* path, const base::Value& value); |
262 void SetBoolean(const char* path, bool value); | 264 void SetBoolean(const char* path, bool value); |
263 void SetInteger(const char* path, int value); | 265 void SetInteger(const char* path, int value); |
264 void SetDouble(const char* path, double value); | 266 void SetDouble(const char* path, double value); |
265 void SetString(const char* path, const std::string& value); | 267 void SetString(const char* path, const std::string& value); |
266 void SetFilePath(const char* path, const FilePath& value); | 268 void SetFilePath(const char* path, const FilePath& value); |
267 | 269 |
268 // Int64 helper methods that actually store the given value as a string. | 270 // Int64 helper methods that actually store the given value as a string. |
269 // Note that if obtaining the named value via GetDictionary or GetList, the | 271 // Note that if obtaining the named value via GetDictionary or GetList, the |
270 // Value type will be TYPE_STRING. | 272 // Value type will be TYPE_STRING. |
271 void SetInt64(const char* path, int64 value); | 273 void SetInt64(const char* path, int64 value); |
272 int64 GetInt64(const char* path) const; | 274 int64 GetInt64(const char* path) const; |
273 | 275 |
274 // Returns true if a value has been set for the specified path. | 276 // Returns true if a value has been set for the specified path. |
275 // NOTE: this is NOT the same as FindPreference. In particular | 277 // NOTE: this is NOT the same as FindPreference. In particular |
276 // FindPreference returns whether RegisterXXX has been invoked, where as | 278 // FindPreference returns whether RegisterXXX has been invoked, where as |
277 // this checks if a value exists for the path. | 279 // this checks if a value exists for the path. |
278 bool HasPrefPath(const char* path) const; | 280 bool HasPrefPath(const char* path) const; |
279 | 281 |
280 // Returns a dictionary with effective preference values. The ownership | 282 // Returns a dictionary with effective preference values. The ownership |
281 // is passed to the caller. | 283 // is passed to the caller. |
282 DictionaryValue* GetPreferenceValues() const; | 284 base::DictionaryValue* GetPreferenceValues() const; |
283 | 285 |
284 // A helper method to quickly look up a preference. Returns NULL if the | 286 // A helper method to quickly look up a preference. Returns NULL if the |
285 // preference is not registered. | 287 // preference is not registered. |
286 const Preference* FindPreference(const char* pref_name) const; | 288 const Preference* FindPreference(const char* pref_name) const; |
287 | 289 |
288 bool ReadOnly() const; | 290 bool ReadOnly() const; |
289 | 291 |
290 // SyncableService getter. | 292 // SyncableService getter. |
291 // TODO(zea): Have PrefService implement SyncableService directly. | 293 // TODO(zea): Have PrefService implement SyncableService directly. |
292 SyncableService* GetSyncableService(); | 294 SyncableService* GetSyncableService(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 // directly but rather use a PrefChangeRegistrar to make sure the observer | 349 // directly but rather use a PrefChangeRegistrar to make sure the observer |
348 // gets cleaned up properly. | 350 // gets cleaned up properly. |
349 virtual void AddPrefObserver(const char* path, NotificationObserver* obs); | 351 virtual void AddPrefObserver(const char* path, NotificationObserver* obs); |
350 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs); | 352 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs); |
351 | 353 |
352 // Registers a new preference at |path|. The |default_value| must not be | 354 // Registers a new preference at |path|. The |default_value| must not be |
353 // NULL as it determines the preference value's type. | 355 // NULL as it determines the preference value's type. |
354 // RegisterPreference must not be called twice for the same path. | 356 // RegisterPreference must not be called twice for the same path. |
355 // This method takes ownership of |default_value|. | 357 // This method takes ownership of |default_value|. |
356 void RegisterPreference(const char* path, | 358 void RegisterPreference(const char* path, |
357 Value* default_value, | 359 base::Value* default_value, |
358 PrefSyncStatus sync_status); | 360 PrefSyncStatus sync_status); |
359 | 361 |
360 // Sets the value for this pref path in the user pref store and informs the | 362 // Sets the value for this pref path in the user pref store and informs the |
361 // PrefNotifier of the change. | 363 // PrefNotifier of the change. |
362 void SetUserPrefValue(const char* path, Value* new_value); | 364 void SetUserPrefValue(const char* path, base::Value* new_value); |
363 | 365 |
364 // Load preferences from storage, attempting to diagnose and handle errors. | 366 // Load preferences from storage, attempting to diagnose and handle errors. |
365 // This should only be called from the constructor. | 367 // This should only be called from the constructor. |
366 void InitFromStorage(bool async); | 368 void InitFromStorage(bool async); |
367 | 369 |
368 // Used to set the value of dictionary or list values in the user pref store. | 370 // Used to set the value of dictionary or list values in the user pref store. |
369 // This will create a dictionary or list if one does not exist in the user | 371 // This will create a dictionary or list if one does not exist in the user |
370 // pref store. This method returns NULL only if you're requesting an | 372 // pref store. This method returns NULL only if you're requesting an |
371 // unregistered pref or a non-dict/non-list pref. | 373 // unregistered pref or a non-dict/non-list pref. |
372 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and | 374 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and |
373 // |path| must point to a registered preference of type |type|. | 375 // |path| must point to a registered preference of type |type|. |
374 // Ownership of the returned value remains at the user pref store. | 376 // Ownership of the returned value remains at the user pref store. |
375 Value* GetMutableUserPref(const char* path, Value::ValueType type); | 377 base::Value* GetMutableUserPref(const char* path, |
| 378 base::Value::ValueType type); |
376 | 379 |
377 // The PrefValueStore provides prioritized preference values. It is created | 380 // The PrefValueStore provides prioritized preference values. It is created |
378 // and owned by this PrefService. Subclasses may access it for unit testing. | 381 // and owned by this PrefService. Subclasses may access it for unit testing. |
379 scoped_ptr<PrefValueStore> pref_value_store_; | 382 scoped_ptr<PrefValueStore> pref_value_store_; |
380 | 383 |
381 // Pref Stores and profile that we passed to the PrefValueStore. | 384 // Pref Stores and profile that we passed to the PrefValueStore. |
382 scoped_refptr<PersistentPrefStore> user_pref_store_; | 385 scoped_refptr<PersistentPrefStore> user_pref_store_; |
383 scoped_refptr<DefaultPrefStore> default_store_; | 386 scoped_refptr<DefaultPrefStore> default_store_; |
384 | 387 |
385 // Local cache of registered Preference objects. The default_store_ | 388 // Local cache of registered Preference objects. The default_store_ |
386 // is authoritative with respect to what the types and default values | 389 // is authoritative with respect to what the types and default values |
387 // of registered preferences are. | 390 // of registered preferences are. |
388 mutable PreferenceSet prefs_; | 391 mutable PreferenceSet prefs_; |
389 | 392 |
390 // The model associator that maintains the links with the sync db. | 393 // The model associator that maintains the links with the sync db. |
391 scoped_ptr<PrefModelAssociator> pref_sync_associator_; | 394 scoped_ptr<PrefModelAssociator> pref_sync_associator_; |
392 | 395 |
393 DISALLOW_COPY_AND_ASSIGN(PrefService); | 396 DISALLOW_COPY_AND_ASSIGN(PrefService); |
394 }; | 397 }; |
395 | 398 |
396 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 399 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
OLD | NEW |