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.
5 #ifndef COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_
6 #define COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_
15 #include "components/prefs/pref_store.h"
21 // A pref store that can be written to as well as read from.
22 class COMPONENTS_PREFS_EXPORT WriteablePrefStore : public PrefStore {
24 // PrefWriteFlags can be used to change the way a pref will be written to
26 enum PrefWriteFlags : uint32_t {
27 // No flags are specified.
28 DEFAULT_PREF_WRITE_FLAGS = 0,
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
35 WriteablePrefStore() {}
37 WriteablePrefStore(const WriteablePrefStore&) = delete;
38 WriteablePrefStore& operator=(const WriteablePrefStore&) = delete;
40 // Sets a |value| for |key| in the store. |flags| is a bitmask of
42 virtual void SetValue(const std::string& key,
46 // Removes the value for |key|.
47 virtual void RemoveValue(const std::string& key, uint32_t flags) = 0;
49 // Equivalent to PrefStore::GetValue but returns a mutable value.
50 virtual bool GetMutableValue(const std::string& key,
51 base::Value** result) = 0;
53 // Triggers a value changed notification. This function or
54 // ReportSubValuesChanged needs to be called if one retrieves a list or
55 // dictionary with GetMutableValue and change its value. SetValue takes care
56 // of notifications itself. Note that ReportValueChanged will trigger
57 // notifications even if nothing has changed. |flags| is a bitmask of
59 virtual void ReportValueChanged(const std::string& key, uint32_t flags) = 0;
61 // Triggers a value changed notification for |path_components| in the |key|
62 // pref. This function or ReportValueChanged needs to be called if one
63 // retrieves a list or dictionary with GetMutableValue and change its value.
64 // SetValue takes care of notifications itself. Note that
65 // ReportSubValuesChanged will trigger notifications even if nothing has
66 // changed. |flags| is a bitmask of PrefWriteFlags.
67 virtual void ReportSubValuesChanged(
68 const std::string& key,
69 std::set<std::vector<std::string>> path_components,
72 // Same as SetValue, but doesn't generate notifications. This is used by
73 // PrefService::GetMutableUserPref() in order to put empty entries
74 // into the user pref store. Using SetValue is not an option since existing
75 // tests rely on the number of notifications generated. |flags| is a bitmask
77 virtual void SetValueSilently(const std::string& key,
81 // Clears all the preferences which names start with |prefix| and doesn't
82 // generate update notifications.
83 virtual void RemoveValuesByPrefixSilently(const std::string& prefix) = 0;
86 ~WriteablePrefStore() override {}
89 #endif // COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_