| OLD | NEW |
| 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/hash_tables.h" | 21 #include "base/hash_tables.h" |
| 21 #include "base/non_thread_safe.h" | 22 #include "base/non_thread_safe.h" |
| 22 #include "base/observer_list.h" | 23 #include "base/observer_list.h" |
| 23 #include "base/scoped_ptr.h" | 24 #include "base/scoped_ptr.h" |
| 24 #include "base/task.h" | 25 #include "base/task.h" |
| 25 #include "base/values.h" | 26 #include "base/values.h" |
| 26 #include "testing/gtest/include/gtest/gtest_prod.h" | 27 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 27 | 28 |
| 28 class NotificationObserver; | 29 class NotificationObserver; |
| 29 class Preference; | 30 class Preference; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 std::wstring name_; | 70 std::wstring name_; |
| 70 scoped_ptr<Value> default_value_; | 71 scoped_ptr<Value> default_value_; |
| 71 | 72 |
| 72 // A reference to the pref service's persistent prefs. | 73 // A reference to the pref service's persistent prefs. |
| 73 DictionaryValue* root_pref_; | 74 DictionaryValue* root_pref_; |
| 74 | 75 |
| 75 DISALLOW_COPY_AND_ASSIGN(Preference); | 76 DISALLOW_COPY_AND_ASSIGN(Preference); |
| 76 }; | 77 }; |
| 77 | 78 |
| 78 // |pref_filename| is the path to the prefs file we will try to load or save t
o. | 79 // |pref_filename| is the path to the prefs file we will try to load or save t
o. |
| 79 explicit PrefService(const std::wstring& pref_filename); | 80 explicit PrefService(const FilePath& pref_filename); |
| 80 ~PrefService(); | 81 ~PrefService(); |
| 81 | 82 |
| 82 // Reloads the data from file. This should only be called when the importer | 83 // Reloads the data from file. This should only be called when the importer |
| 83 // is running during first run, and the main process may not change pref | 84 // is running during first run, and the main process may not change pref |
| 84 // values while the importer process is running. | 85 // values while the importer process is running. |
| 85 void ReloadPersistentPrefs(); | 86 void ReloadPersistentPrefs(); |
| 86 | 87 |
| 87 // Writes the data to disk on the provided thread. In Chrome, |thread| should | 88 // Writes the data to disk on the provided thread. In Chrome, |thread| should |
| 88 // be the file thread. The return value only reflects whether serialization | 89 // be the file thread. The return value only reflects whether serialization |
| 89 // was successful; we don't know whether the data actually made it on disk | 90 // was successful; we don't know whether the data actually made it on disk |
| (...skipping 11 matching lines...) Expand all Loading... |
| 101 | 102 |
| 102 // Make the PrefService aware of a pref. | 103 // Make the PrefService aware of a pref. |
| 103 void RegisterBooleanPref(const wchar_t* path, | 104 void RegisterBooleanPref(const wchar_t* path, |
| 104 bool default_value); | 105 bool default_value); |
| 105 void RegisterIntegerPref(const wchar_t* path, | 106 void RegisterIntegerPref(const wchar_t* path, |
| 106 int default_value); | 107 int default_value); |
| 107 void RegisterRealPref(const wchar_t* path, | 108 void RegisterRealPref(const wchar_t* path, |
| 108 double default_value); | 109 double default_value); |
| 109 void RegisterStringPref(const wchar_t* path, | 110 void RegisterStringPref(const wchar_t* path, |
| 110 const std::wstring& default_value); | 111 const std::wstring& default_value); |
| 112 void RegisterFilePathPref(const wchar_t* path, |
| 113 const FilePath& default_value); |
| 111 void RegisterListPref(const wchar_t* path); | 114 void RegisterListPref(const wchar_t* path); |
| 112 void RegisterDictionaryPref(const wchar_t* path); | 115 void RegisterDictionaryPref(const wchar_t* path); |
| 113 | 116 |
| 114 // These varients use a default value from the locale dll instead. | 117 // These varients use a default value from the locale dll instead. |
| 115 void RegisterLocalizedBooleanPref(const wchar_t* path, | 118 void RegisterLocalizedBooleanPref(const wchar_t* path, |
| 116 int locale_default_message_id); | 119 int locale_default_message_id); |
| 117 void RegisterLocalizedIntegerPref(const wchar_t* path, | 120 void RegisterLocalizedIntegerPref(const wchar_t* path, |
| 118 int locale_default_message_id); | 121 int locale_default_message_id); |
| 119 void RegisterLocalizedRealPref(const wchar_t* path, | 122 void RegisterLocalizedRealPref(const wchar_t* path, |
| 120 int locale_default_message_id); | 123 int locale_default_message_id); |
| 121 void RegisterLocalizedStringPref(const wchar_t* path, | 124 void RegisterLocalizedStringPref(const wchar_t* path, |
| 122 int locale_default_message_id); | 125 int locale_default_message_id); |
| 123 | 126 |
| 124 // Returns whether the specified pref has been registered. | 127 // Returns whether the specified pref has been registered. |
| 125 bool IsPrefRegistered(const wchar_t* path); | 128 bool IsPrefRegistered(const wchar_t* path); |
| 126 | 129 |
| 127 // If the path is valid and the value at the end of the path matches the type | 130 // If the path is valid and the value at the end of the path matches the type |
| 128 // specified, it will return the specified value. Otherwise, the default valu
e | 131 // specified, it will return the specified value. Otherwise, the default valu
e |
| 129 // (set when the pref was registered) will be returned. | 132 // (set when the pref was registered) will be returned. |
| 130 bool GetBoolean(const wchar_t* path) const; | 133 bool GetBoolean(const wchar_t* path) const; |
| 131 int GetInteger(const wchar_t* path) const; | 134 int GetInteger(const wchar_t* path) const; |
| 132 double GetReal(const wchar_t* path) const; | 135 double GetReal(const wchar_t* path) const; |
| 133 std::wstring GetString(const wchar_t* path) const; | 136 std::wstring GetString(const wchar_t* path) const; |
| 137 FilePath GetFilePath(const wchar_t* path) const; |
| 134 | 138 |
| 135 // Returns the branch if it exists. If it's not a branch or the branch does | 139 // Returns the branch if it exists. If it's not a branch or the branch does |
| 136 // not exist, returns NULL. This does | 140 // not exist, returns NULL. This does |
| 137 const DictionaryValue* GetDictionary(const wchar_t* path) const; | 141 const DictionaryValue* GetDictionary(const wchar_t* path) const; |
| 138 const ListValue* GetList(const wchar_t* path) const; | 142 const ListValue* GetList(const wchar_t* path) const; |
| 139 | 143 |
| 140 // If the pref at the given path changes, we call the observer's Observe | 144 // If the pref at the given path changes, we call the observer's Observe |
| 141 // method with NOTIFY_PREF_CHANGED. | 145 // method with NOTIFY_PREF_CHANGED. |
| 142 void AddPrefObserver(const wchar_t* path, NotificationObserver* obs); | 146 void AddPrefObserver(const wchar_t* path, NotificationObserver* obs); |
| 143 void RemovePrefObserver(const wchar_t* path, NotificationObserver* obs); | 147 void RemovePrefObserver(const wchar_t* path, NotificationObserver* obs); |
| 144 | 148 |
| 145 // Removes a user pref and restores the pref to its default value. | 149 // Removes a user pref and restores the pref to its default value. |
| 146 void ClearPref(const wchar_t* path); | 150 void ClearPref(const wchar_t* path); |
| 147 | 151 |
| 148 // If the path is valid (i.e., registered), update the pref value. | 152 // If the path is valid (i.e., registered), update the pref value. |
| 149 void SetBoolean(const wchar_t* path, bool value); | 153 void SetBoolean(const wchar_t* path, bool value); |
| 150 void SetInteger(const wchar_t* path, int value); | 154 void SetInteger(const wchar_t* path, int value); |
| 151 void SetReal(const wchar_t* path, double value); | 155 void SetReal(const wchar_t* path, double value); |
| 152 void SetString(const wchar_t* path, const std::wstring& value); | 156 void SetString(const wchar_t* path, const std::wstring& value); |
| 157 void SetFilePath(const wchar_t* path, const FilePath& value); |
| 153 | 158 |
| 154 // Used to set the value of dictionary or list values in the pref tree. This | 159 // Used to set the value of dictionary or list values in the pref tree. This |
| 155 // will create a dictionary or list if one does not exist in the pref tree. | 160 // will create a dictionary or list if one does not exist in the pref tree. |
| 156 // This method returns NULL only if you're requesting an unregistered pref or | 161 // This method returns NULL only if you're requesting an unregistered pref or |
| 157 // a non-dict/non-list pref. | 162 // a non-dict/non-list pref. |
| 158 // WARNING: Changes to the dictionary or list will not automatically notify | 163 // WARNING: Changes to the dictionary or list will not automatically notify |
| 159 // pref observers. TODO(tc): come up with a way to still fire observers. | 164 // pref observers. TODO(tc): come up with a way to still fire observers. |
| 160 DictionaryValue* GetMutableDictionary(const wchar_t* path); | 165 DictionaryValue* GetMutableDictionary(const wchar_t* path); |
| 161 ListValue* GetMutableList(const wchar_t* path); | 166 ListValue* GetMutableList(const wchar_t* path); |
| 162 | 167 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 189 | 194 |
| 190 FRIEND_TEST(PrefMemberTest, BasicGetAndSet); | 195 FRIEND_TEST(PrefMemberTest, BasicGetAndSet); |
| 191 FRIEND_TEST(PrefMemberTest, TwoPrefs); | 196 FRIEND_TEST(PrefMemberTest, TwoPrefs); |
| 192 FRIEND_TEST(PrefMemberTest, Observer); | 197 FRIEND_TEST(PrefMemberTest, Observer); |
| 193 | 198 |
| 194 // This constructor is used only for some unittests. It doesn't try to load | 199 // This constructor is used only for some unittests. It doesn't try to load |
| 195 // any existing prefs from a file. | 200 // any existing prefs from a file. |
| 196 PrefService(); | 201 PrefService(); |
| 197 | 202 |
| 198 // Reads the data from the given file, returning true on success. | 203 // Reads the data from the given file, returning true on success. |
| 199 bool LoadPersistentPrefs(const std::wstring& file_path); | 204 bool LoadPersistentPrefs(const FilePath& file_path); |
| 200 | 205 |
| 201 // Add a preference to the PreferenceMap. If the pref already exists, return | 206 // Add a preference to the PreferenceMap. If the pref already exists, return |
| 202 // false. This method takes ownership of |pref|. | 207 // false. This method takes ownership of |pref|. |
| 203 void RegisterPreference(Preference* pref); | 208 void RegisterPreference(Preference* pref); |
| 204 | 209 |
| 205 // Returns a copy of the current pref value. The caller is responsible for | 210 // Returns a copy of the current pref value. The caller is responsible for |
| 206 // deleting the returned object. | 211 // deleting the returned object. |
| 207 Value* GetPrefCopy(const wchar_t* pref_name); | 212 Value* GetPrefCopy(const wchar_t* pref_name); |
| 208 | 213 |
| 209 // For the given pref_name, fire any observer of the pref. | 214 // For the given pref_name, fire any observer of the pref. |
| 210 void FireObservers(const wchar_t* pref_name); | 215 void FireObservers(const wchar_t* pref_name); |
| 211 | 216 |
| 212 // For the given pref_name, fire any observer of the pref only if |old_value| | 217 // For the given pref_name, fire any observer of the pref only if |old_value| |
| 213 // is different from the current value. | 218 // is different from the current value. |
| 214 void FireObserversIfChanged(const wchar_t* pref_name, | 219 void FireObserversIfChanged(const wchar_t* pref_name, |
| 215 const Value* old_value); | 220 const Value* old_value); |
| 216 | 221 |
| 217 scoped_ptr<DictionaryValue> persistent_; | 222 scoped_ptr<DictionaryValue> persistent_; |
| 218 scoped_ptr<DictionaryValue> transient_; | 223 scoped_ptr<DictionaryValue> transient_; |
| 219 | 224 |
| 220 // The filename that we're loading/saving the prefs to. | 225 // The filename that we're loading/saving the prefs to. |
| 221 std::wstring pref_filename_; | 226 FilePath pref_filename_; |
| 222 | 227 |
| 223 // Task used by ScheduleSavePersistentPrefs to avoid lots of little saves. | 228 // Task used by ScheduleSavePersistentPrefs to avoid lots of little saves. |
| 224 ScopedRunnableMethodFactory<PrefService> save_preferences_factory_; | 229 ScopedRunnableMethodFactory<PrefService> save_preferences_factory_; |
| 225 | 230 |
| 226 // A set of all the registered Preference objects. | 231 // A set of all the registered Preference objects. |
| 227 PreferenceSet prefs_; | 232 PreferenceSet prefs_; |
| 228 | 233 |
| 229 // A map from pref names to a list of observers. Observers get fired in the | 234 // A map from pref names to a list of observers. Observers get fired in the |
| 230 // order they are added. | 235 // order they are added. |
| 231 typedef ObserverList<NotificationObserver> NotificationObserverList; | 236 typedef ObserverList<NotificationObserver> NotificationObserverList; |
| 232 typedef base::hash_map<std::wstring, NotificationObserverList*> | 237 typedef base::hash_map<std::wstring, NotificationObserverList*> |
| 233 PrefObserverMap; | 238 PrefObserverMap; |
| 234 PrefObserverMap pref_observers_; | 239 PrefObserverMap pref_observers_; |
| 235 | 240 |
| 236 DISALLOW_COPY_AND_ASSIGN(PrefService); | 241 DISALLOW_COPY_AND_ASSIGN(PrefService); |
| 237 }; | 242 }; |
| 238 | 243 |
| 239 #endif // CHROME_COMMON_PREF_SERVICE_H_ | 244 #endif // CHROME_COMMON_PREF_SERVICE_H_ |
| OLD | NEW |