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

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: Increased ID range for Messages. Created 5 years, 3 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"
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 observer updates.
Thiemo Nagel 2015/09/04 20:15:54 It would be nice to mention which observer benefit
fhorschig 2015/09/07 14:09:34 Done.
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
76 void PolicyMap::SetEntrySources(PolicySource source) {
77 for (PolicyMapType::iterator it = map_.begin(); it != map_.end(); ++it) {
Thiemo Nagel 2015/09/04 20:15:54 Please use range loop.
fhorschig 2015/09/07 14:09:34 Done.
78 if (source != POLICY_SOURCE_PLATFORM &&
79 (it->second.source == POLICY_SOURCE_ENTERPRISE_DEFAULT ||
Thiemo Nagel 2015/09/04 20:15:54 I still don't get what's happening here. It seems
fhorschig 2015/09/07 14:09:34 Currently, Yes. (If policies were set multiple tim
80 it->second.source == POLICY_SOURCE_ENFORCED))
81 continue; // Enterprise and enforced policies can only be overridden by
82 // platform-specific configurations (Like debug JSONs).
83 it->second.source = source;
84 }
85 }
86
73 void PolicyMap::Set(const std::string& policy, 87 void PolicyMap::Set(const std::string& policy,
74 PolicyLevel level, 88 PolicyLevel level,
75 PolicyScope scope, 89 PolicyScope scope,
76 base::Value* value, 90 base::Value* value,
77 ExternalDataFetcher* external_data_fetcher) { 91 ExternalDataFetcher* external_data_fetcher) {
92 SetWithSource(policy, level, scope, value, external_data_fetcher,
93 POLICY_SOURCE_UNKNOWN);
94 }
95
96 void PolicyMap::SetWithSource(const std::string& policy,
97 PolicyLevel level,
98 PolicyScope scope,
99 base::Value* value,
100 ExternalDataFetcher* external_data_fetcher,
101 PolicySource source) {
78 Entry& entry = map_[policy]; 102 Entry& entry = map_[policy];
79 entry.DeleteOwnedMembers(); 103 entry.DeleteOwnedMembers();
80 entry.level = level; 104 entry.level = level;
81 entry.scope = scope; 105 entry.scope = scope;
106 entry.source = source;
82 entry.value = value; 107 entry.value = value;
83 entry.external_data_fetcher = external_data_fetcher; 108 entry.external_data_fetcher = external_data_fetcher;
84 } 109 }
85 110
86 void PolicyMap::Erase(const std::string& policy) { 111 void PolicyMap::Erase(const std::string& policy) {
87 PolicyMapType::iterator it = map_.find(policy); 112 PolicyMapType::iterator it = map_.find(policy);
88 if (it != map_.end()) { 113 if (it != map_.end()) {
89 it->second.DeleteOwnedMembers(); 114 it->second.DeleteOwnedMembers();
90 map_.erase(it); 115 map_.erase(it);
91 } 116 }
92 } 117 }
93 118
94 void PolicyMap::Swap(PolicyMap* other) { 119 void PolicyMap::Swap(PolicyMap* other) {
95 map_.swap(other->map_); 120 map_.swap(other->map_);
96 } 121 }
97 122
98 void PolicyMap::CopyFrom(const PolicyMap& other) { 123 void PolicyMap::CopyFrom(const PolicyMap& other) {
99 Clear(); 124 Clear();
100 for (const_iterator it = other.begin(); it != other.end(); ++it) { 125 for (const_iterator it = other.begin(); it != other.end(); ++it) {
101 const Entry& entry = it->second; 126 const Entry& entry = it->second;
102 Set(it->first, entry.level, entry.scope, 127 SetWithSource(it->first, entry.level, entry.scope, entry.value->DeepCopy(),
103 entry.value->DeepCopy(), entry.external_data_fetcher ? 128 entry.external_data_fetcher
104 new ExternalDataFetcher(*entry.external_data_fetcher) : NULL); 129 ? new ExternalDataFetcher(*entry.external_data_fetcher)
130 : NULL,
131 entry.source);
105 } 132 }
106 } 133 }
107 134
108 scoped_ptr<PolicyMap> PolicyMap::DeepCopy() const { 135 scoped_ptr<PolicyMap> PolicyMap::DeepCopy() const {
109 PolicyMap* copy = new PolicyMap(); 136 PolicyMap* copy = new PolicyMap();
110 copy->CopyFrom(*this); 137 copy->CopyFrom(*this);
111 return make_scoped_ptr(copy); 138 return make_scoped_ptr(copy);
112 } 139 }
113 140
114 void PolicyMap::MergeFrom(const PolicyMap& other) { 141 void PolicyMap::MergeFrom(const PolicyMap& other) {
115 for (const_iterator it = other.begin(); it != other.end(); ++it) { 142 for (const_iterator it = other.begin(); it != other.end(); ++it) {
116 const Entry* entry = Get(it->first); 143 const Entry* entry = Get(it->first);
117 if (!entry || it->second.has_higher_priority_than(*entry)) { 144 if (!entry || it->second.has_higher_priority_than(*entry)) {
118 Set(it->first, it->second.level, it->second.scope, 145 SetWithSource(it->first, it->second.level, it->second.scope,
119 it->second.value->DeepCopy(), it->second.external_data_fetcher ? 146 it->second.value->DeepCopy(),
120 new ExternalDataFetcher(*it->second.external_data_fetcher) : 147 it->second.external_data_fetcher
121 NULL); 148 ? new ExternalDataFetcher(
149 *it->second.external_data_fetcher)
150 : NULL,
151 it->second.source);
122 } 152 }
123 } 153 }
124 } 154 }
125 155
126 void PolicyMap::LoadFrom( 156 void PolicyMap::LoadFrom(
127 const base::DictionaryValue* policies, 157 const base::DictionaryValue* policies,
128 PolicyLevel level, 158 PolicyLevel level,
129 PolicyScope scope) { 159 PolicyScope scope) {
130 for (base::DictionaryValue::Iterator it(*policies); 160 for (base::DictionaryValue::Iterator it(*policies);
131 !it.IsAtEnd(); it.Advance()) { 161 !it.IsAtEnd(); it.Advance()) {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 map_.clear(); 230 map_.clear();
201 } 231 }
202 232
203 // static 233 // static
204 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a, 234 bool PolicyMap::MapEntryEquals(const PolicyMap::PolicyMapType::value_type& a,
205 const PolicyMap::PolicyMapType::value_type& b) { 235 const PolicyMap::PolicyMapType::value_type& b) {
206 return a.first == b.first && a.second.Equals(b.second); 236 return a.first == b.first && a.second.Equals(b.second);
207 } 237 }
208 238
209 } // namespace policy 239 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698