pref_value_store.h 14 KB

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