OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/prefs/pref_value_map.h" | 5 #include "components/prefs/pref_value_map.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/stl_util.h" | |
14 #include "base/values.h" | 13 #include "base/values.h" |
15 | 14 |
16 PrefValueMap::PrefValueMap() {} | 15 PrefValueMap::PrefValueMap() {} |
17 | 16 |
18 PrefValueMap::~PrefValueMap() {} | 17 PrefValueMap::~PrefValueMap() {} |
19 | 18 |
20 bool PrefValueMap::GetValue(const std::string& key, | 19 bool PrefValueMap::GetValue(const std::string& key, |
21 const base::Value** value) const { | 20 const base::Value** value) const { |
22 const base::Value* got_value = prefs_.get(key); | 21 auto it = prefs_.find(key); |
| 22 if (it == prefs_.end()) |
| 23 return false; |
| 24 |
| 25 const base::Value* got_value = it->second.get(); |
23 if (value && got_value) | 26 if (value && got_value) |
24 *value = got_value; | 27 *value = got_value; |
25 | 28 |
26 return !!got_value; | 29 return !!got_value; |
27 } | 30 } |
28 | 31 |
29 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) { | 32 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) { |
30 base::Value* got_value = prefs_.get(key); | 33 auto it = prefs_.find(key); |
| 34 if (it == prefs_.end()) |
| 35 return false; |
| 36 |
| 37 base::Value* got_value = it->second.get(); |
31 if (value && got_value) | 38 if (value && got_value) |
32 *value = got_value; | 39 *value = got_value; |
33 | 40 |
34 return !!got_value; | 41 return !!got_value; |
35 } | 42 } |
36 | 43 |
37 bool PrefValueMap::SetValue(const std::string& key, | 44 bool PrefValueMap::SetValue(const std::string& key, |
38 std::unique_ptr<base::Value> value) { | 45 std::unique_ptr<base::Value> value) { |
39 DCHECK(value); | 46 DCHECK(value); |
40 | 47 |
41 base::Value* old_value = prefs_.get(key); | 48 std::unique_ptr<base::Value>& existing_value = prefs_[key]; |
42 if (old_value && value->Equals(old_value)) | 49 if (existing_value && value->Equals(existing_value.get())) |
43 return false; | 50 return false; |
44 | 51 |
45 prefs_.set(key, std::move(value)); | 52 existing_value = std::move(value); |
46 return true; | 53 return true; |
47 } | 54 } |
48 | 55 |
49 bool PrefValueMap::RemoveValue(const std::string& key) { | 56 bool PrefValueMap::RemoveValue(const std::string& key) { |
50 return prefs_.erase(key) != 0; | 57 return prefs_.erase(key) != 0; |
51 } | 58 } |
52 | 59 |
53 void PrefValueMap::Clear() { | 60 void PrefValueMap::Clear() { |
54 prefs_.clear(); | 61 prefs_.clear(); |
55 } | 62 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 void PrefValueMap::SetDouble(const std::string& key, const double value) { | 118 void PrefValueMap::SetDouble(const std::string& key, const double value) { |
112 SetValue(key, base::MakeUnique<base::FundamentalValue>(value)); | 119 SetValue(key, base::MakeUnique<base::FundamentalValue>(value)); |
113 } | 120 } |
114 | 121 |
115 void PrefValueMap::GetDifferingKeys( | 122 void PrefValueMap::GetDifferingKeys( |
116 const PrefValueMap* other, | 123 const PrefValueMap* other, |
117 std::vector<std::string>* differing_keys) const { | 124 std::vector<std::string>* differing_keys) const { |
118 differing_keys->clear(); | 125 differing_keys->clear(); |
119 | 126 |
120 // Put everything into ordered maps. | 127 // Put everything into ordered maps. |
121 std::map<std::string, base::Value*> this_prefs(prefs_.begin(), prefs_.end()); | 128 std::map<std::string, base::Value*> this_prefs; |
122 std::map<std::string, base::Value*> other_prefs(other->prefs_.begin(), | 129 std::map<std::string, base::Value*> other_prefs; |
123 other->prefs_.end()); | 130 for (const auto& pair : prefs_) |
| 131 this_prefs[pair.first] = pair.second.get(); |
| 132 for (const auto& pair : other->prefs_) |
| 133 other_prefs[pair.first] = pair.second.get(); |
124 | 134 |
125 // Walk over the maps in lockstep, adding everything that is different. | 135 // Walk over the maps in lockstep, adding everything that is different. |
126 auto this_pref(this_prefs.begin()); | 136 auto this_pref = this_prefs.begin(); |
127 auto other_pref(other_prefs.begin()); | 137 auto other_pref = other_prefs.begin(); |
128 while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) { | 138 while (this_pref != this_prefs.end() && other_pref != other_prefs.end()) { |
129 const int diff = this_pref->first.compare(other_pref->first); | 139 const int diff = this_pref->first.compare(other_pref->first); |
130 if (diff == 0) { | 140 if (diff == 0) { |
131 if (!this_pref->second->Equals(other_pref->second)) | 141 if (!this_pref->second->Equals(other_pref->second)) |
132 differing_keys->push_back(this_pref->first); | 142 differing_keys->push_back(this_pref->first); |
133 ++this_pref; | 143 ++this_pref; |
134 ++other_pref; | 144 ++other_pref; |
135 } else if (diff < 0) { | 145 } else if (diff < 0) { |
136 differing_keys->push_back(this_pref->first); | 146 differing_keys->push_back(this_pref->first); |
137 ++this_pref; | 147 ++this_pref; |
138 } else if (diff > 0) { | 148 } else if (diff > 0) { |
139 differing_keys->push_back(other_pref->first); | 149 differing_keys->push_back(other_pref->first); |
140 ++other_pref; | 150 ++other_pref; |
141 } | 151 } |
142 } | 152 } |
143 | 153 |
144 // Add the remaining entries. | 154 // Add the remaining entries. |
145 for ( ; this_pref != this_prefs.end(); ++this_pref) | 155 for ( ; this_pref != this_prefs.end(); ++this_pref) |
146 differing_keys->push_back(this_pref->first); | 156 differing_keys->push_back(this_pref->first); |
147 for ( ; other_pref != other_prefs.end(); ++other_pref) | 157 for ( ; other_pref != other_prefs.end(); ++other_pref) |
148 differing_keys->push_back(other_pref->first); | 158 differing_keys->push_back(other_pref->first); |
149 } | 159 } |
OLD | NEW |