Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: chrome/browser/prefs/pref_service.h

Issue 6905044: Refactor preference syncing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Really fix. And rebase Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
11 #include <set> 11 #include <set>
12 #include <string> 12 #include <string>
13 13
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/threading/non_thread_safe.h" 16 #include "base/threading/non_thread_safe.h"
17 #include "base/values.h" 17 #include "base/values.h"
18 #include "chrome/common/json_pref_store.h" 18 #include "chrome/common/json_pref_store.h"
19 19
20 class DefaultPrefStore; 20 class DefaultPrefStore;
21 class FilePath; 21 class FilePath;
22 class NotificationObserver; 22 class NotificationObserver;
23 class PersistentPrefStore; 23 class PersistentPrefStore;
24 class PrefChangeObserver; 24 class PrefModelAssociator;
25 class PrefNotifier; 25 class PrefNotifier;
26 class PrefNotifierImpl; 26 class PrefNotifierImpl;
27 class PrefStore; 27 class PrefStore;
28 class PrefValueStore; 28 class PrefValueStore;
29 class Profile; 29 class Profile;
30 class SyncableService;
30 31
31 namespace subtle { 32 namespace subtle {
32 class PrefMemberBase; 33 class PrefMemberBase;
33 class ScopedUserPrefUpdateBase; 34 class ScopedUserPrefUpdateBase;
34 }; 35 };
35 36
36 class PrefService; 37 class PrefService;
37 38
38 class PrefServiceDelegate { 39 class PrefServiceDelegate {
39 public: 40 public:
40 virtual void OnPrefsLoaded(PrefService* prefs, bool success) = 0; 41 virtual void OnPrefsLoaded(PrefService* prefs, bool success) = 0;
41 }; 42 };
42 43
43 class PrefService : public base::NonThreadSafe, 44 class PrefService : public base::NonThreadSafe,
44 public JsonPrefStore::Delegate { 45 public JsonPrefStore::Delegate {
45 public: 46 public:
47 // Enum used when registering preferences to determine if it should be synced
48 // or not. This is only used for profile prefs, not local state prefs.
49 // See the Register*Pref methods for profile prefs below.
50 enum PrefSyncStatus {
51 UNSYNCABLE_PREF,
52 SYNCABLE_PREF
53 };
54
46 // A helper class to store all the information associated with a preference. 55 // A helper class to store all the information associated with a preference.
47 class Preference { 56 class Preference {
48 public: 57 public:
49 58
50 // The type of the preference is determined by the type with which it is 59 // The type of the preference is determined by the type with which it is
51 // registered. This type needs to be a boolean, integer, double, string, 60 // registered. This type needs to be a boolean, integer, double, string,
52 // dictionary (a branch), or list. You shouldn't need to construct this on 61 // dictionary (a branch), or list. You shouldn't need to construct this on
53 // your own; use the PrefService::Register*Pref methods instead. 62 // your own; use the PrefService::Register*Pref methods instead.
54 Preference(const PrefService* service, 63 Preference(const PrefService* service,
55 const char* name, 64 const char* name,
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 // should use ScheduleSavePersistentPrefs. 174 // should use ScheduleSavePersistentPrefs.
166 bool SavePersistentPrefs(); 175 bool SavePersistentPrefs();
167 176
168 // Serializes the data and schedules save using ImportantFileWriter. 177 // Serializes the data and schedules save using ImportantFileWriter.
169 void ScheduleSavePersistentPrefs(); 178 void ScheduleSavePersistentPrefs();
170 179
171 // Lands pending writes to disk. 180 // Lands pending writes to disk.
172 void CommitPendingWrite(); 181 void CommitPendingWrite();
173 182
174 // Make the PrefService aware of a pref. 183 // Make the PrefService aware of a pref.
184 // TODO(zea): split local state and profile prefs into their own subclasses.
185 // ---------- Local state prefs ----------
175 void RegisterBooleanPref(const char* path, bool default_value); 186 void RegisterBooleanPref(const char* path, bool default_value);
176 void RegisterIntegerPref(const char* path, int default_value); 187 void RegisterIntegerPref(const char* path, int default_value);
177 void RegisterDoublePref(const char* path, double default_value); 188 void RegisterDoublePref(const char* path, double default_value);
178 void RegisterStringPref(const char* path, const std::string& default_value); 189 void RegisterStringPref(const char* path, const std::string& default_value);
179 void RegisterFilePathPref(const char* path, const FilePath& default_value); 190 void RegisterFilePathPref(const char* path, const FilePath& default_value);
180 void RegisterListPref(const char* path); 191 void RegisterListPref(const char* path);
181 void RegisterDictionaryPref(const char* path); 192 void RegisterDictionaryPref(const char* path);
182 // These take ownership of the default_value: 193 // These take ownership of the default_value:
183 void RegisterListPref(const char* path, ListValue* default_value); 194 void RegisterListPref(const char* path, ListValue* default_value);
184 void RegisterDictionaryPref(const char* path, DictionaryValue* default_value); 195 void RegisterDictionaryPref(const char* path, DictionaryValue* default_value);
185
186 // These variants use a default value from the locale dll instead. 196 // These variants use a default value from the locale dll instead.
187 void RegisterLocalizedBooleanPref(const char* path, 197 void RegisterLocalizedBooleanPref(const char* path,
188 int locale_default_message_id); 198 int locale_default_message_id);
189 void RegisterLocalizedIntegerPref(const char* path, 199 void RegisterLocalizedIntegerPref(const char* path,
190 int locale_default_message_id); 200 int locale_default_message_id);
191 void RegisterLocalizedDoublePref(const char* path, 201 void RegisterLocalizedDoublePref(const char* path,
192 int locale_default_message_id); 202 int locale_default_message_id);
193 void RegisterLocalizedStringPref(const char* path, 203 void RegisterLocalizedStringPref(const char* path,
194 int locale_default_message_id); 204 int locale_default_message_id);
205 void RegisterInt64Pref(const char* path, int64 default_value);
206
207 // ---------- Profile prefs ----------
208 // Profile prefs must specify whether the pref should be synchronized across
209 // machines or not (see PrefSyncStatus enum above).
210 void RegisterBooleanPref(const char* path,
211 bool default_value,
212 PrefSyncStatus sync_status);
213 void RegisterIntegerPref(const char* path,
214 int default_value,
215 PrefSyncStatus sync_status);
216 void RegisterDoublePref(const char* path,
217 double default_value,
218 PrefSyncStatus sync_status);
219 void RegisterStringPref(const char* path,
220 const std::string& default_value,
221 PrefSyncStatus sync_status);
222 void RegisterFilePathPref(const char* path,
223 const FilePath& default_value,
224 PrefSyncStatus sync_status);
225 void RegisterListPref(const char* path, PrefSyncStatus sync_status);
226 void RegisterDictionaryPref(const char* path, PrefSyncStatus sync_status);
227 // These take ownership of the default_value:
228 void RegisterListPref(const char* path,
229 ListValue* default_value,
230 PrefSyncStatus sync_status);
231 void RegisterDictionaryPref(const char* path,
232 DictionaryValue* default_value,
233 PrefSyncStatus sync_status);
234 // These variants use a default value from the locale dll instead.
235 void RegisterLocalizedBooleanPref(const char* path,
236 int locale_default_message_id,
237 PrefSyncStatus sync_status);
238 void RegisterLocalizedIntegerPref(const char* path,
239 int locale_default_message_id,
240 PrefSyncStatus sync_status);
241 void RegisterLocalizedDoublePref(const char* path,
242 int locale_default_message_id,
243 PrefSyncStatus sync_status);
244 void RegisterLocalizedStringPref(const char* path,
245 int locale_default_message_id,
246 PrefSyncStatus sync_status);
247 void RegisterInt64Pref(const char* path,
248 int64 default_value,
249 PrefSyncStatus sync_status);
195 250
196 // If the path is valid and the value at the end of the path matches the type 251 // If the path is valid and the value at the end of the path matches the type
197 // specified, it will return the specified value. Otherwise, the default 252 // specified, it will return the specified value. Otherwise, the default
198 // value (set when the pref was registered) will be returned. 253 // value (set when the pref was registered) will be returned.
199 bool GetBoolean(const char* path) const; 254 bool GetBoolean(const char* path) const;
200 int GetInteger(const char* path) const; 255 int GetInteger(const char* path) const;
201 double GetDouble(const char* path) const; 256 double GetDouble(const char* path) const;
202 std::string GetString(const char* path) const; 257 std::string GetString(const char* path) const;
203 FilePath GetFilePath(const char* path) const; 258 FilePath GetFilePath(const char* path) const;
204 259
(...skipping 16 matching lines...) Expand all
221 void SetInteger(const char* path, int value); 276 void SetInteger(const char* path, int value);
222 void SetDouble(const char* path, double value); 277 void SetDouble(const char* path, double value);
223 void SetString(const char* path, const std::string& value); 278 void SetString(const char* path, const std::string& value);
224 void SetFilePath(const char* path, const FilePath& value); 279 void SetFilePath(const char* path, const FilePath& value);
225 280
226 // Int64 helper methods that actually store the given value as a string. 281 // Int64 helper methods that actually store the given value as a string.
227 // Note that if obtaining the named value via GetDictionary or GetList, the 282 // Note that if obtaining the named value via GetDictionary or GetList, the
228 // Value type will be TYPE_STRING. 283 // Value type will be TYPE_STRING.
229 void SetInt64(const char* path, int64 value); 284 void SetInt64(const char* path, int64 value);
230 int64 GetInt64(const char* path) const; 285 int64 GetInt64(const char* path) const;
231 void RegisterInt64Pref(const char* path, int64 default_value);
232 286
233 // Returns true if a value has been set for the specified path. 287 // Returns true if a value has been set for the specified path.
234 // NOTE: this is NOT the same as FindPreference. In particular 288 // NOTE: this is NOT the same as FindPreference. In particular
235 // FindPreference returns whether RegisterXXX has been invoked, where as 289 // FindPreference returns whether RegisterXXX has been invoked, where as
236 // this checks if a value exists for the path. 290 // this checks if a value exists for the path.
237 bool HasPrefPath(const char* path) const; 291 bool HasPrefPath(const char* path) const;
238 292
239 // Returns a dictionary with effective preference values. The ownership 293 // Returns a dictionary with effective preference values. The ownership
240 // is passed to the caller. 294 // is passed to the caller.
241 DictionaryValue* GetPreferenceValues() const; 295 DictionaryValue* GetPreferenceValues() const;
242 296
243 // A helper method to quickly look up a preference. Returns NULL if the 297 // A helper method to quickly look up a preference. Returns NULL if the
244 // preference is not registered. 298 // preference is not registered.
245 const Preference* FindPreference(const char* pref_name) const; 299 const Preference* FindPreference(const char* pref_name) const;
246 300
247 bool ReadOnly() const; 301 bool ReadOnly() const;
248 302
303 // SyncableService getter.
304 // TODO(zea): Have PrefService implement SyncableService directly.
305 SyncableService* GetSyncableService();
306
249 protected: 307 protected:
250 // Construct a new pref service, specifying the pref sources as explicit 308 // Construct a new pref service, specifying the pref sources as explicit
251 // PrefStore pointers. This constructor is what CreatePrefService() ends up 309 // PrefStore pointers. This constructor is what CreatePrefService() ends up
252 // calling. It's also used for unit tests. 310 // calling. It's also used for unit tests.
253 PrefService(PrefStore* managed_platform_prefs, 311 PrefService(PrefStore* managed_platform_prefs,
254 PrefStore* managed_cloud_prefs, 312 PrefStore* managed_cloud_prefs,
255 PrefStore* extension_prefs, 313 PrefStore* extension_prefs,
256 PrefStore* command_line_prefs, 314 PrefStore* command_line_prefs,
257 PersistentPrefStore* user_prefs, 315 PersistentPrefStore* user_prefs,
258 PrefStore* recommended_platform_prefs, 316 PrefStore* recommended_platform_prefs,
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 // method with PREF_CHANGED. Note that observers should not call these methods 359 // method with PREF_CHANGED. Note that observers should not call these methods
302 // directly but rather use a PrefChangeRegistrar to make sure the observer 360 // directly but rather use a PrefChangeRegistrar to make sure the observer
303 // gets cleaned up properly. 361 // gets cleaned up properly.
304 virtual void AddPrefObserver(const char* path, NotificationObserver* obs); 362 virtual void AddPrefObserver(const char* path, NotificationObserver* obs);
305 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs); 363 virtual void RemovePrefObserver(const char* path, NotificationObserver* obs);
306 364
307 // Registers a new preference at |path|. The |default_value| must not be 365 // Registers a new preference at |path|. The |default_value| must not be
308 // NULL as it determines the preference value's type. 366 // NULL as it determines the preference value's type.
309 // RegisterPreference must not be called twice for the same path. 367 // RegisterPreference must not be called twice for the same path.
310 // This method takes ownership of |default_value|. 368 // This method takes ownership of |default_value|.
311 void RegisterPreference(const char* path, Value* default_value); 369 void RegisterPreference(const char* path,
370 Value* default_value,
371 PrefSyncStatus sync_status);
312 372
313 // Sets the value for this pref path in the user pref store and informs the 373 // Sets the value for this pref path in the user pref store and informs the
314 // PrefNotifier of the change. 374 // PrefNotifier of the change.
315 void SetUserPrefValue(const char* path, Value* new_value); 375 void SetUserPrefValue(const char* path, Value* new_value);
316 376
317 // Load preferences from storage, attempting to diagnose and handle errors. 377 // Load preferences from storage, attempting to diagnose and handle errors.
318 // This should only be called from the constructor. 378 // This should only be called from the constructor.
319 void InitFromStorage(); 379 void InitFromStorage();
320 380
321 // Used to set the value of dictionary or list values in the user pref store. 381 // Used to set the value of dictionary or list values in the user pref store.
(...skipping 15 matching lines...) Expand all
337 397
338 // Local cache of registered Preference objects. The default_store_ 398 // Local cache of registered Preference objects. The default_store_
339 // is authoritative with respect to what the types and default values 399 // is authoritative with respect to what the types and default values
340 // of registered preferences are. 400 // of registered preferences are.
341 mutable PreferenceSet prefs_; 401 mutable PreferenceSet prefs_;
342 402
343 // Holds delegator to be called after initialization, if async version 403 // Holds delegator to be called after initialization, if async version
344 // is used. 404 // is used.
345 PrefServiceDelegate* delegate_; 405 PrefServiceDelegate* delegate_;
346 406
407 // The model associator that maintains the links with the sync db.
408 scoped_ptr<PrefModelAssociator> pref_sync_associator_;
409
347 DISALLOW_COPY_AND_ASSIGN(PrefService); 410 DISALLOW_COPY_AND_ASSIGN(PrefService);
348 }; 411 };
349 412
350 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ 413 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_notifier_impl_unittest.cc ('k') | chrome/browser/prefs/pref_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698