testing_pref_service.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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_TESTING_PREF_SERVICE_H_
  5. #define COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_
  6. #include <memory>
  7. #include <utility>
  8. #include "base/memory/ref_counted.h"
  9. #include "components/prefs/pref_registry.h"
  10. #include "components/prefs/pref_service.h"
  11. #include "components/prefs/testing_pref_store.h"
  12. class PrefNotifierImpl;
  13. class PrefRegistrySimple;
  14. class TestingPrefStore;
  15. // A PrefService subclass for testing. It operates totally in memory and
  16. // provides additional API for manipulating preferences at the different levels
  17. // (managed, extension, user) conveniently.
  18. //
  19. // Use this via its specializations, e.g. TestingPrefServiceSimple.
  20. template <class SuperPrefService, class ConstructionPrefRegistry>
  21. class TestingPrefServiceBase : public SuperPrefService {
  22. public:
  23. TestingPrefServiceBase(const TestingPrefServiceBase&) = delete;
  24. TestingPrefServiceBase& operator=(const TestingPrefServiceBase&) = delete;
  25. virtual ~TestingPrefServiceBase();
  26. // Reads the value of a preference from the managed layer. Returns NULL if the
  27. // preference is not defined at the managed layer.
  28. const base::Value* GetManagedPref(const std::string& path) const;
  29. // Sets a preference on the managed layer and fires observers if the
  30. // preference changed.
  31. void SetManagedPref(const std::string& path,
  32. std::unique_ptr<base::Value> value);
  33. void SetManagedPref(const std::string& path, base::Value value);
  34. // Clears the preference on the managed layer and fire observers if the
  35. // preference has been defined previously.
  36. void RemoveManagedPref(const std::string& path);
  37. // Similar to the above, but for supervised user preferences.
  38. const base::Value* GetSupervisedUserPref(const std::string& path) const;
  39. void SetSupervisedUserPref(const std::string& path,
  40. std::unique_ptr<base::Value> value);
  41. void RemoveSupervisedUserPref(const std::string& path);
  42. // Similar to the above, but for extension preferences.
  43. // Does not really know about extensions and their order of installation.
  44. // Useful in tests that only check that a preference is overridden by an
  45. // extension.
  46. const base::Value* GetExtensionPref(const std::string& path) const;
  47. void SetExtensionPref(const std::string& path,
  48. std::unique_ptr<base::Value> value);
  49. void RemoveExtensionPref(const std::string& path);
  50. // Similar to the above, but for user preferences.
  51. const base::Value* GetUserPref(const std::string& path) const;
  52. void SetUserPref(const std::string& path, std::unique_ptr<base::Value> value);
  53. void SetUserPref(const std::string& path, base::Value value);
  54. void RemoveUserPref(const std::string& path);
  55. // Similar to the above, but for recommended policy preferences.
  56. const base::Value* GetRecommendedPref(const std::string& path) const;
  57. void SetRecommendedPref(const std::string& path,
  58. std::unique_ptr<base::Value> value);
  59. void SetRecommendedPref(const std::string& path, base::Value value);
  60. void RemoveRecommendedPref(const std::string& path);
  61. // Do-nothing implementation for TestingPrefService.
  62. static void HandleReadError(PersistentPrefStore::PrefReadError error) {}
  63. // Set initialization status of pref stores.
  64. void SetInitializationCompleted();
  65. scoped_refptr<TestingPrefStore> user_prefs_store() { return user_prefs_; }
  66. protected:
  67. TestingPrefServiceBase(TestingPrefStore* managed_prefs,
  68. TestingPrefStore* supervised_user_prefs,
  69. TestingPrefStore* extension_prefs,
  70. TestingPrefStore* standalone_browser_prefs,
  71. TestingPrefStore* user_prefs,
  72. TestingPrefStore* recommended_prefs,
  73. ConstructionPrefRegistry* pref_registry,
  74. PrefNotifierImpl* pref_notifier);
  75. private:
  76. // Reads the value of the preference indicated by |path| from |pref_store|.
  77. // Returns NULL if the preference was not found.
  78. const base::Value* GetPref(TestingPrefStore* pref_store,
  79. const std::string& path) const;
  80. // Sets the value for |path| in |pref_store|.
  81. void SetPref(TestingPrefStore* pref_store,
  82. const std::string& path,
  83. std::unique_ptr<base::Value> value);
  84. // Removes the preference identified by |path| from |pref_store|.
  85. void RemovePref(TestingPrefStore* pref_store, const std::string& path);
  86. // Pointers to the pref stores our value store uses.
  87. scoped_refptr<TestingPrefStore> managed_prefs_;
  88. scoped_refptr<TestingPrefStore> supervised_user_prefs_;
  89. scoped_refptr<TestingPrefStore> extension_prefs_;
  90. scoped_refptr<TestingPrefStore> standalone_browser_prefs_;
  91. scoped_refptr<TestingPrefStore> user_prefs_;
  92. scoped_refptr<TestingPrefStore> recommended_prefs_;
  93. };
  94. // Test version of PrefService.
  95. class TestingPrefServiceSimple
  96. : public TestingPrefServiceBase<PrefService, PrefRegistry> {
  97. public:
  98. TestingPrefServiceSimple();
  99. TestingPrefServiceSimple(const TestingPrefServiceSimple&) = delete;
  100. TestingPrefServiceSimple& operator=(const TestingPrefServiceSimple&) = delete;
  101. ~TestingPrefServiceSimple() override;
  102. // This is provided as a convenience for registering preferences on
  103. // an existing TestingPrefServiceSimple instance. On a production
  104. // PrefService you would do all registrations before constructing
  105. // it, passing it a PrefRegistry via its constructor (or via
  106. // e.g. PrefServiceFactory).
  107. PrefRegistrySimple* registry();
  108. };
  109. template <>
  110. TestingPrefServiceBase<PrefService, PrefRegistry>::TestingPrefServiceBase(
  111. TestingPrefStore* managed_prefs,
  112. TestingPrefStore* supervised_user_prefs,
  113. TestingPrefStore* extension_prefs,
  114. TestingPrefStore* standalone_browser_prefs,
  115. TestingPrefStore* user_prefs,
  116. TestingPrefStore* recommended_prefs,
  117. PrefRegistry* pref_registry,
  118. PrefNotifierImpl* pref_notifier);
  119. template<class SuperPrefService, class ConstructionPrefRegistry>
  120. TestingPrefServiceBase<
  121. SuperPrefService, ConstructionPrefRegistry>::~TestingPrefServiceBase() {
  122. }
  123. template <class SuperPrefService, class ConstructionPrefRegistry>
  124. const base::Value* TestingPrefServiceBase<
  125. SuperPrefService,
  126. ConstructionPrefRegistry>::GetManagedPref(const std::string& path) const {
  127. return GetPref(managed_prefs_.get(), path);
  128. }
  129. template <class SuperPrefService, class ConstructionPrefRegistry>
  130. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  131. SetManagedPref(const std::string& path,
  132. std::unique_ptr<base::Value> value) {
  133. SetPref(managed_prefs_.get(), path, std::move(value));
  134. }
  135. template <class SuperPrefService, class ConstructionPrefRegistry>
  136. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  137. SetManagedPref(const std::string& path, base::Value value) {
  138. SetManagedPref(path, base::Value::ToUniquePtrValue(std::move(value)));
  139. }
  140. template <class SuperPrefService, class ConstructionPrefRegistry>
  141. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  142. RemoveManagedPref(const std::string& path) {
  143. RemovePref(managed_prefs_.get(), path);
  144. }
  145. template <class SuperPrefService, class ConstructionPrefRegistry>
  146. const base::Value*
  147. TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  148. GetSupervisedUserPref(const std::string& path) const {
  149. return GetPref(supervised_user_prefs_.get(), path);
  150. }
  151. template <class SuperPrefService, class ConstructionPrefRegistry>
  152. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  153. SetSupervisedUserPref(const std::string& path,
  154. std::unique_ptr<base::Value> value) {
  155. SetPref(supervised_user_prefs_.get(), path, std::move(value));
  156. }
  157. template <class SuperPrefService, class ConstructionPrefRegistry>
  158. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  159. RemoveSupervisedUserPref(const std::string& path) {
  160. RemovePref(supervised_user_prefs_.get(), path);
  161. }
  162. template <class SuperPrefService, class ConstructionPrefRegistry>
  163. const base::Value* TestingPrefServiceBase<
  164. SuperPrefService,
  165. ConstructionPrefRegistry>::GetExtensionPref(const std::string& path) const {
  166. return GetPref(extension_prefs_.get(), path);
  167. }
  168. template <class SuperPrefService, class ConstructionPrefRegistry>
  169. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  170. SetExtensionPref(const std::string& path,
  171. std::unique_ptr<base::Value> value) {
  172. SetPref(extension_prefs_.get(), path, std::move(value));
  173. }
  174. template <class SuperPrefService, class ConstructionPrefRegistry>
  175. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  176. RemoveExtensionPref(const std::string& path) {
  177. RemovePref(extension_prefs_.get(), path);
  178. }
  179. template <class SuperPrefService, class ConstructionPrefRegistry>
  180. const base::Value*
  181. TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetUserPref(
  182. const std::string& path) const {
  183. return GetPref(user_prefs_.get(), path);
  184. }
  185. template <class SuperPrefService, class ConstructionPrefRegistry>
  186. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  187. SetUserPref(const std::string& path, std::unique_ptr<base::Value> value) {
  188. SetPref(user_prefs_.get(), path, std::move(value));
  189. }
  190. template <class SuperPrefService, class ConstructionPrefRegistry>
  191. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  192. SetUserPref(const std::string& path, base::Value value) {
  193. SetUserPref(path, base::Value::ToUniquePtrValue(std::move(value)));
  194. }
  195. template <class SuperPrefService, class ConstructionPrefRegistry>
  196. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  197. RemoveUserPref(const std::string& path) {
  198. RemovePref(user_prefs_.get(), path);
  199. }
  200. template <class SuperPrefService, class ConstructionPrefRegistry>
  201. const base::Value*
  202. TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  203. GetRecommendedPref(const std::string& path) const {
  204. return GetPref(recommended_prefs_, path);
  205. }
  206. template <class SuperPrefService, class ConstructionPrefRegistry>
  207. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  208. SetRecommendedPref(const std::string& path,
  209. std::unique_ptr<base::Value> value) {
  210. SetPref(recommended_prefs_.get(), path, std::move(value));
  211. }
  212. template <class SuperPrefService, class ConstructionPrefRegistry>
  213. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  214. SetRecommendedPref(const std::string& path, base::Value value) {
  215. SetPref(recommended_prefs_.get(), path,
  216. base::Value::ToUniquePtrValue(std::move(value)));
  217. }
  218. template <class SuperPrefService, class ConstructionPrefRegistry>
  219. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  220. RemoveRecommendedPref(const std::string& path) {
  221. RemovePref(recommended_prefs_.get(), path);
  222. }
  223. template <class SuperPrefService, class ConstructionPrefRegistry>
  224. const base::Value*
  225. TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetPref(
  226. TestingPrefStore* pref_store,
  227. const std::string& path) const {
  228. const base::Value* res;
  229. return pref_store->GetValue(path, &res) ? res : NULL;
  230. }
  231. template <class SuperPrefService, class ConstructionPrefRegistry>
  232. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  233. SetPref(TestingPrefStore* pref_store,
  234. const std::string& path,
  235. std::unique_ptr<base::Value> value) {
  236. pref_store->SetValue(path, std::move(value),
  237. WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  238. }
  239. template <class SuperPrefService, class ConstructionPrefRegistry>
  240. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  241. RemovePref(TestingPrefStore* pref_store, const std::string& path) {
  242. pref_store->RemoveValue(path, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  243. }
  244. template <class SuperPrefService, class ConstructionPrefRegistry>
  245. void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
  246. SetInitializationCompleted() {
  247. managed_prefs_->SetInitializationCompleted();
  248. supervised_user_prefs_->SetInitializationCompleted();
  249. extension_prefs_->SetInitializationCompleted();
  250. recommended_prefs_->SetInitializationCompleted();
  251. // |user_prefs_| and |standalone_browser_prefs_| are initialized in
  252. // PrefService constructor so no need to set initialization status again.
  253. }
  254. #endif // COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_