Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(869)

Side by Side Diff: components/policy/core/common/policy_map.cc

Issue 1940153002: Use std::unique_ptr to express ownership of base::Value in PolicyMap::Entry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: another-fix Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/policy/core/common/policy_map.h" 5 #include "components/policy/core/common/policy_map.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 12
13 namespace policy { 13 namespace policy {
14 14
15 PolicyMap::Entry::Entry() 15 PolicyMap::Entry::Entry()
16 : level(POLICY_LEVEL_RECOMMENDED), 16 : level(POLICY_LEVEL_RECOMMENDED), scope(POLICY_SCOPE_USER) {}
17 scope(POLICY_SCOPE_USER),
18 value(NULL),
19 external_data_fetcher(NULL) {}
20 17
21 void PolicyMap::Entry::DeleteOwnedMembers() { 18 PolicyMap::Entry::~Entry() = default;
22 delete value;
23 value = NULL;
24 delete external_data_fetcher;
25 external_data_fetcher = NULL;
26 }
27 19
28 std::unique_ptr<PolicyMap::Entry> PolicyMap::Entry::DeepCopy() const { 20 PolicyMap::Entry::Entry(Entry&&) = default;
29 std::unique_ptr<Entry> copy(new Entry); 21
30 copy->level = level; 22 PolicyMap::Entry& PolicyMap::Entry::operator=(Entry&&) = default;
31 copy->scope = scope; 23
32 copy->source = source; 24 PolicyMap::Entry PolicyMap::Entry::DeepCopy() const {
25 Entry copy;
26 copy.level = level;
27 copy.scope = scope;
28 copy.source = source;
33 if (value) 29 if (value)
34 copy->value = value->DeepCopy(); 30 copy.value = value->CreateDeepCopy();
35 if (external_data_fetcher) { 31 if (external_data_fetcher) {
36 copy->external_data_fetcher = 32 copy.external_data_fetcher.reset(
37 new ExternalDataFetcher(*external_data_fetcher); 33 new ExternalDataFetcher(*external_data_fetcher));
38 } 34 }
39 return copy; 35 return copy;
40 } 36 }
41 37
42 bool PolicyMap::Entry::has_higher_priority_than( 38 bool PolicyMap::Entry::has_higher_priority_than(
43 const PolicyMap::Entry& other) const { 39 const PolicyMap::Entry& other) const {
44 if (level == other.level) 40 if (level == other.level)
45 return scope > other.scope; 41 return scope > other.scope;
46 else 42 else
47 return level > other.level; 43 return level > other.level;
48 } 44 }
49 45
50 bool PolicyMap::Entry::Equals(const PolicyMap::Entry& other) const { 46 bool PolicyMap::Entry::Equals(const PolicyMap::Entry& other) const {
51 return level == other.level && 47 return level == other.level && scope == other.scope &&
52 scope == other.scope &&
53 source == other.source && // Necessary for PolicyUIHandler observers. 48 source == other.source && // Necessary for PolicyUIHandler observers.
54 // They have to update when sources change. 49 // They have to update when sources change.
55 base::Value::Equals(value, other.value) && 50 base::Value::Equals(value.get(), other.value.get()) &&
56 ExternalDataFetcher::Equals(external_data_fetcher, 51 ExternalDataFetcher::Equals(external_data_fetcher.get(),
57 other.external_data_fetcher); 52 other.external_data_fetcher.get());
58 } 53 }
59 54
60 PolicyMap::PolicyMap() { 55 PolicyMap::PolicyMap() {
61 } 56 }
62 57
63 PolicyMap::~PolicyMap() { 58 PolicyMap::~PolicyMap() {
64 Clear(); 59 Clear();
65 } 60 }
66 61
67 const PolicyMap::Entry* PolicyMap::Get(const std::string& policy) const { 62 const PolicyMap::Entry* PolicyMap::Get(const std::string& policy) const {
68 PolicyMapType::const_iterator entry = map_.find(policy); 63 PolicyMapType::const_iterator entry = map_.find(policy);
69 return entry == map_.end() ? NULL : &entry->second; 64 return entry == map_.end() ? nullptr : &entry->second;
70 } 65 }
71 66
72 const base::Value* PolicyMap::GetValue(const std::string& policy) const { 67 const base::Value* PolicyMap::GetValue(const std::string& policy) const {
73 PolicyMapType::const_iterator entry = map_.find(policy); 68 PolicyMapType::const_iterator entry = map_.find(policy);
74 return entry == map_.end() ? NULL : entry->second.value; 69 return entry == map_.end() ? nullptr : entry->second.value.get();
75 } 70 }
76 71
77 void PolicyMap::Set(const std::string& policy, 72 void PolicyMap::Set(
78 PolicyLevel level, 73 const std::string& policy,
79 PolicyScope scope, 74 PolicyLevel level,
80 PolicySource source, 75 PolicyScope scope,
81 base::Value* value, 76 PolicySource source,
82 ExternalDataFetcher* external_data_fetcher) { 77 std::unique_ptr<base::Value> value,
83 Entry& entry = map_[policy]; 78 std::unique_ptr<ExternalDataFetcher> external_data_fetcher) {
84 entry.DeleteOwnedMembers(); 79 Entry entry;
85 entry.level = level; 80 entry.level = level;
86 entry.scope = scope; 81 entry.scope = scope;
87 entry.source = source; 82 entry.source = source;
88 entry.value = value; 83 entry.value = std::move(value);
89 entry.external_data_fetcher = external_data_fetcher; 84 entry.external_data_fetcher = std::move(external_data_fetcher);
85 Set(policy, std::move(entry));
86 }
87
88 void PolicyMap::Set(const std::string& policy, Entry entry) {
89 map_[policy] = std::move(entry);
90 } 90 }
91 91
92 void PolicyMap::Erase(const std::string& policy) { 92 void PolicyMap::Erase(const std::string& policy) {
93 PolicyMapType::iterator it = map_.find(policy); 93 map_.erase(policy);
94 if (it != map_.end()) {
95 it->second.DeleteOwnedMembers();
96 map_.erase(it);
97 }
98 } 94 }
99 95
100 void PolicyMap::Swap(PolicyMap* other) { 96 void PolicyMap::Swap(PolicyMap* other) {
101 map_.swap(other->map_); 97 map_.swap(other->map_);
102 } 98 }
103 99
104 void PolicyMap::CopyFrom(const PolicyMap& other) { 100 void PolicyMap::CopyFrom(const PolicyMap& other) {
105 Clear(); 101 Clear();
106 for (const_iterator it = other.begin(); it != other.end(); ++it) { 102 for (const auto& it : other)
107 const Entry& entry = it->second; 103 Set(it.first, it.second.DeepCopy());
108 Set(it->first, entry.level, entry.scope, entry.source, 104 }
109 entry.value->DeepCopy(), 105
110 entry.external_data_fetcher 106 std::unique_ptr<PolicyMap> PolicyMap::DeepCopy() const {
111 ? new ExternalDataFetcher(*entry.external_data_fetcher) 107 std::unique_ptr<PolicyMap> copy(new PolicyMap());
112 : nullptr); 108 copy->CopyFrom(*this);
109 return copy;
110 }
111
112 void PolicyMap::MergeFrom(const PolicyMap& other) {
113 for (const auto& it : other) {
114 const Entry* entry = Get(it.first);
115 if (!entry || it.second.has_higher_priority_than(*entry))
116 Set(it.first, it.second.DeepCopy());
113 } 117 }
114 } 118 }
115 119
116 std::unique_ptr<PolicyMap> PolicyMap::DeepCopy() const {
117 PolicyMap* copy = new PolicyMap();
118 copy->CopyFrom(*this);
119 return base::WrapUnique(copy);
120 }
121
122 void PolicyMap::MergeFrom(const PolicyMap& other) {
123 for (const_iterator it = other.begin(); it != other.end(); ++it) {
124 const Entry* entry = Get(it->first);
125 if (!entry || it->second.has_higher_priority_than(*entry)) {
126 Set(it->first, it->second.level, it->second.scope, it->second.source,
127 it->second.value->DeepCopy(),
128 it->second.external_data_fetcher
129 ? new ExternalDataFetcher(
130 *it->second.external_data_fetcher)
131 : nullptr);
132 }
133 }
134 }
135
136 void PolicyMap::LoadFrom( 120 void PolicyMap::LoadFrom(
137 const base::DictionaryValue* policies, 121 const base::DictionaryValue* policies,
138 PolicyLevel level, 122 PolicyLevel level,
139 PolicyScope scope, 123 PolicyScope scope,
140 PolicySource source) { 124 PolicySource source) {
141 for (base::DictionaryValue::Iterator it(*policies); 125 for (base::DictionaryValue::Iterator it(*policies);
142 !it.IsAtEnd(); it.Advance()) { 126 !it.IsAtEnd(); it.Advance()) {
143 Set(it.key(), level, scope, source, it.value().DeepCopy(), nullptr); 127 Set(it.key(), level, scope, source, it.value().CreateDeepCopy(), nullptr);
144 } 128 }
145 } 129 }
146 130
147 void PolicyMap::GetDifferingKeys(const PolicyMap& other, 131 void PolicyMap::GetDifferingKeys(const PolicyMap& other,
148 std::set<std::string>* differing_keys) const { 132 std::set<std::string>* differing_keys) const {
149 // Walk over the maps in lockstep, adding everything that is different. 133 // Walk over the maps in lockstep, adding everything that is different.
150 const_iterator iter_this(begin()); 134 const_iterator iter_this(begin());
151 const_iterator iter_other(other.begin()); 135 const_iterator iter_other(other.begin());
152 while (iter_this != end() && iter_other != other.end()) { 136 while (iter_this != end() && iter_other != other.end()) {
153 const int diff = iter_this->first.compare(iter_other->first); 137 const int diff = iter_this->first.compare(iter_other->first);
(...skipping 15 matching lines...) Expand all
169 for ( ; iter_this != end(); ++iter_this) 153 for ( ; iter_this != end(); ++iter_this)
170 differing_keys->insert(iter_this->first); 154 differing_keys->insert(iter_this->first);
171 for ( ; iter_other != other.end(); ++iter_other) 155 for ( ; iter_other != other.end(); ++iter_other)
172 differing_keys->insert(iter_other->first); 156 differing_keys->insert(iter_other->first);
173 } 157 }
174 158
175 void PolicyMap::FilterLevel(PolicyLevel level) { 159 void PolicyMap::FilterLevel(PolicyLevel level) {
176 PolicyMapType::iterator iter(map_.begin()); 160 PolicyMapType::iterator iter(map_.begin());
177 while (iter != map_.end()) { 161 while (iter != map_.end()) {
178 if (iter->second.level != level) { 162 if (iter->second.level != level) {
179 iter->second.DeleteOwnedMembers();
180 map_.erase(iter++); 163 map_.erase(iter++);
181 } else { 164 } else {
182 ++iter; 165 ++iter;
183 } 166 }
184 } 167 }
185 } 168 }
186 169
187 bool PolicyMap::Equals(const PolicyMap& other) const { 170 bool PolicyMap::Equals(const PolicyMap& other) const {
188 return other.size() == size() && 171 return other.size() == size() &&
189 std::equal(begin(), end(), other.begin(), MapEntryEquals); 172 std::equal(begin(), end(), other.begin(), MapEntryEquals);
190 } 173 }
191 174
192 bool PolicyMap::empty() const { 175 bool PolicyMap::empty() const {
193 return map_.empty(); 176 return map_.empty();
194 } 177 }
195 178
196 size_t PolicyMap::size() const { 179 size_t PolicyMap::size() const {
197 return map_.size(); 180 return map_.size();
198 } 181 }
199 182
200 PolicyMap::const_iterator PolicyMap::begin() const { 183 PolicyMap::const_iterator PolicyMap::begin() const {
201 return map_.begin(); 184 return map_.begin();
202 } 185 }
203 186
204 PolicyMap::const_iterator PolicyMap::end() const { 187 PolicyMap::const_iterator PolicyMap::end() const {
205 return map_.end(); 188 return map_.end();
206 } 189 }
207 190
208 void PolicyMap::Clear() { 191 void PolicyMap::Clear() {
209 for (PolicyMapType::iterator it = map_.begin(); it != map_.end(); ++it)
210 it->second.DeleteOwnedMembers();
211 map_.clear(); 192 map_.clear();
212 } 193 }
213 194
214 // static 195 // static
215 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, 196 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a,
216 const PolicyMap::PolicyMapType::value_type& b) { 197 const PolicyMap::PolicyMapType::value_type& b) {
217 return a.first == b.first && a.second.Equals(b.second); 198 return a.first == b.first && a.second.Equals(b.second);
218 } 199 }
219 200
220 } // namespace policy 201 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/policy_map.h ('k') | components/policy/core/common/policy_map_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698