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|. 47 virtual void RemoveValue(const std::string& key, uint32_t flags) = 0; 48 49 // Equivalent to PrefStore::GetValue but returns a mutable value. 50 virtual bool GetMutableValue(const std::string& key, 51 base::Value** result) = 0; 52 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 58 // PrefWriteFlags. 59 virtual void ReportValueChanged(const std::string& key, uint32_t flags) = 0; 60 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, 70 uint32_t flags); 71 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 76 // of PrefWriteFlags. 77 virtual void SetValueSilently(const std::string& key, 78 base::Value value, 79 uint32_t flags) = 0; 80 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; 84 85 protected: ~WriteablePrefStore()86 ~WriteablePrefStore() override {} 87 }; 88 89 #endif // COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_ 90