1 // Copyright 2011 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_PREF_VALUE_MAP_H_ 6 #define COMPONENTS_PREFS_PREF_VALUE_MAP_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/strings/string_piece.h" 13 #include "base/values.h" 14 #include "components/prefs/prefs_export.h" 15 16 // A generic string to value map used by the PrefStore implementations. 17 class COMPONENTS_PREFS_EXPORT PrefValueMap { 18 public: 19 using Map = std::map<std::string, base::Value, std::less<void>>; 20 using iterator = Map::iterator; 21 using const_iterator = Map::const_iterator; 22 23 PrefValueMap(); 24 25 PrefValueMap(const PrefValueMap&) = delete; 26 PrefValueMap& operator=(const PrefValueMap&) = delete; 27 28 virtual ~PrefValueMap(); 29 30 // Gets the value for |key| and stores it in |value|. Ownership remains with 31 // the map. Returns true if a value is present. If not, |value| is not 32 // touched. 33 bool GetValue(base::StringPiece key, const base::Value** value) const; 34 bool GetValue(base::StringPiece key, base::Value** value); 35 36 // Sets a new |value| for |key|. Returns true if the value changed. 37 bool SetValue(const std::string& key, base::Value value); 38 39 // Removes the value for |key| from the map. Returns true if a value was 40 // removed. 41 bool RemoveValue(const std::string& key); 42 43 // Clears the map. 44 void Clear(); 45 46 // Clear the preferences which start with |prefix|. 47 void ClearWithPrefix(const std::string& prefix); 48 49 // Swaps the contents of two maps. 50 void Swap(PrefValueMap* other); 51 52 iterator begin(); 53 iterator end(); 54 const_iterator begin() const; 55 const_iterator end() const; 56 bool empty() const; 57 58 // Gets a boolean value for |key| and stores it in |value|. Returns true if 59 // the value was found and of the proper type. 60 bool GetBoolean(const std::string& key, bool* value) const; 61 62 // Sets the value for |key| to the boolean |value|. 63 void SetBoolean(const std::string& key, bool value); 64 65 // Gets a string value for |key| and stores it in |value|. Returns true if 66 // the value was found and of the proper type. 67 bool GetString(const std::string& key, std::string* value) const; 68 69 // Sets the value for |key| to the string |value|. 70 void SetString(const std::string& key, const std::string& value); 71 72 // Gets an int value for |key| and stores it in |value|. Returns true if 73 // the value was found and of the proper type. 74 bool GetInteger(const std::string& key, int* value) const; 75 76 // Sets the value for |key| to the int |value|. 77 void SetInteger(const std::string& key, const int value); 78 79 // Sets the value for |key| to the double |value|. 80 void SetDouble(const std::string& key, const double value); 81 82 // Compares this value map against |other| and stores all key names that have 83 // different values in |differing_keys|. This includes keys that are present 84 // only in one of the maps. 85 void GetDifferingKeys(const PrefValueMap* other, 86 std::vector<std::string>* differing_keys) const; 87 88 // Copies the map into a Value::Dict. 89 base::Value::Dict AsDict() const; 90 91 private: 92 Map prefs_; 93 }; 94 95 #endif // COMPONENTS_PREFS_PREF_VALUE_MAP_H_ 96