brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 1 | // 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 | |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 5 | #ifndef COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_ |
| 6 | #define COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_ |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 7 | |
| 8 | #include <stdint.h> |
| 9 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 10 | #include <memory> |
Sam McNally | 6957232 | 2017-05-01 00:41:38 | [diff] [blame] | 11 | #include <set> |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 12 | #include <string> |
Sam McNally | 6957232 | 2017-05-01 00:41:38 | [diff] [blame] | 13 | #include <vector> |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 14 | |
| 15 | #include "base/macros.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 16 | #include "components/prefs/pref_store.h" |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 17 | |
| 18 | namespace base { |
| 19 | class Value; |
| 20 | } |
| 21 | |
| 22 | // A pref store that can be written to as well as read from. |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 23 | class COMPONENTS_PREFS_EXPORT WriteablePrefStore : public PrefStore { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 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() {} |
| 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, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 41 | std::unique_ptr<base::Value> value, |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 42 | 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 McNally | 6957232 | 2017-05-01 00:41:38 | [diff] [blame] | 51 | // 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. |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 57 | virtual void ReportValueChanged(const std::string& key, uint32_t flags) = 0; |
| 58 | |
Sam McNally | 6957232 | 2017-05-01 00:41:38 | [diff] [blame] | 59 | // 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 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 70 | // 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, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 76 | std::unique_ptr<base::Value> value, |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 77 | uint32_t flags) = 0; |
| 78 | |
| 79 | protected: |
| 80 | ~WriteablePrefStore() override {} |
| 81 | |
| 82 | private: |
| 83 | DISALLOW_COPY_AND_ASSIGN(WriteablePrefStore); |
| 84 | }; |
| 85 | |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 86 | #endif // COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_ |