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 <utility> | 9 #include <utility> |
9 | 10 |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/ptr_util.h" |
12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 | 15 |
15 PrefValueMap::PrefValueMap() {} | 16 PrefValueMap::PrefValueMap() {} |
16 | 17 |
17 PrefValueMap::~PrefValueMap() {} | 18 PrefValueMap::~PrefValueMap() {} |
18 | 19 |
19 bool PrefValueMap::GetValue(const std::string& key, | 20 bool PrefValueMap::GetValue(const std::string& key, |
20 const base::Value** value) const { | 21 const base::Value** value) const { |
21 const base::Value* got_value = prefs_.get(key); | 22 const base::Value* got_value = prefs_.get(key); |
22 if (value && got_value) | 23 if (value && got_value) |
23 *value = got_value; | 24 *value = got_value; |
24 | 25 |
25 return !!got_value; | 26 return !!got_value; |
26 } | 27 } |
27 | 28 |
28 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) { | 29 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) { |
29 base::Value* got_value = prefs_.get(key); | 30 base::Value* got_value = prefs_.get(key); |
30 if (value && got_value) | 31 if (value && got_value) |
31 *value = got_value; | 32 *value = got_value; |
32 | 33 |
33 return !!got_value; | 34 return !!got_value; |
34 } | 35 } |
35 | 36 |
36 bool PrefValueMap::SetValue(const std::string& key, | 37 bool PrefValueMap::SetValue(const std::string& key, |
37 scoped_ptr<base::Value> value) { | 38 std::unique_ptr<base::Value> value) { |
38 DCHECK(value); | 39 DCHECK(value); |
39 | 40 |
40 base::Value* old_value = prefs_.get(key); | 41 base::Value* old_value = prefs_.get(key); |
41 if (old_value && value->Equals(old_value)) | 42 if (old_value && value->Equals(old_value)) |
42 return false; | 43 return false; |
43 | 44 |
44 prefs_.set(key, std::move(value)); | 45 prefs_.set(key, std::move(value)); |
45 return true; | 46 return true; |
46 } | 47 } |
47 | 48 |
(...skipping 25 matching lines...) Expand all Loading... |
73 return prefs_.end(); | 74 return prefs_.end(); |
74 } | 75 } |
75 | 76 |
76 bool PrefValueMap::GetBoolean(const std::string& key, | 77 bool PrefValueMap::GetBoolean(const std::string& key, |
77 bool* value) const { | 78 bool* value) const { |
78 const base::Value* stored_value = nullptr; | 79 const base::Value* stored_value = nullptr; |
79 return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value); | 80 return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value); |
80 } | 81 } |
81 | 82 |
82 void PrefValueMap::SetBoolean(const std::string& key, bool value) { | 83 void PrefValueMap::SetBoolean(const std::string& key, bool value) { |
83 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value))); | 84 SetValue(key, base::WrapUnique(new base::FundamentalValue(value))); |
84 } | 85 } |
85 | 86 |
86 bool PrefValueMap::GetString(const std::string& key, | 87 bool PrefValueMap::GetString(const std::string& key, |
87 std::string* value) const { | 88 std::string* value) const { |
88 const base::Value* stored_value = nullptr; | 89 const base::Value* stored_value = nullptr; |
89 return GetValue(key, &stored_value) && stored_value->GetAsString(value); | 90 return GetValue(key, &stored_value) && stored_value->GetAsString(value); |
90 } | 91 } |
91 | 92 |
92 void PrefValueMap::SetString(const std::string& key, | 93 void PrefValueMap::SetString(const std::string& key, |
93 const std::string& value) { | 94 const std::string& value) { |
94 SetValue(key, make_scoped_ptr(new base::StringValue(value))); | 95 SetValue(key, base::WrapUnique(new base::StringValue(value))); |
95 } | 96 } |
96 | 97 |
97 bool PrefValueMap::GetInteger(const std::string& key, int* value) const { | 98 bool PrefValueMap::GetInteger(const std::string& key, int* value) const { |
98 const base::Value* stored_value = nullptr; | 99 const base::Value* stored_value = nullptr; |
99 return GetValue(key, &stored_value) && stored_value->GetAsInteger(value); | 100 return GetValue(key, &stored_value) && stored_value->GetAsInteger(value); |
100 } | 101 } |
101 | 102 |
102 void PrefValueMap::SetInteger(const std::string& key, const int value) { | 103 void PrefValueMap::SetInteger(const std::string& key, const int value) { |
103 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value))); | 104 SetValue(key, base::WrapUnique(new base::FundamentalValue(value))); |
104 } | 105 } |
105 | 106 |
106 void PrefValueMap::SetDouble(const std::string& key, const double value) { | 107 void PrefValueMap::SetDouble(const std::string& key, const double value) { |
107 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value))); | 108 SetValue(key, base::WrapUnique(new base::FundamentalValue(value))); |
108 } | 109 } |
109 | 110 |
110 void PrefValueMap::GetDifferingKeys( | 111 void PrefValueMap::GetDifferingKeys( |
111 const PrefValueMap* other, | 112 const PrefValueMap* other, |
112 std::vector<std::string>* differing_keys) const { | 113 std::vector<std::string>* differing_keys) const { |
113 differing_keys->clear(); | 114 differing_keys->clear(); |
114 | 115 |
115 // Put everything into ordered maps. | 116 // Put everything into ordered maps. |
116 std::map<std::string, base::Value*> this_prefs(prefs_.begin(), prefs_.end()); | 117 std::map<std::string, base::Value*> this_prefs(prefs_.begin(), prefs_.end()); |
117 std::map<std::string, base::Value*> other_prefs(other->prefs_.begin(), | 118 std::map<std::string, base::Value*> other_prefs(other->prefs_.begin(), |
(...skipping 17 matching lines...) Expand all Loading... |
135 ++other_pref; | 136 ++other_pref; |
136 } | 137 } |
137 } | 138 } |
138 | 139 |
139 // Add the remaining entries. | 140 // Add the remaining entries. |
140 for ( ; this_pref != this_prefs.end(); ++this_pref) | 141 for ( ; this_pref != this_prefs.end(); ++this_pref) |
141 differing_keys->push_back(this_pref->first); | 142 differing_keys->push_back(this_pref->first); |
142 for ( ; other_pref != other_prefs.end(); ++other_pref) | 143 for ( ; other_pref != other_prefs.end(); ++other_pref) |
143 differing_keys->push_back(other_pref->first); | 144 differing_keys->push_back(other_pref->first); |
144 } | 145 } |
OLD | NEW |