1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_
#define COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_
#include <memory>
#include <utility>
#include "base/memory/ref_counted.h"
#include "components/prefs/pref_registry.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_store.h"
class PrefNotifierImpl;
class PrefRegistrySimple;
class TestingPrefStore;
// A PrefService subclass for testing. It operates totally in memory and
// provides additional API for manipulating preferences at the different levels
// (managed, extension, user) conveniently.
//
// Use this via its specializations, e.g. TestingPrefServiceSimple.
template <class SuperPrefService, class ConstructionPrefRegistry>
class TestingPrefServiceBase : public SuperPrefService {
public:
TestingPrefServiceBase(const TestingPrefServiceBase&) = delete;
TestingPrefServiceBase& operator=(const TestingPrefServiceBase&) = delete;
virtual ~TestingPrefServiceBase();
// Reads the value of a preference from the managed layer. Returns NULL if the
// preference is not defined at the managed layer.
const base::Value* GetManagedPref(const std::string& path) const;
// Sets a preference on the managed layer and fires observers if the
// preference changed.
void SetManagedPref(const std::string& path,
std::unique_ptr<base::Value> value);
void SetManagedPref(const std::string& path, base::Value value);
void SetManagedPref(const std::string& path, base::Value::Dict dict);
void SetManagedPref(const std::string& path, base::Value::List list);
// Clears the preference on the managed layer and fire observers if the
// preference has been defined previously.
void RemoveManagedPref(const std::string& path);
// Similar to the above, but for supervised user preferences.
const base::Value* GetSupervisedUserPref(const std::string& path) const;
void SetSupervisedUserPref(const std::string& path,
std::unique_ptr<base::Value> value);
void SetSupervisedUserPref(const std::string& path, base::Value value);
void SetSupervisedUserPref(const std::string& path, base::Value::Dict dict);
void SetSupervisedUserPref(const std::string& path, base::Value::List list);
void RemoveSupervisedUserPref(const std::string& path);
// Similar to the above, but for extension preferences.
// Does not really know about extensions and their order of installation.
// Useful in tests that only check that a preference is overridden by an
// extension.
const base::Value* GetExtensionPref(const std::string& path) const;
void SetExtensionPref(const std::string& path,
std::unique_ptr<base::Value> value);
void SetExtensionPref(const std::string& path, base::Value value);
void SetExtensionPref(const std::string& path, base::Value::Dict dict);
void SetExtensionPref(const std::string& path, base::Value::List list);
void RemoveExtensionPref(const std::string& path);
// Similar to the above, but for user preferences.
const base::Value* GetUserPref(const std::string& path) const;
void SetUserPref(const std::string& path, std::unique_ptr<base::Value> value);
void SetUserPref(const std::string& path, base::Value value);
void SetUserPref(const std::string& path, base::Value::Dict dict);
void SetUserPref(const std::string& path, base::Value::List list);
void RemoveUserPref(const std::string& path);
// Similar to the above, but for recommended policy preferences.
const base::Value* GetRecommendedPref(const std::string& path) const;
void SetRecommendedPref(const std::string& path,
std::unique_ptr<base::Value> value);
void SetRecommendedPref(const std::string& path, base::Value value);
void SetRecommendedPref(const std::string& path, base::Value::Dict dict);
void SetRecommendedPref(const std::string& path, base::Value::List list);
void RemoveRecommendedPref(const std::string& path);
// Do-nothing implementation for TestingPrefService.
static void HandleReadError(PersistentPrefStore::PrefReadError error) {}
// Set initialization status of pref stores.
void SetInitializationCompleted();
scoped_refptr<TestingPrefStore> user_prefs_store() { return user_prefs_; }
protected:
TestingPrefServiceBase(TestingPrefStore* managed_prefs,
TestingPrefStore* supervised_user_prefs,
TestingPrefStore* extension_prefs,
TestingPrefStore* standalone_browser_prefs,
TestingPrefStore* user_prefs,
TestingPrefStore* recommended_prefs,
ConstructionPrefRegistry* pref_registry,
PrefNotifierImpl* pref_notifier);
private:
// Reads the value of the preference indicated by |path| from |pref_store|.
// Returns NULL if the preference was not found.
const base::Value* GetPref(TestingPrefStore* pref_store,
const std::string& path) const;
// Sets the value for |path| in |pref_store|.
void SetPref(TestingPrefStore* pref_store,
const std::string& path,
std::unique_ptr<base::Value> value);
// Removes the preference identified by |path| from |pref_store|.
void RemovePref(TestingPrefStore* pref_store, const std::string& path);
// Pointers to the pref stores our value store uses.
scoped_refptr<TestingPrefStore> managed_prefs_;
scoped_refptr<TestingPrefStore> supervised_user_prefs_;
scoped_refptr<TestingPrefStore> extension_prefs_;
scoped_refptr<TestingPrefStore> standalone_browser_prefs_;
scoped_refptr<TestingPrefStore> user_prefs_;
scoped_refptr<TestingPrefStore> recommended_prefs_;
};
// Test version of PrefService.
class TestingPrefServiceSimple
: public TestingPrefServiceBase<PrefService, PrefRegistry> {
public:
TestingPrefServiceSimple();
TestingPrefServiceSimple(const TestingPrefServiceSimple&) = delete;
TestingPrefServiceSimple& operator=(const TestingPrefServiceSimple&) = delete;
~TestingPrefServiceSimple() override;
// This is provided as a convenience for registering preferences on
// an existing TestingPrefServiceSimple instance. On a production
// PrefService you would do all registrations before constructing
// it, passing it a PrefRegistry via its constructor (or via
// e.g. PrefServiceFactory).
PrefRegistrySimple* registry();
};
template <>
TestingPrefServiceBase<PrefService, PrefRegistry>::TestingPrefServiceBase(
TestingPrefStore* managed_prefs,
TestingPrefStore* supervised_user_prefs,
TestingPrefStore* extension_prefs,
TestingPrefStore* standalone_browser_prefs,
TestingPrefStore* user_prefs,
TestingPrefStore* recommended_prefs,
PrefRegistry* pref_registry,
PrefNotifierImpl* pref_notifier);
template<class SuperPrefService, class ConstructionPrefRegistry>
TestingPrefServiceBase<
SuperPrefService, ConstructionPrefRegistry>::~TestingPrefServiceBase() {
}
template <class SuperPrefService, class ConstructionPrefRegistry>
const base::Value* TestingPrefServiceBase<
SuperPrefService,
ConstructionPrefRegistry>::GetManagedPref(const std::string& path) const {
return GetPref(managed_prefs_.get(), path);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetManagedPref(const std::string& path,
std::unique_ptr<base::Value> value) {
SetPref(managed_prefs_.get(), path, std::move(value));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetManagedPref(const std::string& path, base::Value value) {
SetManagedPref(path, base::Value::ToUniquePtrValue(std::move(value)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetManagedPref(const std::string& path, base::Value::Dict dict) {
SetManagedPref(path, std::make_unique<base::Value>(std::move(dict)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetManagedPref(const std::string& path, base::Value::List list) {
SetManagedPref(path, std::make_unique<base::Value>(std::move(list)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
RemoveManagedPref(const std::string& path) {
RemovePref(managed_prefs_.get(), path);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
const base::Value*
TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
GetSupervisedUserPref(const std::string& path) const {
return GetPref(supervised_user_prefs_.get(), path);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetSupervisedUserPref(const std::string& path,
std::unique_ptr<base::Value> value) {
SetPref(supervised_user_prefs_.get(), path, std::move(value));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetSupervisedUserPref(const std::string& path, base::Value value) {
SetSupervisedUserPref(path, base::Value::ToUniquePtrValue(std::move(value)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetSupervisedUserPref(const std::string& path, base::Value::Dict dict) {
SetSupervisedUserPref(path, std::make_unique<base::Value>(std::move(dict)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetSupervisedUserPref(const std::string& path, base::Value::List list) {
SetSupervisedUserPref(path, std::make_unique<base::Value>(std::move(list)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
RemoveSupervisedUserPref(const std::string& path) {
RemovePref(supervised_user_prefs_.get(), path);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
const base::Value* TestingPrefServiceBase<
SuperPrefService,
ConstructionPrefRegistry>::GetExtensionPref(const std::string& path) const {
return GetPref(extension_prefs_.get(), path);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetExtensionPref(const std::string& path,
std::unique_ptr<base::Value> value) {
SetPref(extension_prefs_.get(), path, std::move(value));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetExtensionPref(const std::string& path, base::Value value) {
SetExtensionPref(path, base::Value::ToUniquePtrValue(std::move(value)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetExtensionPref(const std::string& path, base::Value::Dict dict) {
SetExtensionPref(path, std::make_unique<base::Value>(std::move(dict)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetExtensionPref(const std::string& path, base::Value::List list) {
SetExtensionPref(path, std::make_unique<base::Value>(std::move(list)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
RemoveExtensionPref(const std::string& path) {
RemovePref(extension_prefs_.get(), path);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
const base::Value*
TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetUserPref(
const std::string& path) const {
return GetPref(user_prefs_.get(), path);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetUserPref(const std::string& path, std::unique_ptr<base::Value> value) {
SetPref(user_prefs_.get(), path, std::move(value));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetUserPref(const std::string& path, base::Value value) {
SetUserPref(path, base::Value::ToUniquePtrValue(std::move(value)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetUserPref(const std::string& path, base::Value::Dict dict) {
SetUserPref(path, std::make_unique<base::Value>(std::move(dict)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetUserPref(const std::string& path, base::Value::List list) {
SetUserPref(path, std::make_unique<base::Value>(std::move(list)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
RemoveUserPref(const std::string& path) {
RemovePref(user_prefs_.get(), path);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
const base::Value*
TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
GetRecommendedPref(const std::string& path) const {
return GetPref(recommended_prefs_, path);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetRecommendedPref(const std::string& path,
std::unique_ptr<base::Value> value) {
SetPref(recommended_prefs_.get(), path, std::move(value));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetRecommendedPref(const std::string& path, base::Value value) {
SetPref(recommended_prefs_.get(), path,
base::Value::ToUniquePtrValue(std::move(value)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetRecommendedPref(const std::string& path, base::Value::Dict dict) {
SetRecommendedPref(path, std::make_unique<base::Value>(std::move(dict)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetRecommendedPref(const std::string& path, base::Value::List list) {
SetRecommendedPref(path, std::make_unique<base::Value>(std::move(list)));
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
RemoveRecommendedPref(const std::string& path) {
RemovePref(recommended_prefs_.get(), path);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
const base::Value*
TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::GetPref(
TestingPrefStore* pref_store,
const std::string& path) const {
const base::Value* res;
return pref_store->GetValue(path, &res) ? res : NULL;
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetPref(TestingPrefStore* pref_store,
const std::string& path,
std::unique_ptr<base::Value> value) {
pref_store->SetValue(path, base::Value::FromUniquePtrValue(std::move(value)),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
RemovePref(TestingPrefStore* pref_store, const std::string& path) {
pref_store->RemoveValue(path, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}
template <class SuperPrefService, class ConstructionPrefRegistry>
void TestingPrefServiceBase<SuperPrefService, ConstructionPrefRegistry>::
SetInitializationCompleted() {
managed_prefs_->SetInitializationCompleted();
supervised_user_prefs_->SetInitializationCompleted();
extension_prefs_->SetInitializationCompleted();
recommended_prefs_->SetInitializationCompleted();
// |user_prefs_| and |standalone_browser_prefs_| are initialized in
// PrefService constructor so no need to set initialization status again.
}
#endif // COMPONENTS_PREFS_TESTING_PREF_SERVICE_H_
|