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

Side by Side Diff: chrome/common/pref_service.h

Issue 83001: ImportantFileWriter (Closed)
Patch Set: share more code Created 11 years, 8 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
« no previous file with comments | « chrome/common/pref_member_unittest.cc ('k') | chrome/common/pref_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 // This service has two preference stores, one for "persistent" preferences, 6 // This service has two preference stores, one for "persistent" preferences,
7 // which get serialized for use in the next session, and one for "transient" 7 // which get serialized for use in the next session, and one for "transient"
8 // preferences, which are in effect for only the current session 8 // preferences, which are in effect for only the current session
9 // (this usually encodes things like command-line switches). 9 // (this usually encodes things like command-line switches).
10 // 10 //
11 // Calling the getter functions in this class basically looks at both the 11 // Calling the getter functions in this class basically looks at both the
12 // persistent and transient stores, where any corresponding value in the 12 // persistent and transient stores, where any corresponding value in the
13 // transient store overrides the one in the persistent store. 13 // transient store overrides the one in the persistent store.
14 14
15 #ifndef CHROME_COMMON_PREF_SERVICE_H_ 15 #ifndef CHROME_COMMON_PREF_SERVICE_H_
16 #define CHROME_COMMON_PREF_SERVICE_H_ 16 #define CHROME_COMMON_PREF_SERVICE_H_
17 17
18 #include <set> 18 #include <set>
19 19
20 #include "base/file_path.h" 20 #include "base/file_path.h"
21 #include "base/hash_tables.h" 21 #include "base/hash_tables.h"
22 #include "base/non_thread_safe.h" 22 #include "base/non_thread_safe.h"
23 #include "base/observer_list.h" 23 #include "base/observer_list.h"
24 #include "base/scoped_ptr.h" 24 #include "base/scoped_ptr.h"
25 #include "base/task.h"
26 #include "base/values.h" 25 #include "base/values.h"
27 #include "testing/gtest/include/gtest/gtest_prod.h" 26 #include "chrome/common/important_file_writer.h"
28 27
29 class NotificationObserver; 28 class NotificationObserver;
30 class Preference; 29 class Preference;
31 30
32 namespace base { 31 namespace base {
33 class Thread; 32 class Thread;
34 } 33 }
35 34
36 class PrefService : public NonThreadSafe { 35 class PrefService : public NonThreadSafe {
37 public: 36 public:
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 std::wstring name_; 69 std::wstring name_;
71 scoped_ptr<Value> default_value_; 70 scoped_ptr<Value> default_value_;
72 71
73 // A reference to the pref service's persistent prefs. 72 // A reference to the pref service's persistent prefs.
74 DictionaryValue* root_pref_; 73 DictionaryValue* root_pref_;
75 74
76 DISALLOW_COPY_AND_ASSIGN(Preference); 75 DISALLOW_COPY_AND_ASSIGN(Preference);
77 }; 76 };
78 77
79 // |pref_filename| is the path to the prefs file we will try to load or save 78 // |pref_filename| is the path to the prefs file we will try to load or save
80 // to. 79 // to. Saves will be executed on |backend_thread|. It should be the file
81 explicit PrefService(const FilePath& pref_filename); 80 // thread in Chrome. You can pass NULL for unit tests, and then no separate
81 // thread will be used.
82 PrefService(const FilePath& pref_filename,
83 const base::Thread* backend_thread);
82 ~PrefService(); 84 ~PrefService();
83 85
84 // Reloads the data from file. This should only be called when the importer 86 // Reloads the data from file. This should only be called when the importer
85 // is running during first run, and the main process may not change pref 87 // is running during first run, and the main process may not change pref
86 // values while the importer process is running. 88 // values while the importer process is running. Returns true on success.
87 void ReloadPersistentPrefs(); 89 bool ReloadPersistentPrefs();
88 90
89 // Writes the data to disk on the provided thread. In Chrome, |thread| should 91 // Writes the data to disk. The return value only reflects whether
90 // be the file thread. The return value only reflects whether serialization 92 // serialization was successful; we don't know whether the data actually made
91 // was successful; we don't know whether the data actually made it on disk 93 // it on disk (since it's on a different thread). This should only be used if
92 // (since it's on a different thread). This should only be used if we need 94 // we need to save immediately (basically, during shutdown). Otherwise, you
93 // to save immediately (basically, during shutdown). Otherwise, you should 95 // should use ScheduleSavePersistentPrefs.
94 // use ScheduleSavePersistentPrefs. 96 bool SavePersistentPrefs();
95 bool SavePersistentPrefs(base::Thread* thread) const;
96 97
97 // Starts a timer that ends up saving the preferences. This helps to batch 98 // Serializes the data and schedules save using ImportantFileWriter.
98 // together save requests that happen in a close time frame so we don't write 99 bool ScheduleSavePersistentPrefs();
99 // to disk too frequently.
100 void ScheduleSavePersistentPrefs(base::Thread* thread);
101 100
102 DictionaryValue* transient() { return transient_.get(); } 101 DictionaryValue* transient() { return transient_.get(); }
103 102
104 // Make the PrefService aware of a pref. 103 // Make the PrefService aware of a pref.
105 void RegisterBooleanPref(const wchar_t* path, 104 void RegisterBooleanPref(const wchar_t* path,
106 bool default_value); 105 bool default_value);
107 void RegisterIntegerPref(const wchar_t* path, 106 void RegisterIntegerPref(const wchar_t* path,
108 int default_value); 107 int default_value);
109 void RegisterRealPref(const wchar_t* path, 108 void RegisterRealPref(const wchar_t* path,
110 double default_value); 109 double default_value);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 } 185 }
187 }; 186 };
188 typedef std::set<Preference*, PreferencePathComparator> PreferenceSet; 187 typedef std::set<Preference*, PreferencePathComparator> PreferenceSet;
189 const PreferenceSet& preference_set() const { return prefs_; } 188 const PreferenceSet& preference_set() const { return prefs_; }
190 189
191 // A helper method to quickly look up a preference. Returns NULL if the 190 // A helper method to quickly look up a preference. Returns NULL if the
192 // preference is not registered. 191 // preference is not registered.
193 const Preference* FindPreference(const wchar_t* pref_name) const; 192 const Preference* FindPreference(const wchar_t* pref_name) const;
194 193
195 private: 194 private:
196 FRIEND_TEST(PrefServiceTest, Basic);
197 FRIEND_TEST(PrefServiceTest, Overlay);
198 FRIEND_TEST(PrefServiceTest, Observers);
199 FRIEND_TEST(PrefServiceTest, LocalizedPrefs);
200 FRIEND_TEST(PrefServiceTest, NoObserverFire);
201 FRIEND_TEST(PrefServiceTest, HasPrefPath);
202
203 FRIEND_TEST(PrefMemberTest, BasicGetAndSet);
204 FRIEND_TEST(PrefMemberTest, TwoPrefs);
205 FRIEND_TEST(PrefMemberTest, Observer);
206
207 // This constructor is used only for some unittests. It doesn't try to load
208 // any existing prefs from a file.
209 PrefService();
210
211 // Reads the data from the given file, returning true on success.
212 bool LoadPersistentPrefs(const FilePath& file_path);
213
214 // Add a preference to the PreferenceMap. If the pref already exists, return 195 // Add a preference to the PreferenceMap. If the pref already exists, return
215 // false. This method takes ownership of |pref|. 196 // false. This method takes ownership of |pref|.
216 void RegisterPreference(Preference* pref); 197 void RegisterPreference(Preference* pref);
217 198
218 // Returns a copy of the current pref value. The caller is responsible for 199 // Returns a copy of the current pref value. The caller is responsible for
219 // deleting the returned object. 200 // deleting the returned object.
220 Value* GetPrefCopy(const wchar_t* pref_name); 201 Value* GetPrefCopy(const wchar_t* pref_name);
221 202
222 // For the given pref_name, fire any observer of the pref. 203 // For the given pref_name, fire any observer of the pref.
223 void FireObservers(const wchar_t* pref_name); 204 void FireObservers(const wchar_t* pref_name);
224 205
225 // For the given pref_name, fire any observer of the pref only if |old_value| 206 // For the given pref_name, fire any observer of the pref only if |old_value|
226 // is different from the current value. 207 // is different from the current value.
227 void FireObserversIfChanged(const wchar_t* pref_name, 208 void FireObserversIfChanged(const wchar_t* pref_name,
228 const Value* old_value); 209 const Value* old_value);
229 210
211 // Serializes stored data to string. |output| is modified only
212 // if serialization was successful. Returns true on success.
213 bool SerializePrefData(std::string* output) const;
214
230 scoped_ptr<DictionaryValue> persistent_; 215 scoped_ptr<DictionaryValue> persistent_;
231 scoped_ptr<DictionaryValue> transient_; 216 scoped_ptr<DictionaryValue> transient_;
232 217
233 // The filename that we're loading/saving the prefs to. 218 // Helper for safe writing pref data.
234 FilePath pref_filename_; 219 ImportantFileWriter writer_;
235
236 // Task used by ScheduleSavePersistentPrefs to avoid lots of little saves.
237 ScopedRunnableMethodFactory<PrefService> save_preferences_factory_;
238 220
239 // A set of all the registered Preference objects. 221 // A set of all the registered Preference objects.
240 PreferenceSet prefs_; 222 PreferenceSet prefs_;
241 223
242 // A map from pref names to a list of observers. Observers get fired in the 224 // A map from pref names to a list of observers. Observers get fired in the
243 // order they are added. 225 // order they are added.
244 typedef ObserverList<NotificationObserver> NotificationObserverList; 226 typedef ObserverList<NotificationObserver> NotificationObserverList;
245 typedef base::hash_map<std::wstring, NotificationObserverList*> 227 typedef base::hash_map<std::wstring, NotificationObserverList*>
246 PrefObserverMap; 228 PrefObserverMap;
247 PrefObserverMap pref_observers_; 229 PrefObserverMap pref_observers_;
248 230
249 DISALLOW_COPY_AND_ASSIGN(PrefService); 231 DISALLOW_COPY_AND_ASSIGN(PrefService);
250 }; 232 };
251 233
252 #endif // CHROME_COMMON_PREF_SERVICE_H_ 234 #endif // CHROME_COMMON_PREF_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/common/pref_member_unittest.cc ('k') | chrome/common/pref_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698