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 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 const base::Value* GetManagedPref(const std::string& path) const; | 33 const base::Value* GetManagedPref(const std::string& path) const; |
34 | 34 |
35 // Set a preference on the managed layer and fire observers if the preference | 35 // Set a preference on the managed layer and fire observers if the preference |
36 // changed. Assumes ownership of |value|. | 36 // changed. Assumes ownership of |value|. |
37 void SetManagedPref(const std::string& path, base::Value* value); | 37 void SetManagedPref(const std::string& path, base::Value* value); |
38 | 38 |
39 // Clear the preference on the managed layer and fire observers if the | 39 // Clear the preference on the managed layer and fire observers if the |
40 // preference has been defined previously. | 40 // preference has been defined previously. |
41 void RemoveManagedPref(const std::string& path); | 41 void RemoveManagedPref(const std::string& path); |
42 | 42 |
| 43 // Similar to the above, but for extension preferences. |
| 44 // 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 |
| 46 // extension. |
| 47 const base::Value* GetExtensionPref(const std::string& path) const; |
| 48 void SetExtensionPref(const std::string& path, base::Value* value); |
| 49 void RemoveExtensionPref(const std::string& path); |
| 50 |
43 // Similar to the above, but for user preferences. | 51 // Similar to the above, but for user preferences. |
44 const base::Value* GetUserPref(const std::string& path) const; | 52 const base::Value* GetUserPref(const std::string& path) const; |
45 void SetUserPref(const std::string& path, base::Value* value); | 53 void SetUserPref(const std::string& path, base::Value* value); |
46 void RemoveUserPref(const std::string& path); | 54 void RemoveUserPref(const std::string& path); |
47 | 55 |
48 // Similar to the above, but for recommended policy preferences. | 56 // Similar to the above, but for recommended policy preferences. |
49 const base::Value* GetRecommendedPref(const std::string& path) const; | 57 const base::Value* GetRecommendedPref(const std::string& path) const; |
50 void SetRecommendedPref(const std::string& path, base::Value* value); | 58 void SetRecommendedPref(const std::string& path, base::Value* value); |
51 void RemoveRecommendedPref(const std::string& path); | 59 void RemoveRecommendedPref(const std::string& path); |
52 | 60 |
53 // Do-nothing implementation for TestingPrefService. | 61 // Do-nothing implementation for TestingPrefService. |
54 static void HandleReadError(PersistentPrefStore::PrefReadError error) {} | 62 static void HandleReadError(PersistentPrefStore::PrefReadError error) {} |
55 | 63 |
56 protected: | 64 protected: |
57 TestingPrefServiceBase( | 65 TestingPrefServiceBase(TestingPrefStore* managed_prefs, |
58 TestingPrefStore* managed_prefs, | 66 TestingPrefStore* extension_prefs, |
59 TestingPrefStore* user_prefs, | 67 TestingPrefStore* user_prefs, |
60 TestingPrefStore* recommended_prefs, | 68 TestingPrefStore* recommended_prefs, |
61 ConstructionPrefRegistry* pref_registry, | 69 ConstructionPrefRegistry* pref_registry, |
62 PrefNotifierImpl* pref_notifier); | 70 PrefNotifierImpl* pref_notifier); |
63 | 71 |
64 private: | 72 private: |
65 // Reads the value of the preference indicated by |path| from |pref_store|. | 73 // Reads the value of the preference indicated by |path| from |pref_store|. |
66 // Returns NULL if the preference was not found. | 74 // Returns NULL if the preference was not found. |
67 const base::Value* GetPref(TestingPrefStore* pref_store, | 75 const base::Value* GetPref(TestingPrefStore* pref_store, |
68 const std::string& path) const; | 76 const std::string& path) const; |
69 | 77 |
70 // Sets the value for |path| in |pref_store|. | 78 // Sets the value for |path| in |pref_store|. |
71 void SetPref(TestingPrefStore* pref_store, | 79 void SetPref(TestingPrefStore* pref_store, |
72 const std::string& path, | 80 const std::string& path, |
73 base::Value* value); | 81 base::Value* value); |
74 | 82 |
75 // Removes the preference identified by |path| from |pref_store|. | 83 // Removes the preference identified by |path| from |pref_store|. |
76 void RemovePref(TestingPrefStore* pref_store, const std::string& path); | 84 void RemovePref(TestingPrefStore* pref_store, const std::string& path); |
77 | 85 |
78 // Pointers to the pref stores our value store uses. | 86 // Pointers to the pref stores our value store uses. |
79 scoped_refptr<TestingPrefStore> managed_prefs_; | 87 scoped_refptr<TestingPrefStore> managed_prefs_; |
| 88 scoped_refptr<TestingPrefStore> extension_prefs_; |
80 scoped_refptr<TestingPrefStore> user_prefs_; | 89 scoped_refptr<TestingPrefStore> user_prefs_; |
81 scoped_refptr<TestingPrefStore> recommended_prefs_; | 90 scoped_refptr<TestingPrefStore> recommended_prefs_; |
82 | 91 |
83 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase); | 92 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase); |
84 }; | 93 }; |
85 | 94 |
86 // Test version of PrefService. | 95 // Test version of PrefService. |
87 class TestingPrefServiceSimple | 96 class TestingPrefServiceSimple |
88 : public TestingPrefServiceBase<PrefService, PrefRegistry> { | 97 : public TestingPrefServiceBase<PrefService, PrefRegistry> { |
89 public: | 98 public: |
90 TestingPrefServiceSimple(); | 99 TestingPrefServiceSimple(); |
91 ~TestingPrefServiceSimple() override; | 100 ~TestingPrefServiceSimple() override; |
92 | 101 |
93 // This is provided as a convenience for registering preferences on | 102 // This is provided as a convenience for registering preferences on |
94 // an existing TestingPrefServiceSimple instance. On a production | 103 // an existing TestingPrefServiceSimple instance. On a production |
95 // PrefService you would do all registrations before constructing | 104 // PrefService you would do all registrations before constructing |
96 // it, passing it a PrefRegistry via its constructor (or via | 105 // it, passing it a PrefRegistry via its constructor (or via |
97 // e.g. PrefServiceFactory). | 106 // e.g. PrefServiceFactory). |
98 PrefRegistrySimple* registry(); | 107 PrefRegistrySimple* registry(); |
99 | 108 |
100 private: | 109 private: |
101 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple); | 110 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple); |
102 }; | 111 }; |
103 | 112 |
104 template<> | 113 template<> |
105 TestingPrefServiceBase<PrefService, PrefRegistry>::TestingPrefServiceBase( | 114 TestingPrefServiceBase<PrefService, PrefRegistry>::TestingPrefServiceBase( |
106 TestingPrefStore* managed_prefs, | 115 TestingPrefStore* managed_prefs, |
| 116 TestingPrefStore* extension_prefs, |
107 TestingPrefStore* user_prefs, | 117 TestingPrefStore* user_prefs, |
108 TestingPrefStore* recommended_prefs, | 118 TestingPrefStore* recommended_prefs, |
109 PrefRegistry* pref_registry, | 119 PrefRegistry* pref_registry, |
110 PrefNotifierImpl* pref_notifier); | 120 PrefNotifierImpl* pref_notifier); |
111 | 121 |
112 template<class SuperPrefService, class ConstructionPrefRegistry> | 122 template<class SuperPrefService, class ConstructionPrefRegistry> |
113 TestingPrefServiceBase< | 123 TestingPrefServiceBase< |
114 SuperPrefService, ConstructionPrefRegistry>::~TestingPrefServiceBase() { | 124 SuperPrefService, ConstructionPrefRegistry>::~TestingPrefServiceBase() { |
115 } | 125 } |
116 | 126 |
(...skipping 10 matching lines...) Expand all Loading... |
127 SetPref(managed_prefs_.get(), path, value); | 137 SetPref(managed_prefs_.get(), path, value); |
128 } | 138 } |
129 | 139 |
130 template <class SuperPrefService, class ConstructionPrefRegistry> | 140 template <class SuperPrefService, class ConstructionPrefRegistry> |
131 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 141 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
132 RemoveManagedPref(const std::string& path) { | 142 RemoveManagedPref(const std::string& path) { |
133 RemovePref(managed_prefs_.get(), path); | 143 RemovePref(managed_prefs_.get(), path); |
134 } | 144 } |
135 | 145 |
136 template <class SuperPrefService, class ConstructionPrefRegistry> | 146 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 147 const base::Value* TestingPrefServiceBase< |
| 148 SuperPrefService, |
| 149 ConstructionPrefRegistry>::GetExtensionPref(const std::string& path) const { |
| 150 return GetPref(extension_prefs_.get(), path); |
| 151 } |
| 152 |
| 153 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 154 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 155 SetExtensionPref(const std::string& path, base::Value* value) { |
| 156 SetPref(extension_prefs_.get(), path, value); |
| 157 } |
| 158 |
| 159 template <class SuperPrefService, class ConstructionPrefRegistry> |
| 160 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
| 161 RemoveExtensionPref(const std::string& path) { |
| 162 RemovePref(extension_prefs_.get(), path); |
| 163 } |
| 164 |
| 165 template <class SuperPrefService, class ConstructionPrefRegistry> |
137 const base::Value* | 166 const base::Value* |
138 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetUserPref( | 167 TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetUserPref( |
139 const std::string& path) const { | 168 const std::string& path) const { |
140 return GetPref(user_prefs_.get(), path); | 169 return GetPref(user_prefs_.get(), path); |
141 } | 170 } |
142 | 171 |
143 template <class SuperPrefService, class ConstructionPrefRegistry> | 172 template <class SuperPrefService, class ConstructionPrefRegistry> |
144 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 173 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
145 SetUserPref(const std::string& path, base::Value* value) { | 174 SetUserPref(const std::string& path, base::Value* value) { |
146 SetPref(user_prefs_.get(), path, value); | 175 SetPref(user_prefs_.get(), path, value); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 218 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
190 } | 219 } |
191 | 220 |
192 template <class SuperPrefService, class ConstructionPrefRegistry> | 221 template <class SuperPrefService, class ConstructionPrefRegistry> |
193 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: | 222 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>:: |
194 RemovePref(TestingPrefStore* pref_store, const std::string& path) { | 223 RemovePref(TestingPrefStore* pref_store, const std::string& path) { |
195 pref_store->RemoveValue(path, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 224 pref_store->RemoveValue(path, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
196 } | 225 } |
197 | 226 |
198 #endif // COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_ | 227 #endif // COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_ |
OLD | NEW |