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