xref: /aosp_15_r20/external/cronet/components/prefs/writeable_pref_store.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 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_WRITEABLE_PREF_STORE_H_
6 #define COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <set>
12 #include <string>
13 #include <vector>
14 
15 #include "components/prefs/pref_store.h"
16 
17 namespace base {
18 class Value;
19 }
20 
21 // A pref store that can be written to as well as read from.
22 class COMPONENTS_PREFS_EXPORT WriteablePrefStore : public PrefStore {
23  public:
24   // PrefWriteFlags can be used to change the way a pref will be written to
25   // storage.
26   enum PrefWriteFlags : uint32_t {
27     // No flags are specified.
28     DEFAULT_PREF_WRITE_FLAGS = 0,
29 
30     // This marks the pref as "lossy". There is no strict time guarantee on when
31     // a lossy pref will be persisted to permanent storage when it is modified.
32     LOSSY_PREF_WRITE_FLAG = 1 << 1
33   };
34 
WriteablePrefStore()35   WriteablePrefStore() {}
36 
37   WriteablePrefStore(const WriteablePrefStore&) = delete;
38   WriteablePrefStore& operator=(const WriteablePrefStore&) = delete;
39 
40   // Sets a |value| for |key| in the store. |flags| is a bitmask of
41   // PrefWriteFlags.
42   virtual void SetValue(const std::string& key,
43                         base::Value value,
44                         uint32_t flags) = 0;
45 
46   // Removes the value for |key|. |flags| is a bitmask of
47   // PrefWriteFlags.
48   virtual void RemoveValue(const std::string& key, uint32_t flags) = 0;
49 
50   // Equivalent to PrefStore::GetValue but returns a mutable value.
51   virtual bool GetMutableValue(const std::string& key,
52                                base::Value** result) = 0;
53 
54   // Triggers a value changed notification. This function or
55   // ReportSubValuesChanged needs to be called if one retrieves a list or
56   // dictionary with GetMutableValue and change its value. SetValue takes care
57   // of notifications itself. Note that ReportValueChanged will trigger
58   // notifications even if nothing has changed.  |flags| is a bitmask of
59   // PrefWriteFlags.
60   virtual void ReportValueChanged(const std::string& key, uint32_t flags) = 0;
61 
62   // Triggers a value changed notification for |path_components| in the |key|
63   // pref. This function or ReportValueChanged needs to be called if one
64   // retrieves a list or dictionary with GetMutableValue and change its value.
65   // SetValue takes care of notifications itself. Note that
66   // ReportSubValuesChanged will trigger notifications even if nothing has
67   // changed. |flags| is a bitmask of PrefWriteFlags.
68   virtual void ReportSubValuesChanged(
69       const std::string& key,
70       std::set<std::vector<std::string>> path_components,
71       uint32_t flags);
72 
73   // Same as SetValue, but doesn't generate notifications. This is used by
74   // PrefService::GetMutableUserPref() in order to put empty entries
75   // into the user pref store. Using SetValue is not an option since existing
76   // tests rely on the number of notifications generated. |flags| is a bitmask
77   // of PrefWriteFlags.
78   virtual void SetValueSilently(const std::string& key,
79                                 base::Value value,
80                                 uint32_t flags) = 0;
81 
82   // Clears all the preferences which names start with |prefix| and doesn't
83   // generate update notifications.
84   virtual void RemoveValuesByPrefixSilently(const std::string& prefix) = 0;
85 
86  protected:
~WriteablePrefStore()87   ~WriteablePrefStore() override {}
88 };
89 
90 #endif  // COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_
91