| 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 #ifndef COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_ | 5 #ifndef COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_ |
| 6 #define COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_ | 6 #define COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "components/prefs/pref_registry.h" | 14 #include "components/prefs/pref_registry.h" |
| 14 #include "components/prefs/pref_service.h" | 15 #include "components/prefs/pref_service.h" |
| 15 #include "components/prefs/testing_pref_store.h" | 16 #include "components/prefs/testing_pref_store.h" |
| 16 | 17 |
| 17 class PrefNotifierImpl; | 18 class PrefNotifierImpl; |
| 18 class PrefRegistrySimple; | 19 class PrefRegistrySimple; |
| 19 class TestingPrefStore; | 20 class TestingPrefStore; |
| 20 | 21 |
| 21 // A PrefService subclass for testing. It operates totally in memory and | 22 // A PrefService subclass for testing. It operates totally in memory and |
| 22 // provides additional API for manipulating preferences at the different levels | 23 // provides additional API for manipulating preferences at the different levels |
| 23 // (managed, extension, user) conveniently. | 24 // (managed, extension, user) conveniently. |
| 24 // | 25 // |
| 25 // Use this via its specializations, e.g. TestingPrefServiceSimple. | 26 // Use this via its specializations, e.g. TestingPrefServiceSimple. |
| 26 template <class SuperPrefService, class ConstructionPrefRegistry> | 27 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 27 class TestingPrefServiceBase : public SuperPrefService { | 28 class TestingPrefServiceBase : public SuperPrefService { |
| 28 public: | 29 public: |
| 29 virtual ~TestingPrefServiceBase(); | 30 virtual ~TestingPrefServiceBase(); |
| 30 | 31 |
| 31 // Read the value of a preference from the managed layer. Returns NULL if the | 32 // Read the value of a preference from the managed layer. Returns NULL if the |
| 32 // preference is not defined at the managed layer. | 33 // preference is not defined at the managed layer. |
| 33 const base::Value* GetManagedPref(const std::string& path) const; | 34 const base::Value* GetManagedPref(const std::string& path) const; |
| 34 | 35 |
| 35 // Set a preference on the managed layer and fire observers if the preference | 36 // Set a preference on the managed layer and fire observers if the preference |
| 36 // changed. Assumes ownership of |value|. | 37 // changed. |
| 37 void SetManagedPref(const std::string& path, base::Value* value); | 38 void SetManagedPref(const std::string& path, |
| 39 std::unique_ptr<base::Value> value); |
| 38 | 40 |
| 39 // Clear the preference on the managed layer and fire observers if the | 41 // Clear the preference on the managed layer and fire observers if the |
| 40 // preference has been defined previously. | 42 // preference has been defined previously. |
| 41 void RemoveManagedPref(const std::string& path); | 43 void RemoveManagedPref(const std::string& path); |
| 42 | 44 |
| 43 // Similar to the above, but for extension preferences. | 45 // Similar to the above, but for extension preferences. |
| 44 // Does not really know about extensions and their order of installation. | 46 // Does not really know about extensions and their order of installation. |
| 45 // Useful in tests that only check that a preference is overridden by an | 47 // Useful in tests that only check that a preference is overridden by an |
| 46 // extension. | 48 // extension. |
| 47 const base::Value* GetExtensionPref(const std::string& path) const; | 49 const base::Value* GetExtensionPref(const std::string& path) const; |
| 48 void SetExtensionPref(const std::string& path, base::Value* value); | 50 void SetExtensionPref(const std::string& path, |
| 51 std::unique_ptr<base::Value> value); |
| 49 void RemoveExtensionPref(const std::string& path); | 52 void RemoveExtensionPref(const std::string& path); |
| 50 | 53 |
| 51 // Similar to the above, but for user preferences. | 54 // Similar to the above, but for user preferences. |
| 52 const base::Value* GetUserPref(const std::string& path) const; | 55 const base::Value* GetUserPref(const std::string& path) const; |
| 53 void SetUserPref(const std::string& path, base::Value* value); | 56 void SetUserPref(const std::string& path, std::unique_ptr<base::Value> value); |
| 54 void RemoveUserPref(const std::string& path); | 57 void RemoveUserPref(const std::string& path); |
| 55 | 58 |
| 56 // Similar to the above, but for recommended policy preferences. | 59 // Similar to the above, but for recommended policy preferences. |
| 57 const base::Value* GetRecommendedPref(const std::string& path) const; | 60 const base::Value* GetRecommendedPref(const std::string& path) const; |
| 58 void SetRecommendedPref(const std::string& path, base::Value* value); | 61 void SetRecommendedPref(const std::string& path, |
| 62 std::unique_ptr<base::Value> value); |
| 59 void RemoveRecommendedPref(const std::string& path); | 63 void RemoveRecommendedPref(const std::string& path); |
| 60 | 64 |
| 61 // Do-nothing implementation for TestingPrefService. | 65 // Do-nothing implementation for TestingPrefService. |
| 62 static void HandleReadError(PersistentPrefStore::PrefReadError error) {} | 66 static void HandleReadError(PersistentPrefStore::PrefReadError error) {} |
| 63 | 67 |
| 64 protected: | 68 protected: |
| 65 TestingPrefServiceBase(TestingPrefStore* managed_prefs, | 69 TestingPrefServiceBase(TestingPrefStore* managed_prefs, |
| 66 TestingPrefStore* extension_prefs, | 70 TestingPrefStore* extension_prefs, |
| 67 TestingPrefStore* user_prefs, | 71 TestingPrefStore* user_prefs, |
| 68 TestingPrefStore* recommended_prefs, | 72 TestingPrefStore* recommended_prefs, |
| 69 ConstructionPrefRegistry* pref_registry, | 73 ConstructionPrefRegistry* pref_registry, |
| 70 PrefNotifierImpl* pref_notifier); | 74 PrefNotifierImpl* pref_notifier); |
| 71 | 75 |
| 72 private: | 76 private: |
| 73 // Reads the value of the preference indicated by |path| from |pref_store|. | 77 // Reads the value of the preference indicated by |path| from |pref_store|. |
| 74 // Returns NULL if the preference was not found. | 78 // Returns NULL if the preference was not found. |
| 75 const base::Value* GetPref(TestingPrefStore* pref_store, | 79 const base::Value* GetPref(TestingPrefStore* pref_store, |
| 76 const std::string& path) const; | 80 const std::string& path) const; |
| 77 | 81 |
| 78 // Sets the value for |path| in |pref_store|. | 82 // Sets the value for |path| in |pref_store|. |
| 79 void SetPref(TestingPrefStore* pref_store, | 83 void SetPref(TestingPrefStore* pref_store, |
| 80 const std::string& path, | 84 const std::string& path, |
| 81 base::Value* value); | 85 std::unique_ptr<base::Value> value); |
| 82 | 86 |
| 83 // Removes the preference identified by |path| from |pref_store|. | 87 // Removes the preference identified by |path| from |pref_store|. |
| 84 void RemovePref(TestingPrefStore* pref_store, const std::string& path); | 88 void RemovePref(TestingPrefStore* pref_store, const std::string& path); |
| 85 | 89 |
| 86 // Pointers to the pref stores our value store uses. | 90 // Pointers to the pref stores our value store uses. |
| 87 scoped_refptr<TestingPrefStore> managed_prefs_; | 91 scoped_refptr<TestingPrefStore> managed_prefs_; |
| 88 scoped_refptr<TestingPrefStore> extension_prefs_; | 92 scoped_refptr<TestingPrefStore> extension_prefs_; |
| 89 scoped_refptr<TestingPrefStore> user_prefs_; | 93 scoped_refptr<TestingPrefStore> user_prefs_; |
| 90 scoped_refptr<TestingPrefStore> recommended_prefs_; | 94 scoped_refptr<TestingPrefStore> recommended_prefs_; |
| 91 | 95 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 | 130 |
| 127 template <class SuperPrefService, class ConstructionPrefRegistry> | 131 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 128 const base::Value* TestingPrefServiceBase< | 132 const base::Value* TestingPrefServiceBase< |
| 129 SuperPrefService, | 133 SuperPrefService, |
| 130 ConstructionPrefRegistry>::GetManagedPref(const std::string& path) const { | 134 ConstructionPrefRegistry>::GetManagedPref(const std::string& path) const { |
| 131 return GetPref(managed_prefs_.get(), path); | 135 return GetPref(managed_prefs_.get(), path); |
| 132 } | 136 } |
| 133 | 137 |
| 134 template <class SuperPrefService, class ConstructionPrefRegistry> | 138 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 135 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 139 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 136 SetManagedPref(const std::string& path, base::Value* value) { | 140 SetManagedPref(const std::string& path, |
| 137 SetPref(managed_prefs_.get(), path, value); | 141 std::unique_ptr<base::Value> value) { |
| 142 SetPref(managed_prefs_.get(), path, std::move(value)); |
| 138 } | 143 } |
| 139 | 144 |
| 140 template <class SuperPrefService, class ConstructionPrefRegistry> | 145 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 141 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 146 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 142 RemoveManagedPref(const std::string& path) { | 147 RemoveManagedPref(const std::string& path) { |
| 143 RemovePref(managed_prefs_.get(), path); | 148 RemovePref(managed_prefs_.get(), path); |
| 144 } | 149 } |
| 145 | 150 |
| 146 template <class SuperPrefService, class ConstructionPrefRegistry> | 151 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 147 const base::Value* TestingPrefServiceBase< | 152 const base::Value* TestingPrefServiceBase< |
| 148 SuperPrefService, | 153 SuperPrefService, |
| 149 ConstructionPrefRegistry>::GetExtensionPref(const std::string& path) const { | 154 ConstructionPrefRegistry>::GetExtensionPref(const std::string& path) const { |
| 150 return GetPref(extension_prefs_.get(), path); | 155 return GetPref(extension_prefs_.get(), path); |
| 151 } | 156 } |
| 152 | 157 |
| 153 template <class SuperPrefService, class ConstructionPrefRegistry> | 158 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 154 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 159 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 155 SetExtensionPref(const std::string& path, base::Value* value) { | 160 SetExtensionPref(const std::string& path, |
| 156 SetPref(extension_prefs_.get(), path, value); | 161 std::unique_ptr<base::Value> value) { |
| 162 SetPref(extension_prefs_.get(), path, std::move(value)); |
| 157 } | 163 } |
| 158 | 164 |
| 159 template <class SuperPrefService, class ConstructionPrefRegistry> | 165 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 160 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 166 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 161 RemoveExtensionPref(const std::string& path) { | 167 RemoveExtensionPref(const std::string& path) { |
| 162 RemovePref(extension_prefs_.get(), path); | 168 RemovePref(extension_prefs_.get(), path); |
| 163 } | 169 } |
| 164 | 170 |
| 165 template <class SuperPrefService, class ConstructionPrefRegistry> | 171 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 166 const base::Value* | 172 const base::Value* |
| 167 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetUserPref( | 173 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetUserPref( |
| 168 const std::string& path) const { | 174 const std::string& path) const { |
| 169 return GetPref(user_prefs_.get(), path); | 175 return GetPref(user_prefs_.get(), path); |
| 170 } | 176 } |
| 171 | 177 |
| 172 template <class SuperPrefService, class ConstructionPrefRegistry> | 178 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 173 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 179 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 174 SetUserPref(const std::string& path, base::Value* value) { | 180 SetUserPref(const std::string& path, std::unique_ptr<base::Value> value) { |
| 175 SetPref(user_prefs_.get(), path, value); | 181 SetPref(user_prefs_.get(), path, std::move(value)); |
| 176 } | 182 } |
| 177 | 183 |
| 178 template <class SuperPrefService, class ConstructionPrefRegistry> | 184 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 179 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 185 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 180 RemoveUserPref(const std::string& path) { | 186 RemoveUserPref(const std::string& path) { |
| 181 RemovePref(user_prefs_.get(), path); | 187 RemovePref(user_prefs_.get(), path); |
| 182 } | 188 } |
| 183 | 189 |
| 184 template <class SuperPrefService, class ConstructionPrefRegistry> | 190 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 185 const base::Value* | 191 const base::Value* |
| 186 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 192 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 187 GetRecommendedPref(const std::string& path) const { | 193 GetRecommendedPref(const std::string& path) const { |
| 188 return GetPref(recommended_prefs_, path); | 194 return GetPref(recommended_prefs_, path); |
| 189 } | 195 } |
| 190 | 196 |
| 191 template <class SuperPrefService, class ConstructionPrefRegistry> | 197 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 192 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 198 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 193 SetRecommendedPref(const std::string& path, base::Value* value) { | 199 SetRecommendedPref(const std::string& path, |
| 194 SetPref(recommended_prefs_.get(), path, value); | 200 std::unique_ptr<base::Value> value) { |
| 201 SetPref(recommended_prefs_.get(), path, std::move(value)); |
| 195 } | 202 } |
| 196 | 203 |
| 197 template <class SuperPrefService, class ConstructionPrefRegistry> | 204 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 198 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 205 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 199 RemoveRecommendedPref(const std::string& path) { | 206 RemoveRecommendedPref(const std::string& path) { |
| 200 RemovePref(recommended_prefs_.get(), path); | 207 RemovePref(recommended_prefs_.get(), path); |
| 201 } | 208 } |
| 202 | 209 |
| 203 template <class SuperPrefService, class ConstructionPrefRegistry> | 210 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 204 const base::Value* | 211 const base::Value* |
| 205 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetPref( | 212 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetPref( |
| 206 TestingPrefStore* pref_store, | 213 TestingPrefStore* pref_store, |
| 207 const std::string& path) const { | 214 const std::string& path) const { |
| 208 const base::Value* res; | 215 const base::Value* res; |
| 209 return pref_store->GetValue(path, &res) ? res : NULL; | 216 return pref_store->GetValue(path, &res) ? res : NULL; |
| 210 } | 217 } |
| 211 | 218 |
| 212 template <class SuperPrefService, class ConstructionPrefRegistry> | 219 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 213 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 220 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 214 SetPref(TestingPrefStore* pref_store, | 221 SetPref(TestingPrefStore* pref_store, |
| 215 const std::string& path, | 222 const std::string& path, |
| 216 base::Value* value) { | 223 std::unique_ptr<base::Value> value) { |
| 217 pref_store->SetValue(path, base::WrapUnique(value), | 224 pref_store->SetValue(path, std::move(value), |
| 218 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 225 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 219 } | 226 } |
| 220 | 227 |
| 221 template <class SuperPrefService, class ConstructionPrefRegistry> | 228 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 222 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 229 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 223 RemovePref(TestingPrefStore* pref_store, const std::string& path) { | 230 RemovePref(TestingPrefStore* pref_store, const std::string& path) { |
| 224 pref_store->RemoveValue(path, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 231 pref_store->RemoveValue(path, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 225 } | 232 } |
| 226 | 233 |
| 227 #endif // COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_ | 234 #endif // COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_ |
| OLD | NEW |