• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string_view>
14 #include <vector>
15 
16 #include "components/prefs/pref_store.h"
17 
18 namespace base {
19 class Value;
20 }
21 
22 // A pref store that can be written to as well as read from.
23 class COMPONENTS_PREFS_EXPORT WriteablePrefStore : public PrefStore {
24  public:
25   // PrefWriteFlags can be used to change the way a pref will be written to
26   // storage.
27   enum PrefWriteFlags : uint32_t {
28     // No flags are specified.
29     DEFAULT_PREF_WRITE_FLAGS = 0,
30 
31     // This marks the pref as "lossy". There is no strict time guarantee on when
32     // a lossy pref will be persisted to permanent storage when it is modified.
33     LOSSY_PREF_WRITE_FLAG = 1 << 1
34   };
35 
36   WriteablePrefStore() = default;
37 
38   WriteablePrefStore(const WriteablePrefStore&) = delete;
39   WriteablePrefStore& operator=(const WriteablePrefStore&) = delete;
40 
41   // Sets a `value` for `key` in the store. `flags` is a bitmask of
42   // PrefWriteFlags.
43   virtual void SetValue(std::string_view key,
44                         base::Value value,
45                         uint32_t flags) = 0;
46 
47   // Removes the value for `key`. `flags` is a bitmask of
48   // PrefWriteFlags.
49   virtual void RemoveValue(std::string_view key, uint32_t flags) = 0;
50 
51   // Equivalent to PrefStore::GetValue but returns a mutable value.
52   virtual bool GetMutableValue(std::string_view key, 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(std::string_view 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       std::string_view 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(std::string_view 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(std::string_view prefix) = 0;
85 
86  protected:
87   ~WriteablePrefStore() override = default;
88 };
89 
90 #endif  // COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_
91