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_TESTING_PREF_SERVICE_H_ |
6 // components. | 6 #define BASE_PREFS_TESTING_PREF_SERVICE_H_ |
7 #include "components/prefs/testing_pref_service.h" | 7 |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/prefs/pref_registry.h" |
| 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/prefs/testing_pref_store.h" |
| 14 |
| 15 class PrefNotifierImpl; |
| 16 class PrefRegistrySimple; |
| 17 class TestingPrefStore; |
| 18 |
| 19 // A PrefService subclass for testing. It operates totally in memory and |
| 20 // provides additional API for manipulating preferences at the different levels |
| 21 // (managed, extension, user) conveniently. |
| 22 // |
| 23 // Use this via its specializations, e.g. TestingPrefServiceSimple. |
| 24 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 25 class TestingPrefServiceBase : public SuperPrefService { |
| 26 public: |
| 27 virtual ~TestingPrefServiceBase(); |
| 28 |
| 29 // Read the value of a preference from the managed layer. Returns NULL if the |
| 30 // preference is not defined at the managed layer. |
| 31 const base::Value* GetManagedPref(const std::string& path) const; |
| 32 |
| 33 // Set a preference on the managed layer and fire observers if the preference |
| 34 // changed. Assumes ownership of |value|. |
| 35 void SetManagedPref(const std::string& path, base::Value* value); |
| 36 |
| 37 // Clear the preference on the managed layer and fire observers if the |
| 38 // preference has been defined previously. |
| 39 void RemoveManagedPref(const std::string& path); |
| 40 |
| 41 // Similar to the above, but for user preferences. |
| 42 const base::Value* GetUserPref(const std::string& path) const; |
| 43 void SetUserPref(const std::string& path, base::Value* value); |
| 44 void RemoveUserPref(const std::string& path); |
| 45 |
| 46 // Similar to the above, but for recommended policy preferences. |
| 47 const base::Value* GetRecommendedPref(const std::string& path) const; |
| 48 void SetRecommendedPref(const std::string& path, base::Value* value); |
| 49 void RemoveRecommendedPref(const std::string& path); |
| 50 |
| 51 // Do-nothing implementation for TestingPrefService. |
| 52 static void HandleReadError(PersistentPrefStore::PrefReadError error) {} |
| 53 |
| 54 protected: |
| 55 TestingPrefServiceBase( |
| 56 TestingPrefStore* managed_prefs, |
| 57 TestingPrefStore* user_prefs, |
| 58 TestingPrefStore* recommended_prefs, |
| 59 ConstructionPrefRegistry* pref_registry, |
| 60 PrefNotifierImpl* pref_notifier); |
| 61 |
| 62 private: |
| 63 // Reads the value of the preference indicated by |path| from |pref_store|. |
| 64 // Returns NULL if the preference was not found. |
| 65 const base::Value* GetPref(TestingPrefStore* pref_store, |
| 66 const std::string& path) const; |
| 67 |
| 68 // Sets the value for |path| in |pref_store|. |
| 69 void SetPref(TestingPrefStore* pref_store, |
| 70 const std::string& path, |
| 71 base::Value* value); |
| 72 |
| 73 // Removes the preference identified by |path| from |pref_store|. |
| 74 void RemovePref(TestingPrefStore* pref_store, const std::string& path); |
| 75 |
| 76 // Pointers to the pref stores our value store uses. |
| 77 scoped_refptr<TestingPrefStore> managed_prefs_; |
| 78 scoped_refptr<TestingPrefStore> user_prefs_; |
| 79 scoped_refptr<TestingPrefStore> recommended_prefs_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase); |
| 82 }; |
| 83 |
| 84 // Test version of PrefService. |
| 85 class TestingPrefServiceSimple |
| 86 : public TestingPrefServiceBase<PrefService, PrefRegistry> { |
| 87 public: |
| 88 TestingPrefServiceSimple(); |
| 89 ~TestingPrefServiceSimple() override; |
| 90 |
| 91 // This is provided as a convenience for registering preferences on |
| 92 // an existing TestingPrefServiceSimple instance. On a production |
| 93 // PrefService you would do all registrations before constructing |
| 94 // it, passing it a PrefRegistry via its constructor (or via |
| 95 // e.g. PrefServiceFactory). |
| 96 PrefRegistrySimple* registry(); |
| 97 |
| 98 private: |
| 99 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple); |
| 100 }; |
| 101 |
| 102 template<> |
| 103 TestingPrefServiceBase<PrefService, PrefRegistry>::TestingPrefServiceBase( |
| 104 TestingPrefStore* managed_prefs, |
| 105 TestingPrefStore* user_prefs, |
| 106 TestingPrefStore* recommended_prefs, |
| 107 PrefRegistry* pref_registry, |
| 108 PrefNotifierImpl* pref_notifier); |
| 109 |
| 110 template<class SuperPrefService, class ConstructionPrefRegistry> |
| 111 TestingPrefServiceBase< |
| 112 SuperPrefService, ConstructionPrefRegistry>::~TestingPrefServiceBase() { |
| 113 } |
| 114 |
| 115 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 116 const base::Value* TestingPrefServiceBase< |
| 117 SuperPrefService, |
| 118 ConstructionPrefRegistry>::GetManagedPref(const std::string& path) const { |
| 119 return GetPref(managed_prefs_.get(), path); |
| 120 } |
| 121 |
| 122 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 123 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 124 SetManagedPref(const std::string& path, base::Value* value) { |
| 125 SetPref(managed_prefs_.get(), path, value); |
| 126 } |
| 127 |
| 128 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 129 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 130 RemoveManagedPref(const std::string& path) { |
| 131 RemovePref(managed_prefs_.get(), path); |
| 132 } |
| 133 |
| 134 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 135 const base::Value* |
| 136 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetUserPref( |
| 137 const std::string& path) const { |
| 138 return GetPref(user_prefs_.get(), path); |
| 139 } |
| 140 |
| 141 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 142 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 143 SetUserPref(const std::string& path, base::Value* value) { |
| 144 SetPref(user_prefs_.get(), path, value); |
| 145 } |
| 146 |
| 147 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 148 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 149 RemoveUserPref(const std::string& path) { |
| 150 RemovePref(user_prefs_.get(), path); |
| 151 } |
| 152 |
| 153 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 154 const base::Value* |
| 155 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 156 GetRecommendedPref(const std::string& path) const { |
| 157 return GetPref(recommended_prefs_, path); |
| 158 } |
| 159 |
| 160 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 161 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 162 SetRecommendedPref(const std::string& path, base::Value* value) { |
| 163 SetPref(recommended_prefs_.get(), path, value); |
| 164 } |
| 165 |
| 166 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 167 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 168 RemoveRecommendedPref(const std::string& path) { |
| 169 RemovePref(recommended_prefs_.get(), path); |
| 170 } |
| 171 |
| 172 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 173 const base::Value* |
| 174 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetPref( |
| 175 TestingPrefStore* pref_store, |
| 176 const std::string& path) const { |
| 177 const base::Value* res; |
| 178 return pref_store->GetValue(path, &res) ? res : NULL; |
| 179 } |
| 180 |
| 181 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 182 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 183 SetPref(TestingPrefStore* pref_store, |
| 184 const std::string& path, |
| 185 base::Value* value) { |
| 186 pref_store->SetValue(path, make_scoped_ptr(value), |
| 187 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 188 } |
| 189 |
| 190 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 191 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 192 RemovePref(TestingPrefStore* pref_store, const std::string& path) { |
| 193 pref_store->RemoveValue(path, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 194 } |
| 195 |
| 196 #endif // BASE_PREFS_TESTING_PREF_SERVICE_H_ |
OLD | NEW |