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 base::Value* GetManagedPref(const char* path) const; | 30 const base::Value* GetManagedPref(const std::string& 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, base::Value* value); | 34 void SetManagedPref(const std::string& 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 std::string& path); |
39 | 39 |
40 // Similar to the above, but for user preferences. | 40 // Similar to the above, but for user preferences. |
41 const base::Value* GetUserPref(const char* path) const; | 41 const base::Value* GetUserPref(const std::string& path) const; |
42 void SetUserPref(const char* path, base::Value* value); | 42 void SetUserPref(const std::string& path, base::Value* value); |
43 void RemoveUserPref(const char* path); | 43 void RemoveUserPref(const std::string& 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 base::Value* GetRecommendedPref(const char* path) const; | 46 const base::Value* GetRecommendedPref(const std::string& path) const; |
47 void SetRecommendedPref(const char* path, base::Value* value); | 47 void SetRecommendedPref(const std::string& path, base::Value* value); |
48 void RemoveRecommendedPref(const char* path); | 48 void RemoveRecommendedPref(const std::string& 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 base::Value* GetPref(TestingPrefStore* pref_store, | 64 const base::Value* GetPref(TestingPrefStore* pref_store, |
65 const char* path) const; | 65 const std::string& path) const; |
66 | 66 |
67 // Sets the value for |path| in |pref_store|. | 67 // Sets the value for |path| in |pref_store|. |
68 void SetPref(TestingPrefStore* pref_store, const char* path, | 68 void SetPref(TestingPrefStore* pref_store, |
| 69 const std::string& path, |
69 base::Value* value); | 70 base::Value* value); |
70 | 71 |
71 // Removes the preference identified by |path| from |pref_store|. | 72 // Removes the preference identified by |path| from |pref_store|. |
72 void RemovePref(TestingPrefStore* pref_store, const char* path); | 73 void RemovePref(TestingPrefStore* pref_store, const std::string& path); |
73 | 74 |
74 // Pointers to the pref stores our value store uses. | 75 // Pointers to the pref stores our value store uses. |
75 scoped_refptr<TestingPrefStore> managed_prefs_; | 76 scoped_refptr<TestingPrefStore> managed_prefs_; |
76 scoped_refptr<TestingPrefStore> user_prefs_; | 77 scoped_refptr<TestingPrefStore> user_prefs_; |
77 scoped_refptr<TestingPrefStore> recommended_prefs_; | 78 scoped_refptr<TestingPrefStore> recommended_prefs_; |
78 | 79 |
79 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase); | 80 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase); |
80 }; | 81 }; |
81 | 82 |
82 // Test version of PrefService. | 83 // Test version of PrefService. |
(...skipping 20 matching lines...) Expand all Loading... |
103 TestingPrefStore* user_prefs, | 104 TestingPrefStore* user_prefs, |
104 TestingPrefStore* recommended_prefs, | 105 TestingPrefStore* recommended_prefs, |
105 PrefRegistry* pref_registry, | 106 PrefRegistry* pref_registry, |
106 PrefNotifierImpl* pref_notifier); | 107 PrefNotifierImpl* pref_notifier); |
107 | 108 |
108 template<class SuperPrefService, class ConstructionPrefRegistry> | 109 template<class SuperPrefService, class ConstructionPrefRegistry> |
109 TestingPrefServiceBase< | 110 TestingPrefServiceBase< |
110 SuperPrefService, ConstructionPrefRegistry>::~TestingPrefServiceBase() { | 111 SuperPrefService, ConstructionPrefRegistry>::~TestingPrefServiceBase() { |
111 } | 112 } |
112 | 113 |
113 template<class SuperPrefService, class ConstructionPrefRegistry> | 114 template <class SuperPrefService, class ConstructionPrefRegistry> |
114 const base::Value* TestingPrefServiceBase< | 115 const base::Value* TestingPrefServiceBase< |
115 SuperPrefService, ConstructionPrefRegistry>::GetManagedPref( | 116 SuperPrefService, |
116 const char* path) const { | 117 ConstructionPrefRegistry>::GetManagedPref(const std::string& path) const { |
117 return GetPref(managed_prefs_.get(), path); | 118 return GetPref(managed_prefs_.get(), path); |
118 } | 119 } |
119 | 120 |
120 template<class SuperPrefService, class ConstructionPrefRegistry> | 121 template <class SuperPrefService, class ConstructionPrefRegistry> |
121 void TestingPrefServiceBase< | 122 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
122 SuperPrefService, ConstructionPrefRegistry>::SetManagedPref( | 123 SetManagedPref(const std::string& path, base::Value* value) { |
123 const char* path, base::Value* value) { | |
124 SetPref(managed_prefs_.get(), path, value); | 124 SetPref(managed_prefs_.get(), path, value); |
125 } | 125 } |
126 | 126 |
127 template<class SuperPrefService, class ConstructionPrefRegistry> | 127 template <class SuperPrefService, class ConstructionPrefRegistry> |
128 void TestingPrefServiceBase< | 128 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
129 SuperPrefService, ConstructionPrefRegistry>::RemoveManagedPref( | 129 RemoveManagedPref(const std::string& path) { |
130 const char* path) { | |
131 RemovePref(managed_prefs_.get(), path); | 130 RemovePref(managed_prefs_.get(), path); |
132 } | 131 } |
133 | 132 |
134 template<class SuperPrefService, class ConstructionPrefRegistry> | 133 template <class SuperPrefService, class ConstructionPrefRegistry> |
135 const base::Value* TestingPrefServiceBase< | 134 const base::Value* |
136 SuperPrefService, ConstructionPrefRegistry>::GetUserPref( | 135 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetUserPref( |
137 const char* path) const { | 136 const std::string& path) const { |
138 return GetPref(user_prefs_.get(), path); | 137 return GetPref(user_prefs_.get(), path); |
139 } | 138 } |
140 | 139 |
141 template<class SuperPrefService, class ConstructionPrefRegistry> | 140 template <class SuperPrefService, class ConstructionPrefRegistry> |
142 void TestingPrefServiceBase< | 141 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
143 SuperPrefService, ConstructionPrefRegistry>::SetUserPref( | 142 SetUserPref(const std::string& path, base::Value* value) { |
144 const char* path, base::Value* value) { | |
145 SetPref(user_prefs_.get(), path, value); | 143 SetPref(user_prefs_.get(), path, value); |
146 } | 144 } |
147 | 145 |
148 template<class SuperPrefService, class ConstructionPrefRegistry> | 146 template <class SuperPrefService, class ConstructionPrefRegistry> |
149 void TestingPrefServiceBase< | 147 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
150 SuperPrefService, ConstructionPrefRegistry>::RemoveUserPref( | 148 RemoveUserPref(const std::string& path) { |
151 const char* path) { | |
152 RemovePref(user_prefs_.get(), path); | 149 RemovePref(user_prefs_.get(), path); |
153 } | 150 } |
154 | 151 |
155 template<class SuperPrefService, class ConstructionPrefRegistry> | 152 template <class SuperPrefService, class ConstructionPrefRegistry> |
156 const base::Value* TestingPrefServiceBase< | 153 const base::Value* |
157 SuperPrefService, ConstructionPrefRegistry>::GetRecommendedPref( | 154 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
158 const char* path) const { | 155 GetRecommendedPref(const std::string& path) const { |
159 return GetPref(recommended_prefs_, path); | 156 return GetPref(recommended_prefs_, path); |
160 } | 157 } |
161 | 158 |
162 template<class SuperPrefService, class ConstructionPrefRegistry> | 159 template <class SuperPrefService, class ConstructionPrefRegistry> |
163 void TestingPrefServiceBase< | 160 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
164 SuperPrefService, ConstructionPrefRegistry>::SetRecommendedPref( | 161 SetRecommendedPref(const std::string& path, base::Value* value) { |
165 const char* path, base::Value* value) { | |
166 SetPref(recommended_prefs_.get(), path, value); | 162 SetPref(recommended_prefs_.get(), path, value); |
167 } | 163 } |
168 | 164 |
169 template<class SuperPrefService, class ConstructionPrefRegistry> | 165 template <class SuperPrefService, class ConstructionPrefRegistry> |
170 void TestingPrefServiceBase< | 166 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
171 SuperPrefService, ConstructionPrefRegistry>::RemoveRecommendedPref( | 167 RemoveRecommendedPref(const std::string& path) { |
172 const char* path) { | |
173 RemovePref(recommended_prefs_.get(), path); | 168 RemovePref(recommended_prefs_.get(), path); |
174 } | 169 } |
175 | 170 |
176 template<class SuperPrefService, class ConstructionPrefRegistry> | 171 template <class SuperPrefService, class ConstructionPrefRegistry> |
177 const base::Value* TestingPrefServiceBase< | 172 const base::Value* |
178 SuperPrefService, ConstructionPrefRegistry>::GetPref( | 173 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetPref( |
179 TestingPrefStore* pref_store, const char* path) const { | 174 TestingPrefStore* pref_store, |
| 175 const std::string& path) const { |
180 const base::Value* res; | 176 const base::Value* res; |
181 return pref_store->GetValue(path, &res) ? res : NULL; | 177 return pref_store->GetValue(path, &res) ? res : NULL; |
182 } | 178 } |
183 | 179 |
184 template<class SuperPrefService, class ConstructionPrefRegistry> | 180 template <class SuperPrefService, class ConstructionPrefRegistry> |
185 void TestingPrefServiceBase< | 181 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
186 SuperPrefService, ConstructionPrefRegistry>::SetPref( | 182 SetPref(TestingPrefStore* pref_store, |
187 TestingPrefStore* pref_store, const char* path, base::Value* value) { | 183 const std::string& path, |
| 184 base::Value* value) { |
188 pref_store->SetValue(path, value); | 185 pref_store->SetValue(path, value); |
189 } | 186 } |
190 | 187 |
191 template<class SuperPrefService, class ConstructionPrefRegistry> | 188 template <class SuperPrefService, class ConstructionPrefRegistry> |
192 void TestingPrefServiceBase< | 189 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
193 SuperPrefService, ConstructionPrefRegistry>::RemovePref( | 190 RemovePref(TestingPrefStore* pref_store, const std::string& path) { |
194 TestingPrefStore* pref_store, const char* path) { | |
195 pref_store->RemoveValue(path); | 191 pref_store->RemoveValue(path); |
196 } | 192 } |
197 | 193 |
198 #endif // BASE_PREFS_TESTING_PREF_SERVICE_H_ | 194 #endif // BASE_PREFS_TESTING_PREF_SERVICE_H_ |
OLD | NEW |