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.
5 #ifndef BASE_PREFS_TESTING_PREF_SERVICE_H_
6 #define BASE_PREFS_TESTING_PREF_SERVICE_H_
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"
14 class PrefNotifierImpl
;
15 class PrefRegistrySimple
;
16 class TestingPrefStore
;
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.
22 // Use this via its specializations, e.g. TestingPrefServiceSimple.
23 template <class SuperPrefService
, class ConstructionPrefRegistry
>
24 class TestingPrefServiceBase
: public SuperPrefService
{
26 virtual ~TestingPrefServiceBase();
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 Value
* GetManagedPref(const char* path
) const;
32 // Set a preference on the managed layer and fire observers if the preference
33 // changed. Assumes ownership of |value|.
34 void SetManagedPref(const char* path
, Value
* value
);
36 // Clear the preference on the managed layer and fire observers if the
37 // preference has been defined previously.
38 void RemoveManagedPref(const char* path
);
40 // Similar to the above, but for user preferences.
41 const Value
* GetUserPref(const char* path
) const;
42 void SetUserPref(const char* path
, Value
* value
);
43 void RemoveUserPref(const char* path
);
45 // Similar to the above, but for recommended policy preferences.
46 const Value
* GetRecommendedPref(const char* path
) const;
47 void SetRecommendedPref(const char* path
, Value
* value
);
48 void RemoveRecommendedPref(const char* path
);
50 // Do-nothing implementation for TestingPrefService.
51 static void HandleReadError(PersistentPrefStore::PrefReadError error
) {}
54 TestingPrefServiceBase(
55 TestingPrefStore
* managed_prefs
,
56 TestingPrefStore
* user_prefs
,
57 TestingPrefStore
* recommended_prefs
,
58 ConstructionPrefRegistry
* pref_registry
,
59 PrefNotifierImpl
* pref_notifier
);
62 // Reads the value of the preference indicated by |path| from |pref_store|.
63 // Returns NULL if the preference was not found.
64 const Value
* GetPref(TestingPrefStore
* pref_store
, const char* path
) const;
66 // Sets the value for |path| in |pref_store|.
67 void SetPref(TestingPrefStore
* pref_store
, const char* path
, Value
* value
);
69 // Removes the preference identified by |path| from |pref_store|.
70 void RemovePref(TestingPrefStore
* pref_store
, const char* path
);
72 // Pointers to the pref stores our value store uses.
73 scoped_refptr
<TestingPrefStore
> managed_prefs_
;
74 scoped_refptr
<TestingPrefStore
> user_prefs_
;
75 scoped_refptr
<TestingPrefStore
> recommended_prefs_
;
77 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase
);
80 // Test version of PrefService.
81 class TestingPrefServiceSimple
82 : public TestingPrefServiceBase
<PrefService
, PrefRegistry
> {
84 TestingPrefServiceSimple();
85 virtual ~TestingPrefServiceSimple();
87 // This is provided as a convenience for registering preferences on
88 // an existing TestingPrefServiceSimple instance. On a production
89 // PrefService you would do all registrations before constructing
90 // it, passing it a PrefRegistry via its constructor (or via
91 // e.g. PrefServiceBuilder).
92 PrefRegistrySimple
* registry();
95 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple
);
99 TestingPrefServiceBase
<PrefService
, PrefRegistry
>::TestingPrefServiceBase(
100 TestingPrefStore
* managed_prefs
,
101 TestingPrefStore
* user_prefs
,
102 TestingPrefStore
* recommended_prefs
,
103 PrefRegistry
* pref_registry
,
104 PrefNotifierImpl
* pref_notifier
);
106 template<class SuperPrefService
, class ConstructionPrefRegistry
>
107 TestingPrefServiceBase
<
108 SuperPrefService
, ConstructionPrefRegistry
>::~TestingPrefServiceBase() {
111 template<class SuperPrefService
, class ConstructionPrefRegistry
>
112 const Value
* TestingPrefServiceBase
<
113 SuperPrefService
, ConstructionPrefRegistry
>::GetManagedPref(
114 const char* path
) const {
115 return GetPref(managed_prefs_
, path
);
118 template<class SuperPrefService
, class ConstructionPrefRegistry
>
119 void TestingPrefServiceBase
<
120 SuperPrefService
, ConstructionPrefRegistry
>::SetManagedPref(
121 const char* path
, Value
* value
) {
122 SetPref(managed_prefs_
, path
, value
);
125 template<class SuperPrefService
, class ConstructionPrefRegistry
>
126 void TestingPrefServiceBase
<
127 SuperPrefService
, ConstructionPrefRegistry
>::RemoveManagedPref(
129 RemovePref(managed_prefs_
, path
);
132 template<class SuperPrefService
, class ConstructionPrefRegistry
>
133 const Value
* TestingPrefServiceBase
<
134 SuperPrefService
, ConstructionPrefRegistry
>::GetUserPref(
135 const char* path
) const {
136 return GetPref(user_prefs_
, path
);
139 template<class SuperPrefService
, class ConstructionPrefRegistry
>
140 void TestingPrefServiceBase
<
141 SuperPrefService
, ConstructionPrefRegistry
>::SetUserPref(
142 const char* path
, Value
* value
) {
143 SetPref(user_prefs_
, path
, value
);
146 template<class SuperPrefService
, class ConstructionPrefRegistry
>
147 void TestingPrefServiceBase
<
148 SuperPrefService
, ConstructionPrefRegistry
>::RemoveUserPref(
150 RemovePref(user_prefs_
, path
);
153 template<class SuperPrefService
, class ConstructionPrefRegistry
>
154 const Value
* TestingPrefServiceBase
<
155 SuperPrefService
, ConstructionPrefRegistry
>::GetRecommendedPref(
156 const char* path
) const {
157 return GetPref(recommended_prefs_
, path
);
160 template<class SuperPrefService
, class ConstructionPrefRegistry
>
161 void TestingPrefServiceBase
<
162 SuperPrefService
, ConstructionPrefRegistry
>::SetRecommendedPref(
163 const char* path
, Value
* value
) {
164 SetPref(recommended_prefs_
, path
, value
);
167 template<class SuperPrefService
, class ConstructionPrefRegistry
>
168 void TestingPrefServiceBase
<
169 SuperPrefService
, ConstructionPrefRegistry
>::RemoveRecommendedPref(
171 RemovePref(recommended_prefs_
, path
);
174 template<class SuperPrefService
, class ConstructionPrefRegistry
>
175 const Value
* TestingPrefServiceBase
<
176 SuperPrefService
, ConstructionPrefRegistry
>::GetPref(
177 TestingPrefStore
* pref_store
, const char* path
) const {
179 return pref_store
->GetValue(path
, &res
) ? res
: NULL
;
182 template<class SuperPrefService
, class ConstructionPrefRegistry
>
183 void TestingPrefServiceBase
<
184 SuperPrefService
, ConstructionPrefRegistry
>::SetPref(
185 TestingPrefStore
* pref_store
, const char* path
, Value
* value
) {
186 pref_store
->SetValue(path
, value
);
189 template<class SuperPrefService
, class ConstructionPrefRegistry
>
190 void TestingPrefServiceBase
<
191 SuperPrefService
, ConstructionPrefRegistry
>::RemovePref(
192 TestingPrefStore
* pref_store
, const char* path
) {
193 pref_store
->RemoveValue(path
);
196 #endif // BASE_PREFS_TESTING_PREF_SERVICE_H_