OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 '''python %prog [options] platform chromium_os_flag template | 6 '''python %prog [options] platform chromium_os_flag template |
7 | 7 |
8 platform specifies which platform source is being generated for | 8 platform specifies which platform source is being generated for |
9 and can be one of (win, mac, linux) | 9 and can be one of (win, mac, linux) |
10 chromium_os_flag should be 1 if this is a Chromium OS build | 10 chromium_os_flag should be 1 if this is a Chromium OS build |
11 template is the path to a .json policy template file.''' | 11 template is the path to a .json policy template file.''' |
12 | 12 |
13 from __future__ import with_statement | 13 from __future__ import with_statement |
14 from optparse import OptionParser | 14 from optparse import OptionParser |
15 import re | 15 import re |
16 import sys | 16 import sys |
17 import textwrap | 17 import textwrap |
18 | 18 |
19 | 19 |
20 CHROME_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome' | 20 CHROME_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome' |
21 CHROMIUM_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Chromium' | 21 CHROMIUM_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Chromium' |
22 | 22 |
23 | 23 |
24 class PolicyDetails: | 24 class PolicyDetails: |
25 """Parses a policy template and caches all its details.""" | 25 """Parses a policy template and caches all its details.""" |
26 | 26 |
27 # Maps policy types to a tuple with 3 other types: | 27 # Maps policy types to a tuple with 3 other types: |
28 # - the equivalent base::Value::Type | 28 # - the equivalent base::Value::Type or 'TYPE_EXTERNAL' if the policy |
29 # references external data | |
29 # - the equivalent Protobuf field type | 30 # - the equivalent Protobuf field type |
30 # - the name of one of the protobufs for shared policy types | 31 # - the name of one of the protobufs for shared policy types |
31 TYPE_MAP = { | 32 TYPE_MAP = { |
32 'dict': ('TYPE_DICTIONARY', 'string', 'String'), | 33 'dict': ('TYPE_DICTIONARY', 'string', 'String'), |
34 'external': ('TYPE_EXTERNAL', 'string', 'String'), | |
33 'int': ('TYPE_INTEGER', 'int64', 'Integer'), | 35 'int': ('TYPE_INTEGER', 'int64', 'Integer'), |
34 'int-enum': ('TYPE_INTEGER', 'int64', 'Integer'), | 36 'int-enum': ('TYPE_INTEGER', 'int64', 'Integer'), |
35 'list': ('TYPE_LIST', 'StringList', 'StringList'), | 37 'list': ('TYPE_LIST', 'StringList', 'StringList'), |
36 'main': ('TYPE_BOOLEAN', 'bool', 'Boolean'), | 38 'main': ('TYPE_BOOLEAN', 'bool', 'Boolean'), |
37 'string': ('TYPE_STRING', 'string', 'String'), | 39 'string': ('TYPE_STRING', 'string', 'String'), |
38 'string-enum': ('TYPE_STRING', 'string', 'String'), | 40 'string-enum': ('TYPE_STRING', 'string', 'String'), |
39 } | 41 } |
40 | 42 |
41 def __init__(self, policy, os, is_chromium_os): | 43 def __init__(self, policy, os, is_chromium_os): |
42 self.id = policy['id'] | 44 self.id = policy['id'] |
(...skipping 13 matching lines...) Expand all Loading... | |
56 is_supported = False | 58 is_supported = False |
57 for platform, version in [ p.split(':') for p in policy['supported_on'] ]: | 59 for platform, version in [ p.split(':') for p in policy['supported_on'] ]: |
58 if (platform == expected_platform or platform == wildcard_platform) and \ | 60 if (platform == expected_platform or platform == wildcard_platform) and \ |
59 version.endswith('-'): | 61 version.endswith('-'): |
60 is_supported = True | 62 is_supported = True |
61 self.is_supported = is_supported | 63 self.is_supported = is_supported |
62 | 64 |
63 if not PolicyDetails.TYPE_MAP.has_key(policy['type']): | 65 if not PolicyDetails.TYPE_MAP.has_key(policy['type']): |
64 raise NotImplementedError('Unknown policy type for %s: %s' % | 66 raise NotImplementedError('Unknown policy type for %s: %s' % |
65 (policy['name'], policy['type'])) | 67 (policy['name'], policy['type'])) |
66 self.value_type, self.protobuf_type, self.policy_protobuf_type = \ | 68 self.policy_type, self.protobuf_type, self.policy_protobuf_type = \ |
67 PolicyDetails.TYPE_MAP[policy['type']] | 69 PolicyDetails.TYPE_MAP[policy['type']] |
68 | 70 |
69 self.desc = '\n'.join( | 71 self.desc = '\n'.join( |
70 map(str.strip, self._RemovePlaceholders(policy['desc']).splitlines())) | 72 map(str.strip, self._RemovePlaceholders(policy['desc']).splitlines())) |
71 self.caption = self._RemovePlaceholders(policy['caption']) | 73 self.caption = self._RemovePlaceholders(policy['caption']) |
74 self.max_size = policy.get('max_size', 0) | |
Joao da Silva
2013/07/19 11:15:15
Document "max_size" in the template
bartfab (slow)
2013/07/19 13:06:30
Done.
| |
72 | 75 |
73 PH_PATTERN = re.compile('<ph[^>]*>([^<]*|[^<]*<ex>([^<]*)</ex>[^<]*)</ph>') | 76 PH_PATTERN = re.compile('<ph[^>]*>([^<]*|[^<]*<ex>([^<]*)</ex>[^<]*)</ph>') |
74 | 77 |
75 # Simplistic grit placeholder stripper. | 78 # Simplistic grit placeholder stripper. |
76 def _RemovePlaceholders(self, text): | 79 def _RemovePlaceholders(self, text): |
77 result = '' | 80 result = '' |
78 pos = 0 | 81 pos = 0 |
79 for m in PolicyDetails.PH_PATTERN.finditer(text): | 82 for m in PolicyDetails.PH_PATTERN.finditer(text): |
80 result += text[pos:m.start(0)] | 83 result += text[pos:m.start(0)] |
81 result += m.group(2) or m.group(1) | 84 result += m.group(2) or m.group(1) |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 | 183 |
181 | 184 |
182 #------------------ policy constants header ------------------------# | 185 #------------------ policy constants header ------------------------# |
183 | 186 |
184 def _WritePolicyConstantHeader(policies, os, f): | 187 def _WritePolicyConstantHeader(policies, os, f): |
185 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 188 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
186 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 189 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
187 '\n' | 190 '\n' |
188 '#include <string>\n' | 191 '#include <string>\n' |
189 '\n' | 192 '\n' |
193 '#include "base/basictypes.h"\n' | |
190 '#include "base/values.h"\n' | 194 '#include "base/values.h"\n' |
191 '\n' | 195 '\n' |
192 'namespace policy {\n\n') | 196 'namespace policy {\n\n') |
193 | 197 |
194 if os == 'win': | 198 if os == 'win': |
195 f.write('// The windows registry path where Chrome policy ' | 199 f.write('// The windows registry path where Chrome policy ' |
196 'configuration resides.\n' | 200 'configuration resides.\n' |
197 'extern const wchar_t kRegistryChromePolicyKey[];\n') | 201 'extern const wchar_t kRegistryChromePolicyKey[];\n') |
198 | 202 |
199 f.write('// Lists policy types mapped to their names and expected types.\n' | 203 f.write('// Lists metadata such as name, expected type and id for all\n' |
200 '// Used to initialize ConfigurationPolicyProviders.\n' | 204 '// policies. Used to initialize ConfigurationPolicyProviders and\n' |
205 '// CloudExternalDataManagers.\n' | |
201 'struct PolicyDefinitionList {\n' | 206 'struct PolicyDefinitionList {\n' |
202 ' struct Entry {\n' | 207 ' struct Entry {\n' |
203 ' const char* name;\n' | 208 ' const char* name;\n' |
204 ' base::Value::Type value_type;\n' | 209 ' base::Value::Type value_type;\n' |
205 ' bool device_policy;\n' | 210 ' bool device_policy;\n' |
206 ' int id;\n' | 211 ' int id;\n' |
212 ' size_t max_external_data_size;\n' | |
207 ' };\n' | 213 ' };\n' |
208 '\n' | 214 '\n' |
209 ' const Entry* begin;\n' | 215 ' const Entry* begin;\n' |
210 ' const Entry* end;\n' | 216 ' const Entry* end;\n' |
211 '};\n' | 217 '};\n' |
212 '\n' | 218 '\n' |
213 '// Returns true if the given policy is deprecated.\n' | 219 '// Returns true if the given policy is deprecated.\n' |
214 'bool IsDeprecatedPolicy(const std::string& policy);\n' | 220 'bool IsDeprecatedPolicy(const std::string& policy);\n' |
215 '\n' | 221 '\n' |
216 '// Returns the default policy definition list for Chrome.\n' | 222 '// Returns the default policy definition list for Chrome.\n' |
217 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n') | 223 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n') |
218 f.write('// Key names for the policy settings.\n' | 224 f.write('// Key names for the policy settings.\n' |
219 'namespace key {\n\n') | 225 'namespace key {\n\n') |
220 for policy in policies: | 226 for policy in policies: |
221 # TODO(joaodasilva): Include only supported policies in | 227 # TODO(joaodasilva): Include only supported policies in |
222 # configuration_policy_handler.cc and configuration_policy_handler_list.cc | 228 # configuration_policy_handler.cc and configuration_policy_handler_list.cc |
223 # so that these names can be conditional on 'policy.is_supported'. | 229 # so that these names can be conditional on 'policy.is_supported'. |
224 # http://crbug.com/223616 | 230 # http://crbug.com/223616 |
225 f.write('extern const char k' + policy.name + '[];\n') | 231 f.write('extern const char k' + policy.name + '[];\n') |
226 f.write('\n} // namespace key\n\n' | 232 f.write('\n} // namespace key\n\n' |
227 '} // namespace policy\n\n' | 233 '} // namespace policy\n\n' |
228 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') | 234 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') |
229 | 235 |
230 | 236 |
231 #------------------ policy constants source ------------------------# | 237 #------------------ policy constants source ------------------------# |
232 | 238 |
239 def _GetValueType(policy_type): | |
240 return policy_type if policy_type != 'TYPE_EXTERNAL' else 'TYPE_DICTIONARY' | |
241 | |
242 | |
233 def _WritePolicyConstantSource(policies, os, f): | 243 def _WritePolicyConstantSource(policies, os, f): |
234 f.write('#include "base/basictypes.h"\n' | 244 f.write('#include "base/basictypes.h"\n' |
235 '#include "base/logging.h"\n' | 245 '#include "base/logging.h"\n' |
236 '#include "policy/policy_constants.h"\n' | 246 '#include "policy/policy_constants.h"\n' |
237 '\n' | 247 '\n' |
238 'namespace policy {\n\n') | 248 'namespace policy {\n\n') |
239 | 249 |
240 f.write('namespace {\n\n') | 250 f.write('namespace {\n\n') |
241 | 251 |
242 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') | 252 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') |
243 for policy in policies: | 253 for policy in policies: |
244 if policy.is_supported: | 254 if policy.is_supported: |
245 f.write(' { key::k%s, base::Value::%s, %s, %s },\n' % | 255 f.write(' { key::k%s, base::Value::%s, %s, %s, %s },\n' % |
246 (policy.name, policy.value_type, | 256 (policy.name, _GetValueType(policy.policy_type), |
247 'true' if policy.is_device_only else 'false', policy.id)) | 257 'true' if policy.is_device_only else 'false', policy.id, |
258 policy.max_size)) | |
248 f.write('};\n\n') | 259 f.write('};\n\n') |
249 | 260 |
250 f.write('const PolicyDefinitionList kChromePolicyList = {\n' | 261 f.write('const PolicyDefinitionList kChromePolicyList = {\n' |
251 ' kEntries,\n' | 262 ' kEntries,\n' |
252 ' kEntries + arraysize(kEntries),\n' | 263 ' kEntries + arraysize(kEntries),\n' |
253 '};\n\n') | 264 '};\n\n') |
254 | 265 |
255 has_deprecated_policies = any( | 266 has_deprecated_policies = any( |
256 [p.is_supported and p.is_deprecated for p in policies]) | 267 [p.is_supported and p.is_deprecated for p in policies]) |
257 | 268 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
408 | 419 |
409 CPP_HEAD = ''' | 420 CPP_HEAD = ''' |
410 #include <limits> | 421 #include <limits> |
411 #include <string> | 422 #include <string> |
412 | 423 |
413 #include "base/basictypes.h" | 424 #include "base/basictypes.h" |
414 #include "base/callback.h" | 425 #include "base/callback.h" |
415 #include "base/json/json_reader.h" | 426 #include "base/json/json_reader.h" |
416 #include "base/logging.h" | 427 #include "base/logging.h" |
417 #include "base/memory/scoped_ptr.h" | 428 #include "base/memory/scoped_ptr.h" |
429 #include "base/memory/weak_ptr.h" | |
418 #include "base/values.h" | 430 #include "base/values.h" |
431 #include "chrome/browser/policy/cloud/cloud_external_data_manager.h" | |
419 #include "chrome/browser/policy/external_data_fetcher.h" | 432 #include "chrome/browser/policy/external_data_fetcher.h" |
420 #include "chrome/browser/policy/policy_map.h" | 433 #include "chrome/browser/policy/policy_map.h" |
421 #include "policy/policy_constants.h" | 434 #include "policy/policy_constants.h" |
422 #include "policy/proto/cloud_policy.pb.h" | 435 #include "policy/proto/cloud_policy.pb.h" |
423 | 436 |
424 using google::protobuf::RepeatedPtrField; | 437 using google::protobuf::RepeatedPtrField; |
425 | 438 |
426 namespace policy { | 439 namespace policy { |
427 | 440 |
428 namespace em = enterprise_management; | 441 namespace em = enterprise_management; |
(...skipping 26 matching lines...) Expand all Loading... | |
455 if (!root || !root->GetAsDictionary(&dict) || !dict) { | 468 if (!root || !root->GetAsDictionary(&dict) || !dict) { |
456 LOG(WARNING) << "Invalid JSON string, ignoring: " << json; | 469 LOG(WARNING) << "Invalid JSON string, ignoring: " << json; |
457 // TODO(bartfab): Figure out a way to show errors in chrome://policy. | 470 // TODO(bartfab): Figure out a way to show errors in chrome://policy. |
458 return new base::DictionaryValue; | 471 return new base::DictionaryValue; |
459 } | 472 } |
460 | 473 |
461 ignore_result(root.release()); | 474 ignore_result(root.release()); |
462 return dict; | 475 return dict; |
463 } | 476 } |
464 | 477 |
465 void DecodePolicy(const em::CloudPolicySettings& policy, PolicyMap* map) { | 478 void DecodePolicy(const em::CloudPolicySettings& policy, |
479 base::WeakPtr<CloudExternalDataManager> external_data_manager, | |
480 PolicyMap* map) { | |
466 ''' | 481 ''' |
467 | 482 |
468 | 483 |
469 CPP_FOOT = '''} | 484 CPP_FOOT = '''} |
470 | 485 |
471 } // namespace policy | 486 } // namespace policy |
472 ''' | 487 ''' |
473 | 488 |
474 | 489 |
475 def _CreateValue(type, arg): | 490 def _CreateValue(type, arg): |
476 if type == 'TYPE_BOOLEAN': | 491 if type == 'TYPE_BOOLEAN': |
477 return 'base::Value::CreateBooleanValue(%s)' % arg | 492 return 'base::Value::CreateBooleanValue(%s)' % arg |
478 elif type == 'TYPE_INTEGER': | 493 elif type == 'TYPE_INTEGER': |
479 return 'DecodeIntegerValue(%s)' % arg | 494 return 'DecodeIntegerValue(%s)' % arg |
480 elif type == 'TYPE_STRING': | 495 elif type == 'TYPE_STRING': |
481 return 'base::Value::CreateStringValue(%s)' % arg | 496 return 'base::Value::CreateStringValue(%s)' % arg |
482 elif type == 'TYPE_LIST': | 497 elif type == 'TYPE_LIST': |
483 return 'DecodeStringList(%s)' % arg | 498 return 'DecodeStringList(%s)' % arg |
484 elif type == 'TYPE_DICTIONARY': | 499 elif type == 'TYPE_DICTIONARY' or type == 'TYPE_EXTERNAL': |
485 return 'DecodeDictionaryValue(%s)' % arg | 500 return 'DecodeDictionaryValue(%s)' % arg |
486 else: | 501 else: |
487 raise NotImplementedError('Unknown type %s' % type) | 502 raise NotImplementedError('Unknown type %s' % type) |
488 | 503 |
489 | 504 |
505 def _CreateExternalDataFetcher(type, name): | |
506 if type == 'TYPE_EXTERNAL': | |
507 return 'new ExternalDataFetcher(external_data_manager, key::k%s)' % name | |
508 return 'NULL' | |
509 | |
510 | |
490 def _WritePolicyCode(f, policy): | 511 def _WritePolicyCode(f, policy): |
491 membername = policy.name.lower() | 512 membername = policy.name.lower() |
492 proto_type = '%sPolicyProto' % policy.policy_protobuf_type | 513 proto_type = '%sPolicyProto' % policy.policy_protobuf_type |
493 f.write(' if (policy.has_%s()) {\n' % membername) | 514 f.write(' if (policy.has_%s()) {\n' % membername) |
494 f.write(' const em::%s& policy_proto = policy.%s();\n' % | 515 f.write(' const em::%s& policy_proto = policy.%s();\n' % |
495 (proto_type, membername)) | 516 (proto_type, membername)) |
496 f.write(' if (policy_proto.has_value()) {\n') | 517 f.write(' if (policy_proto.has_value()) {\n') |
497 f.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n' | 518 f.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n' |
498 ' bool do_set = true;\n' | 519 ' bool do_set = true;\n' |
499 ' if (policy_proto.has_policy_options()) {\n' | 520 ' if (policy_proto.has_policy_options()) {\n' |
500 ' do_set = false;\n' | 521 ' do_set = false;\n' |
501 ' switch(policy_proto.policy_options().mode()) {\n' | 522 ' switch(policy_proto.policy_options().mode()) {\n' |
502 ' case em::PolicyOptions::MANDATORY:\n' | 523 ' case em::PolicyOptions::MANDATORY:\n' |
503 ' do_set = true;\n' | 524 ' do_set = true;\n' |
504 ' level = POLICY_LEVEL_MANDATORY;\n' | 525 ' level = POLICY_LEVEL_MANDATORY;\n' |
505 ' break;\n' | 526 ' break;\n' |
506 ' case em::PolicyOptions::RECOMMENDED:\n' | 527 ' case em::PolicyOptions::RECOMMENDED:\n' |
507 ' do_set = true;\n' | 528 ' do_set = true;\n' |
508 ' level = POLICY_LEVEL_RECOMMENDED;\n' | 529 ' level = POLICY_LEVEL_RECOMMENDED;\n' |
509 ' break;\n' | 530 ' break;\n' |
510 ' case em::PolicyOptions::UNSET:\n' | 531 ' case em::PolicyOptions::UNSET:\n' |
511 ' break;\n' | 532 ' break;\n' |
512 ' }\n' | 533 ' }\n' |
513 ' }\n' | 534 ' }\n' |
514 ' if (do_set) {\n') | 535 ' if (do_set) {\n') |
515 f.write(' base::Value* value = %s;\n' % | 536 f.write(' base::Value* value = %s;\n' % |
516 (_CreateValue(policy.value_type, 'policy_proto.value()'))) | 537 (_CreateValue(policy.policy_type, 'policy_proto.value()'))) |
538 f.write(' ExternalDataFetcher* external_data_fetcher = %s;\n' % | |
539 _CreateExternalDataFetcher(policy.policy_type, policy.name)) | |
517 f.write(' map->Set(key::k%s, level, POLICY_SCOPE_USER,\n' % | 540 f.write(' map->Set(key::k%s, level, POLICY_SCOPE_USER,\n' % |
518 policy.name) | 541 policy.name) |
519 f.write(' value, NULL);\n') | 542 f.write(' value, external_data_fetcher);\n') |
520 f.write(' }\n' | 543 f.write(' }\n' |
521 ' }\n' | 544 ' }\n' |
522 ' }\n') | 545 ' }\n') |
523 | 546 |
524 | 547 |
525 def _WriteCloudPolicyDecoder(policies, os, f): | 548 def _WriteCloudPolicyDecoder(policies, os, f): |
526 f.write(CPP_HEAD) | 549 f.write(CPP_HEAD) |
527 for policy in policies: | 550 for policy in policies: |
528 if policy.is_supported and not policy.is_device_only: | 551 if policy.is_supported and not policy.is_device_only: |
529 _WritePolicyCode(f, policy) | 552 _WritePolicyCode(f, policy) |
530 f.write(CPP_FOOT) | 553 f.write(CPP_FOOT) |
531 | 554 |
532 | 555 |
533 if __name__ == '__main__': | 556 if __name__ == '__main__': |
534 sys.exit(main()) | 557 sys.exit(main()) |
OLD | NEW |