blob: 3cd2d8750b7c8a7771e171b2d872a087039d2e13 [file] [log] [blame]
[email protected]9a8c4022011-01-25 14:25:331// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]f2d1f612010-12-09 15:10:172// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettwf00b9b42016-02-01 22:11:385#include "components/prefs/pref_value_map.h"
[email protected]f2d1f612010-12-09 15:10:176
bauerb67b11bd2015-04-23 14:53:057#include <map>
dcheng5f043bc2016-04-22 19:09:068#include <memory>
danakj0c8d4aa2015-11-25 05:29:589#include <utility>
bauerb67b11bd2015-04-23 14:53:0510
[email protected]f2d1f612010-12-09 15:10:1711#include "base/logging.h"
[email protected]f2d1f612010-12-09 15:10:1712#include "base/values.h"
13
[email protected]e4be2dd2010-12-14 00:44:3914PrefValueMap::PrefValueMap() {}
15
estade0bd407f2015-06-26 18:16:1816PrefValueMap::~PrefValueMap() {}
[email protected]f2d1f612010-12-09 15:10:1717
[email protected]a43a667b2013-06-14 17:56:0818bool PrefValueMap::GetValue(const std::string& key,
19 const base::Value** value) const {
aviad545a82017-01-04 20:09:2920 auto it = prefs_.find(key);
21 if (it == prefs_.end())
22 return false;
23
24 const base::Value* got_value = it->second.get();
estade0bd407f2015-06-26 18:16:1825 if (value && got_value)
26 *value = got_value;
[email protected]68bf41a2011-03-25 16:38:3127
estade0bd407f2015-06-26 18:16:1828 return !!got_value;
[email protected]68bf41a2011-03-25 16:38:3129}
30
[email protected]a43a667b2013-06-14 17:56:0831bool PrefValueMap::GetValue(const std::string& key, base::Value** value) {
aviad545a82017-01-04 20:09:2932 auto it = prefs_.find(key);
33 if (it == prefs_.end())
34 return false;
35
36 base::Value* got_value = it->second.get();
estade0bd407f2015-06-26 18:16:1837 if (value && got_value)
38 *value = got_value;
[email protected]f2d1f612010-12-09 15:10:1739
estade0bd407f2015-06-26 18:16:1840 return !!got_value;
[email protected]f2d1f612010-12-09 15:10:1741}
42
estade0bd407f2015-06-26 18:16:1843bool PrefValueMap::SetValue(const std::string& key,
dcheng5f043bc2016-04-22 19:09:0644 std::unique_ptr<base::Value> value) {
[email protected]f2d1f612010-12-09 15:10:1745 DCHECK(value);
bauerb67b11bd2015-04-23 14:53:0546
aviad545a82017-01-04 20:09:2947 std::unique_ptr<base::Value>& existing_value = prefs_[key];
48 if (existing_value && value->Equals(existing_value.get()))
bauerb67b11bd2015-04-23 14:53:0549 return false;
50
aviad545a82017-01-04 20:09:2951 existing_value = std::move(value);
[email protected]f2d1f612010-12-09 15:10:1752 return true;
53}
54
55bool PrefValueMap::RemoveValue(const std::string& key) {
estade0bd407f2015-06-26 18:16:1856 return prefs_.erase(key) != 0;
[email protected]f2d1f612010-12-09 15:10:1757}
58
59void PrefValueMap::Clear() {
estade0bd407f2015-06-26 18:16:1860 prefs_.clear();
[email protected]f2d1f612010-12-09 15:10:1761}
[email protected]f00768e2010-12-23 12:39:0162
[email protected]617b83792011-12-21 17:53:1863void PrefValueMap::Swap(PrefValueMap* other) {
64 prefs_.swap(other->prefs_);
65}
66
[email protected]9a8c4022011-01-25 14:25:3367PrefValueMap::iterator PrefValueMap::begin() {
68 return prefs_.begin();
69}
70
71PrefValueMap::iterator PrefValueMap::end() {
72 return prefs_.end();
73}
74
75PrefValueMap::const_iterator PrefValueMap::begin() const {
76 return prefs_.begin();
77}
78
79PrefValueMap::const_iterator PrefValueMap::end() const {
80 return prefs_.end();
81}
82
thestige7615d6c2016-07-19 19:43:4683bool PrefValueMap::empty() const {
84 return prefs_.empty();
85}
86
[email protected]f00768e2010-12-23 12:39:0187bool PrefValueMap::GetBoolean(const std::string& key,
88 bool* value) const {
bauerb67b11bd2015-04-23 14:53:0589 const base::Value* stored_value = nullptr;
[email protected]f00768e2010-12-23 12:39:0190 return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value);
91}
92
[email protected]233bb3ec2011-12-02 16:15:4993void PrefValueMap::SetBoolean(const std::string& key, bool value) {
Jinho Bang84b58bd2018-01-01 21:44:4894 SetValue(key, std::make_unique<base::Value>(value));
[email protected]233bb3ec2011-12-02 16:15:4995}
96
[email protected]f00768e2010-12-23 12:39:0197bool PrefValueMap::GetString(const std::string& key,
98 std::string* value) const {
bauerb67b11bd2015-04-23 14:53:0599 const base::Value* stored_value = nullptr;
[email protected]f00768e2010-12-23 12:39:01100 return GetValue(key, &stored_value) && stored_value->GetAsString(value);
101}
102
103void PrefValueMap::SetString(const std::string& key,
104 const std::string& value) {
Jinho Bang84b58bd2018-01-01 21:44:48105 SetValue(key, std::make_unique<base::Value>(value));
[email protected]f00768e2010-12-23 12:39:01106}
107
[email protected]4e94ab32011-08-05 05:28:27108bool PrefValueMap::GetInteger(const std::string& key, int* value) const {
bauerb67b11bd2015-04-23 14:53:05109 const base::Value* stored_value = nullptr;
[email protected]4e94ab32011-08-05 05:28:27110 return GetValue(key, &stored_value) && stored_value->GetAsInteger(value);
111}
112
113void PrefValueMap::SetInteger(const std::string& key, const int value) {
Jinho Bang84b58bd2018-01-01 21:44:48114 SetValue(key, std::make_unique<base::Value>(value));
[email protected]4e94ab32011-08-05 05:28:27115}
116
[email protected]e7518a12014-07-19 04:40:29117void PrefValueMap::SetDouble(const std::string& key, const double value) {
Jinho Bang84b58bd2018-01-01 21:44:48118 SetValue(key, std::make_unique<base::Value>(value));
[email protected]e7518a12014-07-19 04:40:29119}
120
[email protected]f00768e2010-12-23 12:39:01121void PrefValueMap::GetDifferingKeys(
122 const PrefValueMap* other,
123 std::vector<std::string>* differing_keys) const {
124 differing_keys->clear();
125
bauerb67b11bd2015-04-23 14:53:05126 // Put everything into ordered maps.
aviad545a82017-01-04 20:09:29127 std::map<std::string, base::Value*> this_prefs;
128 std::map<std::string, base::Value*> other_prefs;
129 for (const auto& pair : prefs_)
130 this_prefs[pair.first] = pair.second.get();
131 for (const auto& pair : other->prefs_)
132 other_prefs[pair.first] = pair.second.get();
bauerb67b11bd2015-04-23 14:53:05133
[email protected]f00768e2010-12-23 12:39:01134 // Walk over the maps in lockstep, adding everything that is different.
aviad545a82017-01-04 20:09:29135 auto this_pref = this_prefs.begin();
136 auto other_pref = other_prefs.begin();
bauerb67b11bd2015-04-23 14:53:05137 while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) {
[email protected]f00768e2010-12-23 12:39:01138 const int diff = this_pref->first.compare(other_pref->first);
139 if (diff == 0) {
140 if (!this_pref->second->Equals(other_pref->second))
141 differing_keys->push_back(this_pref->first);
142 ++this_pref;
143 ++other_pref;
144 } else if (diff < 0) {
145 differing_keys->push_back(this_pref->first);
146 ++this_pref;
147 } else if (diff > 0) {
148 differing_keys->push_back(other_pref->first);
149 ++other_pref;
150 }
151 }
152
153 // Add the remaining entries.
bauerb67b11bd2015-04-23 14:53:05154 for ( ; this_pref != this_prefs.end(); ++this_pref)
[email protected]f00768e2010-12-23 12:39:01155 differing_keys->push_back(this_pref->first);
bauerb67b11bd2015-04-23 14:53:05156 for ( ; other_pref != other_prefs.end(); ++other_pref)
[email protected]f00768e2010-12-23 12:39:01157 differing_keys->push_back(other_pref->first);
158}
tibelle23659b42017-02-23 01:44:13159
160std::unique_ptr<base::DictionaryValue> PrefValueMap::AsDictionaryValue() const {
Jinho Bang84b58bd2018-01-01 21:44:48161 auto dictionary = std::make_unique<base::DictionaryValue>();
tibelle23659b42017-02-23 01:44:13162 for (const auto& value : prefs_) {
163 dictionary->Set(value.first, value.second->CreateDeepCopy());
164 }
165 return dictionary;
166}