• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef COMPONENTS_PREFS_PREF_STORE_H_
6 #define COMPONENTS_PREFS_PREF_STORE_H_
7 
8 #include <memory>
9 #include <string_view>
10 
11 #include "base/memory/ref_counted.h"
12 #include "base/observer_list_types.h"
13 #include "base/values.h"
14 #include "components/prefs/prefs_export.h"
15 
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> {
24  public:
25   // Observer interface for monitoring PrefStore.
26   class COMPONENTS_PREFS_EXPORT Observer : public base::CheckedObserver {
27    public:
28     // Called when the value for the given `key` in the store changes.
OnPrefValueChanged(std::string_view key)29     virtual void OnPrefValueChanged(std::string_view key) {}
30     // Notification about the PrefStore being fully initialized.
OnInitializationCompleted(bool succeeded)31     virtual void OnInitializationCompleted(bool succeeded) {}
32   };
33 
34   PrefStore() = default;
35 
36   PrefStore(const PrefStore&) = delete;
37   PrefStore& operator=(const PrefStore&) = delete;
38 
39   // Add and remove observers.
AddObserver(Observer * observer)40   virtual void AddObserver(Observer* observer) {}
RemoveObserver(Observer * observer)41   virtual void RemoveObserver(Observer* observer) {}
42   virtual bool HasObservers() const;
43 
44   // Whether the store has completed all asynchronous initialization.
45   virtual bool IsInitializationComplete() const;
46 
47   // Get the value for a given preference `key` and stores it in `*result`.
48   // `*result` is only modified if the return value is true and if `result`
49   // is not NULL. Ownership of the `*result` value remains with the PrefStore.
50   virtual bool GetValue(std::string_view key,
51                         const base::Value** result) const = 0;
52 
53   // Get all the values.
54   virtual base::Value::Dict GetValues() const = 0;
55 
56  protected:
57   friend class base::RefCounted<PrefStore>;
58   virtual ~PrefStore() = default;
59 };
60 
61 #endif  // COMPONENTS_PREFS_PREF_STORE_H_
62