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