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. | |
bartfab (slow)
2015/09/14 14:42:27
You can leave the comment here if you want but in
fhorschig
2015/09/16 13:52:05
This value is far less important for the Equality.
| |
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::ApplySourceToEntries(PolicySource source) { | |
78 for (auto& map_entry : map_) { | |
79 if (map_entry.second.source == POLICY_SOURCE_UNKNOWN) | |
80 map_entry.second.source = source; | |
81 } | |
82 } | |
83 | |
73 void PolicyMap::Set(const std::string& policy, | 84 void PolicyMap::Set(const std::string& policy, |
74 PolicyLevel level, | 85 PolicyLevel level, |
75 PolicyScope scope, | 86 PolicyScope scope, |
76 base::Value* value, | 87 base::Value* value, |
77 ExternalDataFetcher* external_data_fetcher) { | 88 ExternalDataFetcher* external_data_fetcher) { |
89 SetWithSource(policy, level, scope, value, external_data_fetcher, | |
90 POLICY_SOURCE_UNKNOWN); | |
91 } | |
92 | |
93 void PolicyMap::SetWithSource(const std::string& policy, | |
94 PolicyLevel level, | |
95 PolicyScope scope, | |
96 base::Value* value, | |
97 ExternalDataFetcher* external_data_fetcher, | |
98 PolicySource source) { | |
78 Entry& entry = map_[policy]; | 99 Entry& entry = map_[policy]; |
79 entry.DeleteOwnedMembers(); | 100 entry.DeleteOwnedMembers(); |
80 entry.level = level; | 101 entry.level = level; |
81 entry.scope = scope; | 102 entry.scope = scope; |
103 entry.source = source; | |
82 entry.value = value; | 104 entry.value = value; |
83 entry.external_data_fetcher = external_data_fetcher; | 105 entry.external_data_fetcher = external_data_fetcher; |
84 } | 106 } |
85 | 107 |
86 void PolicyMap::Erase(const std::string& policy) { | 108 void PolicyMap::Erase(const std::string& policy) { |
87 PolicyMapType::iterator it = map_.find(policy); | 109 PolicyMapType::iterator it = map_.find(policy); |
88 if (it != map_.end()) { | 110 if (it != map_.end()) { |
89 it->second.DeleteOwnedMembers(); | 111 it->second.DeleteOwnedMembers(); |
90 map_.erase(it); | 112 map_.erase(it); |
91 } | 113 } |
92 } | 114 } |
93 | 115 |
94 void PolicyMap::Swap(PolicyMap* other) { | 116 void PolicyMap::Swap(PolicyMap* other) { |
95 map_.swap(other->map_); | 117 map_.swap(other->map_); |
96 } | 118 } |
97 | 119 |
98 void PolicyMap::CopyFrom(const PolicyMap& other) { | 120 void PolicyMap::CopyFrom(const PolicyMap& other) { |
99 Clear(); | 121 Clear(); |
100 for (const_iterator it = other.begin(); it != other.end(); ++it) { | 122 for (const_iterator it = other.begin(); it != other.end(); ++it) { |
101 const Entry& entry = it->second; | 123 const Entry& entry = it->second; |
102 Set(it->first, entry.level, entry.scope, | 124 SetWithSource(it->first, entry.level, entry.scope, entry.value->DeepCopy(), |
103 entry.value->DeepCopy(), entry.external_data_fetcher ? | 125 entry.external_data_fetcher |
104 new ExternalDataFetcher(*entry.external_data_fetcher) : NULL); | 126 ? new ExternalDataFetcher(*entry.external_data_fetcher) |
127 : NULL, | |
bartfab (slow)
2015/09/14 14:42:27
Nit: s/NULL/nullptr/
fhorschig
2015/09/16 13:52:05
Done.
| |
128 entry.source); | |
105 } | 129 } |
106 } | 130 } |
107 | 131 |
108 scoped_ptr<PolicyMap> PolicyMap::DeepCopy() const { | 132 scoped_ptr<PolicyMap> PolicyMap::DeepCopy() const { |
109 PolicyMap* copy = new PolicyMap(); | 133 PolicyMap* copy = new PolicyMap(); |
110 copy->CopyFrom(*this); | 134 copy->CopyFrom(*this); |
111 return make_scoped_ptr(copy); | 135 return make_scoped_ptr(copy); |
112 } | 136 } |
113 | 137 |
114 void PolicyMap::MergeFrom(const PolicyMap& other) { | 138 void PolicyMap::MergeFrom(const PolicyMap& other) { |
115 for (const_iterator it = other.begin(); it != other.end(); ++it) { | 139 for (const_iterator it = other.begin(); it != other.end(); ++it) { |
116 const Entry* entry = Get(it->first); | 140 const Entry* entry = Get(it->first); |
117 if (!entry || it->second.has_higher_priority_than(*entry)) { | 141 if (!entry || it->second.has_higher_priority_than(*entry)) { |
118 Set(it->first, it->second.level, it->second.scope, | 142 SetWithSource(it->first, it->second.level, it->second.scope, |
119 it->second.value->DeepCopy(), it->second.external_data_fetcher ? | 143 it->second.value->DeepCopy(), |
120 new ExternalDataFetcher(*it->second.external_data_fetcher) : | 144 it->second.external_data_fetcher |
121 NULL); | 145 ? new ExternalDataFetcher( |
146 *it->second.external_data_fetcher) | |
147 : NULL, | |
bartfab (slow)
2015/09/14 14:42:27
Nit: s/NULL/nullptr/
fhorschig
2015/09/16 13:52:05
Done.
| |
148 it->second.source); | |
122 } | 149 } |
123 } | 150 } |
124 } | 151 } |
125 | 152 |
126 void PolicyMap::LoadFrom( | 153 void PolicyMap::LoadFrom( |
127 const base::DictionaryValue* policies, | 154 const base::DictionaryValue* policies, |
128 PolicyLevel level, | 155 PolicyLevel level, |
129 PolicyScope scope) { | 156 PolicyScope scope) { |
130 for (base::DictionaryValue::Iterator it(*policies); | 157 for (base::DictionaryValue::Iterator it(*policies); |
131 !it.IsAtEnd(); it.Advance()) { | 158 !it.IsAtEnd(); it.Advance()) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 map_.clear(); | 227 map_.clear(); |
201 } | 228 } |
202 | 229 |
203 // static | 230 // static |
204 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, | 231 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, |
205 const PolicyMap::PolicyMapType::value_type& b) { | 232 const PolicyMap::PolicyMapType::value_type& b) { |
206 return a.first == b.first && a.second.Equals(b.second); | 233 return a.first == b.first && a.second.Equals(b.second); |
207 } | 234 } |
208 | 235 |
209 } // namespace policy | 236 } // namespace policy |
OLD | NEW |