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 value(NULL), | 17 value(NULL), |
18 external_data_fetcher(NULL) {} | 18 external_data_fetcher(NULL), |
19 source(POLICY_SOURCE_UNKNOWN) {} | |
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 && |
50 scope == other.scope && | 52 scope == other.scope && |
53 source == other.source && // Necessary for PolicyUIHandler observers. | |
54 // They have to update when sources change. | |
51 base::Value::Equals(value, other.value) && | 55 base::Value::Equals(value, other.value) && |
52 ExternalDataFetcher::Equals(external_data_fetcher, | 56 ExternalDataFetcher::Equals(external_data_fetcher, |
53 other.external_data_fetcher); | 57 other.external_data_fetcher); |
54 } | 58 } |
55 | 59 |
56 PolicyMap::PolicyMap() { | 60 PolicyMap::PolicyMap() { |
57 } | 61 } |
58 | 62 |
59 PolicyMap::~PolicyMap() { | 63 PolicyMap::~PolicyMap() { |
60 Clear(); | 64 Clear(); |
61 } | 65 } |
62 | 66 |
63 const PolicyMap::Entry* PolicyMap::Get(const std::string& policy) const { | 67 const PolicyMap::Entry* PolicyMap::Get(const std::string& policy) const { |
64 PolicyMapType::const_iterator entry = map_.find(policy); | 68 PolicyMapType::const_iterator entry = map_.find(policy); |
65 return entry == map_.end() ? NULL : &entry->second; | 69 return entry == map_.end() ? NULL : &entry->second; |
66 } | 70 } |
67 | 71 |
68 const base::Value* PolicyMap::GetValue(const std::string& policy) const { | 72 const base::Value* PolicyMap::GetValue(const std::string& policy) const { |
69 PolicyMapType::const_iterator entry = map_.find(policy); | 73 PolicyMapType::const_iterator entry = map_.find(policy); |
70 return entry == map_.end() ? NULL : entry->second.value; | 74 return entry == map_.end() ? NULL : entry->second.value; |
71 } | 75 } |
72 | 76 |
77 void PolicyMap::SetEntrySources(PolicySource source) { | |
78 for (auto& policy : map_) { | |
Thiemo Nagel
2015/09/07 15:18:57
This is not really a "policy", it's more a "map_en
fhorschig
2015/09/07 15:59:35
Done.
| |
79 if (policy.second.source != POLICY_SOURCE_UNKNOWN) | |
Thiemo Nagel
2015/09/07 15:18:57
You can save one more line of code by inverting th
fhorschig
2015/09/07 15:59:35
Done.
| |
80 continue; | |
81 policy.second.source = source; | |
82 } | |
83 } | |
84 | |
73 void PolicyMap::Set(const std::string& policy, | 85 void PolicyMap::Set(const std::string& policy, |
74 PolicyLevel level, | 86 PolicyLevel level, |
75 PolicyScope scope, | 87 PolicyScope scope, |
76 base::Value* value, | 88 base::Value* value, |
77 ExternalDataFetcher* external_data_fetcher) { | 89 ExternalDataFetcher* external_data_fetcher) { |
90 SetWithSource(policy, level, scope, value, external_data_fetcher, | |
91 POLICY_SOURCE_UNKNOWN); | |
92 } | |
93 | |
94 void PolicyMap::SetWithSource(const std::string& policy, | |
95 PolicyLevel level, | |
96 PolicyScope scope, | |
97 base::Value* value, | |
98 ExternalDataFetcher* external_data_fetcher, | |
99 PolicySource source) { | |
78 Entry& entry = map_[policy]; | 100 Entry& entry = map_[policy]; |
79 entry.DeleteOwnedMembers(); | 101 entry.DeleteOwnedMembers(); |
80 entry.level = level; | 102 entry.level = level; |
81 entry.scope = scope; | 103 entry.scope = scope; |
104 entry.source = source; | |
82 entry.value = value; | 105 entry.value = value; |
83 entry.external_data_fetcher = external_data_fetcher; | 106 entry.external_data_fetcher = external_data_fetcher; |
84 } | 107 } |
85 | 108 |
86 void PolicyMap::Erase(const std::string& policy) { | 109 void PolicyMap::Erase(const std::string& policy) { |
87 PolicyMapType::iterator it = map_.find(policy); | 110 PolicyMapType::iterator it = map_.find(policy); |
88 if (it != map_.end()) { | 111 if (it != map_.end()) { |
89 it->second.DeleteOwnedMembers(); | 112 it->second.DeleteOwnedMembers(); |
90 map_.erase(it); | 113 map_.erase(it); |
91 } | 114 } |
92 } | 115 } |
93 | 116 |
94 void PolicyMap::Swap(PolicyMap* other) { | 117 void PolicyMap::Swap(PolicyMap* other) { |
95 map_.swap(other->map_); | 118 map_.swap(other->map_); |
96 } | 119 } |
97 | 120 |
98 void PolicyMap::CopyFrom(const PolicyMap& other) { | 121 void PolicyMap::CopyFrom(const PolicyMap& other) { |
99 Clear(); | 122 Clear(); |
100 for (const_iterator it = other.begin(); it != other.end(); ++it) { | 123 for (const_iterator it = other.begin(); it != other.end(); ++it) { |
101 const Entry& entry = it->second; | 124 const Entry& entry = it->second; |
102 Set(it->first, entry.level, entry.scope, | 125 SetWithSource(it->first, entry.level, entry.scope, entry.value->DeepCopy(), |
103 entry.value->DeepCopy(), entry.external_data_fetcher ? | 126 entry.external_data_fetcher |
104 new ExternalDataFetcher(*entry.external_data_fetcher) : NULL); | 127 ? new ExternalDataFetcher(*entry.external_data_fetcher) |
128 : NULL, | |
129 entry.source); | |
105 } | 130 } |
106 } | 131 } |
107 | 132 |
108 scoped_ptr<PolicyMap> PolicyMap::DeepCopy() const { | 133 scoped_ptr<PolicyMap> PolicyMap::DeepCopy() const { |
109 PolicyMap* copy = new PolicyMap(); | 134 PolicyMap* copy = new PolicyMap(); |
110 copy->CopyFrom(*this); | 135 copy->CopyFrom(*this); |
111 return make_scoped_ptr(copy); | 136 return make_scoped_ptr(copy); |
112 } | 137 } |
113 | 138 |
114 void PolicyMap::MergeFrom(const PolicyMap& other) { | 139 void PolicyMap::MergeFrom(const PolicyMap& other) { |
115 for (const_iterator it = other.begin(); it != other.end(); ++it) { | 140 for (const_iterator it = other.begin(); it != other.end(); ++it) { |
116 const Entry* entry = Get(it->first); | 141 const Entry* entry = Get(it->first); |
117 if (!entry || it->second.has_higher_priority_than(*entry)) { | 142 if (!entry || it->second.has_higher_priority_than(*entry)) { |
118 Set(it->first, it->second.level, it->second.scope, | 143 SetWithSource(it->first, it->second.level, it->second.scope, |
119 it->second.value->DeepCopy(), it->second.external_data_fetcher ? | 144 it->second.value->DeepCopy(), |
120 new ExternalDataFetcher(*it->second.external_data_fetcher) : | 145 it->second.external_data_fetcher |
121 NULL); | 146 ? new ExternalDataFetcher( |
147 *it->second.external_data_fetcher) | |
148 : NULL, | |
149 it->second.source); | |
122 } | 150 } |
123 } | 151 } |
124 } | 152 } |
125 | 153 |
126 void PolicyMap::LoadFrom( | 154 void PolicyMap::LoadFrom( |
127 const base::DictionaryValue* policies, | 155 const base::DictionaryValue* policies, |
128 PolicyLevel level, | 156 PolicyLevel level, |
129 PolicyScope scope) { | 157 PolicyScope scope) { |
130 for (base::DictionaryValue::Iterator it(*policies); | 158 for (base::DictionaryValue::Iterator it(*policies); |
131 !it.IsAtEnd(); it.Advance()) { | 159 !it.IsAtEnd(); it.Advance()) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 map_.clear(); | 228 map_.clear(); |
201 } | 229 } |
202 | 230 |
203 // static | 231 // static |
204 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, | 232 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, |
205 const PolicyMap::PolicyMapType::value_type& b) { | 233 const PolicyMap::PolicyMapType::value_type& b) { |
206 return a.first == b.first && a.second.Equals(b.second); | 234 return a.first == b.first && a.second.Equals(b.second); |
207 } | 235 } |
208 | 236 |
209 } // namespace policy | 237 } // namespace policy |
OLD | NEW |