1 // Copyright (c) 2011 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.
5 #include "components/prefs/pref_value_map.h"
12 #include "base/values.h"
14 PrefValueMap::PrefValueMap() {}
16 PrefValueMap::~PrefValueMap() {}
18 bool PrefValueMap::GetValue(const std::string& key,
19 const base::Value** value) const {
20 auto it = prefs_.find(key);
21 if (it == prefs_.end())
30 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) {
31 auto it = prefs_.find(key);
32 if (it == prefs_.end())
41 bool PrefValueMap::SetValue(const std::string& key, base::Value value) {
42 base::Value& existing_value = prefs_[key];
43 if (value == existing_value)
46 existing_value = std::move(value);
50 bool PrefValueMap::RemoveValue(const std::string& key) {
51 return prefs_.erase(key) != 0;
54 void PrefValueMap::Clear() {
58 void PrefValueMap::ClearWithPrefix(const std::string& prefix) {
59 Map::iterator low = prefs_.lower_bound(prefix);
60 // Appending maximum possible character so that there will be no string with
61 // prefix |prefix| that we may miss.
62 Map::iterator high = prefs_.upper_bound(prefix + char(CHAR_MAX));
63 prefs_.erase(low, high);
66 void PrefValueMap::Swap(PrefValueMap* other) {
67 prefs_.swap(other->prefs_);
70 PrefValueMap::iterator PrefValueMap::begin() {
71 return prefs_.begin();
74 PrefValueMap::iterator PrefValueMap::end() {
78 PrefValueMap::const_iterator PrefValueMap::begin() const {
79 return prefs_.begin();
82 PrefValueMap::const_iterator PrefValueMap::end() const {
86 bool PrefValueMap::empty() const {
87 return prefs_.empty();
90 bool PrefValueMap::GetBoolean(const std::string& key, bool* value) const {
91 const base::Value* stored_value = nullptr;
92 if (GetValue(key, &stored_value) && stored_value->is_bool()) {
93 *value = stored_value->GetBool();
99 void PrefValueMap::SetBoolean(const std::string& key, bool value) {
100 SetValue(key, base::Value(value));
103 bool PrefValueMap::GetString(const std::string& key, std::string* value) const {
104 const base::Value* stored_value = nullptr;
105 if (GetValue(key, &stored_value) && stored_value->is_string()) {
106 *value = stored_value->GetString();
112 void PrefValueMap::SetString(const std::string& key, const std::string& value) {
113 SetValue(key, base::Value(value));
116 bool PrefValueMap::GetInteger(const std::string& key, int* value) const {
117 const base::Value* stored_value = nullptr;
118 if (GetValue(key, &stored_value) && stored_value->is_int()) {
119 *value = stored_value->GetInt();
125 void PrefValueMap::SetInteger(const std::string& key, const int value) {
126 SetValue(key, base::Value(value));
129 void PrefValueMap::SetDouble(const std::string& key, const double value) {
130 SetValue(key, base::Value(value));
133 void PrefValueMap::GetDifferingKeys(
134 const PrefValueMap* other,
135 std::vector<std::string>* differing_keys) const {
136 differing_keys->clear();
138 // Put everything into ordered maps.
139 std::map<std::string, const base::Value*> this_prefs;
140 std::map<std::string, const base::Value*> other_prefs;
141 for (const auto& pair : prefs_)
142 this_prefs.emplace(pair.first, &pair.second);
143 for (const auto& pair : other->prefs_)
144 other_prefs.emplace(pair.first, &pair.second);
146 // Walk over the maps in lockstep, adding everything that is different.
147 auto this_pref = this_prefs.begin();
148 auto other_pref = other_prefs.begin();
149 while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) {
150 const int diff = this_pref->first.compare(other_pref->first);
152 if (*this_pref->second != *other_pref->second)
153 differing_keys->push_back(this_pref->first);
156 } else if (diff < 0) {
157 differing_keys->push_back(this_pref->first);
159 } else if (diff > 0) {
160 differing_keys->push_back(other_pref->first);
165 // Add the remaining entries.
166 for (; this_pref != this_prefs.end(); ++this_pref)
167 differing_keys->push_back(this_pref->first);
168 for (; other_pref != other_prefs.end(); ++other_pref)
169 differing_keys->push_back(other_pref->first);
172 std::unique_ptr<base::DictionaryValue> PrefValueMap::AsDictionaryValue() const {
173 auto dictionary = std::make_unique<base::DictionaryValue>();
174 for (const auto& value : prefs_)
175 dictionary->SetPath(value.first, value.second.Clone());