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; | |
27 class DefaultPrefStore; | 26 class DefaultPrefStore; |
28 class PrefModelAssociator; | |
29 class PrefNotifier; | 27 class PrefNotifier; |
30 class PrefNotifierImpl; | 28 class PrefNotifierImpl; |
31 class PrefObserver; | 29 class PrefObserver; |
32 class PrefServiceObserver; | |
33 class PrefStore; | 30 class PrefStore; |
34 class PrefValueStore; | 31 class PrefValueStore; |
35 | 32 |
36 namespace syncer { | |
37 class SyncableService; | |
38 } | |
39 | |
40 namespace subtle { | 33 namespace subtle { |
41 class ScopedUserPrefUpdateBase; | 34 class ScopedUserPrefUpdateBase; |
42 }; | 35 }; |
43 | 36 |
| 37 // Base class for PrefServices. You can use the base class to read and |
| 38 // interact with preferences, but not to register new preferences; for |
| 39 // that see subclasses like PrefServiceSimple. |
44 class PrefService : public PrefServiceBase, public base::NonThreadSafe { | 40 class PrefService : public PrefServiceBase, public base::NonThreadSafe { |
45 public: | 41 public: |
46 enum PrefInitializationStatus { | 42 enum PrefInitializationStatus { |
47 INITIALIZATION_STATUS_WAITING, | 43 INITIALIZATION_STATUS_WAITING, |
48 INITIALIZATION_STATUS_SUCCESS, | 44 INITIALIZATION_STATUS_SUCCESS, |
49 INITIALIZATION_STATUS_CREATED_NEW_PROFILE, | 45 INITIALIZATION_STATUS_CREATED_NEW_PROFILE, |
50 INITIALIZATION_STATUS_ERROR | 46 INITIALIZATION_STATUS_ERROR |
51 }; | 47 }; |
52 | 48 |
53 // 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. |
(...skipping 29 matching lines...) Expand all Loading... |
83 PrefValueStore* pref_value_store() const { | 79 PrefValueStore* pref_value_store() const { |
84 return pref_service_->pref_value_store_.get(); | 80 return pref_service_->pref_value_store_.get(); |
85 } | 81 } |
86 | 82 |
87 const std::string name_; | 83 const std::string name_; |
88 | 84 |
89 const base::Value::Type type_; | 85 const base::Value::Type type_; |
90 | 86 |
91 // Reference to the PrefService in which this pref was created. | 87 // Reference to the PrefService in which this pref was created. |
92 const PrefService* pref_service_; | 88 const PrefService* pref_service_; |
93 | |
94 }; | 89 }; |
95 | 90 |
96 // Creates an incognito copy of the pref service that shares most pref stores | 91 // You may wish to use PrefServiceBuilder or one of its subclasses |
97 // but uses a fresh non-persistent overlay for the user pref store and an | 92 // for simplified construction. |
98 // individual extension pref store (to cache the effective extension prefs for | 93 PrefService( |
99 // incognito windows). | 94 PrefNotifierImpl* pref_notifier, |
100 PrefService* CreateIncognitoPrefService(PrefStore* incognito_extension_prefs); | 95 PrefValueStore* pref_value_store, |
101 | 96 PersistentPrefStore* user_prefs, |
| 97 DefaultPrefStore* default_store, |
| 98 base::Callback<void(PersistentPrefStore::PrefReadError)> |
| 99 read_error_callback, |
| 100 bool async); |
102 virtual ~PrefService(); | 101 virtual ~PrefService(); |
103 | 102 |
104 // Reloads the data from file. This should only be called when the importer | 103 // 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 | 104 // 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. | 105 // values while the importer process is running. Returns true on success. |
107 bool ReloadPersistentPrefs(); | 106 bool ReloadPersistentPrefs(); |
108 | 107 |
109 // Lands pending writes to disk. This should only be used if we need to save | 108 // Lands pending writes to disk. This should only be used if we need to save |
110 // immediately (basically, during shutdown). | 109 // immediately (basically, during shutdown). |
111 void CommitPendingWrite(); | 110 void CommitPendingWrite(); |
112 | 111 |
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. | 112 // PrefServiceBase implementation. |
126 virtual bool IsManagedPreference(const char* pref_name) const OVERRIDE; | 113 virtual bool IsManagedPreference(const char* pref_name) const OVERRIDE; |
127 virtual bool IsUserModifiablePreference(const char* pref_name) const OVERRIDE; | 114 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; | 115 virtual void UnregisterPreference(const char* path) OVERRIDE; |
202 virtual const PrefService::Preference* FindPreference( | 116 virtual const PrefService::Preference* FindPreference( |
203 const char* path) const OVERRIDE; | 117 const char* path) const OVERRIDE; |
204 virtual bool GetBoolean(const char* path) const OVERRIDE; | 118 virtual bool GetBoolean(const char* path) const OVERRIDE; |
205 virtual int GetInteger(const char* path) const OVERRIDE; | 119 virtual int GetInteger(const char* path) const OVERRIDE; |
206 virtual double GetDouble(const char* path) const OVERRIDE; | 120 virtual double GetDouble(const char* path) const OVERRIDE; |
207 virtual std::string GetString(const char* path) const OVERRIDE; | 121 virtual std::string GetString(const char* path) const OVERRIDE; |
208 virtual FilePath GetFilePath(const char* path) const OVERRIDE; | 122 virtual FilePath GetFilePath(const char* path) const OVERRIDE; |
209 virtual const base::DictionaryValue* GetDictionary( | 123 virtual const base::DictionaryValue* GetDictionary( |
210 const char* path) const OVERRIDE; | 124 const char* path) const OVERRIDE; |
(...skipping 25 matching lines...) Expand all Loading... |
236 bool HasPrefPath(const char* path) const; | 150 bool HasPrefPath(const char* path) const; |
237 | 151 |
238 // Returns a dictionary with effective preference values. The ownership | 152 // Returns a dictionary with effective preference values. The ownership |
239 // is passed to the caller. | 153 // is passed to the caller. |
240 base::DictionaryValue* GetPreferenceValues() const; | 154 base::DictionaryValue* GetPreferenceValues() const; |
241 | 155 |
242 bool ReadOnly() const; | 156 bool ReadOnly() const; |
243 | 157 |
244 PrefInitializationStatus GetInitializationStatus() const; | 158 PrefInitializationStatus GetInitializationStatus() const; |
245 | 159 |
246 // syncer::SyncableService getter. | 160 // Tell our PrefValueStore to update itself to |command_line_store|. |
247 // TODO(zea): Have PrefService implement syncer::SyncableService directly. | 161 // Takes ownership of the store. |
248 syncer::SyncableService* GetSyncableService(); | 162 virtual void UpdateCommandLinePrefStore(PrefStore* command_line_store); |
249 | |
250 // Tell our PrefValueStore to update itself using |command_line|. | |
251 // Do not call this after having derived an incognito or per tab pref service. | |
252 void UpdateCommandLinePrefStore(CommandLine* command_line); | |
253 | 163 |
254 // We run the callback once, when initialization completes. The bool | 164 // We run the callback once, when initialization completes. The bool |
255 // parameter will be set to true for successful initialization, | 165 // parameter will be set to true for successful initialization, |
256 // false for unsuccessful. | 166 // false for unsuccessful. |
257 void AddPrefInitObserver(base::Callback<void(bool)> callback); | 167 void AddPrefInitObserver(base::Callback<void(bool)> callback); |
258 | 168 |
259 protected: | 169 protected: |
260 // Construct a new pref service. This constructor is what | 170 // Registers a new preference at |path|. The |default_value| must not be |
261 // factory methods end up calling and what is used for unit tests. | 171 // NULL as it determines the preference value's type. |
262 PrefService(PrefNotifierImpl* pref_notifier, | 172 // RegisterPreference must not be called twice for the same path. |
263 PrefValueStore* pref_value_store, | 173 // This method takes ownership of |default_value|. |
264 PersistentPrefStore* user_prefs, | 174 void RegisterPreference(const char* path, base::Value* default_value); |
265 DefaultPrefStore* default_store, | |
266 PrefModelAssociator* pref_sync_associator, | |
267 base::Callback<void(PersistentPrefStore::PrefReadError)> | |
268 read_error_callback, | |
269 bool async); | |
270 | 175 |
271 // The PrefNotifier handles registering and notifying preference observers. | 176 // The PrefNotifier handles registering and notifying preference observers. |
272 // It is created and owned by this PrefService. Subclasses may access it for | 177 // It is created and owned by this PrefService. Subclasses may access it for |
273 // unit testing. | 178 // unit testing. |
274 scoped_ptr<PrefNotifierImpl> pref_notifier_; | 179 scoped_ptr<PrefNotifierImpl> pref_notifier_; |
275 | 180 |
| 181 // The PrefValueStore provides prioritized preference values. It is owned by |
| 182 // this PrefService. Subclasses may access it for unit testing. |
| 183 scoped_ptr<PrefValueStore> pref_value_store_; |
| 184 |
| 185 // Pref Stores and profile that we passed to the PrefValueStore. |
| 186 scoped_refptr<PersistentPrefStore> user_pref_store_; |
| 187 scoped_refptr<DefaultPrefStore> default_store_; |
| 188 |
| 189 // Callback to call when a read error occurs. |
| 190 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_; |
| 191 |
276 private: | 192 private: |
277 // Hash map expected to be fastest here since it minimises expensive | 193 // Hash map expected to be fastest here since it minimises expensive |
278 // string comparisons. Order is unimportant, and deletions are rare. | 194 // string comparisons. Order is unimportant, and deletions are rare. |
279 // Confirmed on Android where this speeded Chrome startup by roughly 50ms | 195 // Confirmed on Android where this speeded Chrome startup by roughly 50ms |
280 // vs. std::map, and by roughly 180ms vs. std::set of Preference pointers. | 196 // vs. std::map, and by roughly 180ms vs. std::set of Preference pointers. |
281 typedef base::hash_map<std::string, Preference> PreferenceMap; | 197 typedef base::hash_map<std::string, Preference> PreferenceMap; |
282 | 198 |
| 199 // Give access to Initialize(). |
283 friend class PrefServiceBuilder; | 200 friend class PrefServiceBuilder; |
284 | 201 |
285 // Give access to ReportUserPrefChanged() and GetMutableUserPref(). | 202 // Give access to ReportUserPrefChanged() and GetMutableUserPref(). |
286 friend class subtle::ScopedUserPrefUpdateBase; | 203 friend class subtle::ScopedUserPrefUpdateBase; |
287 | 204 |
288 // PrefServiceBase implementation (protected in base, private here). | 205 // PrefServiceBase implementation (protected in base, private here). |
289 virtual void AddPrefObserver(const char* path, | 206 virtual void AddPrefObserver(const char* path, |
290 PrefObserver* obs) OVERRIDE; | 207 PrefObserver* obs) OVERRIDE; |
291 virtual void RemovePrefObserver(const char* path, | 208 virtual void RemovePrefObserver(const char* path, |
292 PrefObserver* obs) OVERRIDE; | 209 PrefObserver* obs) OVERRIDE; |
293 | 210 |
294 // Sends notification of a changed preference. This needs to be called by | 211 // Sends notification of a changed preference. This needs to be called by |
295 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed. | 212 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed. |
296 void ReportUserPrefChanged(const std::string& key); | 213 void ReportUserPrefChanged(const std::string& key); |
297 | 214 |
298 // Registers a new preference at |path|. The |default_value| must not be | |
299 // NULL as it determines the preference value's type. | |
300 // RegisterPreference must not be called twice for the same path. | |
301 // This method takes ownership of |default_value|. | |
302 void RegisterPreference(const char* path, | |
303 base::Value* default_value, | |
304 PrefSyncStatus sync_status); | |
305 | |
306 // Sets the value for this pref path in the user pref store and informs the | 215 // Sets the value for this pref path in the user pref store and informs the |
307 // PrefNotifier of the change. | 216 // PrefNotifier of the change. |
308 void SetUserPrefValue(const char* path, base::Value* new_value); | 217 void SetUserPrefValue(const char* path, base::Value* new_value); |
309 | 218 |
310 // Load preferences from storage, attempting to diagnose and handle errors. | 219 // Load preferences from storage, attempting to diagnose and handle errors. |
311 // This should only be called from the constructor. | 220 // This should only be called from the constructor. |
312 void InitFromStorage(bool async); | 221 void InitFromStorage(bool async); |
313 | 222 |
314 // Used to set the value of dictionary or list values in the user pref store. | 223 // Used to set the value of dictionary or list values in the user pref store. |
315 // This will create a dictionary or list if one does not exist in the user | 224 // This will create a dictionary or list if one does not exist in the user |
316 // pref store. This method returns NULL only if you're requesting an | 225 // pref store. This method returns NULL only if you're requesting an |
317 // unregistered pref or a non-dict/non-list pref. | 226 // unregistered pref or a non-dict/non-list pref. |
318 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and | 227 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and |
319 // |path| must point to a registered preference of type |type|. | 228 // |path| must point to a registered preference of type |type|. |
320 // Ownership of the returned value remains at the user pref store. | 229 // Ownership of the returned value remains at the user pref store. |
321 base::Value* GetMutableUserPref(const char* path, | 230 base::Value* GetMutableUserPref(const char* path, |
322 base::Value::Type type); | 231 base::Value::Type type); |
323 | 232 |
324 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(), | 233 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(), |
325 // it has been added for performance. If is faster because it does | 234 // it has been added for performance. If is faster because it does |
326 // not need to find or create a Preference object to get the | 235 // not need to find or create a Preference object to get the |
327 // value (GetValue() calls back though the preference service to | 236 // value (GetValue() calls back though the preference service to |
328 // actually get the value.). | 237 // actually get the value.). |
329 const base::Value* GetPreferenceValue(const std::string& path) const; | 238 const base::Value* GetPreferenceValue(const std::string& path) const; |
330 | 239 |
331 // The PrefValueStore provides prioritized preference values. It is owned by | |
332 // this PrefService. Subclasses may access it for unit testing. | |
333 scoped_ptr<PrefValueStore> pref_value_store_; | |
334 | |
335 // Pref Stores and profile that we passed to the PrefValueStore. | |
336 scoped_refptr<PersistentPrefStore> user_pref_store_; | |
337 scoped_refptr<DefaultPrefStore> default_store_; | |
338 | |
339 // Local cache of registered Preference objects. The default_store_ | 240 // Local cache of registered Preference objects. The default_store_ |
340 // is authoritative with respect to what the types and default values | 241 // is authoritative with respect to what the types and default values |
341 // of registered preferences are. | 242 // of registered preferences are. |
342 mutable PreferenceMap prefs_map_; | 243 mutable PreferenceMap prefs_map_; |
343 | 244 |
344 // The model associator that maintains the links with the sync db. | |
345 scoped_ptr<PrefModelAssociator> pref_sync_associator_; | |
346 | |
347 // Callback to call when a read error occurs. | |
348 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_; | |
349 | |
350 // Whether CreateIncognitoPrefService() has been called to create a | |
351 // "forked" PrefService. | |
352 bool pref_service_forked_; | |
353 | |
354 ObserverList<PrefServiceObserver> observer_list_; | |
355 | |
356 DISALLOW_COPY_AND_ASSIGN(PrefService); | 245 DISALLOW_COPY_AND_ASSIGN(PrefService); |
357 }; | 246 }; |
358 | 247 |
| 248 // TODO(joi): Remove these forwards. They were placed here temporarily |
| 249 // to limit the size of the initial change that split |
| 250 // PrefServiceSimple and PrefServiceSyncable out of PrefService. |
| 251 #include "chrome/browser/prefs/pref_service_simple.h" |
| 252 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 253 |
359 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ | 254 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ |
OLD | NEW |