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 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 }; | 58 }; |
59 | 59 |
60 // A helper class to store all the information associated with a preference. | 60 // A helper class to store all the information associated with a preference. |
61 class BASE_PREFS_EXPORT Preference { | 61 class BASE_PREFS_EXPORT Preference { |
62 public: | 62 public: |
63 // The type of the preference is determined by the type with which it is | 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, | 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 | 65 // dictionary (a branch), or list. You shouldn't need to construct this on |
66 // your own; use the PrefService::Register*Pref methods instead. | 66 // your own; use the PrefService::Register*Pref methods instead. |
67 Preference(const PrefService* service, | 67 Preference(const PrefService* service, |
68 const char* name, | 68 const std::string& name, |
69 base::Value::Type type); | 69 base::Value::Type type); |
70 ~Preference() {} | 70 ~Preference() {} |
71 | 71 |
72 // Returns the name of the Preference (i.e., the key, e.g., | 72 // Returns the name of the Preference (i.e., the key, e.g., |
73 // browser.window_placement). | 73 // browser.window_placement). |
74 const std::string name() const; | 74 const std::string name() const; |
75 | 75 |
76 // Returns the registered type of the preference. | 76 // Returns the registered type of the preference. |
77 base::Value::Type GetType() const; | 77 base::Value::Type GetType() const; |
78 | 78 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 read_error_callback, | 149 read_error_callback, |
150 bool async); | 150 bool async); |
151 virtual ~PrefService(); | 151 virtual ~PrefService(); |
152 | 152 |
153 // Lands pending writes to disk. This should only be used if we need to save | 153 // Lands pending writes to disk. This should only be used if we need to save |
154 // immediately (basically, during shutdown). | 154 // immediately (basically, during shutdown). |
155 void CommitPendingWrite(); | 155 void CommitPendingWrite(); |
156 | 156 |
157 // Returns true if the preference for the given preference name is available | 157 // Returns true if the preference for the given preference name is available |
158 // and is managed. | 158 // and is managed. |
159 bool IsManagedPreference(const char* pref_name) const; | 159 bool IsManagedPreference(const std::string& pref_name) const; |
160 | 160 |
161 // Returns |true| if a preference with the given name is available and its | 161 // Returns |true| if a preference with the given name is available and its |
162 // value can be changed by the user. | 162 // value can be changed by the user. |
163 bool IsUserModifiablePreference(const char* pref_name) const; | 163 bool IsUserModifiablePreference(const std::string& pref_name) const; |
164 | 164 |
165 // Look up a preference. Returns NULL if the preference is not | 165 // Look up a preference. Returns NULL if the preference is not |
166 // registered. | 166 // registered. |
167 const PrefService::Preference* FindPreference(const char* path) const; | 167 const PrefService::Preference* FindPreference(const std::string& path) const; |
168 | 168 |
169 // If the path is valid and the value at the end of the path matches the type | 169 // If the path is valid and the value at the end of the path matches the type |
170 // specified, it will return the specified value. Otherwise, the default | 170 // specified, it will return the specified value. Otherwise, the default |
171 // value (set when the pref was registered) will be returned. | 171 // value (set when the pref was registered) will be returned. |
172 bool GetBoolean(const char* path) const; | 172 bool GetBoolean(const std::string& path) const; |
173 int GetInteger(const char* path) const; | 173 int GetInteger(const std::string& path) const; |
174 double GetDouble(const char* path) const; | 174 double GetDouble(const std::string& path) const; |
175 std::string GetString(const char* path) const; | 175 std::string GetString(const std::string& path) const; |
176 base::FilePath GetFilePath(const char* path) const; | 176 base::FilePath GetFilePath(const std::string& path) const; |
177 | 177 |
178 // Returns the branch if it exists, or the registered default value otherwise. | 178 // Returns the branch if it exists, or the registered default value otherwise. |
179 // Note that |path| must point to a registered preference. In that case, these | 179 // Note that |path| must point to a registered preference. In that case, these |
180 // functions will never return NULL. | 180 // functions will never return NULL. |
181 const base::DictionaryValue* GetDictionary( | 181 const base::DictionaryValue* GetDictionary(const std::string& path) const; |
182 const char* path) const; | 182 const base::ListValue* GetList(const std::string& path) const; |
183 const base::ListValue* GetList(const char* path) const; | |
184 | 183 |
185 // Removes a user pref and restores the pref to its default value. | 184 // Removes a user pref and restores the pref to its default value. |
186 void ClearPref(const char* path); | 185 void ClearPref(const std::string& path); |
187 | 186 |
188 // If the path is valid (i.e., registered), update the pref value in the user | 187 // If the path is valid (i.e., registered), update the pref value in the user |
189 // prefs. | 188 // prefs. |
190 // To set the value of dictionary or list values in the pref tree use | 189 // To set the value of dictionary or list values in the pref tree use |
191 // Set(), but to modify the value of a dictionary or list use either | 190 // Set(), but to modify the value of a dictionary or list use either |
192 // ListPrefUpdate or DictionaryPrefUpdate from scoped_user_pref_update.h. | 191 // ListPrefUpdate or DictionaryPrefUpdate from scoped_user_pref_update.h. |
193 void Set(const char* path, const base::Value& value); | 192 void Set(const std::string& path, const base::Value& value); |
194 void SetBoolean(const char* path, bool value); | 193 void SetBoolean(const std::string& path, bool value); |
195 void SetInteger(const char* path, int value); | 194 void SetInteger(const std::string& path, int value); |
196 void SetDouble(const char* path, double value); | 195 void SetDouble(const std::string& path, double value); |
197 void SetString(const char* path, const std::string& value); | 196 void SetString(const std::string& path, const std::string& value); |
198 void SetFilePath(const char* path, const base::FilePath& value); | 197 void SetFilePath(const std::string& path, const base::FilePath& value); |
199 | 198 |
200 // Int64 helper methods that actually store the given value as a string. | 199 // Int64 helper methods that actually store the given value as a string. |
201 // Note that if obtaining the named value via GetDictionary or GetList, the | 200 // Note that if obtaining the named value via GetDictionary or GetList, the |
202 // Value type will be TYPE_STRING. | 201 // Value type will be TYPE_STRING. |
203 void SetInt64(const char* path, int64 value); | 202 void SetInt64(const std::string& path, int64 value); |
204 int64 GetInt64(const char* path) const; | 203 int64 GetInt64(const std::string& path) const; |
205 | 204 |
206 // As above, but for unsigned values. | 205 // As above, but for unsigned values. |
207 void SetUint64(const char* path, uint64 value); | 206 void SetUint64(const std::string& path, uint64 value); |
208 uint64 GetUint64(const char* path) const; | 207 uint64 GetUint64(const std::string& path) const; |
209 | 208 |
210 // Returns the value of the given preference, from the user pref store. If | 209 // Returns the value of the given preference, from the user pref store. If |
211 // the preference is not set in the user pref store, returns NULL. | 210 // the preference is not set in the user pref store, returns NULL. |
212 const base::Value* GetUserPrefValue(const char* path) const; | 211 const base::Value* GetUserPrefValue(const std::string& path) const; |
213 | 212 |
214 // Changes the default value for a preference. Takes ownership of |value|. | 213 // Changes the default value for a preference. Takes ownership of |value|. |
215 // | 214 // |
216 // Will cause a pref change notification to be fired if this causes | 215 // Will cause a pref change notification to be fired if this causes |
217 // the effective value to change. | 216 // the effective value to change. |
218 void SetDefaultPrefValue(const char* path, base::Value* value); | 217 void SetDefaultPrefValue(const std::string& path, base::Value* value); |
219 | 218 |
220 // Returns the default value of the given preference. |path| must point to a | 219 // Returns the default value of the given preference. |path| must point to a |
221 // registered preference. In that case, will never return NULL. | 220 // registered preference. In that case, will never return NULL. |
222 const base::Value* GetDefaultPrefValue(const char* path) const; | 221 const base::Value* GetDefaultPrefValue(const std::string& path) const; |
223 | 222 |
224 // Returns true if a value has been set for the specified path. | 223 // Returns true if a value has been set for the specified path. |
225 // NOTE: this is NOT the same as FindPreference. In particular | 224 // NOTE: this is NOT the same as FindPreference. In particular |
226 // FindPreference returns whether RegisterXXX has been invoked, where as | 225 // FindPreference returns whether RegisterXXX has been invoked, where as |
227 // this checks if a value exists for the path. | 226 // this checks if a value exists for the path. |
228 bool HasPrefPath(const char* path) const; | 227 bool HasPrefPath(const std::string& path) const; |
229 | 228 |
230 // Returns a dictionary with effective preference values. The ownership | 229 // Returns a dictionary with effective preference values. |
231 // is passed to the caller. | |
232 scoped_ptr<base::DictionaryValue> GetPreferenceValues() const; | 230 scoped_ptr<base::DictionaryValue> GetPreferenceValues() const; |
233 | 231 |
| 232 // Returns a dictionary with effective preference values, omitting prefs that |
| 233 // are at their default values. |
| 234 scoped_ptr<base::DictionaryValue> GetPreferenceValuesOmitDefaults() const; |
| 235 |
234 // Returns a dictionary with effective preference values. Contrary to | 236 // Returns a dictionary with effective preference values. Contrary to |
235 // GetPreferenceValues(), the paths of registered preferences are not split on | 237 // GetPreferenceValues(), the paths of registered preferences are not split on |
236 // '.' characters. If a registered preference stores a dictionary, however, | 238 // '.' characters. If a registered preference stores a dictionary, however, |
237 // the hierarchical structure inside the preference will be preserved. | 239 // the hierarchical structure inside the preference will be preserved. |
238 // For example, if "foo.bar" is a registered preference, the result could look | 240 // For example, if "foo.bar" is a registered preference, the result could look |
239 // like this: | 241 // like this: |
240 // {"foo.bar": {"a": {"b": true}}}. | 242 // {"foo.bar": {"a": {"b": true}}}. |
241 // The ownership is passed to the caller. | |
242 scoped_ptr<base::DictionaryValue> GetPreferenceValuesWithoutPathExpansion() | 243 scoped_ptr<base::DictionaryValue> GetPreferenceValuesWithoutPathExpansion() |
243 const; | 244 const; |
244 | 245 |
245 bool ReadOnly() const; | 246 bool ReadOnly() const; |
246 | 247 |
247 PrefInitializationStatus GetInitializationStatus() const; | 248 PrefInitializationStatus GetInitializationStatus() const; |
248 | 249 |
249 // Tell our PrefValueStore to update itself to |command_line_store|. | 250 // Tell our PrefValueStore to update itself to |command_line_store|. |
250 // Takes ownership of the store. | 251 // Takes ownership of the store. |
251 virtual void UpdateCommandLinePrefStore(PrefStore* command_line_store); | 252 virtual void UpdateCommandLinePrefStore(PrefStore* command_line_store); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 | 310 |
310 // These are protected so they can only be accessed by the friend | 311 // These are protected so they can only be accessed by the friend |
311 // classes listed above. | 312 // classes listed above. |
312 // | 313 // |
313 // If the pref at the given path changes, we call the observer's | 314 // If the pref at the given path changes, we call the observer's |
314 // OnPreferenceChanged method. Note that observers should not call | 315 // OnPreferenceChanged method. Note that observers should not call |
315 // these methods directly but rather use a PrefChangeRegistrar to | 316 // these methods directly but rather use a PrefChangeRegistrar to |
316 // make sure the observer gets cleaned up properly. | 317 // make sure the observer gets cleaned up properly. |
317 // | 318 // |
318 // Virtual for testing. | 319 // Virtual for testing. |
319 virtual void AddPrefObserver(const char* path, PrefObserver* obs); | 320 virtual void AddPrefObserver(const std::string& path, PrefObserver* obs); |
320 virtual void RemovePrefObserver(const char* path, PrefObserver* obs); | 321 virtual void RemovePrefObserver(const std::string& path, PrefObserver* obs); |
321 | 322 |
322 // Sends notification of a changed preference. This needs to be called by | 323 // Sends notification of a changed preference. This needs to be called by |
323 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed. | 324 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed. |
324 void ReportUserPrefChanged(const std::string& key); | 325 void ReportUserPrefChanged(const std::string& key); |
325 | 326 |
326 // Sets the value for this pref path in the user pref store and informs the | 327 // Sets the value for this pref path in the user pref store and informs the |
327 // PrefNotifier of the change. | 328 // PrefNotifier of the change. |
328 void SetUserPrefValue(const char* path, base::Value* new_value); | 329 void SetUserPrefValue(const std::string& path, base::Value* new_value); |
329 | 330 |
330 // Load preferences from storage, attempting to diagnose and handle errors. | 331 // Load preferences from storage, attempting to diagnose and handle errors. |
331 // This should only be called from the constructor. | 332 // This should only be called from the constructor. |
332 void InitFromStorage(bool async); | 333 void InitFromStorage(bool async); |
333 | 334 |
334 // Used to set the value of dictionary or list values in the user pref store. | 335 // Used to set the value of dictionary or list values in the user pref store. |
335 // This will create a dictionary or list if one does not exist in the user | 336 // This will create a dictionary or list if one does not exist in the user |
336 // pref store. This method returns NULL only if you're requesting an | 337 // pref store. This method returns NULL only if you're requesting an |
337 // unregistered pref or a non-dict/non-list pref. | 338 // unregistered pref or a non-dict/non-list pref. |
338 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and | 339 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and |
339 // |path| must point to a registered preference of type |type|. | 340 // |path| must point to a registered preference of type |type|. |
340 // Ownership of the returned value remains at the user pref store. | 341 // Ownership of the returned value remains at the user pref store. |
341 base::Value* GetMutableUserPref(const char* path, | 342 base::Value* GetMutableUserPref(const std::string& path, |
342 base::Value::Type type); | 343 base::Value::Type type); |
343 | 344 |
344 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(), | 345 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(), |
345 // it has been added for performance. If is faster because it does | 346 // it has been added for performance. If is faster because it does |
346 // not need to find or create a Preference object to get the | 347 // not need to find or create a Preference object to get the |
347 // value (GetValue() calls back though the preference service to | 348 // value (GetValue() calls back though the preference service to |
348 // actually get the value.). | 349 // actually get the value.). |
349 const base::Value* GetPreferenceValue(const std::string& path) const; | 350 const base::Value* GetPreferenceValue(const std::string& path) const; |
350 | 351 |
351 // Local cache of registered Preference objects. The pref_registry_ | 352 // Local cache of registered Preference objects. The pref_registry_ |
352 // is authoritative with respect to what the types and default values | 353 // is authoritative with respect to what the types and default values |
353 // of registered preferences are. | 354 // of registered preferences are. |
354 mutable PreferenceMap prefs_map_; | 355 mutable PreferenceMap prefs_map_; |
355 | 356 |
356 DISALLOW_COPY_AND_ASSIGN(PrefService); | 357 DISALLOW_COPY_AND_ASSIGN(PrefService); |
357 }; | 358 }; |
358 | 359 |
359 #endif // BASE_PREFS_PREF_SERVICE_H_ | 360 #endif // BASE_PREFS_PREF_SERVICE_H_ |
OLD | NEW |