blob: 9a69c7cbe47e3bc8aae9417039588b3e2b198171 [file] [log] [blame]
brettw58cd1f12016-01-30 05:56:051// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettw066508682016-02-03 08:22:025#ifndef COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_
6#define COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_
brettw58cd1f12016-01-30 05:56:057
8#include <stdint.h>
9
dcheng5f043bc2016-04-22 19:09:0610#include <memory>
Sam McNally69572322017-05-01 00:41:3811#include <set>
brettw58cd1f12016-01-30 05:56:0512#include <string>
Sam McNally69572322017-05-01 00:41:3813#include <vector>
brettw58cd1f12016-01-30 05:56:0514
15#include "base/macros.h"
brettwf00b9b42016-02-01 22:11:3816#include "components/prefs/pref_store.h"
brettw58cd1f12016-01-30 05:56:0517
18namespace base {
19class Value;
20}
21
22// A pref store that can be written to as well as read from.
brettw066508682016-02-03 08:22:0223class COMPONENTS_PREFS_EXPORT WriteablePrefStore : public PrefStore {
brettw58cd1f12016-01-30 05:56:0524 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() {}
37
38 // Sets a |value| for |key| in the store. |value| must be non-NULL. |flags| is
39 // a bitmask of PrefWriteFlags.
40 virtual void SetValue(const std::string& key,
dcheng5f043bc2016-04-22 19:09:0641 std::unique_ptr<base::Value> value,
brettw58cd1f12016-01-30 05:56:0542 uint32_t flags) = 0;
43
44 // Removes the value for |key|.
45 virtual void RemoveValue(const std::string& key, uint32_t flags) = 0;
46
47 // Equivalent to PrefStore::GetValue but returns a mutable value.
48 virtual bool GetMutableValue(const std::string& key,
49 base::Value** result) = 0;
50
Sam McNally69572322017-05-01 00:41:3851 // Triggers a value changed notification. This function or
52 // ReportSubValuesChanged needs to be called if one retrieves a list or
53 // dictionary with GetMutableValue and change its value. SetValue takes care
54 // of notifications itself. Note that ReportValueChanged will trigger
55 // notifications even if nothing has changed. |flags| is a bitmask of
56 // PrefWriteFlags.
brettw58cd1f12016-01-30 05:56:0557 virtual void ReportValueChanged(const std::string& key, uint32_t flags) = 0;
58
Sam McNally69572322017-05-01 00:41:3859 // Triggers a value changed notification for |path_components| in the |key|
60 // pref. This function or ReportValueChanged needs to be called if one
61 // retrieves a list or dictionary with GetMutableValue and change its value.
62 // SetValue takes care of notifications itself. Note that
63 // ReportSubValuesChanged will trigger notifications even if nothing has
64 // changed. |flags| is a bitmask of PrefWriteFlags.
65 virtual void ReportSubValuesChanged(
66 const std::string& key,
67 std::set<std::vector<std::string>> path_components,
68 uint32_t flags);
69
brettw58cd1f12016-01-30 05:56:0570 // Same as SetValue, but doesn't generate notifications. This is used by
71 // PrefService::GetMutableUserPref() in order to put empty entries
72 // into the user pref store. Using SetValue is not an option since existing
73 // tests rely on the number of notifications generated. |flags| is a bitmask
74 // of PrefWriteFlags.
75 virtual void SetValueSilently(const std::string& key,
dcheng5f043bc2016-04-22 19:09:0676 std::unique_ptr<base::Value> value,
brettw58cd1f12016-01-30 05:56:0577 uint32_t flags) = 0;
78
79 protected:
80 ~WriteablePrefStore() override {}
81
82 private:
83 DISALLOW_COPY_AND_ASSIGN(WriteablePrefStore);
84};
85
brettw066508682016-02-03 08:22:0286#endif // COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_