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.
5 #ifndef COMPONENTS_PREFS_PREF_VALUE_MAP_H_
6 #define COMPONENTS_PREFS_PREF_VALUE_MAP_H_
12 #include "base/strings/string_piece.h"
13 #include "base/values.h"
14 #include "components/prefs/prefs_export.h"
16 // A generic string to value map used by the PrefStore implementations.
17 class COMPONENTS_PREFS_EXPORT PrefValueMap {
19 using Map = std::map<std::string, base::Value, std::less<void>>;
20 using iterator = Map::iterator;
21 using const_iterator = Map::const_iterator;
25 PrefValueMap(const PrefValueMap&) = delete;
26 PrefValueMap& operator=(const PrefValueMap&) = delete;
28 virtual ~PrefValueMap();
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
33 bool GetValue(base::StringPiece key, const base::Value** value) const;
34 bool GetValue(base::StringPiece key, base::Value** value);
36 // Sets a new |value| for |key|. Returns true if the value changed.
37 bool SetValue(const std::string& key, base::Value value);
39 // Removes the value for |key| from the map. Returns true if a value was
41 bool RemoveValue(const std::string& key);
46 // Clear the preferences which start with |prefix|.
47 void ClearWithPrefix(const std::string& prefix);
49 // Swaps the contents of two maps.
50 void Swap(PrefValueMap* other);
54 const_iterator begin() const;
55 const_iterator end() const;
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;
62 // Sets the value for |key| to the boolean |value|.
63 void SetBoolean(const std::string& key, bool value);
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;
69 // Sets the value for |key| to the string |value|.
70 void SetString(const std::string& key, const std::string& value);
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;
76 // Sets the value for |key| to the int |value|.
77 void SetInteger(const std::string& key, const int value);
79 // Sets the value for |key| to the double |value|.
80 void SetDouble(const std::string& key, const double value);
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;
88 // Copies the map into a Value::Dict.
89 base::Value::Dict AsDict() const;
95 #endif // COMPONENTS_PREFS_PREF_VALUE_MAP_H_