xref: /aosp_15_r20/external/cronet/components/prefs/pref_store.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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>
10 
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"
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 {
27    public:
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;
32 
33    protected:
~Observer()34     virtual ~Observer() {}
35   };
36 
PrefStore()37   PrefStore() {}
38 
39   PrefStore(const PrefStore&) = delete;
40   PrefStore& operator=(const PrefStore&) = delete;
41 
42   // Add and remove observers.
AddObserver(Observer * observer)43   virtual void AddObserver(Observer* observer) {}
RemoveObserver(Observer * observer)44   virtual void RemoveObserver(Observer* observer) {}
45   virtual bool HasObservers() const;
46 
47   // Whether the store has completed all asynchronous initialization.
48   virtual bool IsInitializationComplete() const;
49 
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;
55 
56   // Get all the values.
57   virtual base::Value::Dict GetValues() const = 0;
58 
59  protected:
60   friend class base::RefCounted<PrefStore>;
~PrefStore()61   virtual ~PrefStore() {}
62 };
63 
64 #endif  // COMPONENTS_PREFS_PREF_STORE_H_
65