Chromium Code Reviews| 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 PolicyMapType::const_iterator it = map_.find(policy); |
| 50 Entry* entry = &map_[policy]; | |
| 51 if (it != map_.end()) | |
| 52 delete entry->value; | |
|
Mattias Nissler (ping if slow)
2012/01/17 09:52:45
If Entry has a default constructor that NULLs valu
Joao da Silva
2012/01/17 13:09:29
Done.
| |
| 53 entry->level = level; | |
| 54 entry->scope = scope; | |
| 55 entry->value = value; | |
| 56 } | |
| 57 | |
| 58 void PolicyMap::Erase(const std::string& policy) { | |
| 59 PolicyMapType::iterator it = map_.find(policy); | |
| 60 if (it != map_.end()) { | |
| 61 delete it->second.value; | |
| 62 map_.erase(it); | |
| 36 } | 63 } |
| 37 } | 64 } |
| 38 | 65 |
| 39 void PolicyMap::Swap(PolicyMap* other) { | 66 void PolicyMap::Swap(PolicyMap* other) { |
| 40 map_.swap(other->map_); | 67 map_.swap(other->map_); |
| 41 } | 68 } |
| 42 | 69 |
| 43 void PolicyMap::CopyFrom(const PolicyMap& other) { | 70 void PolicyMap::CopyFrom(const PolicyMap& other) { |
| 44 Clear(); | 71 Clear(); |
| 45 for (const_iterator i = other.begin(); i != other.end(); ++i) { | 72 for (const_iterator it = other.begin(); it != other.end(); ++it) { |
| 46 Set(i->first, i->second->DeepCopy()); | 73 const Entry& entry = it->second; |
| 74 Set(it->first, entry.level, entry.scope, entry.value->DeepCopy()); | |
| 47 } | 75 } |
| 48 } | 76 } |
| 49 | 77 |
| 50 void PolicyMap::MergeFrom(const PolicyMap& other) { | 78 void PolicyMap::MergeFrom(const PolicyMap& other) { |
| 51 for (const_iterator i = other.begin(); i != other.end(); ++i) { | 79 for (const_iterator it = other.begin(); it != other.end(); ++it) { |
| 52 if (!Get(i->first)) | 80 const Entry* entry = Get(it->first); |
| 53 Set(i->first, i->second->DeepCopy()); | 81 if (!entry || it->second.has_higher_priority_than(*entry)) { |
| 82 Set(it->first, it->second.level, it->second.scope, | |
| 83 it->second.value->DeepCopy()); | |
| 84 } | |
| 54 } | 85 } |
| 55 } | 86 } |
| 56 | 87 |
| 57 void PolicyMap::LoadFrom( | 88 void PolicyMap::LoadFrom( |
| 58 const DictionaryValue* policies, | 89 const DictionaryValue* policies, |
| 59 const PolicyDefinitionList* list) { | 90 const PolicyDefinitionList* list, |
| 91 PolicyLevel level, | |
| 92 PolicyScope scope) { | |
| 60 const PolicyDefinitionList::Entry* entry; | 93 const PolicyDefinitionList::Entry* entry; |
| 61 for (entry = list->begin; entry != list->end; ++entry) { | 94 for (entry = list->begin; entry != list->end; ++entry) { |
| 62 Value* value; | 95 Value* value; |
| 63 if (policies->Get(entry->name, &value)) | 96 if (policies->Get(entry->name, &value)) |
| 64 Set(entry->policy_type, value->DeepCopy()); | 97 Set(entry->name, level, scope, value->DeepCopy()); |
| 98 } | |
| 99 } | |
| 100 | |
| 101 void PolicyMap::GetDifferingKeys(const PolicyMap& other, | |
|
Mattias Nissler (ping if slow)
2012/01/17 09:52:45
I can't see who is calling this.
Joao da Silva
2012/01/17 13:09:29
This is meant to be used by clients of the future
Mattias Nissler (ping if slow)
2012/01/18 15:07:07
I can see what it's good for, but I still think sh
| |
| 102 std::set<std::string>* differing_keys) const { | |
| 103 // Walk over the maps in lockstep, adding everything that is different. | |
| 104 const_iterator iter_this(begin()); | |
| 105 const_iterator iter_other(other.begin()); | |
| 106 while (iter_this != end() && iter_other != other.end()) { | |
| 107 const int diff = iter_this->first.compare(iter_other->first); | |
| 108 if (diff == 0) { | |
| 109 if (!iter_this->second.equals(iter_other->second)) | |
| 110 differing_keys->insert(iter_this->first); | |
| 111 ++iter_this; | |
| 112 ++iter_other; | |
| 113 } else if (diff < 0) { | |
| 114 differing_keys->insert(iter_this->first); | |
| 115 ++iter_this; | |
| 116 } else { | |
| 117 differing_keys->insert(iter_other->first); | |
| 118 ++iter_other; | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 // Add the remaining entries. | |
| 123 for ( ; iter_this != end(); ++iter_this) | |
| 124 differing_keys->insert(iter_this->first); | |
| 125 for ( ; iter_other != other.end(); ++iter_other) | |
| 126 differing_keys->insert(iter_other->first); | |
| 127 } | |
| 128 | |
| 129 void PolicyMap::FilterLevel(PolicyLevel level) { | |
| 130 PolicyMapType::iterator iter(map_.begin()); | |
| 131 while (iter != map_.end()) { | |
| 132 if (iter->second.level != level) | |
| 133 map_.erase(iter++); | |
| 134 else | |
| 135 ++iter; | |
| 65 } | 136 } |
| 66 } | 137 } |
| 67 | 138 |
| 68 bool PolicyMap::Equals(const PolicyMap& other) const { | 139 bool PolicyMap::Equals(const PolicyMap& other) const { |
| 69 return other.map_.size() == map_.size() && | 140 return other.size() == size() && |
| 70 std::equal(map_.begin(), map_.end(), other.map_.begin(), MapEntryEquals); | 141 std::equal(begin(), end(), other.begin(), MapEntryEquals); |
| 71 } | 142 } |
| 72 | 143 |
| 73 bool PolicyMap::empty() const { | 144 bool PolicyMap::empty() const { |
| 74 return map_.empty(); | 145 return map_.empty(); |
| 75 } | 146 } |
| 76 | 147 |
| 77 size_t PolicyMap::size() const { | 148 size_t PolicyMap::size() const { |
| 78 return map_.size(); | 149 return map_.size(); |
| 79 } | 150 } |
| 80 | 151 |
| 81 PolicyMap::const_iterator PolicyMap::begin() const { | 152 PolicyMap::const_iterator PolicyMap::begin() const { |
| 82 return map_.begin(); | 153 return map_.begin(); |
| 83 } | 154 } |
| 84 | 155 |
| 85 PolicyMap::const_iterator PolicyMap::end() const { | 156 PolicyMap::const_iterator PolicyMap::end() const { |
| 86 return map_.end(); | 157 return map_.end(); |
| 87 } | 158 } |
| 88 | 159 |
| 89 void PolicyMap::Clear() { | 160 void PolicyMap::Clear() { |
| 90 STLDeleteValues(&map_); | 161 for (PolicyMapType::iterator it = map_.begin(); it != map_.end(); ++it) |
| 162 delete it->second.value; | |
| 163 map_.clear(); | |
| 91 } | 164 } |
| 92 | 165 |
| 93 // static | 166 // static |
| 94 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, | 167 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, |
| 95 const PolicyMap::PolicyMapType::value_type& b) { | 168 const PolicyMap::PolicyMapType::value_type& b) { |
| 96 return a.first == b.first && Value::Equals(a.second, b.second); | 169 return a.first == b.first && a.second.equals(b.second); |
| 97 } | 170 } |
| 98 | 171 |
| 99 } // namespace policy | 172 } // namespace policy |
| OLD | NEW |