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

Side by Side Diff: chrome/browser/policy/configuration_policy_reader.cc

Issue 8258018: Generate Chrome policy definition list from policy_templates.json. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix the unittest fix Created 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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/configuration_policy_reader.h" 5 #include "chrome/browser/policy/configuration_policy_reader.h"
6 6
7 #include <map> 7 #include <map>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/string16.h" 12 #include "base/string16.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/policy/browser_policy_connector.h" 15 #include "chrome/browser/policy/browser_policy_connector.h"
16 #include "chrome/browser/policy/configuration_policy_pref_store.h" 16 #include "chrome/browser/policy/configuration_policy_pref_store.h"
17 #include "chrome/browser/policy/policy_map.h" 17 #include "chrome/browser/policy/policy_map.h"
18 #include "policy/policy_constants.h"
18 19
19 namespace policy { 20 namespace policy {
20 21
21 // This class functions as a container for policy status information used by the 22 // This class functions as a container for policy status information used by the
22 // ConfigurationPolicyReader class. It obtains policy values from a 23 // ConfigurationPolicyReader class. It obtains policy values from a
23 // ConfigurationPolicyProvider and maps them to their status information. 24 // ConfigurationPolicyProvider and maps them to their status information.
24 class ConfigurationPolicyStatusKeeper { 25 class ConfigurationPolicyStatusKeeper {
25 public: 26 public:
26 ConfigurationPolicyStatusKeeper(ConfigurationPolicyProvider* provider, 27 ConfigurationPolicyStatusKeeper(ConfigurationPolicyProvider* provider,
27 PolicyStatusInfo::PolicyLevel policy_level); 28 PolicyStatusInfo::PolicyLevel policy_level);
28 virtual ~ConfigurationPolicyStatusKeeper(); 29 virtual ~ConfigurationPolicyStatusKeeper();
29 30
30 // Returns a pointer to a DictionaryValue containing the status information 31 // Returns a pointer to a DictionaryValue containing the status information
31 // of the policy |policy|. The caller acquires ownership of the returned 32 // of the policy |policy|. The caller acquires ownership of the returned
32 // value. Returns NULL if no such policy is stored in this keeper. 33 // value. Returns NULL if no such policy is stored in this keeper.
33 DictionaryValue* GetPolicyStatus(ConfigurationPolicyType policy) const; 34 DictionaryValue* GetPolicyStatus(ConfigurationPolicyType policy) const;
34 35
35 private: 36 private:
36 typedef std::map<ConfigurationPolicyType, PolicyStatusInfo*> PolicyStatusMap; 37 typedef std::map<ConfigurationPolicyType, PolicyStatusInfo*> PolicyStatusMap;
37 typedef std::map<ConfigurationPolicyType, string16> PolicyNameMap;
38 typedef ConfigurationPolicyProvider::PolicyDefinitionList
39 PolicyDefinitionList;
40 38
41 // Calls Provide() on the passed in |provider| to get policy values. 39 // Calls Provide() on the passed in |provider| to get policy values.
42 void GetPoliciesFromProvider(ConfigurationPolicyProvider* provider); 40 void GetPoliciesFromProvider(ConfigurationPolicyProvider* provider);
43 41
44 // Mapping from ConfigurationPolicyType to PolicyStatusInfo. 42 // Mapping from ConfigurationPolicyType to PolicyStatusInfo.
45 PolicyStatusMap policy_map_; 43 PolicyStatusMap policy_map_;
46 44
47 // The level of the policies stored in this keeper (mandatory or 45 // The level of the policies stored in this keeper (mandatory or
48 // recommended). 46 // recommended).
49 PolicyStatusInfo::PolicyLevel policy_level_; 47 PolicyStatusInfo::PolicyLevel policy_level_;
(...skipping 21 matching lines...) Expand all
71 } 69 }
72 70
73 void ConfigurationPolicyStatusKeeper::GetPoliciesFromProvider( 71 void ConfigurationPolicyStatusKeeper::GetPoliciesFromProvider(
74 ConfigurationPolicyProvider* provider) { 72 ConfigurationPolicyProvider* provider) {
75 scoped_ptr<PolicyMap> policies(new PolicyMap()); 73 scoped_ptr<PolicyMap> policies(new PolicyMap());
76 if (!provider->Provide(policies.get())) 74 if (!provider->Provide(policies.get()))
77 LOG(WARNING) << "Failed to get policy from provider."; 75 LOG(WARNING) << "Failed to get policy from provider.";
78 76
79 PolicyMap::const_iterator policy = policies->begin(); 77 PolicyMap::const_iterator policy = policies->begin();
80 for ( ; policy != policies->end(); ++policy) { 78 for ( ; policy != policies->end(); ++policy) {
81 string16 name = PolicyStatus::GetPolicyName(policy->first); 79 string16 name = ASCIIToUTF16(GetPolicyName(policy->first));
82 DCHECK(!name.empty());
83 80
84 // TODO(simo) actually determine whether the policy is a user or a device 81 // TODO(simo) actually determine whether the policy is a user or a device
85 // one and whether the policy could be enforced or not once this information 82 // one and whether the policy could be enforced or not once this information
86 // is available. 83 // is available.
87 PolicyStatusInfo* info = new PolicyStatusInfo(name, 84 PolicyStatusInfo* info = new PolicyStatusInfo(name,
88 PolicyStatusInfo::USER, 85 PolicyStatusInfo::USER,
89 policy_level_, 86 policy_level_,
90 policy->second->DeepCopy(), 87 policy->second->DeepCopy(),
91 PolicyStatusInfo::ENFORCED, 88 PolicyStatusInfo::ENFORCED,
92 string16()); 89 string16());
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 recommended_platform_->RemoveObserver(observer); 211 recommended_platform_->RemoveObserver(observer);
215 recommended_cloud_->RemoveObserver(observer); 212 recommended_cloud_->RemoveObserver(observer);
216 } 213 }
217 214
218 ListValue* PolicyStatus::GetPolicyStatusList(bool* any_policies_set) const { 215 ListValue* PolicyStatus::GetPolicyStatusList(bool* any_policies_set) const {
219 ListValue* result = new ListValue(); 216 ListValue* result = new ListValue();
220 std::vector<DictionaryValue*> unsent_policies; 217 std::vector<DictionaryValue*> unsent_policies;
221 218
222 *any_policies_set = false; 219 *any_policies_set = false;
223 const PolicyDefinitionList* supported_policies = 220 const PolicyDefinitionList* supported_policies =
224 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); 221 GetChromePolicyDefinitionList();
225 const PolicyDefinitionList::Entry* policy = supported_policies->begin; 222 const PolicyDefinitionList::Entry* policy = supported_policies->begin;
226 for ( ; policy != supported_policies->end; ++policy) { 223 for ( ; policy != supported_policies->end; ++policy) {
227 if (!AddPolicyFromReaders(policy->policy_type, result)) { 224 if (!AddPolicyFromReaders(policy->policy_type, result)) {
228 PolicyStatusInfo info(ASCIIToUTF16(policy->name), 225 PolicyStatusInfo info(ASCIIToUTF16(policy->name),
229 PolicyStatusInfo::SOURCE_TYPE_UNDEFINED, 226 PolicyStatusInfo::SOURCE_TYPE_UNDEFINED,
230 PolicyStatusInfo::LEVEL_UNDEFINED, 227 PolicyStatusInfo::LEVEL_UNDEFINED,
231 Value::CreateNullValue(), 228 Value::CreateNullValue(),
232 PolicyStatusInfo::STATUS_UNDEFINED, 229 PolicyStatusInfo::STATUS_UNDEFINED,
233 string16()); 230 string16());
234 unsent_policies.push_back(info.GetDictionaryValue()); 231 unsent_policies.push_back(info.GetDictionaryValue());
235 } else { 232 } else {
236 *any_policies_set = true; 233 *any_policies_set = true;
237 } 234 }
238 } 235 }
239 236
240 // Add policies that weren't actually sent from providers to list. 237 // Add policies that weren't actually sent from providers to list.
241 std::vector<DictionaryValue*>::const_iterator info = unsent_policies.begin(); 238 std::vector<DictionaryValue*>::const_iterator info = unsent_policies.begin();
242 for ( ; info != unsent_policies.end(); ++info) 239 for ( ; info != unsent_policies.end(); ++info)
243 result->Append(*info); 240 result->Append(*info);
244 241
245 return result; 242 return result;
246 } 243 }
247 244
248 // static
249 string16 PolicyStatus::GetPolicyName(ConfigurationPolicyType policy_type) {
250 static std::map<ConfigurationPolicyType, string16> name_map;
251 static const ConfigurationPolicyProvider::PolicyDefinitionList*
252 supported_policies = NULL;
253
254 if (!supported_policies) {
255 supported_policies =
256 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList();
257
258 // Create mapping from ConfigurationPolicyTypes to actual policy names.
259 const ConfigurationPolicyProvider::PolicyDefinitionList::Entry* entry =
260 supported_policies->begin;
261 for ( ; entry != supported_policies->end; ++entry)
262 name_map[entry->policy_type] = ASCIIToUTF16(entry->name);
263 }
264
265 std::map<ConfigurationPolicyType, string16>::const_iterator entry =
266 name_map.find(policy_type);
267
268 if (entry == name_map.end())
269 return string16();
270
271 return entry->second;
272 }
273
274 bool PolicyStatus::AddPolicyFromReaders( 245 bool PolicyStatus::AddPolicyFromReaders(
275 ConfigurationPolicyType policy, ListValue* list) const { 246 ConfigurationPolicyType policy, ListValue* list) const {
276 DictionaryValue* mp_policy = 247 DictionaryValue* mp_policy =
277 managed_platform_->GetPolicyStatus(policy); 248 managed_platform_->GetPolicyStatus(policy);
278 DictionaryValue* mc_policy = 249 DictionaryValue* mc_policy =
279 managed_cloud_->GetPolicyStatus(policy); 250 managed_cloud_->GetPolicyStatus(policy);
280 DictionaryValue* rp_policy = 251 DictionaryValue* rp_policy =
281 recommended_platform_->GetPolicyStatus(policy); 252 recommended_platform_->GetPolicyStatus(policy);
282 DictionaryValue* rc_policy = 253 DictionaryValue* rc_policy =
283 recommended_cloud_->GetPolicyStatus(policy); 254 recommended_cloud_->GetPolicyStatus(policy);
284 255
285 bool added_policy = mp_policy || mc_policy || rp_policy || rc_policy; 256 bool added_policy = mp_policy || mc_policy || rp_policy || rc_policy;
286 257
287 if (mp_policy) 258 if (mp_policy)
288 list->Append(mp_policy); 259 list->Append(mp_policy);
289 if (mc_policy) 260 if (mc_policy)
290 list->Append(mc_policy); 261 list->Append(mc_policy);
291 if (rp_policy) 262 if (rp_policy)
292 list->Append(rp_policy); 263 list->Append(rp_policy);
293 if (rc_policy) 264 if (rc_policy)
294 list->Append(rc_policy); 265 list->Append(rc_policy);
295 266
296 return added_policy; 267 return added_policy;
297 } 268 }
298 269
299 } // namespace policy 270 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/configuration_policy_reader.h ('k') | chrome/browser/policy/configuration_policy_reader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698