Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / components / prefs / pref_value_map.cc
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.
4
5 #include "components/prefs/pref_value_map.h"
6
7 #include <map>
8 #include <memory>
9 #include <utility>
10
11 #include "base/logging.h"
12 #include "base/values.h"
13
14 PrefValueMap::PrefValueMap() {}
15
16 PrefValueMap::~PrefValueMap() {}
17
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())
22     return false;
23
24   if (value)
25     *value = &it->second;
26
27   return true;
28 }
29
30 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) {
31   auto it = prefs_.find(key);
32   if (it == prefs_.end())
33     return false;
34
35   if (value)
36     *value = &it->second;
37
38   return true;
39 }
40
41 bool PrefValueMap::SetValue(const std::string& key,
42                             std::unique_ptr<base::Value> value) {
43   DCHECK(value);
44   return SetValue(key, base::Value::FromUniquePtrValue(std::move(value)));
45 }
46
47 bool PrefValueMap::SetValue(const std::string& key, base::Value value) {
48   base::Value& existing_value = prefs_[key];
49   if (value == existing_value)
50     return false;
51
52   existing_value = std::move(value);
53   return true;
54 }
55
56 bool PrefValueMap::RemoveValue(const std::string& key) {
57   return prefs_.erase(key) != 0;
58 }
59
60 void PrefValueMap::Clear() {
61   prefs_.clear();
62 }
63
64 void PrefValueMap::Swap(PrefValueMap* other) {
65   prefs_.swap(other->prefs_);
66 }
67
68 PrefValueMap::iterator PrefValueMap::begin() {
69   return prefs_.begin();
70 }
71
72 PrefValueMap::iterator PrefValueMap::end() {
73   return prefs_.end();
74 }
75
76 PrefValueMap::const_iterator PrefValueMap::begin() const {
77   return prefs_.begin();
78 }
79
80 PrefValueMap::const_iterator PrefValueMap::end() const {
81   return prefs_.end();
82 }
83
84 bool PrefValueMap::empty() const {
85   return prefs_.empty();
86 }
87
88 bool PrefValueMap::GetBoolean(const std::string& key,
89                               bool* value) const {
90   const base::Value* stored_value = nullptr;
91   return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value);
92 }
93
94 void PrefValueMap::SetBoolean(const std::string& key, bool value) {
95   SetValue(key, base::Value(value));
96 }
97
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);
102 }
103
104 void PrefValueMap::SetString(const std::string& key,
105                              const std::string& value) {
106   SetValue(key, base::Value(value));
107 }
108
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);
112 }
113
114 void PrefValueMap::SetInteger(const std::string& key, const int value) {
115   SetValue(key, base::Value(value));
116 }
117
118 void PrefValueMap::SetDouble(const std::string& key, const double value) {
119   SetValue(key, base::Value(value));
120 }
121
122 void PrefValueMap::GetDifferingKeys(
123     const PrefValueMap* other,
124     std::vector<std::string>* differing_keys) const {
125   differing_keys->clear();
126
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);
134
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);
140     if (diff == 0) {
141       if (!this_pref->second->Equals(other_pref->second))
142         differing_keys->push_back(this_pref->first);
143       ++this_pref;
144       ++other_pref;
145     } else if (diff < 0) {
146       differing_keys->push_back(this_pref->first);
147       ++this_pref;
148     } else if (diff > 0) {
149       differing_keys->push_back(other_pref->first);
150       ++other_pref;
151     }
152   }
153
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);
159 }
160
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());
165
166   return dictionary;
167 }