OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/policy/configuration_policy_reader.h" | |
6 | |
7 #include <map> | |
8 #include <vector> | |
9 #include <set> | |
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
alphabetize
simo
2011/08/10 14:28:19
Done.
| |
10 | |
11 #include "base/utf_string_conversions.h" | |
12 #include "chrome/browser/browser_process.h" | |
13 #include "chrome/browser/policy/browser_policy_connector.h" | |
14 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
15 #include "chrome/browser/policy/configuration_policy_store_interface.h" | |
16 | |
17 namespace policy { | |
18 | |
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
explain what the class is good for.
simo
2011/08/10 14:28:19
Done.
| |
19 class ConfigurationPolicyStatusKeeper | |
20 : public ConfigurationPolicyStoreInterface { | |
21 | |
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
no newline here
simo
2011/08/10 14:28:19
Done.
| |
22 public: | |
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
indentation
simo
2011/08/10 14:28:19
Done.
| |
23 explicit ConfigurationPolicyStatusKeeper( | |
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
explicit only for one-argument constructors.
simo
2011/08/10 14:28:19
Done.
| |
24 ConfigurationPolicyProvider* provider, | |
25 bool managed); | |
26 virtual ~ConfigurationPolicyStatusKeeper(); | |
27 | |
28 // ConfigurationPolicyStoreInterface methods. | |
29 virtual void Apply(ConfigurationPolicyType policy, base::Value* value); | |
30 | |
31 // Returns a pointer to a DictionaryValue containing the status information | |
32 // of the policy |policy|. The caller acquires ownership of the returned | |
33 // value. Returns NULL if no such policy is stored in this keeper. | |
34 DictionaryValue* GetPolicyStatus(ConfigurationPolicyType policy) const; | |
35 | |
36 private: | |
37 typedef std::map<ConfigurationPolicyType, string16> PolicyNameMapType; | |
38 typedef ConfigurationPolicyProvider::PolicyDefinitionList | |
39 PolicyDefinitionList; | |
40 | |
41 // Mapping from ConfigurationPolicyType to PolicyStatusInfo*. | |
42 PolicyStatusMap policy_map_; | |
43 | |
44 // Mapping from ConfigurationPolicyType to policy names. | |
45 PolicyNameMapType policy_name_map_; | |
46 | |
47 // The level of the policies stored in this keeper (mandatory or | |
48 // recommended). | |
49 PolicyStatusInfo::PolicyLevel policy_level_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyStatusKeeper); | |
52 }; | |
53 | |
54 // ConfigurationPolicyStatusKeeper | |
55 ConfigurationPolicyStatusKeeper::ConfigurationPolicyStatusKeeper( | |
56 ConfigurationPolicyProvider* provider, | |
57 bool managed) { | |
58 | |
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
no newline here.
simo
2011/08/10 14:28:19
Done.
| |
59 const PolicyDefinitionList* policy_definition_list = | |
60 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); | |
61 | |
62 // Create a mapping from ConfigurationPolicyType's to actual policy names. | |
63 const PolicyDefinitionList::Entry* entry = policy_definition_list->begin; | |
64 for ( ; entry != policy_definition_list->end; ++entry) | |
65 policy_name_map_[entry->policy_type] = ASCIIToUTF16(entry->name); | |
66 | |
67 policy_level_ = managed ? | |
68 PolicyStatusInfo::MANDATORY : PolicyStatusInfo::RECOMMENDED; | |
69 | |
70 if (!provider->Provide(this)) | |
71 LOG(WARNING) << "Failed to get policy from provider."; | |
72 } | |
73 | |
74 ConfigurationPolicyStatusKeeper::~ConfigurationPolicyStatusKeeper() { | |
75 policy_map_.Clear(); | |
76 policy_name_map_.clear(); | |
77 } | |
78 | |
79 void ConfigurationPolicyStatusKeeper::Apply( | |
80 ConfigurationPolicyType policy, base::Value* value) { | |
81 | |
82 PolicyNameMapType::const_iterator entry = policy_name_map_.find(policy); | |
83 | |
84 // This should never happen. | |
85 if (entry == policy_name_map_.end()) { | |
86 LOG(WARNING) << "Received unknown policy from provider."; | |
87 return; | |
88 } | |
89 | |
90 // TODO(simo) actually determine whether the policy is a user or a device one | |
91 // and whether the policy could be enforced or not once this information | |
92 // is available. | |
93 PolicyStatusInfo* info = new PolicyStatusInfo(entry->second, | |
94 PolicyStatusInfo::USER, | |
95 policy_level_, | |
96 value, | |
97 PolicyStatusInfo::ENFORCED, | |
98 string16()); | |
99 policy_map_.Set(policy, info); | |
100 } | |
101 | |
102 DictionaryValue* ConfigurationPolicyStatusKeeper::GetPolicyStatus( | |
103 ConfigurationPolicyType policy) const { | |
104 const PolicyStatusInfo* info = policy_map_.Get(policy); | |
105 return info ? info->GetDictionaryValue() : NULL; | |
106 } | |
107 | |
108 // ConfigurationPolicyReader | |
109 ConfigurationPolicyReader::~ConfigurationPolicyReader() { | |
110 } | |
111 | |
112 bool ConfigurationPolicyReader::IsInitializationComplete() const { | |
113 return initialization_complete_; | |
114 } | |
115 | |
116 void ConfigurationPolicyReader::OnUpdatePolicy() { | |
117 Refresh(); | |
118 } | |
119 | |
120 void ConfigurationPolicyReader::OnProviderGoingAway() { | |
121 provider_ = NULL; | |
122 } | |
123 | |
124 // static | |
125 ConfigurationPolicyReader* | |
126 ConfigurationPolicyReader::CreateManagedPlatformPolicyReader(bool managed) { | |
127 BrowserPolicyConnector* connector = | |
128 g_browser_process->browser_policy_connector(); | |
129 return new ConfigurationPolicyReader( | |
130 connector->GetManagedPlatformProvider(), managed); | |
131 } | |
132 | |
133 // static | |
134 ConfigurationPolicyReader* | |
135 ConfigurationPolicyReader::CreateManagedCloudPolicyReader(bool managed) { | |
136 BrowserPolicyConnector* connector = | |
137 g_browser_process->browser_policy_connector(); | |
138 return new ConfigurationPolicyReader( | |
139 connector->GetManagedCloudProvider(), managed); | |
140 } | |
141 | |
142 // static | |
143 ConfigurationPolicyReader* | |
144 ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader( | |
145 bool managed) { | |
146 BrowserPolicyConnector* connector = | |
147 g_browser_process->browser_policy_connector(); | |
148 return new ConfigurationPolicyReader( | |
149 connector->GetRecommendedPlatformProvider(), managed); | |
150 } | |
151 | |
152 // static | |
153 ConfigurationPolicyReader* | |
154 ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader( | |
155 bool managed) { | |
156 BrowserPolicyConnector* connector = | |
157 g_browser_process->browser_policy_connector(); | |
158 return new ConfigurationPolicyReader( | |
159 connector->GetRecommendedCloudProvider(), managed); | |
160 } | |
161 | |
162 DictionaryValue* ConfigurationPolicyReader::GetPolicyStatus( | |
163 ConfigurationPolicyType policy) const { | |
164 return policy_keeper_.get()->GetPolicyStatus(policy); | |
165 } | |
166 | |
167 ConfigurationPolicyReader::ConfigurationPolicyReader( | |
168 ConfigurationPolicyProvider* provider, | |
169 bool managed) | |
170 : provider_(provider), | |
171 initialization_complete_(false), | |
172 managed_policies_(managed) { | |
173 if (provider_) { | |
174 // Read initial policy. | |
175 policy_keeper_.reset( | |
176 new ConfigurationPolicyStatusKeeper(provider, managed)); | |
177 registrar_.Init(provider_, this); | |
178 initialization_complete_ = provider->IsInitializationComplete(); | |
179 } else { | |
180 initialization_complete_ = true; | |
181 } | |
182 } | |
183 | |
184 void ConfigurationPolicyReader::Refresh() { | |
185 if (!provider_) | |
186 return; | |
187 | |
188 // Read policy state into a new keeper and swap out old keeper. | |
189 scoped_ptr<ConfigurationPolicyStatusKeeper> new_keeper( | |
190 new ConfigurationPolicyStatusKeeper(provider_, managed_policies_)); | |
191 policy_keeper_.reset(new_keeper.release()); | |
192 | |
193 // TODO(simo) create an observer class which PolicyStatus can implement so | |
194 // that it can be notified of changes. | |
195 } | |
196 | |
197 // PolicyStatus | |
198 PolicyStatus::PolicyStatus() { | |
199 managed_platform_.reset( | |
200 ConfigurationPolicyReader::CreateManagedPlatformPolicyReader(true)); | |
201 managed_cloud_.reset( | |
202 ConfigurationPolicyReader::CreateManagedCloudPolicyReader(true)); | |
203 recommended_platform_.reset( | |
204 ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader(false)); | |
205 recommended_cloud_.reset( | |
206 ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader(false)); | |
207 } | |
208 | |
209 PolicyStatus::~PolicyStatus() { | |
210 } | |
211 | |
212 ListValue* PolicyStatus::GetPolicyStatusList() const { | |
213 ListValue* result = new ListValue(); | |
214 std::vector<PolicyStatusInfo*> unsent_policies; | |
215 | |
216 const PolicyDefinitionList* supported_policies = | |
217 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); | |
218 const PolicyDefinitionList::Entry* policy = supported_policies->begin; | |
219 for ( ; policy != supported_policies->end; ++policy) { | |
220 if (!AddPolicyFromReaders(policy->policy_type, result)) { | |
221 PolicyStatusInfo* info = new PolicyStatusInfo( | |
222 ASCIIToUTF16(policy->name), | |
223 PolicyStatusInfo::SOURCE_TYPE_UNDEFINED, | |
224 PolicyStatusInfo::LEVEL_UNDEFINED, | |
225 Value::CreateNullValue(), | |
226 PolicyStatusInfo::STATUS_UNDEFINED, | |
227 string16()); | |
228 unsent_policies.push_back(info); | |
229 } | |
230 } | |
231 | |
232 // Add policies that weren't actually sent from providers to list. | |
233 std::vector<PolicyStatusInfo*>::const_iterator info = unsent_policies.begin(); | |
234 for ( ; info != unsent_policies.end(); ++info) | |
235 result->Append((*info)->GetDictionaryValue()); | |
236 | |
simo
2011/08/09 21:03:02
I am leaking the PolicyStatusInfo pointers here, w
| |
237 return result; | |
238 } | |
239 | |
240 bool PolicyStatus::AddPolicyFromReaders( | |
241 ConfigurationPolicyType policy, ListValue* list) const { | |
242 | |
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
no newline
simo
2011/08/10 14:28:19
Done.
| |
243 DictionaryValue* mp_policy = managed_platform_.get()->GetPolicyStatus(policy); | |
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
you are leaking all these DictionaryValue pointers
simo
2011/08/09 21:03:02
I thought I saw in base/values.h that the pointers
| |
244 DictionaryValue* mc_policy = | |
245 managed_cloud_.get()->GetPolicyStatus(policy); | |
246 DictionaryValue* rp_policy = | |
247 recommended_platform_.get()->GetPolicyStatus(policy); | |
248 DictionaryValue* rc_policy = | |
249 recommended_cloud_.get()->GetPolicyStatus(policy); | |
250 | |
251 bool added_policy = mp_policy || mc_policy || rp_policy || rc_policy; | |
252 | |
253 if (mp_policy) | |
254 list->Append(mp_policy); | |
255 if (mc_policy) | |
256 list->Append(mc_policy); | |
257 if (rp_policy) | |
258 list->Append(rp_policy); | |
259 if (rc_policy) | |
260 list->Append(rc_policy); | |
261 | |
262 return added_policy; | |
263 } | |
264 | |
265 } // policy | |
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
namespace policy
simo
2011/08/10 14:28:19
Done.
| |
266 | |
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
remove trailing newline
simo
2011/08/10 14:28:19
Done.
| |
OLD | NEW |