1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_PREFS_PREF_STORE_H_
6 #define COMPONENTS_PREFS_PREF_STORE_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/string_piece.h"
13 #include "base/values.h"
14 #include "components/prefs/prefs_export.h"
16 // This is an abstract interface for reading and writing from/to a persistent
17 // preference store, used by PrefService. An implementation using a JSON file
18 // can be found in JsonPrefStore, while an implementation without any backing
19 // store for testing can be found in TestingPrefStore. Furthermore, there is
20 // CommandLinePrefStore, which bridges command line options to preferences and
21 // ConfigurationPolicyPrefStore, which is used for hooking up configuration
22 // policy with the preference subsystem.
23 class COMPONENTS_PREFS_EXPORT PrefStore : public base::RefCounted<PrefStore> {
25 // Observer interface for monitoring PrefStore.
26 class COMPONENTS_PREFS_EXPORT Observer {
28 // Called when the value for the given |key| in the store changes.
29 virtual void OnPrefValueChanged(const std::string& key) = 0;
30 // Notification about the PrefStore being fully initialized.
31 virtual void OnInitializationCompleted(bool succeeded) = 0;
34 virtual ~Observer() {}
39 PrefStore(const PrefStore&) = delete;
40 PrefStore& operator=(const PrefStore&) = delete;
42 // Add and remove observers.
43 virtual void AddObserver(Observer* observer) {}
44 virtual void RemoveObserver(Observer* observer) {}
45 virtual bool HasObservers() const;
47 // Whether the store has completed all asynchronous initialization.
48 virtual bool IsInitializationComplete() const;
50 // Get the value for a given preference |key| and stores it in |*result|.
51 // |*result| is only modified if the return value is true and if |result|
52 // is not NULL. Ownership of the |*result| value remains with the PrefStore.
53 virtual bool GetValue(base::StringPiece key,
54 const base::Value** result) const = 0;
56 // Get all the values.
57 virtual base::Value::Dict GetValues() const = 0;
60 friend class base::RefCounted<PrefStore>;
61 virtual ~PrefStore() {}
64 #endif // COMPONENTS_PREFS_PREF_STORE_H_