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

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

Issue 5646003: Sanitize PrefStore interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix PrefService mock construction in PrefServiceTest to include command line store. Created 10 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
« no previous file with comments | « chrome/browser/prefs/pref_value_map.cc ('k') | chrome/browser/prefs/pref_value_store.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) 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" 17 #include "base/scoped_ptr.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "chrome/browser/browser_thread.h" 19 #include "chrome/browser/browser_thread.h"
20 #include "chrome/common/notification_observer.h" 20 #include "chrome/common/notification_observer.h"
21 #include "chrome/common/notification_registrar.h" 21 #include "chrome/common/notification_registrar.h"
22 #include "chrome/common/pref_store.h" 22 #include "chrome/common/pref_store.h"
23 23
24 class FilePath; 24 class FilePath;
25 class PrefNotifier; 25 class PrefNotifier;
26 class PrefStore; 26 class PrefStore;
27 class Profile; 27 class Profile;
28 28
29 // TODO(danno, mnissler): Remove after policy refresh cleanup.
30 namespace policy {
31 class ConfigurationPolicyPrefStore;
32 }
33
29 // The PrefValueStore manages various sources of values for Preferences 34 // The PrefValueStore manages various sources of values for Preferences
30 // (e.g., configuration policies, extensions, and user settings). It returns 35 // (e.g., configuration policies, extensions, and user settings). It returns
31 // the value of a Preference from the source with the highest priority, and 36 // the value of a Preference from the source with the highest priority, and
32 // allows setting user-defined values for preferences that are not managed. 37 // allows setting user-defined values for preferences that are not managed.
33 // 38 //
34 // Unless otherwise explicitly noted, all of the methods of this class must 39 // Unless otherwise explicitly noted, all of the methods of this class must
35 // be called on the UI thread. 40 // be called on the UI thread.
36 class PrefValueStore : public base::RefCountedThreadSafe<PrefValueStore>, 41 class PrefValueStore : public base::RefCountedThreadSafe<PrefValueStore>,
37 public NotificationObserver { 42 public NotificationObserver {
38 public: 43 public:
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 Profile* profile); 77 Profile* profile);
73 virtual ~PrefValueStore(); 78 virtual ~PrefValueStore();
74 79
75 // Gets the value for the given preference name that has a valid value type; 80 // Gets the value for the given preference name that has a valid value type;
76 // that is, the same type the preference was registered with, or NULL for 81 // that is, the same type the preference was registered with, or NULL for
77 // default values of Dictionaries and Lists. Returns true if a valid value 82 // default values of Dictionaries and Lists. Returns true if a valid value
78 // was found in any of the available PrefStores. Most callers should use 83 // was found in any of the available PrefStores. Most callers should use
79 // Preference::GetValue() instead of calling this method directly. 84 // Preference::GetValue() instead of calling this method directly.
80 bool GetValue(const std::string& name, Value** out_value) const; 85 bool GetValue(const std::string& name, Value** out_value) const;
81 86
82 // Same as GetValue but only searches the user store.
83 bool GetUserValue(const std::string& name, Value** out_value) const;
84
85 // Adds a preference to the mapping of names to types. 87 // Adds a preference to the mapping of names to types.
86 void RegisterPreferenceType(const std::string& name, Value::ValueType type); 88 void RegisterPreferenceType(const std::string& name, Value::ValueType type);
87 89
88 // Gets the registered value type for the given preference name. Returns 90 // Gets the registered value type for the given preference name. Returns
89 // Value::TYPE_NULL if the preference has never been registered. 91 // Value::TYPE_NULL if the preference has never been registered.
90 Value::ValueType GetRegisteredType(const std::string& name) const; 92 Value::ValueType GetRegisteredType(const std::string& name) const;
91 93
92 // Read preference values into the three PrefStores so that they are available
93 // through the GetValue method. Return the first error that occurs (but
94 // continue reading the remaining PrefStores).
95 PrefStore::PrefReadError ReadPrefs();
96
97 // Persists prefs (to disk or elsewhere). Returns true if writing values was
98 // successful. In practice, only the user prefs are expected to be written
99 // out.
100 // TODO(mnissler, danno): Handle writes through PrefService and remove.
101 bool WritePrefs();
102
103 // Calls the method ScheduleWritePrefs on the PrefStores. In practice, only
104 // the user prefs are expected to be written out.
105 // TODO(mnissler, danno): Handle writes through PrefService and remove.
106 void ScheduleWritePrefs();
107
108 // Returns true if the PrefValueStore contains the given preference (i.e., 94 // Returns true if the PrefValueStore contains the given preference (i.e.,
109 // it's been registered), and a value with the correct type has been actively 95 // it's been registered), and a value with the correct type has been actively
110 // set in some pref store. The application default specified when the pref was 96 // set in some pref store. The application default specified when the pref was
111 // registered does not count as an "actively set" value, but another pref 97 // registered does not count as an "actively set" value, but another pref
112 // store setting a value that happens to be equal to the default does. 98 // store setting a value that happens to be equal to the default does.
113 bool HasPrefPath(const char* name) const; 99 bool HasPrefPath(const char* name) const;
114 100
115 // Returns true if the PrefValueStore is read-only. Because the managed
116 // platform, device management and recommended PrefStores are always
117 // read-only, the PrefValueStore as a whole is read-only if the PrefStore
118 // containing the user preferences is read-only.
119 bool ReadOnly() const;
120
121 // Alters the user-defined value of a preference. Even if the preference is
122 // managed this method allows the user-defined value of the preference to be
123 // set. However, GetValue calls will not return this value as long as the
124 // preference is overriden by a store of higher precedence. Note that the
125 // PrefValueStore takes the ownership of the value referenced by |in_value|.
126 // It is an error to call this when no user PrefStore has been set. Triggers
127 // notifications if the user-visible value changes.
128 // TODO(mnissler, danno): Handle writes in PrefService and notifications in
129 // the pref store implementation, so we can remove this call.
130 void SetUserPrefValue(const char* name, Value* in_value);
131
132 // Like SetUserPrefValue, but silently puts the value without triggering
133 // notifications.
134 // TODO(mnissler, danno): Handle writes in PrefService and notifications in
135 // the pref store implementation, so we can remove this call.
136 void SetUserPrefValueSilently(const char* name, Value* in_value);
137
138 // Removes a value from the user PrefStore. If a preference is overriden by a
139 // store of higher precedence, this function will have no immediately visible
140 // effect. Triggers notifications if the user-visible value changes.
141 // TODO(mnissler, danno): Handle writes in PrefService and notifications in
142 // the pref store implementation, so we can remove this call.
143 void RemoveUserPrefValue(const char* name);
144
145 // These methods return true if a preference with the given name is in the 101 // These methods return true if a preference with the given name is in the
146 // indicated pref store, even if that value is currently being overridden by 102 // indicated pref store, even if that value is currently being overridden by
147 // a higher-priority source. 103 // a higher-priority source.
148 bool PrefValueInManagedPlatformStore(const char* name) const; 104 bool PrefValueInManagedPlatformStore(const char* name) const;
149 bool PrefValueInDeviceManagementStore(const char* name) const; 105 bool PrefValueInDeviceManagementStore(const char* name) const;
150 bool PrefValueInExtensionStore(const char* name) const; 106 bool PrefValueInExtensionStore(const char* name) const;
151 bool PrefValueInUserStore(const char* name) const; 107 bool PrefValueInUserStore(const char* name) const;
152 108
153 // These methods return true if a preference with the given name is actually 109 // These methods return true if a preference with the given name is actually
154 // being controlled by the indicated pref store and not being overridden by 110 // being controlled by the indicated pref store and not being overridden by
155 // a higher-priority source. 111 // a higher-priority source.
156 bool PrefValueFromExtensionStore(const char* name) const; 112 bool PrefValueFromExtensionStore(const char* name) const;
157 bool PrefValueFromUserStore(const char* name) const; 113 bool PrefValueFromUserStore(const char* name) const;
158 bool PrefValueFromDefaultStore(const char* name) const; 114 bool PrefValueFromDefaultStore(const char* name) const;
159 115
160 // Check whether a Preference value is modifiable by the user, i.e. whether 116 // Check whether a Preference value is modifiable by the user, i.e. whether
161 // there is no higher-priority source controlling it. 117 // there is no higher-priority source controlling it.
162 bool PrefValueUserModifiable(const char* name) const; 118 bool PrefValueUserModifiable(const char* name) const;
163 119
164 // Returns true if there are proxy preferences in user-modifiable
165 // preference stores (e.g. CommandLinePrefStore, ExtensionPrefStore)
166 // that conflict with proxy settings specified by proxy policy.
167 bool HasPolicyConflictingUserProxySettings() const;
168
169 private: 120 private:
170 // PrefStores must be listed here in order from highest to lowest priority. 121 // PrefStores must be listed here in order from highest to lowest priority.
171 // MANAGED_PLATFORM contains all managed preference values that are 122 // MANAGED_PLATFORM contains all managed preference values that are
172 // provided by a platform-specific policy mechanism (e.g. Windows 123 // provided by a platform-specific policy mechanism (e.g. Windows
173 // Group Policy). 124 // Group Policy).
174 // DEVICE_MANAGEMENT contains all managed preference values supplied 125 // DEVICE_MANAGEMENT contains all managed preference values supplied
175 // by the device management server (cloud policy). 126 // by the device management server (cloud policy).
176 // EXTENSION contains preference values set by extensions. 127 // EXTENSION contains preference values set by extensions.
177 // COMMAND_LINE contains preference values set by command-line switches. 128 // COMMAND_LINE contains preference values set by command-line switches.
178 // USER contains all user-set preference values. 129 // USER contains all user-set preference values.
(...skipping 10 matching lines...) Expand all
189 USER_STORE, 140 USER_STORE,
190 RECOMMENDED_STORE, 141 RECOMMENDED_STORE,
191 DEFAULT_STORE, 142 DEFAULT_STORE,
192 PREF_STORE_TYPE_MAX = DEFAULT_STORE 143 PREF_STORE_TYPE_MAX = DEFAULT_STORE
193 }; 144 };
194 145
195 // Keeps a PrefStore reference on behalf of the PrefValueStore and monitors 146 // Keeps a PrefStore reference on behalf of the PrefValueStore and monitors
196 // the PrefStore for changes, forwarding notifications to PrefValueStore. This 147 // the PrefStore for changes, forwarding notifications to PrefValueStore. This
197 // indirection is here for the sake of disambiguating notifications from the 148 // indirection is here for the sake of disambiguating notifications from the
198 // individual PrefStores. 149 // individual PrefStores.
199 class PrefStoreKeeper : public PrefStore::ObserverInterface { 150 class PrefStoreKeeper : public PrefStore::Observer {
200 public: 151 public:
201 PrefStoreKeeper(); 152 PrefStoreKeeper();
202 virtual ~PrefStoreKeeper(); 153 virtual ~PrefStoreKeeper();
203 154
204 // Takes ownership of |pref_store|. 155 // Takes ownership of |pref_store|.
205 void Initialize(PrefValueStore* store, 156 void Initialize(PrefValueStore* store,
206 PrefStore* pref_store, 157 PrefStore* pref_store,
207 PrefStoreType type); 158 PrefStoreType type);
208 159
209 PrefStore* store() { return pref_store_.get(); } 160 PrefStore* store() { return pref_store_.get(); }
210 const PrefStore* store() const { return pref_store_.get(); } 161 const PrefStore* store() const { return pref_store_.get(); }
211 162
212 private: 163 private:
213 // PrefStore::ObserverInterface implementation. 164 // PrefStore::Observer implementation.
214 virtual void OnPrefValueChanged(const std::string& key); 165 virtual void OnPrefValueChanged(const std::string& key);
215 virtual void OnInitializationCompleted(); 166 virtual void OnInitializationCompleted();
216 167
217 // PrefValueStore this keeper is part of. 168 // PrefValueStore this keeper is part of.
218 PrefValueStore* pref_value_store_; 169 PrefValueStore* pref_value_store_;
219 170
220 // The PrefStore managed by this keeper. 171 // The PrefStore managed by this keeper.
221 scoped_ptr<PrefStore> pref_store_; 172 scoped_ptr<PrefStore> pref_store_;
222 173
223 // Type of the pref store. 174 // Type of the pref store.
224 PrefStoreType type_; 175 PrefStoreType type_;
225 176
226 DISALLOW_COPY_AND_ASSIGN(PrefStoreKeeper); 177 DISALLOW_COPY_AND_ASSIGN(PrefStoreKeeper);
227 }; 178 };
228 179
229 typedef std::map<std::string, Value::ValueType> PrefTypeMap; 180 typedef std::map<std::string, Value::ValueType> PrefTypeMap;
230 181
231 friend class PrefValueStoreTest; 182 friend class PrefValueStorePolicyRefreshTest;
232 FRIEND_TEST_ALL_PREFIXES(PrefValueStoreTest, TestPolicyRefresh); 183 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest, TestPolicyRefresh);
233 FRIEND_TEST_ALL_PREFIXES(PrefValueStoreTest, 184 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest,
234 TestRefreshPolicyPrefsCompletion); 185 TestRefreshPolicyPrefsCompletion);
235 FRIEND_TEST_ALL_PREFIXES(PrefValueStoreTest, 186 FRIEND_TEST_ALL_PREFIXES(PrefValueStorePolicyRefreshTest,
236 TestConcurrentPolicyRefresh); 187 TestConcurrentPolicyRefresh);
237 188
238 // Returns true if the actual type is a valid type for the expected type when 189 // Returns true if the actual type is a valid type for the expected type when
239 // found in the given store. 190 // found in the given store.
240 static bool IsValidType(Value::ValueType expected, 191 static bool IsValidType(Value::ValueType expected,
241 Value::ValueType actual, 192 Value::ValueType actual,
242 PrefStoreType store); 193 PrefStoreType store);
243 194
244 // Returns true if the preference with the given name has a value in the 195 // Returns true if the preference with the given name has a value in the
245 // given PrefStoreType, of the same value type as the preference was 196 // given PrefStoreType, of the same value type as the preference was
(...skipping 28 matching lines...) Expand all
274 225
275 // Called as a result of a notification of policy change. Triggers a reload of 226 // Called as a result of a notification of policy change. Triggers a reload of
276 // managed platform, device management and recommended preferences from policy 227 // managed platform, device management and recommended preferences from policy
277 // from a Task on the FILE thread. 228 // from a Task on the FILE thread.
278 void RefreshPolicyPrefs(); 229 void RefreshPolicyPrefs();
279 230
280 // Called during policy refresh after ReadPrefs completes on the thread 231 // Called during policy refresh after ReadPrefs completes on the thread
281 // that initiated the policy refresh. RefreshPolicyPrefsCompletion takes 232 // that initiated the policy refresh. RefreshPolicyPrefsCompletion takes
282 // ownership of the |callback| object. 233 // ownership of the |callback| object.
283 void RefreshPolicyPrefsCompletion( 234 void RefreshPolicyPrefsCompletion(
284 PrefStore* new_managed_platform_pref_store, 235 policy::ConfigurationPolicyPrefStore* new_managed_platform_pref_store,
285 PrefStore* new_device_management_pref_store, 236 policy::ConfigurationPolicyPrefStore* new_device_management_pref_store,
286 PrefStore* new_recommended_pref_store); 237 policy::ConfigurationPolicyPrefStore* new_recommended_pref_store);
287 238
288 // Called during policy refresh to do the ReadPrefs on the FILE thread. 239 // Called during policy refresh to do the ReadPrefs on the FILE thread.
289 // RefreshPolicyPrefsOnFileThread takes ownership of the |callback| object. 240 // RefreshPolicyPrefsOnFileThread takes ownership of the |callback| object.
290 void RefreshPolicyPrefsOnFileThread( 241 void RefreshPolicyPrefsOnFileThread(
291 BrowserThread::ID calling_thread_id, 242 BrowserThread::ID calling_thread_id,
292 PrefStore* new_managed_platform_pref_store, 243 policy::ConfigurationPolicyPrefStore* new_managed_platform_pref_store,
293 PrefStore* new_device_management_pref_store, 244 policy::ConfigurationPolicyPrefStore* new_device_management_pref_store,
294 PrefStore* new_recommended_pref_store); 245 policy::ConfigurationPolicyPrefStore* new_recommended_pref_store);
295 246
296 // NotificationObserver methods: 247 // NotificationObserver methods:
297 virtual void Observe(NotificationType type, 248 virtual void Observe(NotificationType type,
298 const NotificationSource& source, 249 const NotificationSource& source,
299 const NotificationDetails& details); 250 const NotificationDetails& details);
300 251
301 // Called from the PrefStoreKeeper implementation when a pref value for |key| 252 // Called from the PrefStoreKeeper implementation when a pref value for |key|
302 // changed in the pref store for |type|. 253 // changed in the pref store for |type|.
303 void OnPrefValueChanged(PrefStoreType type, const std::string& key); 254 void OnPrefValueChanged(PrefStoreType type, const std::string& key);
304 255
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 // upon policy refresh. 289 // upon policy refresh.
339 Profile* profile_; 290 Profile* profile_;
340 291
341 // TODO(mnissler): Remove this after cleaning up policy refresh handling. 292 // TODO(mnissler): Remove this after cleaning up policy refresh handling.
342 NotificationRegistrar registrar_; 293 NotificationRegistrar registrar_;
343 294
344 DISALLOW_COPY_AND_ASSIGN(PrefValueStore); 295 DISALLOW_COPY_AND_ASSIGN(PrefValueStore);
345 }; 296 };
346 297
347 #endif // CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ 298 #endif // CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_value_map.cc ('k') | chrome/browser/prefs/pref_value_store.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698