Upload upstream chromium 67.0.3396
[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   const base::Value* got_value = it->second.get();
25   if (value && got_value)
26     *value = got_value;
27
28   return !!got_value;
29 }
30
31 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) {
32   auto it = prefs_.find(key);
33   if (it == prefs_.end())
34     return false;
35
36   base::Value* got_value = it->second.get();
37   if (value && got_value)
38     *value = got_value;
39
40   return !!got_value;
41 }
42
43 bool PrefValueMap::SetValue(const std::string& key,
44                             std::unique_ptr<base::Value> value) {
45   DCHECK(value);
46
47   std::unique_ptr<base::Value>& existing_value = prefs_[key];
48   if (existing_value && value->Equals(existing_value.get()))
49     return false;
50
51   existing_value = std::move(value);
52   return true;
53 }
54
55 bool PrefValueMap::RemoveValue(const std::string& key) {
56   return prefs_.erase(key) != 0;
57 }
58
59 void PrefValueMap::Clear() {
60   prefs_.clear();
61 }
62
63 void PrefValueMap::Swap(PrefValueMap* other) {
64   prefs_.swap(other->prefs_);
65 }
66
67 PrefValueMap::iterator PrefValueMap::begin() {
68   return prefs_.begin();
69 }
70
71 PrefValueMap::iterator PrefValueMap::end() {
72   return prefs_.end();
73 }
74
75 PrefValueMap::const_iterator PrefValueMap::begin() const {
76   return prefs_.begin();
77 }
78
79 PrefValueMap::const_iterator PrefValueMap::end() const {
80   return prefs_.end();
81 }
82
83 bool PrefValueMap::empty() const {
84   return prefs_.empty();
85 }
86
87 bool PrefValueMap::GetBoolean(const std::string& key,
88                               bool* value) const {
89   const base::Value* stored_value = nullptr;
90   return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value);
91 }
92
93 void PrefValueMap::SetBoolean(const std::string& key, bool value) {
94   SetValue(key, std::make_unique<base::Value>(value));
95 }
96
97 bool PrefValueMap::GetString(const std::string& key,
98                              std::string* value) const {
99   const base::Value* stored_value = nullptr;
100   return GetValue(key, &stored_value) && stored_value->GetAsString(value);
101 }
102
103 void PrefValueMap::SetString(const std::string& key,
104                              const std::string& value) {
105   SetValue(key, std::make_unique<base::Value>(value));
106 }
107
108 bool PrefValueMap::GetInteger(const std::string& key, int* value) const {
109   const base::Value* stored_value = nullptr;
110   return GetValue(key, &stored_value) && stored_value->GetAsInteger(value);
111 }
112
113 void PrefValueMap::SetInteger(const std::string& key, const int value) {
114   SetValue(key, std::make_unique<base::Value>(value));
115 }
116
117 void PrefValueMap::SetDouble(const std::string& key, const double value) {
118   SetValue(key, std::make_unique<base::Value>(value));
119 }
120
121 void PrefValueMap::GetDifferingKeys(
122     const PrefValueMap* other,
123     std::vector<std::string>* differing_keys) const {
124   differing_keys->clear();
125
126   // Put everything into ordered maps.
127   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();
133
134   // Walk over the maps in lockstep, adding everything that is different.
135   auto this_pref = this_prefs.begin();
136   auto other_pref = other_prefs.begin();
137   while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) {
138     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.
154   for ( ; this_pref != this_prefs.end(); ++this_pref)
155       differing_keys->push_back(this_pref->first);
156   for ( ; other_pref != other_prefs.end(); ++other_pref)
157       differing_keys->push_back(other_pref->first);
158 }
159
160 std::unique_ptr<base::DictionaryValue> PrefValueMap::AsDictionaryValue() const {
161   auto dictionary = std::make_unique<base::DictionaryValue>();
162   for (const auto& value : prefs_) {
163     dictionary->Set(value.first, value.second->CreateDeepCopy());
164   }
165   return dictionary;
166 }