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"
11 #include "base/logging.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,
42 std::unique_ptr<base::Value> value) {
44 return SetValue(key, base::Value::FromUniquePtrValue(std::move(value)));
47 bool PrefValueMap::SetValue(const std::string& key, base::Value value) {
48 base::Value& existing_value = prefs_[key];
49 if (value == existing_value)
52 existing_value = std::move(value);
56 bool PrefValueMap::RemoveValue(const std::string& key) {
57 return prefs_.erase(key) != 0;
60 void PrefValueMap::Clear() {
64 void PrefValueMap::Swap(PrefValueMap* other) {
65 prefs_.swap(other->prefs_);
68 PrefValueMap::iterator PrefValueMap::begin() {
69 return prefs_.begin();
72 PrefValueMap::iterator PrefValueMap::end() {
76 PrefValueMap::const_iterator PrefValueMap::begin() const {
77 return prefs_.begin();
80 PrefValueMap::const_iterator PrefValueMap::end() const {
84 bool PrefValueMap::empty() const {
85 return prefs_.empty();
88 bool PrefValueMap::GetBoolean(const std::string& key,
90 const base::Value* stored_value = nullptr;
91 return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value);
94 void PrefValueMap::SetBoolean(const std::string& key, bool value) {
95 SetValue(key, base::Value(value));
98 bool PrefValueMap::GetString(const std::string& key,
99 std::string* value) const {
100 const base::Value* stored_value = nullptr;
101 return GetValue(key, &stored_value) && stored_value->GetAsString(value);
104 void PrefValueMap::SetString(const std::string& key,
105 const std::string& value) {
106 SetValue(key, base::Value(value));
109 bool PrefValueMap::GetInteger(const std::string& key, int* value) const {
110 const base::Value* stored_value = nullptr;
111 return GetValue(key, &stored_value) && stored_value->GetAsInteger(value);
114 void PrefValueMap::SetInteger(const std::string& key, const int value) {
115 SetValue(key, base::Value(value));
118 void PrefValueMap::SetDouble(const std::string& key, const double value) {
119 SetValue(key, base::Value(value));
122 void PrefValueMap::GetDifferingKeys(
123 const PrefValueMap* other,
124 std::vector<std::string>* differing_keys) const {
125 differing_keys->clear();
127 // Put everything into ordered maps.
128 std::map<std::string, const base::Value*> this_prefs;
129 std::map<std::string, const base::Value*> other_prefs;
130 for (const auto& pair : prefs_)
131 this_prefs.emplace(pair.first, &pair.second);
132 for (const auto& pair : other->prefs_)
133 other_prefs.emplace(pair.first, &pair.second);
135 // Walk over the maps in lockstep, adding everything that is different.
136 auto this_pref = this_prefs.begin();
137 auto other_pref = other_prefs.begin();
138 while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) {
139 const int diff = this_pref->first.compare(other_pref->first);
141 if (!this_pref->second->Equals(other_pref->second))
142 differing_keys->push_back(this_pref->first);
145 } else if (diff < 0) {
146 differing_keys->push_back(this_pref->first);
148 } else if (diff > 0) {
149 differing_keys->push_back(other_pref->first);
154 // Add the remaining entries.
155 for ( ; this_pref != this_prefs.end(); ++this_pref)
156 differing_keys->push_back(this_pref->first);
157 for ( ; other_pref != other_prefs.end(); ++other_pref)
158 differing_keys->push_back(other_pref->first);
161 std::unique_ptr<base::DictionaryValue> PrefValueMap::AsDictionaryValue() const {
162 auto dictionary = std::make_unique<base::DictionaryValue>();
163 for (const auto& value : prefs_)
164 dictionary->Set(value.first, value.second.CreateDeepCopy());