Upload upstream chromium 76.0.3809.146
[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, base::Value value) {
42   base::Value& existing_value = prefs_[key];
43   if (value == existing_value)
44     return false;
45
46   existing_value = std::move(value);
47   return true;
48 }
49
50 bool PrefValueMap::RemoveValue(const std::string& key) {
51   return prefs_.erase(key) != 0;
52 }
53
54 void PrefValueMap::Clear() {
55   prefs_.clear();
56 }
57
58 void PrefValueMap::Swap(PrefValueMap* other) {
59   prefs_.swap(other->prefs_);
60 }
61
62 PrefValueMap::iterator PrefValueMap::begin() {
63   return prefs_.begin();
64 }
65
66 PrefValueMap::iterator PrefValueMap::end() {
67   return prefs_.end();
68 }
69
70 PrefValueMap::const_iterator PrefValueMap::begin() const {
71   return prefs_.begin();
72 }
73
74 PrefValueMap::const_iterator PrefValueMap::end() const {
75   return prefs_.end();
76 }
77
78 bool PrefValueMap::empty() const {
79   return prefs_.empty();
80 }
81
82 bool PrefValueMap::GetBoolean(const std::string& key,
83                               bool* value) const {
84   const base::Value* stored_value = nullptr;
85   return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value);
86 }
87
88 void PrefValueMap::SetBoolean(const std::string& key, bool value) {
89   SetValue(key, base::Value(value));
90 }
91
92 bool PrefValueMap::GetString(const std::string& key,
93                              std::string* value) const {
94   const base::Value* stored_value = nullptr;
95   return GetValue(key, &stored_value) && stored_value->GetAsString(value);
96 }
97
98 void PrefValueMap::SetString(const std::string& key,
99                              const std::string& value) {
100   SetValue(key, base::Value(value));
101 }
102
103 bool PrefValueMap::GetInteger(const std::string& key, int* value) const {
104   const base::Value* stored_value = nullptr;
105   return GetValue(key, &stored_value) && stored_value->GetAsInteger(value);
106 }
107
108 void PrefValueMap::SetInteger(const std::string& key, const int value) {
109   SetValue(key, base::Value(value));
110 }
111
112 void PrefValueMap::SetDouble(const std::string& key, const double value) {
113   SetValue(key, base::Value(value));
114 }
115
116 void PrefValueMap::GetDifferingKeys(
117     const PrefValueMap* other,
118     std::vector<std::string>* differing_keys) const {
119   differing_keys->clear();
120
121   // Put everything into ordered maps.
122   std::map<std::string, const base::Value*> this_prefs;
123   std::map<std::string, const base::Value*> other_prefs;
124   for (const auto& pair : prefs_)
125     this_prefs.emplace(pair.first, &pair.second);
126   for (const auto& pair : other->prefs_)
127     other_prefs.emplace(pair.first, &pair.second);
128
129   // Walk over the maps in lockstep, adding everything that is different.
130   auto this_pref = this_prefs.begin();
131   auto other_pref = other_prefs.begin();
132   while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) {
133     const int diff = this_pref->first.compare(other_pref->first);
134     if (diff == 0) {
135       if (!this_pref->second->Equals(other_pref->second))
136         differing_keys->push_back(this_pref->first);
137       ++this_pref;
138       ++other_pref;
139     } else if (diff < 0) {
140       differing_keys->push_back(this_pref->first);
141       ++this_pref;
142     } else if (diff > 0) {
143       differing_keys->push_back(other_pref->first);
144       ++other_pref;
145     }
146   }
147
148   // Add the remaining entries.
149   for ( ; this_pref != this_prefs.end(); ++this_pref)
150     differing_keys->push_back(this_pref->first);
151   for ( ; other_pref != other_prefs.end(); ++other_pref)
152     differing_keys->push_back(other_pref->first);
153 }
154
155 std::unique_ptr<base::DictionaryValue> PrefValueMap::AsDictionaryValue() const {
156   auto dictionary = std::make_unique<base::DictionaryValue>();
157   for (const auto& value : prefs_)
158     dictionary->Set(value.first, value.second.CreateDeepCopy());
159
160   return dictionary;
161 }