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