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

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

Issue 11573065: Not for check-in. Manually reviewable parts of https://codereview.chromium.org/11570009/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years 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) 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
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
97 // but uses a fresh non-persistent overlay for the user pref store and an
98 // individual extension pref store (to cache the effective extension prefs for
99 // incognito windows).
100 PrefService* CreateIncognitoPrefService(PrefStore* incognito_extension_prefs);
101
102 virtual ~PrefService(); 91 virtual ~PrefService();
103 92
104 // Reloads the data from file. This should only be called when the importer 93 // 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 94 // 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. 95 // values while the importer process is running. Returns true on success.
107 bool ReloadPersistentPrefs(); 96 bool ReloadPersistentPrefs();
108 97
109 // Lands pending writes to disk. This should only be used if we need to save 98 // Lands pending writes to disk. This should only be used if we need to save
110 // immediately (basically, during shutdown). 99 // immediately (basically, during shutdown).
111 void CommitPendingWrite(); 100 void CommitPendingWrite();
112 101
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. 102 // PrefServiceBase implementation.
126 virtual bool IsManagedPreference(const char* pref_name) const OVERRIDE; 103 virtual bool IsManagedPreference(const char* pref_name) const OVERRIDE;
127 virtual bool IsUserModifiablePreference(const char* pref_name) const OVERRIDE; 104 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; 105 virtual void UnregisterPreference(const char* path) OVERRIDE;
202 virtual const PrefService::Preference* FindPreference( 106 virtual const PrefService::Preference* FindPreference(
203 const char* path) const OVERRIDE; 107 const char* path) const OVERRIDE;
204 virtual bool GetBoolean(const char* path) const OVERRIDE; 108 virtual bool GetBoolean(const char* path) const OVERRIDE;
205 virtual int GetInteger(const char* path) const OVERRIDE; 109 virtual int GetInteger(const char* path) const OVERRIDE;
206 virtual double GetDouble(const char* path) const OVERRIDE; 110 virtual double GetDouble(const char* path) const OVERRIDE;
207 virtual std::string GetString(const char* path) const OVERRIDE; 111 virtual std::string GetString(const char* path) const OVERRIDE;
208 virtual FilePath GetFilePath(const char* path) const OVERRIDE; 112 virtual FilePath GetFilePath(const char* path) const OVERRIDE;
209 virtual const base::DictionaryValue* GetDictionary( 113 virtual const base::DictionaryValue* GetDictionary(
210 const char* path) const OVERRIDE; 114 const char* path) const OVERRIDE;
(...skipping 25 matching lines...) Expand all
236 bool HasPrefPath(const char* path) const; 140 bool HasPrefPath(const char* path) const;
237 141
238 // Returns a dictionary with effective preference values. The ownership 142 // Returns a dictionary with effective preference values. The ownership
239 // is passed to the caller. 143 // is passed to the caller.
240 base::DictionaryValue* GetPreferenceValues() const; 144 base::DictionaryValue* GetPreferenceValues() const;
241 145
242 bool ReadOnly() const; 146 bool ReadOnly() const;
243 147
244 PrefInitializationStatus GetInitializationStatus() const; 148 PrefInitializationStatus GetInitializationStatus() const;
245 149
246 // syncer::SyncableService getter. 150 // Tell our PrefValueStore to update itself to |command_line_store|.
247 // TODO(zea): Have PrefService implement syncer::SyncableService directly. 151 // Takes ownership of the store.
248 syncer::SyncableService* GetSyncableService(); 152 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 153
254 // We run the callback once, when initialization completes. The bool 154 // We run the callback once, when initialization completes. The bool
255 // parameter will be set to true for successful initialization, 155 // parameter will be set to true for successful initialization,
256 // false for unsuccessful. 156 // false for unsuccessful.
257 void AddPrefInitObserver(base::Callback<void(bool)> callback); 157 void AddPrefInitObserver(base::Callback<void(bool)> callback);
258 158
259 protected: 159 protected:
260 // Construct a new pref service. This constructor is what 160 // Constructs a PrefService that is uninitialized and unusable. You
261 // factory methods end up calling and what is used for unit tests. 161 // must pass it to PrefServiceBuilder::Build for initialization.
262 PrefService(PrefNotifierImpl* pref_notifier, 162 PrefService();
263 PrefValueStore* pref_value_store, 163
264 PersistentPrefStore* user_prefs, 164 // Initialize a PrefService instance. This is what
265 DefaultPrefStore* default_store, 165 // PrefServiceBuilder::Build ends up calling.
266 PrefModelAssociator* pref_sync_associator, 166 virtual void Initialize(
267 base::Callback<std::string(int)> get_localized_string_method, 167 PrefNotifierImpl* pref_notifier,
268 base::Callback<void(PersistentPrefStore::PrefReadError)> 168 PrefValueStore* pref_value_store,
269 read_error_callback, 169 PersistentPrefStore* user_prefs,
270 bool async); 170 DefaultPrefStore* default_store,
171 base::Callback<void(PersistentPrefStore::PrefReadError)>
172 read_error_callback,
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 // Callback to call when a read error occurs.
195 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_;
196
277 private: 197 private:
278 // Hash map expected to be fastest here since it minimises expensive 198 // Hash map expected to be fastest here since it minimises expensive
279 // string comparisons. Order is unimportant, and deletions are rare. 199 // string comparisons. Order is unimportant, and deletions are rare.
280 // Confirmed on Android where this speeded Chrome startup by roughly 50ms 200 // 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. 201 // vs. std::map, and by roughly 180ms vs. std::set of Preference pointers.
282 typedef base::hash_map<std::string, Preference> PreferenceMap; 202 typedef base::hash_map<std::string, Preference> PreferenceMap;
283 203
204 // Give access to Initialize().
284 friend class PrefServiceBuilder; 205 friend class PrefServiceBuilder;
285 206
286 // Give access to ReportUserPrefChanged() and GetMutableUserPref(). 207 // Give access to ReportUserPrefChanged() and GetMutableUserPref().
287 friend class subtle::ScopedUserPrefUpdateBase; 208 friend class subtle::ScopedUserPrefUpdateBase;
288 209
289 // PrefServiceBase implementation (protected in base, private here). 210 // PrefServiceBase implementation (protected in base, private here).
290 virtual void AddPrefObserver(const char* path, 211 virtual void AddPrefObserver(const char* path,
291 PrefObserver* obs) OVERRIDE; 212 PrefObserver* obs) OVERRIDE;
292 virtual void RemovePrefObserver(const char* path, 213 virtual void RemovePrefObserver(const char* path,
293 PrefObserver* obs) OVERRIDE; 214 PrefObserver* obs) OVERRIDE;
294 215
295 // Sends notification of a changed preference. This needs to be called by 216 // Sends notification of a changed preference. This needs to be called by
296 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed. 217 // a ScopedUserPrefUpdate if a DictionaryValue or ListValue is changed.
297 void ReportUserPrefChanged(const std::string& key); 218 void ReportUserPrefChanged(const std::string& key);
298 219
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 220 // Sets the value for this pref path in the user pref store and informs the
308 // PrefNotifier of the change. 221 // PrefNotifier of the change.
309 void SetUserPrefValue(const char* path, base::Value* new_value); 222 void SetUserPrefValue(const char* path, base::Value* new_value);
310 223
311 // Load preferences from storage, attempting to diagnose and handle errors. 224 // Load preferences from storage, attempting to diagnose and handle errors.
312 // This should only be called from the constructor. 225 // This should only be called from the constructor.
313 void InitFromStorage(bool async); 226 void InitFromStorage(bool async);
314 227
315 // Used to set the value of dictionary or list values in the user pref store. 228 // 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 229 // 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 230 // pref store. This method returns NULL only if you're requesting an
318 // unregistered pref or a non-dict/non-list pref. 231 // unregistered pref or a non-dict/non-list pref.
319 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and 232 // |type| may only be Values::TYPE_DICTIONARY or Values::TYPE_LIST and
320 // |path| must point to a registered preference of type |type|. 233 // |path| must point to a registered preference of type |type|.
321 // Ownership of the returned value remains at the user pref store. 234 // Ownership of the returned value remains at the user pref store.
322 base::Value* GetMutableUserPref(const char* path, 235 base::Value* GetMutableUserPref(const char* path,
323 base::Value::Type type); 236 base::Value::Type type);
324 237
325 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(), 238 // GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(),
326 // it has been added for performance. If is faster because it does 239 // 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 240 // not need to find or create a Preference object to get the
328 // value (GetValue() calls back though the preference service to 241 // value (GetValue() calls back though the preference service to
329 // actually get the value.). 242 // actually get the value.).
330 const base::Value* GetPreferenceValue(const std::string& path) const; 243 const base::Value* GetPreferenceValue(const std::string& path) const;
331 244
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_ 245 // Local cache of registered Preference objects. The default_store_
341 // is authoritative with respect to what the types and default values 246 // is authoritative with respect to what the types and default values
342 // of registered preferences are. 247 // of registered preferences are.
343 mutable PreferenceMap prefs_map_; 248 mutable PreferenceMap prefs_map_;
344 249
345 // The model associator that maintains the links with the sync db.
346 scoped_ptr<PrefModelAssociator> pref_sync_associator_;
347
348 // Function with which to retrieve localized UTF8 resources.
349 base::Callback<std::string(int)> get_localized_string_method_;
350
351 // Callback to call when a read error occurs.
352 base::Callback<void(PersistentPrefStore::PrefReadError)> read_error_callback_;
353
354 // Whether CreateIncognitoPrefService() has been called to create a
355 // "forked" PrefService.
356 bool pref_service_forked_;
357
358 ObserverList<PrefServiceObserver> observer_list_;
359
360 DISALLOW_COPY_AND_ASSIGN(PrefService); 250 DISALLOW_COPY_AND_ASSIGN(PrefService);
361 }; 251 };
362 252
253 // TODO(joi): Remove these forwards. They were placed here temporarily
254 // to limit the size of the initial change that split
255 // PrefServiceSimple and PrefServiceSyncable out of PrefService.
256 #include "chrome/browser/prefs/pref_service_simple.h"
257 #include "chrome/browser/prefs/pref_service_syncable.h"
258
363 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_ 259 #endif // CHROME_BROWSER_PREFS_PREF_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/browser/prefs/chrome_pref_service_factory.cc ('k') | chrome/browser/prefs/pref_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698