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 PolicyMap::Entry::Entry() | 14 PolicyMap::Entry::Entry() |
15 : level(POLICY_LEVEL_RECOMMENDED), | 15 : level(POLICY_LEVEL_RECOMMENDED), |
16 scope(POLICY_SCOPE_USER), | 16 scope(POLICY_SCOPE_USER), |
17 source(POLICY_SOURCE_DEFAULT), | |
17 value(NULL), | 18 value(NULL), |
18 external_data_fetcher(NULL) {} | 19 external_data_fetcher(NULL) {} |
19 | 20 |
20 void PolicyMap::Entry::DeleteOwnedMembers() { | 21 void PolicyMap::Entry::DeleteOwnedMembers() { |
21 delete value; | 22 delete value; |
22 value = NULL; | 23 value = NULL; |
23 delete external_data_fetcher; | 24 delete external_data_fetcher; |
24 external_data_fetcher = NULL; | 25 external_data_fetcher = NULL; |
25 } | 26 } |
26 | 27 |
27 scoped_ptr<PolicyMap::Entry> PolicyMap::Entry::DeepCopy() const { | 28 scoped_ptr<PolicyMap::Entry> PolicyMap::Entry::DeepCopy() const { |
28 scoped_ptr<Entry> copy(new Entry); | 29 scoped_ptr<Entry> copy(new Entry); |
29 copy->level = level; | 30 copy->level = level; |
30 copy->scope = scope; | 31 copy->scope = scope; |
32 copy->source = source; | |
31 if (value) | 33 if (value) |
32 copy->value = value->DeepCopy(); | 34 copy->value = value->DeepCopy(); |
33 if (external_data_fetcher) { | 35 if (external_data_fetcher) { |
34 copy->external_data_fetcher = | 36 copy->external_data_fetcher = |
35 new ExternalDataFetcher(*external_data_fetcher); | 37 new ExternalDataFetcher(*external_data_fetcher); |
36 } | 38 } |
37 return copy.Pass(); | 39 return copy.Pass(); |
38 } | 40 } |
39 | 41 |
40 bool PolicyMap::Entry::has_higher_priority_than( | 42 bool PolicyMap::Entry::has_higher_priority_than( |
41 const PolicyMap::Entry& other) const { | 43 const PolicyMap::Entry& other) const { |
42 if (level == other.level) | 44 if (level == other.level) |
43 return scope > other.scope; | 45 return scope > other.scope; |
44 else | 46 else |
45 return level > other.level; | 47 return level > other.level; |
46 } | 48 } |
47 | 49 |
48 bool PolicyMap::Entry::Equals(const PolicyMap::Entry& other) const { | 50 bool PolicyMap::Entry::Equals(const PolicyMap::Entry& other) const { |
49 return level == other.level && | 51 return level == other.level && scope == other.scope && |
50 scope == other.scope && | 52 source == other.source && base::Value::Equals(value, other.value) && |
Thiemo Nagel
2015/09/01 17:40:37
I'm somehow split about this. Since the source is
fhorschig
2015/09/04 06:53:54
Done.
| |
51 base::Value::Equals(value, other.value) && | |
52 ExternalDataFetcher::Equals(external_data_fetcher, | 53 ExternalDataFetcher::Equals(external_data_fetcher, |
53 other.external_data_fetcher); | 54 other.external_data_fetcher); |
54 } | 55 } |
55 | 56 |
56 PolicyMap::PolicyMap() { | 57 PolicyMap::PolicyMap() { |
57 } | 58 } |
58 | 59 |
59 PolicyMap::~PolicyMap() { | 60 PolicyMap::~PolicyMap() { |
60 Clear(); | 61 Clear(); |
61 } | 62 } |
62 | 63 |
63 const PolicyMap::Entry* PolicyMap::Get(const std::string& policy) const { | 64 const PolicyMap::Entry* PolicyMap::Get(const std::string& policy) const { |
64 PolicyMapType::const_iterator entry = map_.find(policy); | 65 PolicyMapType::const_iterator entry = map_.find(policy); |
65 return entry == map_.end() ? NULL : &entry->second; | 66 return entry == map_.end() ? NULL : &entry->second; |
66 } | 67 } |
67 | 68 |
68 const base::Value* PolicyMap::GetValue(const std::string& policy) const { | 69 const base::Value* PolicyMap::GetValue(const std::string& policy) const { |
69 PolicyMapType::const_iterator entry = map_.find(policy); | 70 PolicyMapType::const_iterator entry = map_.find(policy); |
70 return entry == map_.end() ? NULL : entry->second.value; | 71 return entry == map_.end() ? NULL : entry->second.value; |
71 } | 72 } |
72 | 73 |
74 void PolicyMap::SetSources(PolicySource source) { | |
75 for (PolicyMapType::iterator it = map_.begin(); it != map_.end(); ++it) { | |
76 if (source != POLICY_SOURCE_PLATFORM && | |
Thiemo Nagel
2015/09/01 17:40:37
This deserves a comment!
fhorschig
2015/09/04 06:53:54
Done.
| |
77 it->second.source == POLICY_SOURCE_ENTERPRISE_DEFAULT) | |
78 continue; | |
79 it->second.source = source; | |
80 } | |
81 } | |
82 | |
73 void PolicyMap::Set(const std::string& policy, | 83 void PolicyMap::Set(const std::string& policy, |
74 PolicyLevel level, | 84 PolicyLevel level, |
75 PolicyScope scope, | 85 PolicyScope scope, |
76 base::Value* value, | 86 base::Value* value, |
77 ExternalDataFetcher* external_data_fetcher) { | 87 ExternalDataFetcher* external_data_fetcher) { |
88 Set(policy, level, scope, value, external_data_fetcher, | |
89 POLICY_SOURCE_DEFAULT); | |
90 } | |
91 | |
92 void PolicyMap::Set(const std::string& policy, | |
93 PolicyLevel level, | |
94 PolicyScope scope, | |
95 base::Value* value, | |
96 ExternalDataFetcher* external_data_fetcher, | |
97 PolicySource source) { | |
78 Entry& entry = map_[policy]; | 98 Entry& entry = map_[policy]; |
79 entry.DeleteOwnedMembers(); | 99 entry.DeleteOwnedMembers(); |
80 entry.level = level; | 100 entry.level = level; |
81 entry.scope = scope; | 101 entry.scope = scope; |
102 entry.source = source; | |
82 entry.value = value; | 103 entry.value = value; |
83 entry.external_data_fetcher = external_data_fetcher; | 104 entry.external_data_fetcher = external_data_fetcher; |
84 } | 105 } |
85 | 106 |
86 void PolicyMap::Erase(const std::string& policy) { | 107 void PolicyMap::Erase(const std::string& policy) { |
87 PolicyMapType::iterator it = map_.find(policy); | 108 PolicyMapType::iterator it = map_.find(policy); |
88 if (it != map_.end()) { | 109 if (it != map_.end()) { |
89 it->second.DeleteOwnedMembers(); | 110 it->second.DeleteOwnedMembers(); |
90 map_.erase(it); | 111 map_.erase(it); |
91 } | 112 } |
92 } | 113 } |
93 | 114 |
94 void PolicyMap::Swap(PolicyMap* other) { | 115 void PolicyMap::Swap(PolicyMap* other) { |
95 map_.swap(other->map_); | 116 map_.swap(other->map_); |
96 } | 117 } |
97 | 118 |
98 void PolicyMap::CopyFrom(const PolicyMap& other) { | 119 void PolicyMap::CopyFrom(const PolicyMap& other) { |
99 Clear(); | 120 Clear(); |
100 for (const_iterator it = other.begin(); it != other.end(); ++it) { | 121 for (const_iterator it = other.begin(); it != other.end(); ++it) { |
101 const Entry& entry = it->second; | 122 const Entry& entry = it->second; |
102 Set(it->first, entry.level, entry.scope, | 123 Set(it->first, entry.level, entry.scope, entry.value->DeepCopy(), |
103 entry.value->DeepCopy(), entry.external_data_fetcher ? | 124 entry.external_data_fetcher |
104 new ExternalDataFetcher(*entry.external_data_fetcher) : NULL); | 125 ? new ExternalDataFetcher(*entry.external_data_fetcher) |
126 : NULL, | |
127 entry.source); | |
105 } | 128 } |
106 } | 129 } |
107 | 130 |
108 scoped_ptr<PolicyMap> PolicyMap::DeepCopy() const { | 131 scoped_ptr<PolicyMap> PolicyMap::DeepCopy() const { |
109 PolicyMap* copy = new PolicyMap(); | 132 PolicyMap* copy = new PolicyMap(); |
110 copy->CopyFrom(*this); | 133 copy->CopyFrom(*this); |
111 return make_scoped_ptr(copy); | 134 return make_scoped_ptr(copy); |
112 } | 135 } |
113 | 136 |
114 void PolicyMap::MergeFrom(const PolicyMap& other) { | 137 void PolicyMap::MergeFrom(const PolicyMap& other) { |
115 for (const_iterator it = other.begin(); it != other.end(); ++it) { | 138 for (const_iterator it = other.begin(); it != other.end(); ++it) { |
116 const Entry* entry = Get(it->first); | 139 const Entry* entry = Get(it->first); |
117 if (!entry || it->second.has_higher_priority_than(*entry)) { | 140 if (!entry || it->second.has_higher_priority_than(*entry)) { |
118 Set(it->first, it->second.level, it->second.scope, | 141 Set(it->first, it->second.level, it->second.scope, |
119 it->second.value->DeepCopy(), it->second.external_data_fetcher ? | 142 it->second.value->DeepCopy(), |
120 new ExternalDataFetcher(*it->second.external_data_fetcher) : | 143 it->second.external_data_fetcher |
121 NULL); | 144 ? new ExternalDataFetcher(*it->second.external_data_fetcher) |
145 : NULL, | |
146 it->second.source); | |
122 } | 147 } |
123 } | 148 } |
124 } | 149 } |
125 | 150 |
126 void PolicyMap::LoadFrom( | 151 void PolicyMap::LoadFrom( |
127 const base::DictionaryValue* policies, | 152 const base::DictionaryValue* policies, |
128 PolicyLevel level, | 153 PolicyLevel level, |
129 PolicyScope scope) { | 154 PolicyScope scope) { |
155 LoadFrom(policies, level, scope, POLICY_SOURCE_DEFAULT); | |
156 } | |
157 | |
158 void PolicyMap::LoadFrom(const base::DictionaryValue* policies, | |
159 PolicyLevel level, | |
160 PolicyScope scope, | |
161 PolicySource source) { | |
130 for (base::DictionaryValue::Iterator it(*policies); | 162 for (base::DictionaryValue::Iterator it(*policies); |
131 !it.IsAtEnd(); it.Advance()) { | 163 !it.IsAtEnd(); it.Advance()) { |
132 Set(it.key(), level, scope, it.value().DeepCopy(), NULL); | 164 Set(it.key(), level, scope, it.value().DeepCopy(), NULL, source); |
133 } | 165 } |
134 } | 166 } |
135 | 167 |
136 void PolicyMap::GetDifferingKeys(const PolicyMap& other, | 168 void PolicyMap::GetDifferingKeys(const PolicyMap& other, |
137 std::set<std::string>* differing_keys) const { | 169 std::set<std::string>* differing_keys) const { |
138 // Walk over the maps in lockstep, adding everything that is different. | 170 // Walk over the maps in lockstep, adding everything that is different. |
139 const_iterator iter_this(begin()); | 171 const_iterator iter_this(begin()); |
140 const_iterator iter_other(other.begin()); | 172 const_iterator iter_other(other.begin()); |
141 while (iter_this != end() && iter_other != other.end()) { | 173 while (iter_this != end() && iter_other != other.end()) { |
142 const int diff = iter_this->first.compare(iter_other->first); | 174 const int diff = iter_this->first.compare(iter_other->first); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 map_.clear(); | 232 map_.clear(); |
201 } | 233 } |
202 | 234 |
203 // static | 235 // static |
204 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, | 236 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, |
205 const PolicyMap::PolicyMapType::value_type& b) { | 237 const PolicyMap::PolicyMapType::value_type& b) { |
206 return a.first == b.first && a.second.Equals(b.second); | 238 return a.first == b.first && a.second.Equals(b.second); |
207 } | 239 } |
208 | 240 |
209 } // namespace policy | 241 } // namespace policy |
OLD | NEW |