| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/policy/policy_map.h" | 5 #include "chrome/browser/policy/policy_map.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "policy/policy_constants.h" | 10 #include "policy/policy_constants.h" |
| 11 | 11 |
| 12 namespace policy { | 12 namespace policy { |
| 13 | 13 |
| 14 bool PolicyMap::Entry::has_higher_priority_than( |
| 15 const PolicyMap::Entry& other) const { |
| 16 if (level == other.level) |
| 17 return scope > other.scope; |
| 18 else |
| 19 return level > other.level; |
| 20 } |
| 21 |
| 22 bool PolicyMap::Entry::Equals(const PolicyMap::Entry& other) const { |
| 23 return level == other.level && |
| 24 scope == other.scope && |
| 25 Value::Equals(value, other.value); |
| 26 } |
| 27 |
| 14 PolicyMap::PolicyMap() { | 28 PolicyMap::PolicyMap() { |
| 15 } | 29 } |
| 16 | 30 |
| 17 PolicyMap::~PolicyMap() { | 31 PolicyMap::~PolicyMap() { |
| 18 Clear(); | 32 Clear(); |
| 19 } | 33 } |
| 20 | 34 |
| 21 const Value* PolicyMap::Get(ConfigurationPolicyType policy) const { | 35 const PolicyMap::Entry* PolicyMap::Get(const std::string& policy) const { |
| 22 PolicyMapType::const_iterator entry = map_.find(policy); | 36 PolicyMapType::const_iterator entry = map_.find(policy); |
| 23 return entry == map_.end() ? NULL : entry->second; | 37 return entry == map_.end() ? NULL : &entry->second; |
| 24 } | 38 } |
| 25 | 39 |
| 26 void PolicyMap::Set(ConfigurationPolicyType policy, Value* value) { | 40 const Value* PolicyMap::GetValue(const std::string& policy) const { |
| 27 std::swap(map_[policy], value); | 41 PolicyMapType::const_iterator entry = map_.find(policy); |
| 28 delete value; | 42 return entry == map_.end() ? NULL : entry->second.value; |
| 29 } | 43 } |
| 30 | 44 |
| 31 void PolicyMap::Erase(ConfigurationPolicyType policy) { | 45 void PolicyMap::Set(const std::string& policy, |
| 32 const const_iterator entry = map_.find(policy); | 46 PolicyLevel level, |
| 33 if (entry != map_.end()) { | 47 PolicyScope scope, |
| 34 delete entry->second; | 48 Value* value) { |
| 35 map_.erase(entry->first); | 49 Entry& entry = map_[policy]; |
| 50 delete entry.value; |
| 51 entry.level = level; |
| 52 entry.scope = scope; |
| 53 entry.value = value; |
| 54 } |
| 55 |
| 56 void PolicyMap::Erase(const std::string& policy) { |
| 57 PolicyMapType::iterator it = map_.find(policy); |
| 58 if (it != map_.end()) { |
| 59 delete it->second.value; |
| 60 map_.erase(it); |
| 36 } | 61 } |
| 37 } | 62 } |
| 38 | 63 |
| 39 void PolicyMap::Swap(PolicyMap* other) { | 64 void PolicyMap::Swap(PolicyMap* other) { |
| 40 map_.swap(other->map_); | 65 map_.swap(other->map_); |
| 41 } | 66 } |
| 42 | 67 |
| 43 void PolicyMap::CopyFrom(const PolicyMap& other) { | 68 void PolicyMap::CopyFrom(const PolicyMap& other) { |
| 44 Clear(); | 69 Clear(); |
| 45 for (const_iterator i = other.begin(); i != other.end(); ++i) { | 70 for (const_iterator it = other.begin(); it != other.end(); ++it) { |
| 46 Set(i->first, i->second->DeepCopy()); | 71 const Entry& entry = it->second; |
| 72 Set(it->first, entry.level, entry.scope, entry.value->DeepCopy()); |
| 47 } | 73 } |
| 48 } | 74 } |
| 49 | 75 |
| 50 void PolicyMap::MergeFrom(const PolicyMap& other) { | 76 void PolicyMap::MergeFrom(const PolicyMap& other) { |
| 51 for (const_iterator i = other.begin(); i != other.end(); ++i) { | 77 for (const_iterator it = other.begin(); it != other.end(); ++it) { |
| 52 if (!Get(i->first)) | 78 const Entry* entry = Get(it->first); |
| 53 Set(i->first, i->second->DeepCopy()); | 79 if (!entry || it->second.has_higher_priority_than(*entry)) { |
| 80 Set(it->first, it->second.level, it->second.scope, |
| 81 it->second.value->DeepCopy()); |
| 82 } |
| 54 } | 83 } |
| 55 } | 84 } |
| 56 | 85 |
| 57 void PolicyMap::LoadFrom( | 86 void PolicyMap::LoadFrom( |
| 58 const DictionaryValue* policies, | 87 const DictionaryValue* policies, |
| 59 const PolicyDefinitionList* list) { | 88 const PolicyDefinitionList* list, |
| 89 PolicyLevel level, |
| 90 PolicyScope scope) { |
| 60 const PolicyDefinitionList::Entry* entry; | 91 const PolicyDefinitionList::Entry* entry; |
| 61 for (entry = list->begin; entry != list->end; ++entry) { | 92 for (entry = list->begin; entry != list->end; ++entry) { |
| 62 Value* value; | 93 Value* value; |
| 63 if (policies->Get(entry->name, &value)) | 94 if (policies->Get(entry->name, &value)) |
| 64 Set(entry->policy_type, value->DeepCopy()); | 95 Set(entry->name, level, scope, value->DeepCopy()); |
| 96 } |
| 97 } |
| 98 |
| 99 void PolicyMap::GetDifferingKeys(const PolicyMap& other, |
| 100 std::set<std::string>* differing_keys) const { |
| 101 // Walk over the maps in lockstep, adding everything that is different. |
| 102 const_iterator iter_this(begin()); |
| 103 const_iterator iter_other(other.begin()); |
| 104 while (iter_this != end() && iter_other != other.end()) { |
| 105 const int diff = iter_this->first.compare(iter_other->first); |
| 106 if (diff == 0) { |
| 107 if (!iter_this->second.Equals(iter_other->second)) |
| 108 differing_keys->insert(iter_this->first); |
| 109 ++iter_this; |
| 110 ++iter_other; |
| 111 } else if (diff < 0) { |
| 112 differing_keys->insert(iter_this->first); |
| 113 ++iter_this; |
| 114 } else { |
| 115 differing_keys->insert(iter_other->first); |
| 116 ++iter_other; |
| 117 } |
| 118 } |
| 119 |
| 120 // Add the remaining entries. |
| 121 for ( ; iter_this != end(); ++iter_this) |
| 122 differing_keys->insert(iter_this->first); |
| 123 for ( ; iter_other != other.end(); ++iter_other) |
| 124 differing_keys->insert(iter_other->first); |
| 125 } |
| 126 |
| 127 void PolicyMap::FilterLevel(PolicyLevel level) { |
| 128 PolicyMapType::iterator iter(map_.begin()); |
| 129 while (iter != map_.end()) { |
| 130 if (iter->second.level != level) |
| 131 map_.erase(iter++); |
| 132 else |
| 133 ++iter; |
| 65 } | 134 } |
| 66 } | 135 } |
| 67 | 136 |
| 68 bool PolicyMap::Equals(const PolicyMap& other) const { | 137 bool PolicyMap::Equals(const PolicyMap& other) const { |
| 69 return other.map_.size() == map_.size() && | 138 return other.size() == size() && |
| 70 std::equal(map_.begin(), map_.end(), other.map_.begin(), MapEntryEquals); | 139 std::equal(begin(), end(), other.begin(), MapEntryEquals); |
| 71 } | 140 } |
| 72 | 141 |
| 73 bool PolicyMap::empty() const { | 142 bool PolicyMap::empty() const { |
| 74 return map_.empty(); | 143 return map_.empty(); |
| 75 } | 144 } |
| 76 | 145 |
| 77 size_t PolicyMap::size() const { | 146 size_t PolicyMap::size() const { |
| 78 return map_.size(); | 147 return map_.size(); |
| 79 } | 148 } |
| 80 | 149 |
| 81 PolicyMap::const_iterator PolicyMap::begin() const { | 150 PolicyMap::const_iterator PolicyMap::begin() const { |
| 82 return map_.begin(); | 151 return map_.begin(); |
| 83 } | 152 } |
| 84 | 153 |
| 85 PolicyMap::const_iterator PolicyMap::end() const { | 154 PolicyMap::const_iterator PolicyMap::end() const { |
| 86 return map_.end(); | 155 return map_.end(); |
| 87 } | 156 } |
| 88 | 157 |
| 89 void PolicyMap::Clear() { | 158 void PolicyMap::Clear() { |
| 90 STLDeleteValues(&map_); | 159 for (PolicyMapType::iterator it = map_.begin(); it != map_.end(); ++it) |
| 160 delete it->second.value; |
| 161 map_.clear(); |
| 91 } | 162 } |
| 92 | 163 |
| 93 // static | 164 // static |
| 94 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, | 165 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, |
| 95 const PolicyMap::PolicyMapType::value_type& b) { | 166 const PolicyMap::PolicyMapType::value_type& b) { |
| 96 return a.first == b.first && Value::Equals(a.second, b.second); | 167 return a.first == b.first && a.second.Equals(b.second); |
| 97 } | 168 } |
| 98 | 169 |
| 99 } // namespace policy | 170 } // namespace policy |
| OLD | NEW |