Chromium Code Reviews| 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/callback.h" | 17 #include "base/callback.h" |
| 18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/hash_tables.h" | 20 #include "base/hash_tables.h" |
| 21 #include "base/observer_list.h" | 21 #include "base/observer_list.h" |
| 22 #include "base/prefs/persistent_pref_store.h" | 22 #include "base/prefs/persistent_pref_store.h" |
| 23 #include "base/prefs/public/pref_service_base.h" | 23 #include "base/prefs/public/pref_service_base.h" |
| 24 #include "base/threading/non_thread_safe.h" | 24 #include "base/threading/non_thread_safe.h" |
| 25 | 25 |
| 26 class CommandLine; | 26 class CommandLine; |
| 27 class DefaultPrefStore; | 27 class DefaultPrefStore; |
| 28 class PrefModelAssociator; | |
| 29 class PrefNotifier; | 28 class PrefNotifier; |
| 30 class PrefNotifierImpl; | 29 class PrefNotifierImpl; |
| 31 class PrefObserver; | 30 class PrefObserver; |
| 32 class PrefServiceObserver; | |
| 33 class PrefStore; | 31 class PrefStore; |
| 34 class PrefValueStore; | 32 class PrefValueStore; |
| 35 | 33 |
| 36 namespace syncer { | |
| 37 class SyncableService; | |
| 38 } | |
| 39 | |
| 40 namespace subtle { | 34 namespace subtle { |
| 41 class ScopedUserPrefUpdateBase; | 35 class ScopedUserPrefUpdateBase; |
| 42 }; | 36 }; |
| 43 | 37 |
| 38 // Base class for PrefServices. You can use the base class to read and | |
| 39 // interact with preferences, but not to register new preferences; for | |
| 40 // that see subclasses like PrefServiceSimple. | |
| 44 class PrefService : public PrefServiceBase, public base::NonThreadSafe { | 41 class PrefService : public PrefServiceBase, public base::NonThreadSafe { |
| 45 public: | 42 public: |
| 46 enum PrefInitializationStatus { | 43 enum PrefInitializationStatus { |
| 47 INITIALIZATION_STATUS_WAITING, | 44 INITIALIZATION_STATUS_WAITING, |
| 48 INITIALIZATION_STATUS_SUCCESS, | 45 INITIALIZATION_STATUS_SUCCESS, |
| 49 INITIALIZATION_STATUS_CREATED_NEW_PROFILE, | 46 INITIALIZATION_STATUS_CREATED_NEW_PROFILE, |
| 50 INITIALIZATION_STATUS_ERROR | 47 INITIALIZATION_STATUS_ERROR |
| 51 }; | 48 }; |
| 52 | 49 |
| 53 // A helper class to store all the information associated with a preference. | 50 // A helper class to store all the information associated with a preference. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 83 PrefValueStore* pref_value_store() const { | 80 PrefValueStore* pref_value_store() const { |
| 84 return pref_service_->pref_value_store_.get(); | 81 return pref_service_->pref_value_store_.get(); |
| 85 } | 82 } |
| 86 | 83 |
| 87 const std::string name_; | 84 const std::string name_; |
| 88 | 85 |
| 89 const base::Value::Type type_; | 86 const base::Value::Type type_; |
| 90 | 87 |
| 91 // Reference to the PrefService in which this pref was created. | 88 // Reference to the PrefService in which this pref was created. |
| 92 const PrefService* pref_service_; | 89 const PrefService* pref_service_; |
| 93 | |
| 94 }; | 90 }; |
| 95 | 91 |
| 96 // Creates an incognito copy of the pref service that shares most pref stores | 92 // Constructs a PrefService that is uninitialized and unusable. You |
| 97 // but uses a fresh non-persistent overlay for the user pref store and an | 93 // must pass it to PrefServiceBuilder::Create for initialization. |
| 98 // individual extension pref store (to cache the effective extension prefs for | 94 PrefService(); |
|
Mattias Nissler (ping if slow)
2012/12/14 13:50:06
Please not. Let's pass all dependencies through th
| |
| 99 // incognito windows). | |
| 100 PrefService* CreateIncognitoPrefService(PrefStore* incognito_extension_prefs); | |
| 101 | |
| 102 virtual ~PrefService(); | 95 virtual ~PrefService(); |
| 103 | 96 |
| 104 // Reloads the data from file. This should only be called when the importer | 97 // Reloads the data from file. This should only be called when the importer |
| 105 // is running during first run, and the main process may not change pref | 98 // is running during first run, and the main process may not change pref |
| 106 // values while the importer process is running. Returns true on success. | 99 // values while the importer process is running. Returns true on success. |
| 107 bool ReloadPersistentPrefs(); | 100 bool ReloadPersistentPrefs(); |
| 108 | 101 |
| 109 // Lands pending writes to disk. This should only be used if we need to save | 102 // Lands pending writes to disk. This should only be used if we need to save |
| 110 // immediately (basically, during shutdown). | 103 // immediately (basically, during shutdown). |
| 111 void CommitPendingWrite(); | 104 void CommitPendingWrite(); |
| 112 | 105 |
| 113 void AddObserver(PrefServiceObserver* observer); | |
| 114 void RemoveObserver(PrefServiceObserver* observer); | |
| 115 | |
| 116 // Returns true if preferences state has synchronized with the remote | |
| 117 // preferences. If true is returned it can be assumed the local preferences | |
| 118 // has applied changes from the remote preferences. The two may not be | |
| 119 // identical if a change is in flight (from either side). | |
| 120 bool IsSyncing(); | |
| 121 | |
| 122 // Invoked internally when the IsSyncing() state changes. | |
| 123 void OnIsSyncingChanged(); | |
| 124 | |
| 125 // PrefServiceBase implementation. | 106 // PrefServiceBase implementation. |
| 126 virtual bool IsManagedPreference(const char* pref_name) const OVERRIDE; | 107 virtual bool IsManagedPreference(const char* pref_name) const OVERRIDE; |
| 127 virtual bool IsUserModifiablePreference(const char* pref_name) const OVERRIDE; | 108 virtual bool IsUserModifiablePreference(const char* pref_name) const OVERRIDE; |
| 128 virtual void RegisterBooleanPref(const char* path, | |
| 129 bool default_value) OVERRIDE; | |
| 130 virtual void RegisterIntegerPref(const char* path, | |
| 131 int default_value) OVERRIDE; | |
| 132 virtual void RegisterDoublePref(const char* path, | |
| 133 double default_value) OVERRIDE; | |
| 134 virtual void RegisterStringPref(const char* path, | |
| 135 const std::string& default_value) OVERRIDE; | |
| 136 virtual void RegisterFilePathPref(const char* path, | |
| 137 const FilePath& default_value) OVERRIDE; | |
| 138 virtual void RegisterListPref(const char* path) OVERRIDE; | |
| 139 virtual void RegisterDictionaryPref(const char* path) OVERRIDE; | |
| 140 virtual void RegisterListPref(const char* path, | |
| 141 base::ListValue* default_value) OVERRIDE; | |
| 142 virtual void RegisterDictionaryPref( | |
| 143 const char* path, base::DictionaryValue* default_value) OVERRIDE; | |
| 144 virtual void RegisterLocalizedBooleanPref( | |
| 145 const char* path, int locale_default_message_id) OVERRIDE; | |
| 146 virtual void RegisterLocalizedIntegerPref( | |
| 147 const char* path, int locale_default_message_id) OVERRIDE; | |
| 148 virtual void RegisterLocalizedDoublePref( | |
| 149 const char* path, int locale_default_message_id) OVERRIDE; | |
| 150 virtual void RegisterLocalizedStringPref( | |
| 151 const char* path, int locale_default_message_id) OVERRIDE; | |
| 152 virtual void RegisterInt64Pref(const char* path, | |
| 153 int64 default_value) OVERRIDE; | |
| 154 virtual void RegisterBooleanPref(const char* path, | |
| 155 bool default_value, | |
| 156 PrefSyncStatus sync_status) OVERRIDE; | |
| 157 virtual void RegisterIntegerPref(const char* path, | |
| 158 int default_value, | |
| 159 PrefSyncStatus sync_status) OVERRIDE; | |
| 160 virtual void RegisterDoublePref(const char* path, | |
| 161 double default_value, | |
| 162 PrefSyncStatus sync_status) OVERRIDE; | |
| 163 virtual void RegisterStringPref(const char* path, | |
| 164 const std::string& default_value, | |
| 165 PrefSyncStatus sync_status) OVERRIDE; | |
| 166 virtual void RegisterFilePathPref(const char* path, | |
| 167 const FilePath& default_value, | |
| 168 PrefSyncStatus sync_status) OVERRIDE; | |
| 169 virtual void RegisterListPref(const char* path, | |
| 170 PrefSyncStatus sync_status) OVERRIDE; | |
| 171 virtual void RegisterDictionaryPref(const char* path, | |
| 172 PrefSyncStatus sync_status) OVERRIDE; | |
| 173 virtual void RegisterListPref(const char* path, | |
| 174 base::ListValue* default_value, | |
| 175 PrefSyncStatus sync_status) OVERRIDE; | |
| 176 virtual void RegisterDictionaryPref(const char* path, | |
| 177 base::DictionaryValue* default_value, | |
| 178 PrefSyncStatus sync_status) OVERRIDE; | |
| 179 virtual void RegisterLocalizedBooleanPref( | |
| 180 const char* path, | |
| 181 int locale_default_message_id, | |
| 182 PrefSyncStatus sync_status) OVERRIDE; | |
| 183 virtual void RegisterLocalizedIntegerPref( | |
| 184 const char* path, | |
| 185 int locale_default_message_id, | |
| 186 PrefSyncStatus sync_status) OVERRIDE; | |
| 187 virtual void RegisterLocalizedDoublePref( | |
| 188 const char* path, | |
| 189 int locale_default_message_id, | |
| 190 PrefSyncStatus sync_status) OVERRIDE; | |
| 191 virtual void RegisterLocalizedStringPref( | |
| 192 const char* path, | |
| 193 int locale_default_message_id, | |
| 194 PrefSyncStatus sync_status) OVERRIDE; | |
| 195 virtual void RegisterInt64Pref(const char* path, | |
| 196 int64 default_value, | |
| 197 PrefSyncStatus sync_status) OVERRIDE; | |
| 198 virtual void RegisterUint64Pref(const char* path, | |
| 199 uint64 default_value, | |
| 200 PrefSyncStatus sync_status) OVERRIDE; | |
| 201 virtual void UnregisterPreference(const char* path) OVERRIDE; | 109 virtual void UnregisterPreference(const char* path) OVERRIDE; |
| 202 virtual const PrefService::Preference* FindPreference( | 110 virtual const PrefService::Preference* FindPreference( |
| 203 const char* path) const OVERRIDE; | 111 const char* path) const OVERRIDE; |
| 204 virtual bool GetBoolean(const char* path) const OVERRIDE; | 112 virtual bool GetBoolean(const char* path) const OVERRIDE; |
| 205 virtual int GetInteger(const char* path) const OVERRIDE; | 113 virtual int GetInteger(const char* path) const OVERRIDE; |
| 206 virtual double GetDouble(const char* path) const OVERRIDE; | 114 virtual double GetDouble(const char* path) const OVERRIDE; |
| 207 virtual std::string GetString(const char* path) const OVERRIDE; | 115 virtual std::string GetString(const char* path) const OVERRIDE; |
| 208 virtual FilePath GetFilePath(const char* path) const OVERRIDE; | 116 virtual FilePath GetFilePath(const char* path) const OVERRIDE; |
| 209 virtual const base::DictionaryValue* GetDictionary( | 117 virtual const base::DictionaryValue* GetDictionary( |
| 210 const char* path) const OVERRIDE; | 118 const char* path) const OVERRIDE; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 236 bool HasPrefPath(const char* path) const; | 144 bool HasPrefPath(const char* path) const; |
| 237 | 145 |
| 238 // Returns a dictionary with effective preference values. The ownership | 146 // Returns a dictionary with effective preference values. The ownership |
| 239 // is passed to the caller. | 147 // is passed to the caller. |
| 240 base::DictionaryValue* GetPreferenceValues() const; | 148 base::DictionaryValue* GetPreferenceValues() const; |
| 241 | 149 |
| 242 bool ReadOnly() const; | 150 bool ReadOnly() const; |
| 243 | 151 |
| 244 PrefInitializationStatus GetInitializationStatus() const; | 152 PrefInitializationStatus GetInitializationStatus() const; |
| 245 | 153 |
| 246 // syncer::SyncableService getter. | |
| 247 // TODO(zea): Have PrefService implement syncer::SyncableService directly. | |
| 248 syncer::SyncableService* GetSyncableService(); | |
| 249 | |
| 250 // Tell our PrefValueStore to update itself using |command_line|. | 154 // Tell our PrefValueStore to update itself using |command_line|. |
| 251 // Do not call this after having derived an incognito or per tab pref service. | 155 virtual void UpdateCommandLinePrefStore(CommandLine* command_line); |
| 252 void UpdateCommandLinePrefStore(CommandLine* command_line); | |
| 253 | 156 |
| 254 // We run the callback once, when initialization completes. The bool | 157 // We run the callback once, when initialization completes. The bool |
| 255 // parameter will be set to true for successful initialization, | 158 // parameter will be set to true for successful initialization, |
| 256 // false for unsuccessful. | 159 // false for unsuccessful. |
| 257 void AddPrefInitObserver(base::Callback<void(bool)> callback); | 160 void AddPrefInitObserver(base::Callback<void(bool)> callback); |
| 258 | 161 |
| 259 protected: | 162 protected: |
| 260 // Construct a new pref service. This constructor is what | 163 // Initialize a PrefService instance. This is what |
| 261 // factory methods end up calling and what is used for unit tests. | 164 // PrefServiceBuilder::Create ends up calling. |
| 262 PrefService(PrefNotifierImpl* pref_notifier, | 165 virtual void Initialize( |
| 263 PrefValueStore* pref_value_store, | 166 PrefNotifierImpl* pref_notifier, |
| 264 PersistentPrefStore* user_prefs, | 167 PrefValueStore* pref_value_store, |
| 265 DefaultPrefStore* default_store, | 168 PersistentPrefStore* user_prefs, |
| 266 PrefModelAssociator* pref_sync_associator, | 169 DefaultPrefStore* default_store, |
| 267 base::Callback<std::string(int)> get_localized_string_method, | 170 base::Callback<std::string(int)> get_localized_string_method, |
| 268 base::Callback<void(PersistentPrefStore::PrefReadError)> | 171 base::Callback<void(PersistentPrefStore::PrefReadError)> |
| 269 read_error_callback, | 172 read_error_callback, |
| 270 bool async); | 173 bool async); |
| 174 | |
| 175 // Registers a new preference at |path|. The |default_value| must not be | |
| 176 // NULL as it determines the preference value's type. | |
| 177 // RegisterPreference must not be called twice for the same path. | |
| 178 // This method takes ownership of |default_value|. | |
| 179 void RegisterPreference(const char* path, base::Value* default_value); | |
| 271 | 180 |
| 272 // The PrefNotifier handles registering and notifying preference observers. | 181 // The PrefNotifier handles registering and notifying preference observers. |
| 273 // It is created and owned by this PrefService. Subclasses may access it for | 182 // It is created and owned by this PrefService. Subclasses may access it for |
| 274 // unit testing. | 183 // unit testing. |
| 275 scoped_ptr<PrefNotifierImpl> pref_notifier_; | 184 scoped_ptr<PrefNotifierImpl> pref_notifier_; |
| 276 | 185 |
| 186 // The PrefValueStore provides prioritized preference values. It is owned by | |
| 187 // this PrefService. Subclasses may access it for unit testing. | |
| 188 scoped_ptr<PrefValueStore> pref_value_store_; | |
| 189 | |
| 190 // Pref Stores and profile that we passed to the PrefValueStore. | |
| 191 scoped_refptr<PersistentPrefStore> user_pref_store_; | |
| 192 scoped_refptr<DefaultPrefStore> default_store_; | |
| 193 | |
| 194 // Function with which to retrieve localized UTF8 resources. | |
| 195 base::Callback<std::string(int)> get_localized_string_method_; | |
| 196 | |
| 197 // Callback to call when a read error occurs. | |
| 198 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_; | |
| 199 | |
| 277 private: | 200 private: |
| 278 // Hash map expected to be fastest here since it minimises expensive | 201 // Hash map expected to be fastest here since it minimises expensive |
| 279 // string comparisons. Order is unimportant, and deletions are rare. | 202 // string comparisons. Order is unimportant, and deletions are rare. |
| 280 // Confirmed on Android where this speeded Chrome startup by roughly 50ms | 203 // Confirmed on Android where this speeded Chrome startup by roughly 50ms |
| 281 // vs. std::map, and by roughly 180ms vs. std::set of Preference pointers. | 204 // vs. std::map, and by roughly 180ms vs. std::set of Preference pointers. |
| 282 typedef base::hash_map<std::string, Preference> PreferenceMap; | 205 typedef base::hash_map<std::string, Preference> PreferenceMap; |
| 283 | 206 |
| 284 friend class PrefServiceBuilder; | 207 friend class PrefServiceBuilder; |
| 285 | 208 |
| 286 // Give access to ReportUserPrefChanged() and GetMutableUserPref(). | 209 // Give access to ReportUserPrefChanged() and GetMutableUserPref(). |
| 287 friend class subtle::ScopedUserPrefUpdateBase; | 210 friend class subtle::ScopedUserPrefUpdateBase; |
| 288 | 211 |
| 289 // PrefServiceBase implementation (protected in base, private here). | 212 // PrefServiceBase implementation (protected in base, private here). |
| 290 virtual void AddPrefObserver(const char* path, | 213 virtual void AddPrefObserver(const char* path, |
| 291 PrefObserver* obs) OVERRIDE; | 214 PrefObserver* obs) OVERRIDE; |
| 292 virtual void RemovePrefObserver(const char* path, | 215 virtual void RemovePrefObserver(const char* path, |
| 293 PrefObserver* obs) OVERRIDE; | 216 PrefObserver* obs) OVERRIDE; |
| 294 | 217 |
| 295 // Sends notification of a changed preference. This needs to be called by | 218 // Sends notification of a changed preference. This needs to be called by |
| 296 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed. | 219 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed. |
| 297 void ReportUserPrefChanged(const std::string& key); | 220 void ReportUserPrefChanged(const std::string& key); |
| 298 | 221 |
| 299 // Registers a new preference at |path|. The |default_value| must not be | |
| 300 // NULL as it determines the preference value's type. | |
| 301 // RegisterPreference must not be called twice for the same path. | |
| 302 // This method takes ownership of |default_value|. | |
| 303 void RegisterPreference(const char* path, | |
| 304 base::Value* default_value, | |
| 305 PrefSyncStatus sync_status); | |
| 306 | |
| 307 // Sets the value for this pref path in the user pref store and informs the | 222 // Sets the value for this pref path in the user pref store and informs the |
| 308 // PrefNotifier of the change. | 223 // PrefNotifier of the change. |
| 309 void SetUserPrefValue(const char* path, base::Value* new_value); | 224 void SetUserPrefValue(const char* path, base::Value* new_value); |
| 310 | 225 |
| 311 // Load preferences from storage, attempting to diagnose and handle errors. | 226 // Load preferences from storage, attempting to diagnose and handle errors. |
| 312 // This should only be called from the constructor. | 227 // This should only be called from the constructor. |
| 313 void InitFromStorage(bool async); | 228 void InitFromStorage(bool async); |
| 314 | 229 |
| 315 // Used to set the value of dictionary or list values in the user pref store. | 230 // Used to set the value of dictionary or list values in the user pref store. |
| 316 // This will create a dictionary or list if one does not exist in the user | 231 // This will create a dictionary or list if one does not exist in the user |
| 317 // pref store. This method returns NULL only if you're requesting an | 232 // pref store. This method returns NULL only if you're requesting an |
| 318 // unregistered pref or a non-dict/non-list pref. | 233 // unregistered pref or a non-dict/non-list pref. |
| 319 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and | 234 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and |
| 320 // |path| must point to a registered preference of type |type|. | 235 // |path| must point to a registered preference of type |type|. |
| 321 // Ownership of the returned value remains at the user pref store. | 236 // Ownership of the returned value remains at the user pref store. |
| 322 base::Value* GetMutableUserPref(const char* path, | 237 base::Value* GetMutableUserPref(const char* path, |
| 323 base::Value::Type type); | 238 base::Value::Type type); |
| 324 | 239 |
| 325 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(), | 240 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(), |
| 326 // it has been added for performance. If is faster because it does | 241 // it has been added for performance. If is faster because it does |
| 327 // not need to find or create a Preference object to get the | 242 // not need to find or create a Preference object to get the |
| 328 // value (GetValue() calls back though the preference service to | 243 // value (GetValue() calls back though the preference service to |
| 329 // actually get the value.). | 244 // actually get the value.). |
| 330 const base::Value* GetPreferenceValue(const std::string& path) const; | 245 const base::Value* GetPreferenceValue(const std::string& path) const; |
| 331 | 246 |
| 332 // The PrefValueStore provides prioritized preference values. It is owned by | |
| 333 // this PrefService. Subclasses may access it for unit testing. | |
| 334 scoped_ptr<PrefValueStore> pref_value_store_; | |
| 335 | |
| 336 // Pref Stores and profile that we passed to the PrefValueStore. | |
| 337 scoped_refptr<PersistentPrefStore> user_pref_store_; | |
| 338 scoped_refptr<DefaultPrefStore> default_store_; | |
| 339 | |
| 340 // Local cache of registered Preference objects. The default_store_ | 247 // Local cache of registered Preference objects. The default_store_ |
| 341 // is authoritative with respect to what the types and default values | 248 // is authoritative with respect to what the types and default values |
| 342 // of registered preferences are. | 249 // of registered preferences are. |
| 343 mutable PreferenceMap prefs_map_; | 250 mutable PreferenceMap prefs_map_; |
| 344 | 251 |
| 345 // The model associator that maintains the links with the sync db. | 252 DISALLOW_COPY_AND_ASSIGN(PrefService); |
| 346 scoped_ptr<PrefModelAssociator> pref_sync_associator_; | 253 }; |
| 347 | 254 |
| 348 // Function with which to retrieve localized UTF8 resources. | |
| 349 base::Callback<std::string(int)> get_localized_string_method_; | |
| 350 | 255 |
| 351 // Callback to call when a read error occurs. | 256 |
| 352 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_; | 257 |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 // A simple PrefService implementation. | |
| 269 class PrefServiceSimple : public PrefService { | |
| 270 public: | |
| 271 void RegisterBooleanPref(const char* path, bool default_value); | |
| 272 void RegisterIntegerPref(const char* path, int default_value); | |
| 273 void RegisterDoublePref(const char* path, double default_value); | |
| 274 void RegisterStringPref(const char* path, const std::string& default_value); | |
| 275 void RegisterFilePathPref(const char* path, const FilePath& default_value); | |
| 276 void RegisterListPref(const char* path); | |
| 277 void RegisterDictionaryPref(const char* path); | |
| 278 void RegisterListPref(const char* path, base::ListValue* default_value); | |
| 279 void RegisterDictionaryPref( | |
| 280 const char* path, base::DictionaryValue* default_value); | |
| 281 void RegisterLocalizedBooleanPref( | |
| 282 const char* path, int locale_default_message_id); | |
| 283 void RegisterLocalizedIntegerPref( | |
| 284 const char* path, int locale_default_message_id); | |
| 285 void RegisterLocalizedDoublePref( | |
| 286 const char* path, int locale_default_message_id); | |
| 287 void RegisterLocalizedStringPref( | |
| 288 const char* path, int locale_default_message_id); | |
| 289 void RegisterInt64Pref(const char* path, | |
| 290 int64 default_value); | |
| 291 }; | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 #include "chrome/browser/prefs/pref_model_associator.h" | |
| 300 | |
| 301 // TODO(joi) Move to c/b/prefs and rename PrefServiceSyncableObserver. | |
| 302 class PrefServiceObserver; | |
| 303 | |
| 304 namespace syncer { | |
| 305 class SyncableService; | |
| 306 } | |
| 307 | |
| 308 // A PrefService that can be synced. Users are forced to declare | |
| 309 // whether preferences are syncable or not when registering them to | |
| 310 // this PrefService. | |
| 311 class PrefServiceSyncable : public PrefService { | |
| 312 public: | |
| 313 // Enum used when registering preferences to determine if it should be synced | |
| 314 // or not. This is only used for profile prefs, not local state prefs. | |
| 315 // See the Register*Pref methods for profile prefs below. | |
| 316 enum PrefSyncStatus { | |
| 317 UNSYNCABLE_PREF, | |
| 318 SYNCABLE_PREF | |
| 319 }; | |
| 320 | |
| 321 PrefServiceSyncable(); | |
| 322 virtual ~PrefServiceSyncable(); | |
| 323 | |
| 324 // Creates an incognito copy of the pref service that shares most pref stores | |
| 325 // but uses a fresh non-persistent overlay for the user pref store and an | |
| 326 // individual extension pref store (to cache the effective extension prefs for | |
| 327 // incognito windows). | |
| 328 PrefServiceSyncable* CreateIncognitoPrefService( | |
| 329 PrefStore* incognito_extension_prefs); | |
| 330 | |
| 331 // Returns true if preferences state has synchronized with the remote | |
| 332 // preferences. If true is returned it can be assumed the local preferences | |
| 333 // has applied changes from the remote preferences. The two may not be | |
| 334 // identical if a change is in flight (from either side). | |
| 335 bool IsSyncing(); | |
| 336 | |
| 337 void AddObserver(PrefServiceObserver* observer); | |
| 338 void RemoveObserver(PrefServiceObserver* observer); | |
| 339 | |
| 340 virtual void UnregisterPreference(const char* path) OVERRIDE; | |
| 341 | |
| 342 void RegisterBooleanPref(const char* path, | |
| 343 bool default_value, | |
| 344 PrefSyncStatus sync_status); | |
| 345 void RegisterIntegerPref(const char* path, | |
| 346 int default_value, | |
| 347 PrefSyncStatus sync_status); | |
| 348 void RegisterDoublePref(const char* path, | |
| 349 double default_value, | |
| 350 PrefSyncStatus sync_status); | |
| 351 void RegisterStringPref(const char* path, | |
| 352 const std::string& default_value, | |
| 353 PrefSyncStatus sync_status); | |
| 354 void RegisterFilePathPref(const char* path, | |
| 355 const FilePath& default_value, | |
| 356 PrefSyncStatus sync_status); | |
| 357 void RegisterListPref(const char* path, | |
| 358 PrefSyncStatus sync_status); | |
| 359 void RegisterDictionaryPref(const char* path, | |
| 360 PrefSyncStatus sync_status); | |
| 361 void RegisterListPref(const char* path, | |
| 362 base::ListValue* default_value, | |
| 363 PrefSyncStatus sync_status); | |
| 364 void RegisterDictionaryPref(const char* path, | |
| 365 base::DictionaryValue* default_value, | |
| 366 PrefSyncStatus sync_status); | |
| 367 void RegisterLocalizedBooleanPref(const char* path, | |
| 368 int locale_default_message_id, | |
| 369 PrefSyncStatus sync_status); | |
| 370 void RegisterLocalizedIntegerPref(const char* path, | |
| 371 int locale_default_message_id, | |
| 372 PrefSyncStatus sync_status); | |
| 373 void RegisterLocalizedDoublePref(const char* path, | |
| 374 int locale_default_message_id, | |
| 375 PrefSyncStatus sync_status); | |
| 376 void RegisterLocalizedStringPref(const char* path, | |
| 377 int locale_default_message_id, | |
| 378 PrefSyncStatus sync_status); | |
| 379 void RegisterInt64Pref(const char* path, | |
| 380 int64 default_value, | |
| 381 PrefSyncStatus sync_status); | |
| 382 void RegisterUint64Pref(const char* path, | |
| 383 uint64 default_value, | |
| 384 PrefSyncStatus sync_status); | |
| 385 | |
| 386 // syncer::SyncableService getter. | |
| 387 // TODO(zea): Have PrefService implement syncer::SyncableService directly. | |
| 388 syncer::SyncableService* GetSyncableService(); | |
| 389 | |
| 390 // Do not call this after having derived an incognito or per tab pref service. | |
| 391 virtual void UpdateCommandLinePrefStore(CommandLine* command_line) OVERRIDE; | |
| 392 | |
| 393 protected: | |
| 394 virtual void Initialize( | |
| 395 PrefNotifierImpl* pref_notifier, | |
| 396 PrefValueStore* pref_value_store, | |
| 397 PersistentPrefStore* user_prefs, | |
| 398 DefaultPrefStore* default_store, | |
| 399 base::Callback<std::string(int)> get_localized_string_method, | |
| 400 base::Callback<void(PersistentPrefStore::PrefReadError)> | |
| 401 read_error_callback, | |
| 402 bool async) OVERRIDE; | |
| 403 | |
| 404 private: | |
| 405 // Invoked internally when the IsSyncing() state changes. | |
| 406 void OnIsSyncingChanged(); | |
| 407 | |
| 408 void RegisterSyncablePreference( | |
| 409 const char* path, Value* default_value, PrefSyncStatus sync_status); | |
| 353 | 410 |
| 354 // Whether CreateIncognitoPrefService() has been called to create a | 411 // Whether CreateIncognitoPrefService() has been called to create a |
| 355 // "forked" PrefService. | 412 // "forked" PrefService. |
| 356 bool pref_service_forked_; | 413 bool pref_service_forked_; |
| 357 | 414 |
| 415 PrefModelAssociator pref_sync_associator_; | |
| 416 | |
| 358 ObserverList<PrefServiceObserver> observer_list_; | 417 ObserverList<PrefServiceObserver> observer_list_; |
| 359 | |
| 360 DISALLOW_COPY_AND_ASSIGN(PrefService); | |
| 361 }; | 418 }; |
| 362 | 419 |
| 363 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 420 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
| OLD | NEW |