| 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 # TODO(joaodasilva): refactor the 'dict' type into a more generic 'json' type | 32 # TODO(joaodasilva): refactor the 'dict' type into a more generic 'json' type |
| 32 # that can also be used to represent lists of other JSON objects. | 33 # that can also be used to represent lists of other JSON objects. |
| 33 TYPE_MAP = { | 34 TYPE_MAP = { |
| 34 'dict': ('TYPE_DICTIONARY', 'string', 'String'), | 35 'dict': ('TYPE_DICTIONARY', 'string', 'String'), |
| 36 'external': ('TYPE_EXTERNAL', 'string', 'String'), |
| 35 'int': ('TYPE_INTEGER', 'int64', 'Integer'), | 37 'int': ('TYPE_INTEGER', 'int64', 'Integer'), |
| 36 'int-enum': ('TYPE_INTEGER', 'int64', 'Integer'), | 38 'int-enum': ('TYPE_INTEGER', 'int64', 'Integer'), |
| 37 'list': ('TYPE_LIST', 'StringList', 'StringList'), | 39 'list': ('TYPE_LIST', 'StringList', 'StringList'), |
| 38 'main': ('TYPE_BOOLEAN', 'bool', 'Boolean'), | 40 'main': ('TYPE_BOOLEAN', 'bool', 'Boolean'), |
| 39 'string': ('TYPE_STRING', 'string', 'String'), | 41 'string': ('TYPE_STRING', 'string', 'String'), |
| 40 'string-enum': ('TYPE_STRING', 'string', 'String'), | 42 'string-enum': ('TYPE_STRING', 'string', 'String'), |
| 41 } | 43 } |
| 42 | 44 |
| 43 class EnumItem: | 45 class EnumItem: |
| 44 def __init__(self, item): | 46 def __init__(self, item): |
| (...skipping 18 matching lines...) Expand all Loading... |
| 63 is_supported = False | 65 is_supported = False |
| 64 for platform, version in [ p.split(':') for p in policy['supported_on'] ]: | 66 for platform, version in [ p.split(':') for p in policy['supported_on'] ]: |
| 65 if (platform == expected_platform or platform == wildcard_platform) and \ | 67 if (platform == expected_platform or platform == wildcard_platform) and \ |
| 66 version.endswith('-'): | 68 version.endswith('-'): |
| 67 is_supported = True | 69 is_supported = True |
| 68 self.is_supported = is_supported | 70 self.is_supported = is_supported |
| 69 | 71 |
| 70 if not PolicyDetails.TYPE_MAP.has_key(policy['type']): | 72 if not PolicyDetails.TYPE_MAP.has_key(policy['type']): |
| 71 raise NotImplementedError('Unknown policy type for %s: %s' % | 73 raise NotImplementedError('Unknown policy type for %s: %s' % |
| 72 (policy['name'], policy['type'])) | 74 (policy['name'], policy['type'])) |
| 73 self.value_type, self.protobuf_type, self.policy_protobuf_type = \ | 75 self.policy_type, self.protobuf_type, self.policy_protobuf_type = \ |
| 74 PolicyDetails.TYPE_MAP[policy['type']] | 76 PolicyDetails.TYPE_MAP[policy['type']] |
| 75 | 77 |
| 76 self.desc = '\n'.join( | 78 self.desc = '\n'.join( |
| 77 map(str.strip, | 79 map(str.strip, |
| 78 PolicyDetails._RemovePlaceholders(policy['desc']).splitlines())) | 80 PolicyDetails._RemovePlaceholders(policy['desc']).splitlines())) |
| 79 self.caption = PolicyDetails._RemovePlaceholders(policy['caption']) | 81 self.caption = PolicyDetails._RemovePlaceholders(policy['caption']) |
| 82 self.max_size = policy.get('max_size', 0) |
| 80 | 83 |
| 81 items = policy.get('items') | 84 items = policy.get('items') |
| 82 if items is None: | 85 if items is None: |
| 83 self.items = None | 86 self.items = None |
| 84 else: | 87 else: |
| 85 self.items = [ PolicyDetails.EnumItem(entry) for entry in items ] | 88 self.items = [ PolicyDetails.EnumItem(entry) for entry in items ] |
| 86 | 89 |
| 87 PH_PATTERN = re.compile('<ph[^>]*>([^<]*|[^<]*<ex>([^<]*)</ex>[^<]*)</ph>') | 90 PH_PATTERN = re.compile('<ph[^>]*>([^<]*|[^<]*<ex>([^<]*)</ex>[^<]*)</ph>') |
| 88 | 91 |
| 89 # Simplistic grit placeholder stripper. | 92 # Simplistic grit placeholder stripper. |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 | 198 |
| 196 | 199 |
| 197 #------------------ policy constants header ------------------------# | 200 #------------------ policy constants header ------------------------# |
| 198 | 201 |
| 199 def _WritePolicyConstantHeader(policies, os, f): | 202 def _WritePolicyConstantHeader(policies, os, f): |
| 200 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 203 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| 201 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 204 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| 202 '\n' | 205 '\n' |
| 203 '#include <string>\n' | 206 '#include <string>\n' |
| 204 '\n' | 207 '\n' |
| 208 '#include "base/basictypes.h"\n' |
| 205 '#include "base/values.h"\n' | 209 '#include "base/values.h"\n' |
| 206 '\n' | 210 '\n' |
| 207 'namespace policy {\n\n') | 211 'namespace policy {\n\n') |
| 208 | 212 |
| 209 if os == 'win': | 213 if os == 'win': |
| 210 f.write('// The windows registry path where Chrome policy ' | 214 f.write('// The windows registry path where Chrome policy ' |
| 211 'configuration resides.\n' | 215 'configuration resides.\n' |
| 212 'extern const wchar_t kRegistryChromePolicyKey[];\n') | 216 'extern const wchar_t kRegistryChromePolicyKey[];\n') |
| 213 | 217 |
| 214 f.write('// Lists policy types mapped to their names and expected types.\n' | 218 f.write('// Lists metadata such as name, expected type and id for all\n' |
| 215 '// Used to initialize ConfigurationPolicyProviders.\n' | 219 '// policies. Used to initialize ConfigurationPolicyProviders and\n' |
| 220 '// CloudExternalDataManagers.\n' |
| 216 'struct PolicyDefinitionList {\n' | 221 'struct PolicyDefinitionList {\n' |
| 217 ' struct Entry {\n' | 222 ' struct Entry {\n' |
| 218 ' const char* name;\n' | 223 ' const char* name;\n' |
| 219 ' base::Value::Type value_type;\n' | 224 ' base::Value::Type value_type;\n' |
| 220 ' bool device_policy;\n' | 225 ' bool device_policy;\n' |
| 221 ' int id;\n' | 226 ' int id;\n' |
| 227 ' size_t max_external_data_size;\n' |
| 222 ' };\n' | 228 ' };\n' |
| 223 '\n' | 229 '\n' |
| 224 ' const Entry* begin;\n' | 230 ' const Entry* begin;\n' |
| 225 ' const Entry* end;\n' | 231 ' const Entry* end;\n' |
| 226 '};\n' | 232 '};\n' |
| 227 '\n' | 233 '\n' |
| 228 '// Returns true if the given policy is deprecated.\n' | 234 '// Returns true if the given policy is deprecated.\n' |
| 229 'bool IsDeprecatedPolicy(const std::string& policy);\n' | 235 'bool IsDeprecatedPolicy(const std::string& policy);\n' |
| 230 '\n' | 236 '\n' |
| 231 '// Returns the default policy definition list for Chrome.\n' | 237 '// Returns the default policy definition list for Chrome.\n' |
| 232 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n') | 238 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n') |
| 233 f.write('// Key names for the policy settings.\n' | 239 f.write('// Key names for the policy settings.\n' |
| 234 'namespace key {\n\n') | 240 'namespace key {\n\n') |
| 235 for policy in policies: | 241 for policy in policies: |
| 236 # TODO(joaodasilva): Include only supported policies in | 242 # TODO(joaodasilva): Include only supported policies in |
| 237 # configuration_policy_handler.cc and configuration_policy_handler_list.cc | 243 # configuration_policy_handler.cc and configuration_policy_handler_list.cc |
| 238 # so that these names can be conditional on 'policy.is_supported'. | 244 # so that these names can be conditional on 'policy.is_supported'. |
| 239 # http://crbug.com/223616 | 245 # http://crbug.com/223616 |
| 240 f.write('extern const char k' + policy.name + '[];\n') | 246 f.write('extern const char k' + policy.name + '[];\n') |
| 241 f.write('\n} // namespace key\n\n' | 247 f.write('\n} // namespace key\n\n' |
| 242 '} // namespace policy\n\n' | 248 '} // namespace policy\n\n' |
| 243 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') | 249 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') |
| 244 | 250 |
| 245 | 251 |
| 246 #------------------ policy constants source ------------------------# | 252 #------------------ policy constants source ------------------------# |
| 247 | 253 |
| 254 def _GetValueType(policy_type): |
| 255 return policy_type if policy_type != 'TYPE_EXTERNAL' else 'TYPE_DICTIONARY' |
| 256 |
| 257 |
| 248 def _WritePolicyConstantSource(policies, os, f): | 258 def _WritePolicyConstantSource(policies, os, f): |
| 249 f.write('#include "base/basictypes.h"\n' | 259 f.write('#include "base/basictypes.h"\n' |
| 250 '#include "base/logging.h"\n' | 260 '#include "base/logging.h"\n' |
| 251 '#include "policy/policy_constants.h"\n' | 261 '#include "policy/policy_constants.h"\n' |
| 252 '\n' | 262 '\n' |
| 253 'namespace policy {\n\n') | 263 'namespace policy {\n\n') |
| 254 | 264 |
| 255 f.write('namespace {\n\n') | 265 f.write('namespace {\n\n') |
| 256 | 266 |
| 257 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') | 267 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') |
| 258 for policy in policies: | 268 for policy in policies: |
| 259 if policy.is_supported: | 269 if policy.is_supported: |
| 260 f.write(' { key::k%s, base::Value::%s, %s, %s },\n' % | 270 f.write(' { key::k%s, base::Value::%s, %s, %s, %s },\n' % |
| 261 (policy.name, policy.value_type, | 271 (policy.name, _GetValueType(policy.policy_type), |
| 262 'true' if policy.is_device_only else 'false', policy.id)) | 272 'true' if policy.is_device_only else 'false', policy.id, |
| 273 policy.max_size)) |
| 263 f.write('};\n\n') | 274 f.write('};\n\n') |
| 264 | 275 |
| 265 f.write('const PolicyDefinitionList kChromePolicyList = {\n' | 276 f.write('const PolicyDefinitionList kChromePolicyList = {\n' |
| 266 ' kEntries,\n' | 277 ' kEntries,\n' |
| 267 ' kEntries + arraysize(kEntries),\n' | 278 ' kEntries + arraysize(kEntries),\n' |
| 268 '};\n\n') | 279 '};\n\n') |
| 269 | 280 |
| 270 has_deprecated_policies = any( | 281 has_deprecated_policies = any( |
| 271 [p.is_supported and p.is_deprecated for p in policies]) | 282 [p.is_supported and p.is_deprecated for p in policies]) |
| 272 | 283 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 | 438 |
| 428 CPP_HEAD = ''' | 439 CPP_HEAD = ''' |
| 429 #include <limits> | 440 #include <limits> |
| 430 #include <string> | 441 #include <string> |
| 431 | 442 |
| 432 #include "base/basictypes.h" | 443 #include "base/basictypes.h" |
| 433 #include "base/callback.h" | 444 #include "base/callback.h" |
| 434 #include "base/json/json_reader.h" | 445 #include "base/json/json_reader.h" |
| 435 #include "base/logging.h" | 446 #include "base/logging.h" |
| 436 #include "base/memory/scoped_ptr.h" | 447 #include "base/memory/scoped_ptr.h" |
| 448 #include "base/memory/weak_ptr.h" |
| 437 #include "base/values.h" | 449 #include "base/values.h" |
| 450 #include "chrome/browser/policy/cloud/cloud_external_data_manager.h" |
| 438 #include "chrome/browser/policy/external_data_fetcher.h" | 451 #include "chrome/browser/policy/external_data_fetcher.h" |
| 439 #include "chrome/browser/policy/policy_map.h" | 452 #include "chrome/browser/policy/policy_map.h" |
| 440 #include "policy/policy_constants.h" | 453 #include "policy/policy_constants.h" |
| 441 #include "policy/proto/cloud_policy.pb.h" | 454 #include "policy/proto/cloud_policy.pb.h" |
| 442 | 455 |
| 443 using google::protobuf::RepeatedPtrField; | 456 using google::protobuf::RepeatedPtrField; |
| 444 | 457 |
| 445 namespace policy { | 458 namespace policy { |
| 446 | 459 |
| 447 namespace em = enterprise_management; | 460 namespace em = enterprise_management; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 472 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS)); | 485 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS)); |
| 473 | 486 |
| 474 if (!root) | 487 if (!root) |
| 475 LOG(WARNING) << "Invalid JSON string, ignoring: " << json; | 488 LOG(WARNING) << "Invalid JSON string, ignoring: " << json; |
| 476 | 489 |
| 477 // Accept any Value type that parsed as JSON, and leave it to the handler to | 490 // Accept any Value type that parsed as JSON, and leave it to the handler to |
| 478 // convert and check the concrete type. | 491 // convert and check the concrete type. |
| 479 return root.release(); | 492 return root.release(); |
| 480 } | 493 } |
| 481 | 494 |
| 482 void DecodePolicy(const em::CloudPolicySettings& policy, PolicyMap* map) { | 495 void DecodePolicy(const em::CloudPolicySettings& policy, |
| 496 base::WeakPtr<CloudExternalDataManager> external_data_manager, |
| 497 PolicyMap* map) { |
| 483 ''' | 498 ''' |
| 484 | 499 |
| 485 | 500 |
| 486 CPP_FOOT = '''} | 501 CPP_FOOT = '''} |
| 487 | 502 |
| 488 } // namespace policy | 503 } // namespace policy |
| 489 ''' | 504 ''' |
| 490 | 505 |
| 491 | 506 |
| 492 def _CreateValue(type, arg): | 507 def _CreateValue(type, arg): |
| 493 if type == 'TYPE_BOOLEAN': | 508 if type == 'TYPE_BOOLEAN': |
| 494 return 'base::Value::CreateBooleanValue(%s)' % arg | 509 return 'base::Value::CreateBooleanValue(%s)' % arg |
| 495 elif type == 'TYPE_INTEGER': | 510 elif type == 'TYPE_INTEGER': |
| 496 return 'DecodeIntegerValue(%s)' % arg | 511 return 'DecodeIntegerValue(%s)' % arg |
| 497 elif type == 'TYPE_STRING': | 512 elif type == 'TYPE_STRING': |
| 498 return 'base::Value::CreateStringValue(%s)' % arg | 513 return 'base::Value::CreateStringValue(%s)' % arg |
| 499 elif type == 'TYPE_LIST': | 514 elif type == 'TYPE_LIST': |
| 500 return 'DecodeStringList(%s)' % arg | 515 return 'DecodeStringList(%s)' % arg |
| 501 elif type == 'TYPE_DICTIONARY': | 516 elif type == 'TYPE_DICTIONARY' or type == 'TYPE_EXTERNAL': |
| 502 return 'DecodeJson(%s)' % arg | 517 return 'DecodeJson(%s)' % arg |
| 503 else: | 518 else: |
| 504 raise NotImplementedError('Unknown type %s' % type) | 519 raise NotImplementedError('Unknown type %s' % type) |
| 505 | 520 |
| 506 | 521 |
| 522 def _CreateExternalDataFetcher(type, name): |
| 523 if type == 'TYPE_EXTERNAL': |
| 524 return 'new ExternalDataFetcher(external_data_manager, key::k%s)' % name |
| 525 return 'NULL' |
| 526 |
| 527 |
| 507 def _WritePolicyCode(f, policy): | 528 def _WritePolicyCode(f, policy): |
| 508 membername = policy.name.lower() | 529 membername = policy.name.lower() |
| 509 proto_type = '%sPolicyProto' % policy.policy_protobuf_type | 530 proto_type = '%sPolicyProto' % policy.policy_protobuf_type |
| 510 f.write(' if (policy.has_%s()) {\n' % membername) | 531 f.write(' if (policy.has_%s()) {\n' % membername) |
| 511 f.write(' const em::%s& policy_proto = policy.%s();\n' % | 532 f.write(' const em::%s& policy_proto = policy.%s();\n' % |
| 512 (proto_type, membername)) | 533 (proto_type, membername)) |
| 513 f.write(' if (policy_proto.has_value()) {\n') | 534 f.write(' if (policy_proto.has_value()) {\n') |
| 514 f.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n' | 535 f.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n' |
| 515 ' bool do_set = true;\n' | 536 ' bool do_set = true;\n' |
| 516 ' if (policy_proto.has_policy_options()) {\n' | 537 ' if (policy_proto.has_policy_options()) {\n' |
| 517 ' do_set = false;\n' | 538 ' do_set = false;\n' |
| 518 ' switch(policy_proto.policy_options().mode()) {\n' | 539 ' switch(policy_proto.policy_options().mode()) {\n' |
| 519 ' case em::PolicyOptions::MANDATORY:\n' | 540 ' case em::PolicyOptions::MANDATORY:\n' |
| 520 ' do_set = true;\n' | 541 ' do_set = true;\n' |
| 521 ' level = POLICY_LEVEL_MANDATORY;\n' | 542 ' level = POLICY_LEVEL_MANDATORY;\n' |
| 522 ' break;\n' | 543 ' break;\n' |
| 523 ' case em::PolicyOptions::RECOMMENDED:\n' | 544 ' case em::PolicyOptions::RECOMMENDED:\n' |
| 524 ' do_set = true;\n' | 545 ' do_set = true;\n' |
| 525 ' level = POLICY_LEVEL_RECOMMENDED;\n' | 546 ' level = POLICY_LEVEL_RECOMMENDED;\n' |
| 526 ' break;\n' | 547 ' break;\n' |
| 527 ' case em::PolicyOptions::UNSET:\n' | 548 ' case em::PolicyOptions::UNSET:\n' |
| 528 ' break;\n' | 549 ' break;\n' |
| 529 ' }\n' | 550 ' }\n' |
| 530 ' }\n' | 551 ' }\n' |
| 531 ' if (do_set) {\n') | 552 ' if (do_set) {\n') |
| 532 f.write(' base::Value* value = %s;\n' % | 553 f.write(' base::Value* value = %s;\n' % |
| 533 (_CreateValue(policy.value_type, 'policy_proto.value()'))) | 554 (_CreateValue(policy.policy_type, 'policy_proto.value()'))) |
| 555 f.write(' ExternalDataFetcher* external_data_fetcher = %s;\n' % |
| 556 _CreateExternalDataFetcher(policy.policy_type, policy.name)) |
| 534 f.write(' map->Set(key::k%s, level, POLICY_SCOPE_USER,\n' % | 557 f.write(' map->Set(key::k%s, level, POLICY_SCOPE_USER,\n' % |
| 535 policy.name) | 558 policy.name) |
| 536 f.write(' value, NULL);\n') | 559 f.write(' value, external_data_fetcher);\n') |
| 537 f.write(' }\n' | 560 f.write(' }\n' |
| 538 ' }\n' | 561 ' }\n' |
| 539 ' }\n') | 562 ' }\n') |
| 540 | 563 |
| 541 | 564 |
| 542 def _WriteCloudPolicyDecoder(policies, os, f): | 565 def _WriteCloudPolicyDecoder(policies, os, f): |
| 543 f.write(CPP_HEAD) | 566 f.write(CPP_HEAD) |
| 544 for policy in policies: | 567 for policy in policies: |
| 545 if policy.is_supported and not policy.is_device_only: | 568 if policy.is_supported and not policy.is_device_only: |
| 546 _WritePolicyCode(f, policy) | 569 _WritePolicyCode(f, policy) |
| 547 f.write(CPP_FOOT) | 570 f.write(CPP_FOOT) |
| 548 | 571 |
| 549 | 572 |
| 550 if __name__ == '__main__': | 573 if __name__ == '__main__': |
| 551 sys.exit(main()) | 574 sys.exit(main()) |
| OLD | NEW |