OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ | 5 #ifndef CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ |
6 #define CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ | 6 #define CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/ref_counted.h" | 16 #include "base/ref_counted.h" |
17 #include "base/scoped_ptr.h" | |
18 #include "base/values.h" | 17 #include "base/values.h" |
19 #include "chrome/browser/browser_thread.h" | 18 #include "chrome/browser/browser_thread.h" |
20 #include "chrome/common/notification_observer.h" | 19 #include "chrome/common/notification_observer.h" |
21 #include "chrome/common/notification_registrar.h" | 20 #include "chrome/common/notification_registrar.h" |
22 #include "chrome/common/pref_store.h" | 21 #include "chrome/common/pref_store.h" |
23 | 22 |
24 class FilePath; | 23 class FilePath; |
25 class PrefNotifier; | 24 class PrefNotifier; |
26 class PrefStore; | 25 class PrefStore; |
27 class Profile; | 26 class Profile; |
(...skipping 31 matching lines...) Loading... |
59 // | 58 // |
60 // The |profile| parameter is used to construct a replacement device | 59 // The |profile| parameter is used to construct a replacement device |
61 // management pref store. This is done after policy refresh when we swap out | 60 // management pref store. This is done after policy refresh when we swap out |
62 // the policy pref stores for new ones, so the |profile| pointer needs to be | 61 // the policy pref stores for new ones, so the |profile| pointer needs to be |
63 // kept around for then. It is safe to pass a NULL pointer for local state | 62 // kept around for then. It is safe to pass a NULL pointer for local state |
64 // preferences. | 63 // preferences. |
65 // | 64 // |
66 // TODO(mnissler, danno): Refactor the pref store interface and refresh logic | 65 // TODO(mnissler, danno): Refactor the pref store interface and refresh logic |
67 // so refreshes can be handled by the pref store itself without swapping | 66 // so refreshes can be handled by the pref store itself without swapping |
68 // stores. This way we can get rid of the profile pointer here. | 67 // stores. This way we can get rid of the profile pointer here. |
69 PrefValueStore(PrefStore* managed_platform_prefs, | 68 PrefValueStore(scoped_refptr<PrefStore> managed_platform_prefs, |
70 PrefStore* device_management_prefs, | 69 scoped_refptr<PrefStore> device_management_prefs, |
71 PrefStore* extension_prefs, | 70 scoped_refptr<PrefStore> extension_prefs, |
72 PrefStore* command_line_prefs, | 71 scoped_refptr<PrefStore> command_line_prefs, |
73 PrefStore* user_prefs, | 72 scoped_refptr<PrefStore> user_prefs, |
74 PrefStore* recommended_prefs, | 73 scoped_refptr<PrefStore> recommended_prefs, |
75 PrefStore* default_prefs, | 74 scoped_refptr<PrefStore> default_prefs, |
76 PrefNotifier* pref_notifier, | 75 PrefNotifier* pref_notifier, |
77 Profile* profile); | 76 Profile* profile); |
78 virtual ~PrefValueStore(); | 77 virtual ~PrefValueStore(); |
79 | 78 |
80 // Gets the value for the given preference name that has a valid value type; | 79 // Derives a new PrefValueStore that overrides the specified attributes. |
81 // that is, the same type the preference was registered with, or NULL for | 80 PrefValueStore* Derive(scoped_refptr<PrefStore> extension_prefs, |
82 // default values of Dictionaries and Lists. Returns true if a valid value | 81 scoped_refptr<PrefStore> user_prefs, |
83 // was found in any of the available PrefStores. Most callers should use | 82 PrefNotifier* pref_notifier); |
84 // Preference::GetValue() instead of calling this method directly. | |
85 bool GetValue(const std::string& name, Value** out_value) const; | |
86 | 83 |
87 // Adds a preference to the mapping of names to types. | 84 // Gets the value for the given preference name that has the specified value |
88 void RegisterPreferenceType(const std::string& name, Value::ValueType type); | 85 // type. Returns true if a valid value was found in any of the available |
89 | 86 // PrefStores. Most callers should use Preference::GetValue() instead of |
90 // Gets the registered value type for the given preference name. Returns | 87 // calling this method directly. |
91 // Value::TYPE_NULL if the preference has never been registered. | 88 bool GetValue(const std::string& name, |
92 Value::ValueType GetRegisteredType(const std::string& name) const; | 89 Value::ValueType type, |
93 | 90 Value** out_value) const; |
94 // Returns true if the PrefValueStore contains the given preference (i.e., | |
95 // it's been registered), and a value with the correct type has been actively | |
96 // set in some pref store. The application default specified when the pref was | |
97 // registered does not count as an "actively set" value, but another pref | |
98 // store setting a value that happens to be equal to the default does. | |
99 bool HasPrefPath(const char* name) const; | |
100 | 91 |
101 // These methods return true if a preference with the given name is in the | 92 // These methods return true if a preference with the given name is in the |
102 // indicated pref store, even if that value is currently being overridden by | 93 // indicated pref store, even if that value is currently being overridden by |
103 // a higher-priority source. | 94 // a higher-priority source. |
104 bool PrefValueInManagedPlatformStore(const char* name) const; | 95 bool PrefValueInManagedPlatformStore(const char* name) const; |
105 bool PrefValueInDeviceManagementStore(const char* name) const; | 96 bool PrefValueInDeviceManagementStore(const char* name) const; |
106 bool PrefValueInExtensionStore(const char* name) const; | 97 bool PrefValueInExtensionStore(const char* name) const; |
107 bool PrefValueInUserStore(const char* name) const; | 98 bool PrefValueInUserStore(const char* name) const; |
108 | 99 |
109 // These methods return true if a preference with the given name is actually | 100 // These methods return true if a preference with the given name is actually |
(...skipping 37 matching lines...) Loading... |
147 // the PrefStore for changes, forwarding notifications to PrefValueStore. This | 138 // the PrefStore for changes, forwarding notifications to PrefValueStore. This |
148 // indirection is here for the sake of disambiguating notifications from the | 139 // indirection is here for the sake of disambiguating notifications from the |
149 // individual PrefStores. | 140 // individual PrefStores. |
150 class PrefStoreKeeper : public PrefStore::Observer { | 141 class PrefStoreKeeper : public PrefStore::Observer { |
151 public: | 142 public: |
152 PrefStoreKeeper(); | 143 PrefStoreKeeper(); |
153 virtual ~PrefStoreKeeper(); | 144 virtual ~PrefStoreKeeper(); |
154 | 145 |
155 // Takes ownership of |pref_store|. | 146 // Takes ownership of |pref_store|. |
156 void Initialize(PrefValueStore* store, | 147 void Initialize(PrefValueStore* store, |
157 PrefStore* pref_store, | 148 scoped_refptr<PrefStore> pref_store, |
158 PrefStoreType type); | 149 PrefStoreType type); |
159 | 150 |
160 PrefStore* store() { return pref_store_.get(); } | 151 scoped_refptr<PrefStore> store() { return pref_store_; } |
161 const PrefStore* store() const { return pref_store_.get(); } | 152 const scoped_refptr<PrefStore> store() const { return pref_store_; } |
162 | 153 |
163 private: | 154 private: |
164 // PrefStore::Observer implementation. | 155 // PrefStore::Observer implementation. |
165 virtual void OnPrefValueChanged(const std::string& key); | 156 virtual void OnPrefValueChanged(const std::string& key); |
166 virtual void OnInitializationCompleted(); | 157 virtual void OnInitializationCompleted(); |
167 | 158 |
168 // PrefValueStore this keeper is part of. | 159 // PrefValueStore this keeper is part of. |
169 PrefValueStore* pref_value_store_; | 160 PrefValueStore* pref_value_store_; |
170 | 161 |
171 // The PrefStore managed by this keeper. | 162 // The PrefStore managed by this keeper. |
172 scoped_ptr<PrefStore> pref_store_; | 163 scoped_refptr<PrefStore> pref_store_; |
173 | 164 |
174 // Type of the pref store. | 165 // Type of the pref store. |
175 PrefStoreType type_; | 166 PrefStoreType type_; |
176 | 167 |
177 DISALLOW_COPY_AND_ASSIGN(PrefStoreKeeper); | 168 DISALLOW_COPY_AND_ASSIGN(PrefStoreKeeper); |
178 }; | 169 }; |
179 | 170 |
180 typedef std::map<std::string, Value::ValueType> PrefTypeMap; | 171 typedef std::map<std::string, Value::ValueType> PrefTypeMap; |
181 | 172 |
182 friend class PrefValueStorePolicyRefreshTest; | 173 friend class PrefValueStorePolicyRefreshTest; |
183 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest, TestPolicyRefresh); | 174 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest, TestPolicyRefresh); |
184 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest, | 175 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest, |
185 TestRefreshPolicyPrefsCompletion); | 176 TestRefreshPolicyPrefsCompletion); |
186 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest, | 177 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest, |
187 TestConcurrentPolicyRefresh); | 178 TestConcurrentPolicyRefresh); |
188 | 179 |
189 // Returns true if the actual type is a valid type for the expected type when | |
190 // found in the given store. | |
191 static bool IsValidType(Value::ValueType expected, | |
192 Value::ValueType actual, | |
193 PrefStoreType store); | |
194 | |
195 // Returns true if the preference with the given name has a value in the | 180 // Returns true if the preference with the given name has a value in the |
196 // given PrefStoreType, of the same value type as the preference was | 181 // given PrefStoreType, of the same value type as the preference was |
197 // registered with. | 182 // registered with. |
198 bool PrefValueInStore(const char* name, PrefStoreType store) const; | 183 bool PrefValueInStore(const char* name, PrefStoreType store) const; |
199 | 184 |
200 // Returns true if a preference has an explicit value in any of the | 185 // Returns true if a preference has an explicit value in any of the |
201 // stores in the range specified by |first_checked_store| and | 186 // stores in the range specified by |first_checked_store| and |
202 // |last_checked_store|, even if that value is currently being | 187 // |last_checked_store|, even if that value is currently being |
203 // overridden by a higher-priority store. | 188 // overridden by a higher-priority store. |
204 bool PrefValueInStoreRange(const char* name, | 189 bool PrefValueInStoreRange(const char* name, |
(...skipping 20 matching lines...) Loading... |
225 | 210 |
226 // Called as a result of a notification of policy change. Triggers a reload of | 211 // Called as a result of a notification of policy change. Triggers a reload of |
227 // managed platform, device management and recommended preferences from policy | 212 // managed platform, device management and recommended preferences from policy |
228 // from a Task on the FILE thread. | 213 // from a Task on the FILE thread. |
229 void RefreshPolicyPrefs(); | 214 void RefreshPolicyPrefs(); |
230 | 215 |
231 // Called during policy refresh after ReadPrefs completes on the thread | 216 // Called during policy refresh after ReadPrefs completes on the thread |
232 // that initiated the policy refresh. RefreshPolicyPrefsCompletion takes | 217 // that initiated the policy refresh. RefreshPolicyPrefsCompletion takes |
233 // ownership of the |callback| object. | 218 // ownership of the |callback| object. |
234 void RefreshPolicyPrefsCompletion( | 219 void RefreshPolicyPrefsCompletion( |
235 policy::ConfigurationPolicyPrefStore* new_managed_platform_pref_store, | 220 scoped_refptr<policy::ConfigurationPolicyPrefStore> |
236 policy::ConfigurationPolicyPrefStore* new_device_management_pref_store, | 221 new_managed_platform_pref_store, |
237 policy::ConfigurationPolicyPrefStore* new_recommended_pref_store); | 222 scoped_refptr<policy::ConfigurationPolicyPrefStore> |
| 223 new_device_management_pref_store, |
| 224 scoped_refptr<policy::ConfigurationPolicyPrefStore> |
| 225 new_recommended_pref_store); |
238 | 226 |
239 // Called during policy refresh to do the ReadPrefs on the FILE thread. | 227 // Called during policy refresh to do the ReadPrefs on the FILE thread. |
240 // RefreshPolicyPrefsOnFileThread takes ownership of the |callback| object. | 228 // RefreshPolicyPrefsOnFileThread takes ownership of the |callback| object. |
241 void RefreshPolicyPrefsOnFileThread( | 229 void RefreshPolicyPrefsOnFileThread( |
242 BrowserThread::ID calling_thread_id, | 230 BrowserThread::ID calling_thread_id, |
243 policy::ConfigurationPolicyPrefStore* new_managed_platform_pref_store, | 231 scoped_refptr<policy::ConfigurationPolicyPrefStore> |
244 policy::ConfigurationPolicyPrefStore* new_device_management_pref_store, | 232 new_managed_platform_pref_store, |
245 policy::ConfigurationPolicyPrefStore* new_recommended_pref_store); | 233 scoped_refptr<policy::ConfigurationPolicyPrefStore> |
| 234 new_device_management_pref_store, |
| 235 scoped_refptr<policy::ConfigurationPolicyPrefStore> |
| 236 new_recommended_pref_store); |
246 | 237 |
247 // NotificationObserver methods: | 238 // NotificationObserver methods: |
248 virtual void Observe(NotificationType type, | 239 virtual void Observe(NotificationType type, |
249 const NotificationSource& source, | 240 const NotificationSource& source, |
250 const NotificationDetails& details); | 241 const NotificationDetails& details); |
251 | 242 |
252 // Called from the PrefStoreKeeper implementation when a pref value for |key| | 243 // Called from the PrefStoreKeeper implementation when a pref value for |key| |
253 // changed in the pref store for |type|. | 244 // changed in the pref store for |type|. |
254 void OnPrefValueChanged(PrefStoreType type, const std::string& key); | 245 void OnPrefValueChanged(PrefStoreType type, const std::string& key); |
255 | 246 |
256 // Handle the event that the store for |type| has completed initialization. | 247 // Handle the event that the store for |type| has completed initialization. |
257 void OnInitializationCompleted(PrefStoreType type); | 248 void OnInitializationCompleted(PrefStoreType type); |
258 | 249 |
259 // Initializes a pref store keeper. Sets up a PrefStoreKeeper that will take | 250 // Initializes a pref store keeper. Sets up a PrefStoreKeeper that will take |
260 // ownership of the passed |pref_store|. | 251 // ownership of the passed |pref_store|. |
261 void InitPrefStore(PrefStoreType type, PrefStore* pref_store); | 252 void InitPrefStore(PrefStoreType type, scoped_refptr<PrefStore> pref_store); |
262 | 253 |
263 // Checks whether initialization is completed and tells the notifier if that | 254 // Checks whether initialization is completed and tells the notifier if that |
264 // is the case. | 255 // is the case. |
265 void CheckInitializationCompleted(); | 256 void CheckInitializationCompleted(); |
266 | 257 |
267 // Get the PrefStore pointer for the given type. May return NULL if there is | 258 // Get the PrefStore pointer for the given type. May return NULL if there is |
268 // no PrefStore for that type. | 259 // no PrefStore for that type. |
269 PrefStore* GetPrefStore(PrefStoreType type) { | 260 scoped_refptr<PrefStore> GetPrefStore(PrefStoreType type) { |
270 return pref_stores_[type].store(); | 261 return pref_stores_[type].store(); |
271 } | 262 } |
272 const PrefStore* GetPrefStore(PrefStoreType type) const { | 263 const scoped_refptr<PrefStore> GetPrefStore(PrefStoreType type) const { |
273 return pref_stores_[type].store(); | 264 return pref_stores_[type].store(); |
274 } | 265 } |
275 | 266 |
276 // Keeps the PrefStore references in order of precedence. | 267 // Keeps the PrefStore references in order of precedence. |
277 PrefStoreKeeper pref_stores_[PREF_STORE_TYPE_MAX + 1]; | 268 PrefStoreKeeper pref_stores_[PREF_STORE_TYPE_MAX + 1]; |
278 | 269 |
279 // Used for generating PREF_CHANGED and PREF_INITIALIZATION_COMPLETED | 270 // Used for generating PREF_CHANGED and PREF_INITIALIZATION_COMPLETED |
280 // notifications. This is a weak reference, since the notifier is owned by the | 271 // notifications. This is a weak reference, since the notifier is owned by the |
281 // corresponding PrefService. | 272 // corresponding PrefService. |
282 PrefNotifier* pref_notifier_; | 273 PrefNotifier* pref_notifier_; |
283 | 274 |
284 // A mapping of preference names to their registered types. | |
285 PrefTypeMap pref_types_; | |
286 | |
287 // The associated profile, in case this value store is associated with a | 275 // The associated profile, in case this value store is associated with a |
288 // profile pref service. Used for recreating the device management pref store | 276 // profile pref service. Used for recreating the device management pref store |
289 // upon policy refresh. | 277 // upon policy refresh. |
290 Profile* profile_; | 278 Profile* profile_; |
291 | 279 |
292 // TODO(mnissler): Remove this after cleaning up policy refresh handling. | 280 // TODO(mnissler): Remove this after cleaning up policy refresh handling. |
293 NotificationRegistrar registrar_; | 281 NotificationRegistrar registrar_; |
294 | 282 |
295 DISALLOW_COPY_AND_ASSIGN(PrefValueStore); | 283 DISALLOW_COPY_AND_ASSIGN(PrefValueStore); |
296 }; | 284 }; |
297 | 285 |
298 #endif // CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ | 286 #endif // CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ |
OLD | NEW |