OLD | NEW |
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 // TODO(brettw) remove this forwarding header when prefs is completely moved to | 5 #ifndef BASE_PREFS_PREF_VALUE_STORE_H_ |
6 // components. | 6 #define BASE_PREFS_PREF_VALUE_STORE_H_ |
7 #include "components/prefs/pref_value_store.h" | 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/callback.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/prefs/base_prefs_export.h" |
| 16 #include "base/prefs/pref_store.h" |
| 17 #include "base/values.h" |
| 18 |
| 19 class PrefNotifier; |
| 20 class PrefStore; |
| 21 |
| 22 // The PrefValueStore manages various sources of values for Preferences |
| 23 // (e.g., configuration policies, extensions, and user settings). It returns |
| 24 // the value of a Preference from the source with the highest priority, and |
| 25 // allows setting user-defined values for preferences that are not managed. |
| 26 // |
| 27 // Unless otherwise explicitly noted, all of the methods of this class must |
| 28 // be called on the UI thread. |
| 29 class BASE_PREFS_EXPORT PrefValueStore { |
| 30 public: |
| 31 typedef base::Callback<void(const std::string&)> PrefChangedCallback; |
| 32 |
| 33 // In decreasing order of precedence: |
| 34 // |managed_prefs| contains all preferences from mandatory policies. |
| 35 // |supervised_user_prefs| contains all preferences from supervised user |
| 36 // settings, i.e. settings configured for a supervised user by their |
| 37 // custodian. |
| 38 // |extension_prefs| contains preference values set by extensions. |
| 39 // |command_line_prefs| contains preference values set by command-line |
| 40 // switches. |
| 41 // |user_prefs| contains all user-set preference values. |
| 42 // |recommended_prefs| contains all preferences from recommended policies. |
| 43 // |default_prefs| contains application-default preference values. It must |
| 44 // be non-null if any preferences are to be registered. |
| 45 // |
| 46 // |pref_notifier| facilitates broadcasting preference change notifications |
| 47 // to the world. |
| 48 PrefValueStore(PrefStore* managed_prefs, |
| 49 PrefStore* supervised_user_prefs, |
| 50 PrefStore* extension_prefs, |
| 51 PrefStore* command_line_prefs, |
| 52 PrefStore* user_prefs, |
| 53 PrefStore* recommended_prefs, |
| 54 PrefStore* default_prefs, |
| 55 PrefNotifier* pref_notifier); |
| 56 virtual ~PrefValueStore(); |
| 57 |
| 58 // Creates a clone of this PrefValueStore with PrefStores overwritten |
| 59 // by the parameters passed, if unequal NULL. |
| 60 PrefValueStore* CloneAndSpecialize(PrefStore* managed_prefs, |
| 61 PrefStore* supervised_user_prefs, |
| 62 PrefStore* extension_prefs, |
| 63 PrefStore* command_line_prefs, |
| 64 PrefStore* user_prefs, |
| 65 PrefStore* recommended_prefs, |
| 66 PrefStore* default_prefs, |
| 67 PrefNotifier* pref_notifier); |
| 68 |
| 69 // A PrefValueStore can have exactly one callback that is directly |
| 70 // notified of preferences changing in the store. This does not |
| 71 // filter through the PrefNotifier mechanism, which may not forward |
| 72 // certain changes (e.g. unregistered prefs). |
| 73 void set_callback(const PrefChangedCallback& callback); |
| 74 |
| 75 // Gets the value for the given preference name that has the specified value |
| 76 // type. Values stored in a PrefStore that have the matching |name| but |
| 77 // a non-matching |type| are silently skipped. Returns true if a valid value |
| 78 // was found in any of the available PrefStores. Most callers should use |
| 79 // Preference::GetValue() instead of calling this method directly. |
| 80 bool GetValue(const std::string& name, |
| 81 base::Value::Type type, |
| 82 const base::Value** out_value) const; |
| 83 |
| 84 // Gets the recommended value for the given preference name that has the |
| 85 // specified value type. A value stored in the recommended PrefStore that has |
| 86 // the matching |name| but a non-matching |type| is silently ignored. Returns |
| 87 // true if a valid value was found. Most callers should use |
| 88 // Preference::GetRecommendedValue() instead of calling this method directly. |
| 89 bool GetRecommendedValue(const std::string& name, |
| 90 base::Value::Type type, |
| 91 const base::Value** out_value) const; |
| 92 |
| 93 // These methods return true if a preference with the given name is in the |
| 94 // indicated pref store, even if that value is currently being overridden by |
| 95 // a higher-priority source. |
| 96 bool PrefValueInManagedStore(const std::string& name) const; |
| 97 bool PrefValueInSupervisedStore(const std::string& name) const; |
| 98 bool PrefValueInExtensionStore(const std::string& name) const; |
| 99 bool PrefValueInUserStore(const std::string& name) const; |
| 100 |
| 101 // These methods return true if a preference with the given name is actually |
| 102 // being controlled by the indicated pref store and not being overridden by |
| 103 // a higher-priority source. |
| 104 bool PrefValueFromExtensionStore(const std::string& name) const; |
| 105 bool PrefValueFromUserStore(const std::string& name) const; |
| 106 bool PrefValueFromRecommendedStore(const std::string& name) const; |
| 107 bool PrefValueFromDefaultStore(const std::string& name) const; |
| 108 |
| 109 // Check whether a Preference value is modifiable by the user, i.e. whether |
| 110 // there is no higher-priority source controlling it. |
| 111 bool PrefValueUserModifiable(const std::string& name) const; |
| 112 |
| 113 // Check whether a Preference value is modifiable by an extension, i.e. |
| 114 // whether there is no higher-priority source controlling it. |
| 115 bool PrefValueExtensionModifiable(const std::string& name) const; |
| 116 |
| 117 // Update the command line PrefStore with |command_line_prefs|. |
| 118 void UpdateCommandLinePrefStore(PrefStore* command_line_prefs); |
| 119 |
| 120 private: |
| 121 // PrefStores must be listed here in order from highest to lowest priority. |
| 122 // MANAGED contains all managed preference values that are provided by |
| 123 // mandatory policies (e.g. Windows Group Policy or cloud policy). |
| 124 // SUPERVISED_USER contains preferences that are valid for supervised users. |
| 125 // EXTENSION contains preference values set by extensions. |
| 126 // COMMAND_LINE contains preference values set by command-line switches. |
| 127 // USER contains all user-set preference values. |
| 128 // RECOMMENDED contains all preferences that are provided by recommended |
| 129 // policies. |
| 130 // DEFAULT contains all application default preference values. |
| 131 enum PrefStoreType { |
| 132 // INVALID_STORE is not associated with an actual PrefStore but used as |
| 133 // an invalid marker, e.g. as a return value. |
| 134 INVALID_STORE = -1, |
| 135 MANAGED_STORE = 0, |
| 136 SUPERVISED_USER_STORE, |
| 137 EXTENSION_STORE, |
| 138 COMMAND_LINE_STORE, |
| 139 USER_STORE, |
| 140 RECOMMENDED_STORE, |
| 141 DEFAULT_STORE, |
| 142 PREF_STORE_TYPE_MAX = DEFAULT_STORE |
| 143 }; |
| 144 |
| 145 // Keeps a PrefStore reference on behalf of the PrefValueStore and monitors |
| 146 // the PrefStore for changes, forwarding notifications to PrefValueStore. This |
| 147 // indirection is here for the sake of disambiguating notifications from the |
| 148 // individual PrefStores. |
| 149 class PrefStoreKeeper : public PrefStore::Observer { |
| 150 public: |
| 151 PrefStoreKeeper(); |
| 152 ~PrefStoreKeeper() override; |
| 153 |
| 154 // Takes ownership of |pref_store|. |
| 155 void Initialize(PrefValueStore* store, |
| 156 PrefStore* pref_store, |
| 157 PrefStoreType type); |
| 158 |
| 159 PrefStore* store() { return pref_store_.get(); } |
| 160 const PrefStore* store() const { return pref_store_.get(); } |
| 161 |
| 162 private: |
| 163 // PrefStore::Observer implementation. |
| 164 void OnPrefValueChanged(const std::string& key) override; |
| 165 void OnInitializationCompleted(bool succeeded) override; |
| 166 |
| 167 // PrefValueStore this keeper is part of. |
| 168 PrefValueStore* pref_value_store_; |
| 169 |
| 170 // The PrefStore managed by this keeper. |
| 171 scoped_refptr<PrefStore> pref_store_; |
| 172 |
| 173 // Type of the pref store. |
| 174 PrefStoreType type_; |
| 175 |
| 176 DISALLOW_COPY_AND_ASSIGN(PrefStoreKeeper); |
| 177 }; |
| 178 |
| 179 typedef std::map<std::string, base::Value::Type> PrefTypeMap; |
| 180 |
| 181 // Returns true if the preference with the given name has a value in the |
| 182 // given PrefStoreType, of the same value type as the preference was |
| 183 // registered with. |
| 184 bool PrefValueInStore(const std::string& name, PrefStoreType store) const; |
| 185 |
| 186 // Returns true if a preference has an explicit value in any of the |
| 187 // stores in the range specified by |first_checked_store| and |
| 188 // |last_checked_store|, even if that value is currently being |
| 189 // overridden by a higher-priority store. |
| 190 bool PrefValueInStoreRange(const std::string& name, |
| 191 PrefStoreType first_checked_store, |
| 192 PrefStoreType last_checked_store) const; |
| 193 |
| 194 // Returns the pref store type identifying the source that controls the |
| 195 // Preference identified by |name|. If none of the sources has a value, |
| 196 // INVALID_STORE is returned. In practice, the default PrefStore |
| 197 // should always have a value for any registered preferencem, so INVALID_STORE |
| 198 // indicates an error. |
| 199 PrefStoreType ControllingPrefStoreForPref(const std::string& name) const; |
| 200 |
| 201 // Get a value from the specified |store|. |
| 202 bool GetValueFromStore(const std::string& name, |
| 203 PrefStoreType store, |
| 204 const base::Value** out_value) const; |
| 205 |
| 206 // Get a value from the specified |store| if its |type| matches. |
| 207 bool GetValueFromStoreWithType(const std::string& name, |
| 208 base::Value::Type type, |
| 209 PrefStoreType store, |
| 210 const base::Value** out_value) const; |
| 211 |
| 212 // Called upon changes in individual pref stores in order to determine whether |
| 213 // the user-visible pref value has changed. Triggers the change notification |
| 214 // if the effective value of the preference has changed, or if the store |
| 215 // controlling the pref has changed. |
| 216 void NotifyPrefChanged(const std::string& path, PrefStoreType new_store); |
| 217 |
| 218 // Called from the PrefStoreKeeper implementation when a pref value for |key| |
| 219 // changed in the pref store for |type|. |
| 220 void OnPrefValueChanged(PrefStoreType type, const std::string& key); |
| 221 |
| 222 // Handle the event that the store for |type| has completed initialization. |
| 223 void OnInitializationCompleted(PrefStoreType type, bool succeeded); |
| 224 |
| 225 // Initializes a pref store keeper. Sets up a PrefStoreKeeper that will take |
| 226 // ownership of the passed |pref_store|. |
| 227 void InitPrefStore(PrefStoreType type, PrefStore* pref_store); |
| 228 |
| 229 // Checks whether initialization is completed and tells the notifier if that |
| 230 // is the case. |
| 231 void CheckInitializationCompleted(); |
| 232 |
| 233 // Get the PrefStore pointer for the given type. May return NULL if there is |
| 234 // no PrefStore for that type. |
| 235 PrefStore* GetPrefStore(PrefStoreType type) { |
| 236 return pref_stores_[type].store(); |
| 237 } |
| 238 const PrefStore* GetPrefStore(PrefStoreType type) const { |
| 239 return pref_stores_[type].store(); |
| 240 } |
| 241 |
| 242 // Keeps the PrefStore references in order of precedence. |
| 243 PrefStoreKeeper pref_stores_[PREF_STORE_TYPE_MAX + 1]; |
| 244 |
| 245 PrefChangedCallback pref_changed_callback_; |
| 246 |
| 247 // Used for generating notifications. This is a weak reference, |
| 248 // since the notifier is owned by the corresponding PrefService. |
| 249 PrefNotifier* pref_notifier_; |
| 250 |
| 251 // A mapping of preference names to their registered types. |
| 252 PrefTypeMap pref_types_; |
| 253 |
| 254 // True if not all of the PrefStores were initialized successfully. |
| 255 bool initialization_failed_; |
| 256 |
| 257 DISALLOW_COPY_AND_ASSIGN(PrefValueStore); |
| 258 }; |
| 259 |
| 260 #endif // BASE_PREFS_PREF_VALUE_STORE_H_ |
OLD | NEW |