OLD | NEW |
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/stl_util.h" | 10 #include "base/stl_util.h" |
11 | 11 |
12 namespace policy { | 12 namespace policy { |
13 | 13 |
| 14 void PolicyMap::Entry::DeleteOwnedMembers() { |
| 15 delete value; |
| 16 value = NULL; |
| 17 delete external_data_fetcher; |
| 18 external_data_fetcher = NULL; |
| 19 } |
| 20 |
| 21 scoped_ptr<PolicyMap::Entry> PolicyMap::Entry::DeepCopy() const { |
| 22 scoped_ptr<Entry> copy(new Entry); |
| 23 copy->level = level; |
| 24 copy->scope = scope; |
| 25 if (value) |
| 26 copy->value = value->DeepCopy(); |
| 27 if (external_data_fetcher) { |
| 28 copy->external_data_fetcher = |
| 29 new ExternalDataFetcher(*external_data_fetcher); |
| 30 } |
| 31 return copy.Pass(); |
| 32 } |
| 33 |
14 bool PolicyMap::Entry::has_higher_priority_than( | 34 bool PolicyMap::Entry::has_higher_priority_than( |
15 const PolicyMap::Entry& other) const { | 35 const PolicyMap::Entry& other) const { |
16 if (level == other.level) | 36 if (level == other.level) |
17 return scope > other.scope; | 37 return scope > other.scope; |
18 else | 38 else |
19 return level > other.level; | 39 return level > other.level; |
20 } | 40 } |
21 | 41 |
22 bool PolicyMap::Entry::Equals(const PolicyMap::Entry& other) const { | 42 bool PolicyMap::Entry::Equals(const PolicyMap::Entry& other) const { |
23 return level == other.level && | 43 return level == other.level && |
(...skipping 19 matching lines...) Expand all Loading... |
43 PolicyMapType::const_iterator entry = map_.find(policy); | 63 PolicyMapType::const_iterator entry = map_.find(policy); |
44 return entry == map_.end() ? NULL : entry->second.value; | 64 return entry == map_.end() ? NULL : entry->second.value; |
45 } | 65 } |
46 | 66 |
47 void PolicyMap::Set(const std::string& policy, | 67 void PolicyMap::Set(const std::string& policy, |
48 PolicyLevel level, | 68 PolicyLevel level, |
49 PolicyScope scope, | 69 PolicyScope scope, |
50 Value* value, | 70 Value* value, |
51 ExternalDataFetcher* external_data_fetcher) { | 71 ExternalDataFetcher* external_data_fetcher) { |
52 Entry& entry = map_[policy]; | 72 Entry& entry = map_[policy]; |
53 delete entry.value; | 73 entry.DeleteOwnedMembers(); |
54 delete entry.external_data_fetcher; | |
55 entry.level = level; | 74 entry.level = level; |
56 entry.scope = scope; | 75 entry.scope = scope; |
57 entry.value = value; | 76 entry.value = value; |
58 entry.external_data_fetcher = external_data_fetcher; | 77 entry.external_data_fetcher = external_data_fetcher; |
59 } | 78 } |
60 | 79 |
61 void PolicyMap::Erase(const std::string& policy) { | 80 void PolicyMap::Erase(const std::string& policy) { |
62 PolicyMapType::iterator it = map_.find(policy); | 81 PolicyMapType::iterator it = map_.find(policy); |
63 if (it != map_.end()) { | 82 if (it != map_.end()) { |
64 delete it->second.value; | 83 it->second.DeleteOwnedMembers(); |
65 delete it->second.external_data_fetcher; | |
66 map_.erase(it); | 84 map_.erase(it); |
67 } | 85 } |
68 } | 86 } |
69 | 87 |
70 void PolicyMap::Swap(PolicyMap* other) { | 88 void PolicyMap::Swap(PolicyMap* other) { |
71 map_.swap(other->map_); | 89 map_.swap(other->map_); |
72 } | 90 } |
73 | 91 |
74 void PolicyMap::CopyFrom(const PolicyMap& other) { | 92 void PolicyMap::CopyFrom(const PolicyMap& other) { |
75 Clear(); | 93 Clear(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 for ( ; iter_this != end(); ++iter_this) | 150 for ( ; iter_this != end(); ++iter_this) |
133 differing_keys->insert(iter_this->first); | 151 differing_keys->insert(iter_this->first); |
134 for ( ; iter_other != other.end(); ++iter_other) | 152 for ( ; iter_other != other.end(); ++iter_other) |
135 differing_keys->insert(iter_other->first); | 153 differing_keys->insert(iter_other->first); |
136 } | 154 } |
137 | 155 |
138 void PolicyMap::FilterLevel(PolicyLevel level) { | 156 void PolicyMap::FilterLevel(PolicyLevel level) { |
139 PolicyMapType::iterator iter(map_.begin()); | 157 PolicyMapType::iterator iter(map_.begin()); |
140 while (iter != map_.end()) { | 158 while (iter != map_.end()) { |
141 if (iter->second.level != level) { | 159 if (iter->second.level != level) { |
142 delete iter->second.value; | 160 iter->second.DeleteOwnedMembers(); |
143 delete iter->second.external_data_fetcher; | |
144 map_.erase(iter++); | 161 map_.erase(iter++); |
145 } else { | 162 } else { |
146 ++iter; | 163 ++iter; |
147 } | 164 } |
148 } | 165 } |
149 } | 166 } |
150 | 167 |
151 bool PolicyMap::Equals(const PolicyMap& other) const { | 168 bool PolicyMap::Equals(const PolicyMap& other) const { |
152 return other.size() == size() && | 169 return other.size() == size() && |
153 std::equal(begin(), end(), other.begin(), MapEntryEquals); | 170 std::equal(begin(), end(), other.begin(), MapEntryEquals); |
154 } | 171 } |
155 | 172 |
156 bool PolicyMap::empty() const { | 173 bool PolicyMap::empty() const { |
157 return map_.empty(); | 174 return map_.empty(); |
158 } | 175 } |
159 | 176 |
160 size_t PolicyMap::size() const { | 177 size_t PolicyMap::size() const { |
161 return map_.size(); | 178 return map_.size(); |
162 } | 179 } |
163 | 180 |
164 PolicyMap::const_iterator PolicyMap::begin() const { | 181 PolicyMap::const_iterator PolicyMap::begin() const { |
165 return map_.begin(); | 182 return map_.begin(); |
166 } | 183 } |
167 | 184 |
168 PolicyMap::const_iterator PolicyMap::end() const { | 185 PolicyMap::const_iterator PolicyMap::end() const { |
169 return map_.end(); | 186 return map_.end(); |
170 } | 187 } |
171 | 188 |
172 void PolicyMap::Clear() { | 189 void PolicyMap::Clear() { |
173 for (PolicyMapType::iterator it = map_.begin(); it != map_.end(); ++it) { | 190 for (PolicyMapType::iterator it = map_.begin(); it != map_.end(); ++it) |
174 delete it->second.value; | 191 it->second.DeleteOwnedMembers(); |
175 delete it->second.external_data_fetcher; | |
176 } | |
177 map_.clear(); | 192 map_.clear(); |
178 } | 193 } |
179 | 194 |
180 // static | 195 // static |
181 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, | 196 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, |
182 const PolicyMap::PolicyMapType::value_type& b) { | 197 const PolicyMap::PolicyMapType::value_type& b) { |
183 return a.first == b.first && a.second.Equals(b.second); | 198 return a.first == b.first && a.second.Equals(b.second); |
184 } | 199 } |
185 | 200 |
186 } // namespace policy | 201 } // namespace policy |
OLD | NEW |