Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: components/policy/core/common/policy_map.cc

Issue 1304843004: Add source column to chrome://policy showing the origins of policies. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed another test. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 10 matching lines...) Expand all
21 delete value; 21 delete value;
22 value = NULL; 22 value = NULL;
23 delete external_data_fetcher; 23 delete external_data_fetcher;
24 external_data_fetcher = NULL; 24 external_data_fetcher = NULL;
25 } 25 }
26 26
27 scoped_ptr<PolicyMap::Entry> PolicyMap::Entry::DeepCopy() const { 27 scoped_ptr<PolicyMap::Entry> PolicyMap::Entry::DeepCopy() const {
28 scoped_ptr<Entry> copy(new Entry); 28 scoped_ptr<Entry> copy(new Entry);
29 copy->level = level; 29 copy->level = level;
30 copy->scope = scope; 30 copy->scope = scope;
31 copy->source = source;
31 if (value) 32 if (value)
32 copy->value = value->DeepCopy(); 33 copy->value = value->DeepCopy();
33 if (external_data_fetcher) { 34 if (external_data_fetcher) {
34 copy->external_data_fetcher = 35 copy->external_data_fetcher =
35 new ExternalDataFetcher(*external_data_fetcher); 36 new ExternalDataFetcher(*external_data_fetcher);
36 } 37 }
37 return copy.Pass(); 38 return copy.Pass();
38 } 39 }
39 40
40 bool PolicyMap::Entry::has_higher_priority_than( 41 bool PolicyMap::Entry::has_higher_priority_than(
41 const PolicyMap::Entry& other) const { 42 const PolicyMap::Entry& other) const {
42 if (level == other.level) 43 if (level == other.level)
43 return scope > other.scope; 44 return scope > other.scope;
44 else 45 else
45 return level > other.level; 46 return level > other.level;
46 } 47 }
47 48
48 bool PolicyMap::Entry::Equals(const PolicyMap::Entry& other) const { 49 bool PolicyMap::Entry::Equals(const PolicyMap::Entry& other) const {
49 return level == other.level && 50 return level == other.level &&
50 scope == other.scope && 51 scope == other.scope &&
52 source == other.source && // Necessary for PolicyUIHandler observers.
53 // They have to update when sources change.
51 base::Value::Equals(value, other.value) && 54 base::Value::Equals(value, other.value) &&
52 ExternalDataFetcher::Equals(external_data_fetcher, 55 ExternalDataFetcher::Equals(external_data_fetcher,
53 other.external_data_fetcher); 56 other.external_data_fetcher);
54 } 57 }
55 58
56 PolicyMap::PolicyMap() { 59 PolicyMap::PolicyMap() {
57 } 60 }
58 61
59 PolicyMap::~PolicyMap() { 62 PolicyMap::~PolicyMap() {
60 Clear(); 63 Clear();
61 } 64 }
62 65
63 const PolicyMap::Entry* PolicyMap::Get(const std::string& policy) const { 66 const PolicyMap::Entry* PolicyMap::Get(const std::string& policy) const {
64 PolicyMapType::const_iterator entry = map_.find(policy); 67 PolicyMapType::const_iterator entry = map_.find(policy);
65 return entry == map_.end() ? NULL : &entry->second; 68 return entry == map_.end() ? NULL : &entry->second;
66 } 69 }
67 70
68 const base::Value* PolicyMap::GetValue(const std::string& policy) const { 71 const base::Value* PolicyMap::GetValue(const std::string& policy) const {
69 PolicyMapType::const_iterator entry = map_.find(policy); 72 PolicyMapType::const_iterator entry = map_.find(policy);
70 return entry == map_.end() ? NULL : entry->second.value; 73 return entry == map_.end() ? NULL : entry->second.value;
71 } 74 }
72 75
73 void PolicyMap::Set(const std::string& policy, 76 void PolicyMap::Set(const std::string& policy,
74 PolicyLevel level, 77 PolicyLevel level,
75 PolicyScope scope, 78 PolicyScope scope,
79 PolicySource source,
76 base::Value* value, 80 base::Value* value,
77 ExternalDataFetcher* external_data_fetcher) { 81 ExternalDataFetcher* external_data_fetcher) {
78 Entry& entry = map_[policy]; 82 Entry& entry = map_[policy];
79 entry.DeleteOwnedMembers(); 83 entry.DeleteOwnedMembers();
80 entry.level = level; 84 entry.level = level;
81 entry.scope = scope; 85 entry.scope = scope;
86 entry.source = source;
82 entry.value = value; 87 entry.value = value;
83 entry.external_data_fetcher = external_data_fetcher; 88 entry.external_data_fetcher = external_data_fetcher;
84 } 89 }
85 90
86 void PolicyMap::Erase(const std::string& policy) { 91 void PolicyMap::Erase(const std::string& policy) {
87 PolicyMapType::iterator it = map_.find(policy); 92 PolicyMapType::iterator it = map_.find(policy);
88 if (it != map_.end()) { 93 if (it != map_.end()) {
89 it->second.DeleteOwnedMembers(); 94 it->second.DeleteOwnedMembers();
90 map_.erase(it); 95 map_.erase(it);
91 } 96 }
92 } 97 }
93 98
94 void PolicyMap::Swap(PolicyMap* other) { 99 void PolicyMap::Swap(PolicyMap* other) {
95 map_.swap(other->map_); 100 map_.swap(other->map_);
96 } 101 }
97 102
98 void PolicyMap::CopyFrom(const PolicyMap& other) { 103 void PolicyMap::CopyFrom(const PolicyMap& other) {
99 Clear(); 104 Clear();
100 for (const_iterator it = other.begin(); it != other.end(); ++it) { 105 for (const_iterator it = other.begin(); it != other.end(); ++it) {
101 const Entry& entry = it->second; 106 const Entry& entry = it->second;
102 Set(it->first, entry.level, entry.scope, 107 Set(it->first, entry.level, entry.scope, entry.source,
103 entry.value->DeepCopy(), entry.external_data_fetcher ? 108 entry.value->DeepCopy(),
104 new ExternalDataFetcher(*entry.external_data_fetcher) : NULL); 109 entry.external_data_fetcher
110 ? new ExternalDataFetcher(*entry.external_data_fetcher)
111 : nullptr);
105 } 112 }
106 } 113 }
107 114
108 scoped_ptr<PolicyMap> PolicyMap::DeepCopy() const { 115 scoped_ptr<PolicyMap> PolicyMap::DeepCopy() const {
109 PolicyMap* copy = new PolicyMap(); 116 PolicyMap* copy = new PolicyMap();
110 copy->CopyFrom(*this); 117 copy->CopyFrom(*this);
111 return make_scoped_ptr(copy); 118 return make_scoped_ptr(copy);
112 } 119 }
113 120
114 void PolicyMap::MergeFrom(const PolicyMap& other) { 121 void PolicyMap::MergeFrom(const PolicyMap& other) {
115 for (const_iterator it = other.begin(); it != other.end(); ++it) { 122 for (const_iterator it = other.begin(); it != other.end(); ++it) {
116 const Entry* entry = Get(it->first); 123 const Entry* entry = Get(it->first);
117 if (!entry || it->second.has_higher_priority_than(*entry)) { 124 if (!entry || it->second.has_higher_priority_than(*entry)) {
118 Set(it->first, it->second.level, it->second.scope, 125 Set(it->first, it->second.level, it->second.scope, it->second.source,
119 it->second.value->DeepCopy(), it->second.external_data_fetcher ? 126 it->second.value->DeepCopy(),
120 new ExternalDataFetcher(*it->second.external_data_fetcher) : 127 it->second.external_data_fetcher
121 NULL); 128 ? new ExternalDataFetcher(
129 *it->second.external_data_fetcher)
130 : nullptr);
122 } 131 }
123 } 132 }
124 } 133 }
125 134
126 void PolicyMap::LoadFrom( 135 void PolicyMap::LoadFrom(
127 const base::DictionaryValue* policies, 136 const base::DictionaryValue* policies,
128 PolicyLevel level, 137 PolicyLevel level,
129 PolicyScope scope) { 138 PolicyScope scope,
139 PolicySource source) {
130 for (base::DictionaryValue::Iterator it(*policies); 140 for (base::DictionaryValue::Iterator it(*policies);
131 !it.IsAtEnd(); it.Advance()) { 141 !it.IsAtEnd(); it.Advance()) {
132 Set(it.key(), level, scope, it.value().DeepCopy(), NULL); 142 Set(it.key(), level, scope, source, it.value().DeepCopy(), nullptr);
133 } 143 }
134 } 144 }
135 145
136 void PolicyMap::GetDifferingKeys(const PolicyMap& other, 146 void PolicyMap::GetDifferingKeys(const PolicyMap& other,
137 std::set<std::string>* differing_keys) const { 147 std::set<std::string>* differing_keys) const {
138 // Walk over the maps in lockstep, adding everything that is different. 148 // Walk over the maps in lockstep, adding everything that is different.
139 const_iterator iter_this(begin()); 149 const_iterator iter_this(begin());
140 const_iterator iter_other(other.begin()); 150 const_iterator iter_other(other.begin());
141 while (iter_this != end() && iter_other != other.end()) { 151 while (iter_this != end() && iter_other != other.end()) {
142 const int diff = iter_this->first.compare(iter_other->first); 152 const int diff = iter_this->first.compare(iter_other->first);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 map_.clear(); 210 map_.clear();
201 } 211 }
202 212
203 // static 213 // static
204 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, 214 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a,
205 const PolicyMap::PolicyMapType::value_type& b) { 215 const PolicyMap::PolicyMapType::value_type& b) {
206 return a.first == b.first && a.second.Equals(b.second); 216 return a.first == b.first && a.second.Equals(b.second);
207 } 217 }
208 218
209 } // namespace policy 219 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/policy_map.h ('k') | components/policy/core/common/policy_map_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698