From cd9f7d3afef0d0d4054318e9999aee879eaf26e1 Mon Sep 17 00:00:00 2001 From: knn Date: Mon, 9 Mar 2015 16:11:27 -0700 Subject: [PATCH] Add i18n support for Android App Restrictions. GYP target policy_templates is now enabled for android as well. Remove code that generated Policy resources w/o translation for Android. roll grit 186:187 187: Add a Policy template writer that generates Android resources which can be exposed through Android's App Restriction Schema. Depends on: https://codereview.chromium.org/865563002 BUG=450548 Review URL: https://codereview.chromium.org/865573002 Cr-Commit-Position: refs/heads/master@{#319758} --- DEPS | 2 +- components/policy.gypi | 10 +- components/policy/BUILD.gn | 20 +-- components/policy/resources/policy_templates.grd | 173 ++++++++++++++-------- components/policy/resources/policy_templates.json | 4 + components/policy/tools/generate_policy_source.py | 80 ++-------- 6 files changed, 137 insertions(+), 152 deletions(-) diff --git a/DEPS b/DEPS index f7878bff38c3..43b5d1760695 100644 --- a/DEPS +++ b/DEPS @@ -158,7 +158,7 @@ deps = { Var('chromium_git') + '/external/snappy.git' + '@' + '762bb32f0c9d2f31ba4958c7c0933d22e80c20bf', 'src/tools/grit': - Var('chromium_git') + '/external/grit-i18n.git' + '@' + '0287c187b11ed53590254e4d817e836a44a7a1a7', # from svn revision 186 + Var('chromium_git') + '/external/grit-i18n.git' + '@' + '5b56593a81a883b49509e2e51a779aefd8df3148', # from svn revision 187 'src/tools/gyp': Var('chromium_git') + '/external/gyp.git' + '@' + '4a9b712d5cb4a5ba7a9950128a7219569caf7263', diff --git a/components/policy.gypi b/components/policy.gypi index e6573b6a289f..189c01e02f51 100644 --- a/components/policy.gypi +++ b/components/policy.gypi @@ -12,7 +12,6 @@ 'grit_out_dir': '<(SHARED_INTERMEDIATE_DIR)/chrome', 'policy_out_dir': '<(SHARED_INTERMEDIATE_DIR)/policy', 'protoc_out_dir': '<(SHARED_INTERMEDIATE_DIR)/protoc_out', - 'android_resources_out_dir': '<(policy_out_dir)/android_resources', 'generate_policy_source_script_path': 'policy/tools/generate_policy_source.py', 'policy_constant_header_path': @@ -22,9 +21,7 @@ 'protobuf_decoder_path': '<(policy_out_dir)/policy/cloud_policy_generated.cc', 'app_restrictions_path': - '<(android_resources_out_dir)/xml-v21/app_restrictions.xml', - 'app_resources_path': - '<(android_resources_out_dir)/values-v21/restriction_values.xml', + '<(policy_out_dir)/app_restrictions.xml', # This is the "full" protobuf, which defines one protobuf message per # policy. It is also the format currently used by the server. 'chrome_settings_proto_path': @@ -115,7 +112,6 @@ '<(chrome_settings_proto_path)', '<(cloud_policy_proto_path)', '<(app_restrictions_path)', - '<(app_resources_path)', ], 'action_name': 'generate_policy_source', 'action': [ @@ -127,7 +123,6 @@ '--cloud-policy-protobuf=<(cloud_policy_proto_path)', '--cloud-policy-decoder=<(protobuf_decoder_path)', '--app-restrictions-definition=<(app_restrictions_path)', - '--app-restrictions-resources=<(app_resources_path)', '<(OS)', '<(chromeos)', 'policy/resources/policy_templates.json', @@ -137,7 +132,6 @@ ['OS!="android"', { 'outputs!': [ '<(app_restrictions_path)', - '<(app_resources_path)', ], }], ], @@ -370,7 +364,7 @@ }, ], }], - ['OS=="win" or OS=="mac" or OS=="linux"', { + ['OS!="ios"', { 'targets': [ { # policy_templates has different inputs and outputs, so it can't use diff --git a/components/policy/BUILD.gn b/components/policy/BUILD.gn index c8266dea07a3..5c5d7d6f3d1b 100644 --- a/components/policy/BUILD.gn +++ b/components/policy/BUILD.gn @@ -47,9 +47,6 @@ if (enable_configuration_policy) { # build puts everything into the following directory. We do the same for now. policy_gen_dir = "$root_gen_dir/policy" - # Directory for generating Android App Restrictions resources - android_resources_gen_dir = "$policy_gen_dir/android_resources" - # This protobuf is equivalent to chrome_settings.proto but shares messages # for policies of the same type, so that less classes have to be generated # and compiled. @@ -62,10 +59,7 @@ if (enable_configuration_policy) { constants_header_path = "$policy_gen_dir/policy_constants.h" constants_source_path = "$policy_gen_dir/policy_constants.cc" protobuf_decoder_path = "$policy_gen_dir/cloud_policy_generated.cc" - app_restrictions_path = - "$android_resources_gen_dir/xml-v21/app_restrictions.xml" - app_resources_path = - "$android_resources_gen_dir/values-v21/restriction_values.xml" + app_restrictions_path = "$policy_gen_dir/app_restrictions.xml" action("cloud_policy_code_generate") { script = "tools/generate_policy_source.py" @@ -86,14 +80,10 @@ if (enable_configuration_policy) { chrome_settings_proto_path, cloud_policy_proto_path, app_restrictions_path, - app_resources_path, ] - if (current_os != "android") { - outputs -= [ - app_restrictions_path, - app_resources_path, - ] + if (target_os != "android") { + outputs -= [ app_restrictions_path ] } args = [ @@ -109,9 +99,7 @@ if (enable_configuration_policy) { rebase_path(protobuf_decoder_path, root_build_dir), "--app-restrictions-definition=" + rebase_path(app_restrictions_path, root_build_dir), - "--app-restrictions-resources=" + - rebase_path(app_resources_path, root_build_dir), - current_os, + target_os, chromeos_flag, rebase_path("resources/policy_templates.json", root_build_dir), ] diff --git a/components/policy/resources/policy_templates.grd b/components/policy/resources/policy_templates.grd index cd26190db250..2fb729d01882 100644 --- a/components/policy/resources/policy_templates.grd +++ b/components/policy/resources/policy_templates.grd @@ -6,7 +6,60 @@ templates and will be translated for each locale. --> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -121,62 +174,65 @@ templates and will be translated for each locale. --> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -239,8 +295,7 @@ templates and will be translated for each locale. --> - - + diff --git a/components/policy/resources/policy_templates.json b/components/policy/resources/policy_templates.json index da5c833869c3..2c91318c6d6f 100644 --- a/components/policy/resources/policy_templates.json +++ b/components/policy/resources/policy_templates.json @@ -7297,6 +7297,10 @@ 'desc': '''Caption text of the field 'mac/linux preference name' in the summary chart of a policy in the generated documentation''', 'text': '''Mac/Linux preference name:''' }, + 'doc_android_restriction_name': { + 'desc': '''Caption text of the field 'android restriction name' in the summary chart of a policy in the generated documentation''', + 'text': '''Android restriction name:''' + }, 'doc_supported_on': { 'desc': '''Caption text of the list of 'products, platforms and versions where this policy is supported' in the summary chart of a policy in the generated documentation''', 'text': '''Supported on:''' diff --git a/components/policy/tools/generate_policy_source.py b/components/policy/tools/generate_policy_source.py index e343e33785d4..b3a723b72e6d 100755 --- a/components/policy/tools/generate_policy_source.py +++ b/components/policy/tools/generate_policy_source.py @@ -28,34 +28,33 @@ CHROMIUM_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Chromium' class PolicyDetails: """Parses a policy template and caches all its details.""" - # Maps policy types to a tuple with 5 other types: + # Maps policy types to a tuple with 4 other types: # - the equivalent base::Value::Type or 'TYPE_EXTERNAL' if the policy # references external data # - the equivalent Protobuf field type # - the name of one of the protobufs for shared policy types # - the equivalent type in Android's App Restriction Schema - # - whether the equivalent app restriction type needs supporting resources # TODO(joaodasilva): refactor the 'dict' type into a more generic 'json' type # that can also be used to represent lists of other JSON objects. TYPE_MAP = { 'dict': ('TYPE_DICTIONARY', 'string', 'String', - 'string', False), + 'string'), 'external': ('TYPE_EXTERNAL', 'string', 'String', - 'invalid', False), + 'invalid'), 'int': ('TYPE_INTEGER', 'int64', 'Integer', - 'integer', False), + 'integer'), 'int-enum': ('TYPE_INTEGER', 'int64', 'Integer', - 'choice', True), + 'choice'), 'list': ('TYPE_LIST', 'StringList', 'StringList', - 'string', False), + 'string'), 'main': ('TYPE_BOOLEAN', 'bool', 'Boolean', - 'bool', False), + 'bool'), 'string': ('TYPE_STRING', 'string', 'String', - 'string', False), + 'string'), 'string-enum': ('TYPE_STRING', 'string', 'String', - 'choice', True), + 'choice'), 'string-enum-list': ('TYPE_LIST', 'StringList', 'StringList', - 'multi-select', True), + 'multi-select'), } class EnumItem: @@ -98,8 +97,7 @@ class PolicyDetails: raise NotImplementedError('Unknown policy type for %s: %s' % (policy['name'], policy['type'])) self.policy_type, self.protobuf_type, self.policy_protobuf_type, \ - self.restriction_type, self.has_restriction_resources = \ - PolicyDetails.TYPE_MAP[policy['type']] + self.restriction_type = PolicyDetails.TYPE_MAP[policy['type']] self.schema = policy['schema'] self.desc = '\n'.join( @@ -154,12 +152,6 @@ def main(): help='generate an XML file as specified by ' 'Android\'s App Restriction Schema', metavar='FILE') - parser.add_option('--arr', '--app-restrictions-resources', - dest='app_resources_path', - help='generate an XML file with resources supporting the ' - 'restrictions defined in --app-restrictions-definition ' - 'parameter', - metavar='FILE') (opts, args) = parser.parse_args() @@ -191,7 +183,6 @@ def main(): if os == 'android': GenerateFile(opts.app_restrictions_path, _WriteAppRestrictions, xml=True) - GenerateFile(opts.app_resources_path, _WriteResourcesForPolicies, xml=True) return 0 @@ -1007,14 +998,6 @@ def _WriteCloudPolicyDecoder(policies, os, f): f.write(CPP_FOOT) -def _EscapeResourceString(raw_resource): - if type(raw_resource) == int: - return raw_resource - return xml_escape(raw_resource)\ - .replace('\\', '\\\\')\ - .replace('\"','\\\"')\ - .replace('\'','\\\'') - def _WriteAppRestrictions(policies, os, f): def WriteRestrictionCommon(key): @@ -1031,7 +1014,7 @@ def _WriteAppRestrictions(policies, os, f): policy_name = policy.name WriteRestrictionCommon(policy_name) - if policy.has_restriction_resources: + if policy.items is not None: WriteItemsDefinition(policy_name) f.write(' android:restrictionType="%s"/>' % policy.restriction_type) @@ -1045,44 +1028,5 @@ def _WriteAppRestrictions(policies, os, f): WriteAppRestriction(policy) f.write('') - -def _WriteResourcesForPolicies(policies, os, f): - - # TODO(knn): Update this to support i18n. - def WriteString(key, value): - f.write(' %s\n' - % (key, _EscapeResourceString(value))) - - def WriteItems(key, items): - if items: - f.write(' \n' % key) - for item in items: - f.write(' %s\n' % - _EscapeResourceString(item.caption)) - f.write(' \n') - f.write(' \n' % key) - for item in items: - f.write(' %s\n' % _EscapeResourceString(item.value)) - f.write(' \n') - - def WriteResourceForPolicy(policy): - policy_name = policy.name - WriteString(policy_name + 'Title', policy.caption) - - # Get the first line of the policy description. - description = policy.desc.split('\n', 1)[0] - WriteString(policy_name + 'Desc', description) - - if policy.has_restriction_resources: - WriteItems(policy_name, policy.items) - - # _WriteResourcesForPolicies body - f.write('\n\n') - for policy in policies: - if policy.is_supported and policy.restriction_type != 'invalid': - WriteResourceForPolicy(policy) - f.write('') - - if __name__ == '__main__': sys.exit(main()) -- 2.11.4.GIT