| 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 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 from xml.sax.saxutils import escape as xml_escape | 21 from xml.sax.saxutils import escape as xml_escape |
| 22 | 22 |
| 23 | 23 |
| 24 CHROME_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome' | 24 CHROME_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome' |
| 25 CHROMIUM_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Chromium' | 25 CHROMIUM_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Chromium' |
| 26 | 26 |
| 27 | 27 |
| 28 class PolicyDetails: | 28 class PolicyDetails: |
| 29 """Parses a policy template and caches all its details.""" | 29 """Parses a policy template and caches all its details.""" |
| 30 | 30 |
| 31 # Maps policy types to a tuple with 5 other types: | 31 # Maps policy types to a tuple with 4 other types: |
| 32 # - the equivalent base::Value::Type or 'TYPE_EXTERNAL' if the policy | 32 # - the equivalent base::Value::Type or 'TYPE_EXTERNAL' if the policy |
| 33 # references external data | 33 # references external data |
| 34 # - the equivalent Protobuf field type | 34 # - the equivalent Protobuf field type |
| 35 # - the name of one of the protobufs for shared policy types | 35 # - the name of one of the protobufs for shared policy types |
| 36 # - the equivalent type in Android's App Restriction Schema | 36 # - the equivalent type in Android's App Restriction Schema |
| 37 # - whether the equivalent app restriction type needs supporting resources | |
| 38 # TODO(joaodasilva): refactor the 'dict' type into a more generic 'json' type | 37 # TODO(joaodasilva): refactor the 'dict' type into a more generic 'json' type |
| 39 # that can also be used to represent lists of other JSON objects. | 38 # that can also be used to represent lists of other JSON objects. |
| 40 TYPE_MAP = { | 39 TYPE_MAP = { |
| 41 'dict': ('TYPE_DICTIONARY', 'string', 'String', | 40 'dict': ('TYPE_DICTIONARY', 'string', 'String', |
| 42 'string', False), | 41 'string'), |
| 43 'external': ('TYPE_EXTERNAL', 'string', 'String', | 42 'external': ('TYPE_EXTERNAL', 'string', 'String', |
| 44 'invalid', False), | 43 'invalid'), |
| 45 'int': ('TYPE_INTEGER', 'int64', 'Integer', | 44 'int': ('TYPE_INTEGER', 'int64', 'Integer', |
| 46 'integer', False), | 45 'integer'), |
| 47 'int-enum': ('TYPE_INTEGER', 'int64', 'Integer', | 46 'int-enum': ('TYPE_INTEGER', 'int64', 'Integer', |
| 48 'choice', True), | 47 'choice'), |
| 49 'list': ('TYPE_LIST', 'StringList', 'StringList', | 48 'list': ('TYPE_LIST', 'StringList', 'StringList', |
| 50 'string', False), | 49 'string'), |
| 51 'main': ('TYPE_BOOLEAN', 'bool', 'Boolean', | 50 'main': ('TYPE_BOOLEAN', 'bool', 'Boolean', |
| 52 'bool', False), | 51 'bool'), |
| 53 'string': ('TYPE_STRING', 'string', 'String', | 52 'string': ('TYPE_STRING', 'string', 'String', |
| 54 'string', False), | 53 'string'), |
| 55 'string-enum': ('TYPE_STRING', 'string', 'String', | 54 'string-enum': ('TYPE_STRING', 'string', 'String', |
| 56 'choice', True), | 55 'choice'), |
| 57 'string-enum-list': ('TYPE_LIST', 'StringList', 'StringList', | 56 'string-enum-list': ('TYPE_LIST', 'StringList', 'StringList', |
| 58 'multi-select', True), | 57 'multi-select'), |
| 59 } | 58 } |
| 60 | 59 |
| 61 class EnumItem: | 60 class EnumItem: |
| 62 def __init__(self, item): | 61 def __init__(self, item): |
| 63 self.caption = PolicyDetails._RemovePlaceholders(item['caption']) | 62 self.caption = PolicyDetails._RemovePlaceholders(item['caption']) |
| 64 self.value = item['value'] | 63 self.value = item['value'] |
| 65 | 64 |
| 66 def __init__(self, policy, os, is_chromium_os): | 65 def __init__(self, policy, os, is_chromium_os): |
| 67 self.id = policy['id'] | 66 self.id = policy['id'] |
| 68 self.name = policy['name'] | 67 self.name = policy['name'] |
| (...skipping 22 matching lines...) Expand all Loading... |
| 91 else: | 90 else: |
| 92 self.platforms.append(platform) | 91 self.platforms.append(platform) |
| 93 | 92 |
| 94 self.platforms.sort() | 93 self.platforms.sort() |
| 95 self.is_supported = expected_platform in self.platforms | 94 self.is_supported = expected_platform in self.platforms |
| 96 | 95 |
| 97 if not PolicyDetails.TYPE_MAP.has_key(policy['type']): | 96 if not PolicyDetails.TYPE_MAP.has_key(policy['type']): |
| 98 raise NotImplementedError('Unknown policy type for %s: %s' % | 97 raise NotImplementedError('Unknown policy type for %s: %s' % |
| 99 (policy['name'], policy['type'])) | 98 (policy['name'], policy['type'])) |
| 100 self.policy_type, self.protobuf_type, self.policy_protobuf_type, \ | 99 self.policy_type, self.protobuf_type, self.policy_protobuf_type, \ |
| 101 self.restriction_type, self.has_restriction_resources = \ | 100 self.restriction_type = PolicyDetails.TYPE_MAP[policy['type']] |
| 102 PolicyDetails.TYPE_MAP[policy['type']] | |
| 103 self.schema = policy['schema'] | 101 self.schema = policy['schema'] |
| 104 | 102 |
| 105 self.desc = '\n'.join( | 103 self.desc = '\n'.join( |
| 106 map(str.strip, | 104 map(str.strip, |
| 107 PolicyDetails._RemovePlaceholders(policy['desc']).splitlines())) | 105 PolicyDetails._RemovePlaceholders(policy['desc']).splitlines())) |
| 108 self.caption = PolicyDetails._RemovePlaceholders(policy['caption']) | 106 self.caption = PolicyDetails._RemovePlaceholders(policy['caption']) |
| 109 self.max_size = policy.get('max_size', 0) | 107 self.max_size = policy.get('max_size', 0) |
| 110 | 108 |
| 111 items = policy.get('items') | 109 items = policy.get('items') |
| 112 if items is None: | 110 if items is None: |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 metavar='FILE') | 145 metavar='FILE') |
| 148 parser.add_option('--cpd', '--cloud-policy-decoder', | 146 parser.add_option('--cpd', '--cloud-policy-decoder', |
| 149 dest='cloud_policy_decoder_path', | 147 dest='cloud_policy_decoder_path', |
| 150 help='generate C++ code decoding the cloud policy protobuf', | 148 help='generate C++ code decoding the cloud policy protobuf', |
| 151 metavar='FILE') | 149 metavar='FILE') |
| 152 parser.add_option('--ard', '--app-restrictions-definition', | 150 parser.add_option('--ard', '--app-restrictions-definition', |
| 153 dest='app_restrictions_path', | 151 dest='app_restrictions_path', |
| 154 help='generate an XML file as specified by ' | 152 help='generate an XML file as specified by ' |
| 155 'Android\'s App Restriction Schema', | 153 'Android\'s App Restriction Schema', |
| 156 metavar='FILE') | 154 metavar='FILE') |
| 157 parser.add_option('--arr', '--app-restrictions-resources', | |
| 158 dest='app_resources_path', | |
| 159 help='generate an XML file with resources supporting the ' | |
| 160 'restrictions defined in --app-restrictions-definition ' | |
| 161 'parameter', | |
| 162 metavar='FILE') | |
| 163 | 155 |
| 164 (opts, args) = parser.parse_args() | 156 (opts, args) = parser.parse_args() |
| 165 | 157 |
| 166 if len(args) != 3: | 158 if len(args) != 3: |
| 167 print 'exactly platform, chromium_os flag and input file must be specified.' | 159 print 'exactly platform, chromium_os flag and input file must be specified.' |
| 168 parser.print_help() | 160 parser.print_help() |
| 169 return 2 | 161 return 2 |
| 170 | 162 |
| 171 os = args[0] | 163 os = args[0] |
| 172 is_chromium_os = args[1] == '1' | 164 is_chromium_os = args[1] == '1' |
| (...skipping 11 matching lines...) Expand all Loading... |
| 184 writer(sorted and sorted_policy_details or policy_details, os, f) | 176 writer(sorted and sorted_policy_details or policy_details, os, f) |
| 185 | 177 |
| 186 GenerateFile(opts.header_path, _WritePolicyConstantHeader, sorted=True) | 178 GenerateFile(opts.header_path, _WritePolicyConstantHeader, sorted=True) |
| 187 GenerateFile(opts.source_path, _WritePolicyConstantSource, sorted=True) | 179 GenerateFile(opts.source_path, _WritePolicyConstantSource, sorted=True) |
| 188 GenerateFile(opts.cloud_policy_proto_path, _WriteCloudPolicyProtobuf) | 180 GenerateFile(opts.cloud_policy_proto_path, _WriteCloudPolicyProtobuf) |
| 189 GenerateFile(opts.chrome_settings_proto_path, _WriteChromeSettingsProtobuf) | 181 GenerateFile(opts.chrome_settings_proto_path, _WriteChromeSettingsProtobuf) |
| 190 GenerateFile(opts.cloud_policy_decoder_path, _WriteCloudPolicyDecoder) | 182 GenerateFile(opts.cloud_policy_decoder_path, _WriteCloudPolicyDecoder) |
| 191 | 183 |
| 192 if os == 'android': | 184 if os == 'android': |
| 193 GenerateFile(opts.app_restrictions_path, _WriteAppRestrictions, xml=True) | 185 GenerateFile(opts.app_restrictions_path, _WriteAppRestrictions, xml=True) |
| 194 GenerateFile(opts.app_resources_path, _WriteResourcesForPolicies, xml=True) | |
| 195 | 186 |
| 196 return 0 | 187 return 0 |
| 197 | 188 |
| 198 | 189 |
| 199 #------------------ shared helpers ---------------------------------# | 190 #------------------ shared helpers ---------------------------------# |
| 200 | 191 |
| 201 def _OutputGeneratedWarningHeader(f, template_file_path, xml_style): | 192 def _OutputGeneratedWarningHeader(f, template_file_path, xml_style): |
| 202 left_margin = '//' | 193 left_margin = '//' |
| 203 if xml_style: | 194 if xml_style: |
| 204 left_margin = ' ' | 195 left_margin = ' ' |
| (...skipping 795 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1000 | 991 |
| 1001 | 992 |
| 1002 def _WriteCloudPolicyDecoder(policies, os, f): | 993 def _WriteCloudPolicyDecoder(policies, os, f): |
| 1003 f.write(CPP_HEAD) | 994 f.write(CPP_HEAD) |
| 1004 for policy in policies: | 995 for policy in policies: |
| 1005 if policy.is_supported and not policy.is_device_only: | 996 if policy.is_supported and not policy.is_device_only: |
| 1006 _WritePolicyCode(f, policy) | 997 _WritePolicyCode(f, policy) |
| 1007 f.write(CPP_FOOT) | 998 f.write(CPP_FOOT) |
| 1008 | 999 |
| 1009 | 1000 |
| 1010 def _EscapeResourceString(raw_resource): | |
| 1011 if type(raw_resource) == int: | |
| 1012 return raw_resource | |
| 1013 return xml_escape(raw_resource)\ | |
| 1014 .replace('\\', '\\\\')\ | |
| 1015 .replace('\"','\\\"')\ | |
| 1016 .replace('\'','\\\'') | |
| 1017 | |
| 1018 def _WriteAppRestrictions(policies, os, f): | 1001 def _WriteAppRestrictions(policies, os, f): |
| 1019 | 1002 |
| 1020 def WriteRestrictionCommon(key): | 1003 def WriteRestrictionCommon(key): |
| 1021 f.write(' <restriction\n' | 1004 f.write(' <restriction\n' |
| 1022 ' android:key="%s"\n' % key) | 1005 ' android:key="%s"\n' % key) |
| 1023 f.write(' android:title="@string/%sTitle"\n' % key) | 1006 f.write(' android:title="@string/%sTitle"\n' % key) |
| 1024 f.write(' android:description="@string/%sDesc"\n' % key) | 1007 f.write(' android:description="@string/%sDesc"\n' % key) |
| 1025 | 1008 |
| 1026 def WriteItemsDefinition(key): | 1009 def WriteItemsDefinition(key): |
| 1027 f.write(' android:entries="@array/%sEntries"\n' % key) | 1010 f.write(' android:entries="@array/%sEntries"\n' % key) |
| 1028 f.write(' android:entryValues="@array/%sValues"\n' % key) | 1011 f.write(' android:entryValues="@array/%sValues"\n' % key) |
| 1029 | 1012 |
| 1030 def WriteAppRestriction(policy): | 1013 def WriteAppRestriction(policy): |
| 1031 policy_name = policy.name | 1014 policy_name = policy.name |
| 1032 WriteRestrictionCommon(policy_name) | 1015 WriteRestrictionCommon(policy_name) |
| 1033 | 1016 |
| 1034 if policy.has_restriction_resources: | 1017 if policy.items is not None: |
| 1035 WriteItemsDefinition(policy_name) | 1018 WriteItemsDefinition(policy_name) |
| 1036 | 1019 |
| 1037 f.write(' android:restrictionType="%s"/>' % policy.restriction_type) | 1020 f.write(' android:restrictionType="%s"/>' % policy.restriction_type) |
| 1038 f.write('\n\n') | 1021 f.write('\n\n') |
| 1039 | 1022 |
| 1040 # _WriteAppRestrictions body | 1023 # _WriteAppRestrictions body |
| 1041 f.write('<restrictions xmlns:android="' | 1024 f.write('<restrictions xmlns:android="' |
| 1042 'http://schemas.android.com/apk/res/android">\n\n') | 1025 'http://schemas.android.com/apk/res/android">\n\n') |
| 1043 for policy in policies: | 1026 for policy in policies: |
| 1044 if policy.is_supported and policy.restriction_type != 'invalid': | 1027 if policy.is_supported and policy.restriction_type != 'invalid': |
| 1045 WriteAppRestriction(policy) | 1028 WriteAppRestriction(policy) |
| 1046 f.write('</restrictions>') | 1029 f.write('</restrictions>') |
| 1047 | 1030 |
| 1048 | |
| 1049 def _WriteResourcesForPolicies(policies, os, f): | |
| 1050 | |
| 1051 # TODO(knn): Update this to support i18n. | |
| 1052 def WriteString(key, value): | |
| 1053 f.write(' <string name="%s">%s</string>\n' | |
| 1054 % (key, _EscapeResourceString(value))) | |
| 1055 | |
| 1056 def WriteItems(key, items): | |
| 1057 if items: | |
| 1058 f.write(' <string-array name="%sEntries">\n' % key) | |
| 1059 for item in items: | |
| 1060 f.write(' <item>%s</item>\n' % | |
| 1061 _EscapeResourceString(item.caption)) | |
| 1062 f.write(' </string-array>\n') | |
| 1063 f.write(' <string-array name="%sValues">\n' % key) | |
| 1064 for item in items: | |
| 1065 f.write(' <item>%s</item>\n' % _EscapeResourceString(item.value)) | |
| 1066 f.write(' </string-array>\n') | |
| 1067 | |
| 1068 def WriteResourceForPolicy(policy): | |
| 1069 policy_name = policy.name | |
| 1070 WriteString(policy_name + 'Title', policy.caption) | |
| 1071 | |
| 1072 # Get the first line of the policy description. | |
| 1073 description = policy.desc.split('\n', 1)[0] | |
| 1074 WriteString(policy_name + 'Desc', description) | |
| 1075 | |
| 1076 if policy.has_restriction_resources: | |
| 1077 WriteItems(policy_name, policy.items) | |
| 1078 | |
| 1079 # _WriteResourcesForPolicies body | |
| 1080 f.write('<resources>\n\n') | |
| 1081 for policy in policies: | |
| 1082 if policy.is_supported and policy.restriction_type != 'invalid': | |
| 1083 WriteResourceForPolicy(policy) | |
| 1084 f.write('</resources>') | |
| 1085 | |
| 1086 | |
| 1087 if __name__ == '__main__': | 1031 if __name__ == '__main__': |
| 1088 sys.exit(main()) | 1032 sys.exit(main()) |
| OLD | NEW |