Add estark as an OWNER for c/b/ssl/ and c/b/interstitials/
[chromium-blink-merge.git] / base / prefs / testing_pref_service.h
blobcbc978dd4efbc62d96740fbcf0541fa6b69c0724
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 {
25 public:
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 base::Value* GetManagedPref(const std::string& 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 std::string& path, base::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 std::string& path);
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);
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);
50 // Do-nothing implementation for TestingPrefService.
51 static void HandleReadError(PersistentPrefStore::PrefReadError error) {}
53 protected:
54 TestingPrefServiceBase(
55 TestingPrefStore* managed_prefs,
56 TestingPrefStore* user_prefs,
57 TestingPrefStore* recommended_prefs,
58 ConstructionPrefRegistry* pref_registry,
59 PrefNotifierImpl* pref_notifier);
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;
67 // Sets the value for |path| in |pref_store|.
68 void SetPref(TestingPrefStore* pref_store,
69 const std::string& path,
70 base::Value* value);
72 // Removes the preference identified by |path| from |pref_store|.
73 void RemovePref(TestingPrefStore* pref_store, const std::string& path);
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_;
80 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase);
83 // Test version of PrefService.
84 class TestingPrefServiceSimple
85 : public TestingPrefServiceBase<PrefService, PrefRegistry> {
86 public:
87 TestingPrefServiceSimple();
88 ~TestingPrefServiceSimple() override;
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();
97 private:
98 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple);
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);
109 template<class SuperPrefService, class ConstructionPrefRegistry>
110 TestingPrefServiceBase<
111 SuperPrefService, ConstructionPrefRegistry>::~TestingPrefServiceBase() {
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);
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);
127 template <class SuperPrefService, class ConstructionPrefRegistry>
128 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
129 RemoveManagedPref(const std::string& path) {
130 RemovePref(managed_prefs_.get(), path);
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);
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);
146 template <class SuperPrefService, class ConstructionPrefRegistry>
147 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
148 RemoveUserPref(const std::string& path) {
149 RemovePref(user_prefs_.get(), path);
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);
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);
165 template <class SuperPrefService, class ConstructionPrefRegistry>
166 void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
167 RemoveRecommendedPref(const std::string& path) {
168 RemovePref(recommended_prefs_.get(), path);
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;
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);
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);
195 #endif // BASE_PREFS_TESTING_PREF_SERVICE_H_