summaryrefslogtreecommitdiffstats
path: root/chrome/test/testing_pref_service.h
blob: 069ff321af23a1bc7c62a3ec75b00d48caae8f1e (plain)
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
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_TEST_TESTING_PREF_SERVICE_H_
#define CHROME_TEST_TESTING_PREF_SERVICE_H_
#pragma once

#include "chrome/browser/prefs/pref_service.h"

class CommandLine;
namespace policy {
class ConfigurationPolicyProvider;
}
class PrefStore;

// 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.
class TestingPrefService : public PrefService {
 public:
  // Subclass to allow directly setting PrefStores.
  class TestingPrefValueStore : public PrefValueStore {
   public:
    TestingPrefValueStore(PrefStore* managed_prefs,
                          PrefStore* device_management_prefs,
                          PrefStore* extension_prefs,
                          PrefStore* command_line_prefs,
                          PrefStore* user_prefs,
                          PrefStore* recommended_prefs,
                          PrefStore* default_prefs);
  };

  // Create an empty instance.
  TestingPrefService();

  // Create an instance that has a managed PrefStore and a command- line
  // PrefStore. |managed_platform_provider| contains the provider with which to
  // initialize the managed platform PrefStore. If it is NULL, then a
  // DummyPrefStore will be created. |device_management_provider| contains the
  // provider with which to initialize the device management
  // PrefStore. |command_line| contains the provider with which to initialize
  // the command line PrefStore. If it is NULL then a DummyPrefStore will be
  // created as the command line PrefStore.
  TestingPrefService(
      policy::ConfigurationPolicyProvider* managed_platform_provider,
      policy::ConfigurationPolicyProvider* device_management_provider,
      CommandLine* command_line);

  // Read the value of a preference from the managed layer. Returns NULL if the
  // preference is not defined at the managed layer.
  const Value* GetManagedPref(const char* path);

  // Set a preference on the managed layer and fire observers if the preference
  // changed. Assumes ownership of |value|.
  void SetManagedPref(const char* path, Value* value);

  // Clear the preference on the managed layer and fire observers if the
  // preference has been defined previously.
  void RemoveManagedPref(const char* path);

  // Set a preference on the managed layer.  Assumes ownership of |value|.
  // We don't fire observers for each change because in the real code, the
  // notification is sent after all the prefs have been loaded.  See
  // ConfigurationPolicyPrefStore::ReadPrefs().
  void SetManagedPrefWithoutNotification(const char* path, Value* value);

  // Clear the preference on the managed layer.
  // We don't fire observers for each change because in the real code, the
  // notification is sent after all the prefs have been loaded.  See
  // ConfigurationPolicyPrefStore::ReadPrefs().
  void RemoveManagedPrefWithoutNotification(const char* path);

  // Similar to the above, but for user preferences.
  const Value* GetUserPref(const char* path);
  void SetUserPref(const char* path, Value* value);
  void RemoveUserPref(const char* path);

 private:
  // Creates a ConfigurationPolicyPrefStore based on the provided
  // |provider| or a DummyPrefStore if |provider| is NULL.
  PrefStore* CreatePolicyPrefStoreFromProvider(
      policy::ConfigurationPolicyProvider* provider);

  // Creates a CommandLinePrefStore based on the supplied
  // |command_line| or a DummyPrefStore if |command_line| is NULL.
  PrefStore* CreateCommandLinePrefStore(CommandLine* command_line);

  // Reads the value of the preference indicated by |path| from |pref_store|.
  // Returns NULL if the preference was not found.
  const Value* GetPref(PrefStore* pref_store, const char* path);

  // Sets the value for |path| in |pref_store|.
  void SetPref(PrefStore* pref_store, const char* path, Value* value);

  // Removes the preference identified by |path| from |pref_store|.
  void RemovePref(PrefStore* pref_store, const char* path);

  // Pointers to the pref stores our value store uses.
  PrefStore* managed_platform_prefs_;  // weak
  PrefStore* device_management_prefs_;  // weak
  PrefStore* user_prefs_;  // weak
  PrefStore* default_prefs_;  // weak

  DISALLOW_COPY_AND_ASSIGN(TestingPrefService);
};

#endif  // CHROME_TEST_TESTING_PREF_SERVICE_H_