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 "base/prefs/pref_value_map.h" | 5 #include "base/prefs/pref_value_map.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 | 13 |
14 PrefValueMap::PrefValueMap() {} | 14 PrefValueMap::PrefValueMap() {} |
15 | 15 |
16 PrefValueMap::~PrefValueMap() { | 16 PrefValueMap::~PrefValueMap() {} |
17 Clear(); | |
18 } | |
19 | 17 |
20 bool PrefValueMap::GetValue(const std::string& key, | 18 bool PrefValueMap::GetValue(const std::string& key, |
21 const base::Value** value) const { | 19 const base::Value** value) const { |
22 const Map::const_iterator entry = prefs_.find(key); | 20 const base::Value* got_value = prefs_.get(key); |
23 if (entry == prefs_.end()) | 21 if (value && got_value) |
24 return false; | 22 *value = got_value; |
25 | 23 |
26 if (value) | 24 return !!got_value; |
27 *value = entry->second; | |
28 return true; | |
29 } | 25 } |
30 | 26 |
31 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) { | 27 bool PrefValueMap::GetValue(const std::string& key, base::Value** value) { |
32 const Map::const_iterator entry = prefs_.find(key); | 28 base::Value* got_value = prefs_.get(key); |
33 if (entry == prefs_.end()) | 29 if (value && got_value) |
| 30 *value = got_value; |
| 31 |
| 32 return !!got_value; |
| 33 } |
| 34 |
| 35 bool PrefValueMap::SetValue(const std::string& key, |
| 36 scoped_ptr<base::Value> value) { |
| 37 DCHECK(value); |
| 38 |
| 39 base::Value* old_value = prefs_.get(key); |
| 40 if (old_value && value->Equals(old_value)) |
34 return false; | 41 return false; |
35 | 42 |
36 if (value) | 43 prefs_.set(key, value.Pass()); |
37 *value = entry->second; | |
38 return true; | |
39 } | |
40 | |
41 bool PrefValueMap::SetValue(const std::string& key, base::Value* value) { | |
42 DCHECK(value); | |
43 auto result = prefs_.insert(std::make_pair(key, value)); | |
44 if (result.second) | |
45 return true; | |
46 | |
47 scoped_ptr<base::Value> value_ptr(value); | |
48 const Map::iterator& entry = result.first; | |
49 if (base::Value::Equals(entry->second, value)) | |
50 return false; | |
51 | |
52 delete entry->second; | |
53 entry->second = value_ptr.release(); | |
54 | |
55 return true; | 44 return true; |
56 } | 45 } |
57 | 46 |
58 bool PrefValueMap::RemoveValue(const std::string& key) { | 47 bool PrefValueMap::RemoveValue(const std::string& key) { |
59 const Map::iterator entry = prefs_.find(key); | 48 return prefs_.erase(key) != 0; |
60 if (entry == prefs_.end()) | |
61 return false; | |
62 | |
63 delete entry->second; | |
64 prefs_.erase(entry); | |
65 return true; | |
66 } | 49 } |
67 | 50 |
68 void PrefValueMap::Clear() { | 51 void PrefValueMap::Clear() { |
69 STLDeleteValues(&prefs_); | 52 prefs_.clear(); |
70 } | 53 } |
71 | 54 |
72 void PrefValueMap::Swap(PrefValueMap* other) { | 55 void PrefValueMap::Swap(PrefValueMap* other) { |
73 prefs_.swap(other->prefs_); | 56 prefs_.swap(other->prefs_); |
74 } | 57 } |
75 | 58 |
76 PrefValueMap::iterator PrefValueMap::begin() { | 59 PrefValueMap::iterator PrefValueMap::begin() { |
77 return prefs_.begin(); | 60 return prefs_.begin(); |
78 } | 61 } |
79 | 62 |
80 PrefValueMap::iterator PrefValueMap::end() { | 63 PrefValueMap::iterator PrefValueMap::end() { |
81 return prefs_.end(); | 64 return prefs_.end(); |
82 } | 65 } |
83 | 66 |
84 PrefValueMap::const_iterator PrefValueMap::begin() const { | 67 PrefValueMap::const_iterator PrefValueMap::begin() const { |
85 return prefs_.begin(); | 68 return prefs_.begin(); |
86 } | 69 } |
87 | 70 |
88 PrefValueMap::const_iterator PrefValueMap::end() const { | 71 PrefValueMap::const_iterator PrefValueMap::end() const { |
89 return prefs_.end(); | 72 return prefs_.end(); |
90 } | 73 } |
91 | 74 |
92 bool PrefValueMap::GetBoolean(const std::string& key, | 75 bool PrefValueMap::GetBoolean(const std::string& key, |
93 bool* value) const { | 76 bool* value) const { |
94 const base::Value* stored_value = nullptr; | 77 const base::Value* stored_value = nullptr; |
95 return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value); | 78 return GetValue(key, &stored_value) && stored_value->GetAsBoolean(value); |
96 } | 79 } |
97 | 80 |
98 void PrefValueMap::SetBoolean(const std::string& key, bool value) { | 81 void PrefValueMap::SetBoolean(const std::string& key, bool value) { |
99 SetValue(key, new base::FundamentalValue(value)); | 82 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value))); |
100 } | 83 } |
101 | 84 |
102 bool PrefValueMap::GetString(const std::string& key, | 85 bool PrefValueMap::GetString(const std::string& key, |
103 std::string* value) const { | 86 std::string* value) const { |
104 const base::Value* stored_value = nullptr; | 87 const base::Value* stored_value = nullptr; |
105 return GetValue(key, &stored_value) && stored_value->GetAsString(value); | 88 return GetValue(key, &stored_value) && stored_value->GetAsString(value); |
106 } | 89 } |
107 | 90 |
108 void PrefValueMap::SetString(const std::string& key, | 91 void PrefValueMap::SetString(const std::string& key, |
109 const std::string& value) { | 92 const std::string& value) { |
110 SetValue(key, new base::StringValue(value)); | 93 SetValue(key, make_scoped_ptr(new base::StringValue(value))); |
111 } | 94 } |
112 | 95 |
113 bool PrefValueMap::GetInteger(const std::string& key, int* value) const { | 96 bool PrefValueMap::GetInteger(const std::string& key, int* value) const { |
114 const base::Value* stored_value = nullptr; | 97 const base::Value* stored_value = nullptr; |
115 return GetValue(key, &stored_value) && stored_value->GetAsInteger(value); | 98 return GetValue(key, &stored_value) && stored_value->GetAsInteger(value); |
116 } | 99 } |
117 | 100 |
118 void PrefValueMap::SetInteger(const std::string& key, const int value) { | 101 void PrefValueMap::SetInteger(const std::string& key, const int value) { |
119 SetValue(key, new base::FundamentalValue(value)); | 102 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value))); |
120 } | 103 } |
121 | 104 |
122 void PrefValueMap::SetDouble(const std::string& key, const double value) { | 105 void PrefValueMap::SetDouble(const std::string& key, const double value) { |
123 SetValue(key, new base::FundamentalValue(value)); | 106 SetValue(key, make_scoped_ptr(new base::FundamentalValue(value))); |
124 } | 107 } |
125 | 108 |
126 void PrefValueMap::GetDifferingKeys( | 109 void PrefValueMap::GetDifferingKeys( |
127 const PrefValueMap* other, | 110 const PrefValueMap* other, |
128 std::vector<std::string>* differing_keys) const { | 111 std::vector<std::string>* differing_keys) const { |
129 differing_keys->clear(); | 112 differing_keys->clear(); |
130 | 113 |
131 // Put everything into ordered maps. | 114 // Put everything into ordered maps. |
132 std::map<std::string, base::Value*> this_prefs(prefs_.begin(), prefs_.end()); | 115 std::map<std::string, base::Value*> this_prefs(prefs_.begin(), prefs_.end()); |
133 std::map<std::string, base::Value*> other_prefs(other->prefs_.begin(), | 116 std::map<std::string, base::Value*> other_prefs(other->prefs_.begin(), |
(...skipping 17 matching lines...) Expand all Loading... |
151 ++other_pref; | 134 ++other_pref; |
152 } | 135 } |
153 } | 136 } |
154 | 137 |
155 // Add the remaining entries. | 138 // Add the remaining entries. |
156 for ( ; this_pref != this_prefs.end(); ++this_pref) | 139 for ( ; this_pref != this_prefs.end(); ++this_pref) |
157 differing_keys->push_back(this_pref->first); | 140 differing_keys->push_back(this_pref->first); |
158 for ( ; other_pref != other_prefs.end(); ++other_pref) | 141 for ( ; other_pref != other_prefs.end(); ++other_pref) |
159 differing_keys->push_back(other_pref->first); | 142 differing_keys->push_back(other_pref->first); |
160 } | 143 } |
OLD | NEW |