Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Unified Diff: components/policy/tools/generate_policy_source.py

Issue 2726103004: Generate code for encoding cloud policy protobufs (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/policy/tools/generate_policy_source.py
diff --git a/components/policy/tools/generate_policy_source.py b/components/policy/tools/generate_policy_source.py
index 889038aa97b71d57f4005fe85977998c8b133d27..7f2843f34205885e2c9c758cdbbe1f51f4ebd1b3 100755
--- a/components/policy/tools/generate_policy_source.py
+++ b/components/policy/tools/generate_policy_source.py
@@ -203,6 +203,16 @@ def main():
dest='risk_header_path',
help='generate header file for policy risk tags',
metavar='FILE')
+ parser.add_option('--crospch', '--cros-policy-constants-header',
+ dest='cros_constants_header_path',
+ help='generate header file of policy constants for use in '
+ 'Chrome OS',
+ metavar='FILE')
+ parser.add_option('--crospcc', '--cros-policy-constants-source',
+ dest='cros_constants_source_path',
+ help='generate source file of policy constants for use in '
+ 'Chrome OS',
+ metavar='FILE')
(opts, args) = parser.parse_args()
if len(args) != 4:
@@ -251,6 +261,14 @@ def main():
if os == 'android' and opts.app_restrictions_path:
GenerateFile(opts.app_restrictions_path, _WriteAppRestrictions, xml=True)
+ # Generated code for Chrome OS (unused in Chromium).
+ if opts.cros_constants_header_path:
+ GenerateFile(opts.cros_constants_header_path,
+ _WriteChromeOSPolicyConstantsHeader, sorted=True)
+ if opts.cros_constants_source_path:
+ GenerateFile(opts.cros_constants_source_path,
+ _WriteChromeOSPolicyConstantsSource, sorted=True)
+
return 0
@@ -1098,7 +1116,7 @@ def _WriteCloudPolicyFullRuntimeProtobuf(policies, os, f, riskTags):
#------------------ protobuf decoder -------------------------------#
-CPP_HEAD = '''
+POLICY_DECODER_CPP_HEAD = '''
emaxx 2017/03/02 16:56:15 nit: Add word "CLOUD" into the constant names here
ljusten (tachyonic) 2017/03/16 23:45:27 Done.
#include <limits>
#include <memory>
#include <utility>
@@ -1162,7 +1180,7 @@ void DecodePolicy(const em::CloudPolicySettings& policy,
'''
-CPP_FOOT = '''}
+POLICY_DECODER_CPP_FOOT = '''}
} // namespace policy
'''
@@ -1189,7 +1207,7 @@ def _CreateExternalDataFetcher(type, name):
return 'nullptr'
-def _WritePolicyCode(f, policy):
+def _WritePolicyDecoderCode(f, policy):
emaxx 2017/03/02 16:56:15 nit: Add word "Cloud" into the function name (for
ljusten (tachyonic) 2017/03/16 23:45:27 Done.
membername = policy.name.lower()
proto_type = '%sPolicyProto' % policy.policy_protobuf_type
f.write(' if (policy.has_%s()) {\n' % membername)
@@ -1235,12 +1253,115 @@ def _WritePolicyCode(f, policy):
def _WriteCloudPolicyDecoder(policies, os, f, riskTags):
- f.write(CPP_HEAD)
+ f.write(POLICY_DECODER_CPP_HEAD)
for policy in policies:
if policy.is_supported and not policy.is_device_only:
- _WritePolicyCode(f, policy)
- f.write(CPP_FOOT)
+ _WritePolicyDecoderCode(f, policy)
+ f.write(POLICY_DECODER_CPP_FOOT)
+
+
+#------------------ Chrome OS policy constants header ------------------------#
emaxx 2017/03/02 16:56:15 nit: Could you please reformat this bar and the ot
ljusten (tachyonic) 2017/03/16 23:45:27 Done.
+
+# Returns a list of supported user policies by filtering |policies|.
+def _GetSupportedUserPolicies(policies):
+ return filter(lambda policy: policy.is_supported and \
emaxx 2017/03/02 16:56:15 nit: Backslash is not required.
ljusten (tachyonic) 2017/03/16 23:45:27 Done.
+ not policy.is_device_only, policies)
+
+
+# Returns the set of all policy.policy_protobuf_type strings from |policies|.
+def _GetProtobufTypes(policies):
+ protobuf_types = set()
emaxx 2017/03/02 16:56:15 nit: Maybe simply: return set(policy.policy_protob
ljusten (tachyonic) 2017/03/16 23:45:27 Done.
+ for policy in policies:
+ protobuf_types.add(policy.policy_protobuf_type)
+ return protobuf_types
+
+
+# Writes the definition of an array that contains the pointers to the mutable
+# proto field for each policy in |policies| of the given |protobuf_type|.
+def _WriteChromeOSPolicyAccessHeader(f, protobuf_type):
+ f.write('// Access to the mutable protobuf function of all supported '
+ '%s user\n// policies.\n' % protobuf_type.lower())
+ f.write('struct %sPolicyAccess {\n'
+ ' const char* policy_key;\n'
+ ' enterprise_management::%sPolicyProto*\n'
+ ' (enterprise_management::CloudPolicySettings::'
+ '*mutable_proto_ptr)();\n'
+ '};\n' % (protobuf_type, protobuf_type))
+ f.write('extern const %sPolicyAccess k%sPolicyAccess[];\n\n'
+ % (protobuf_type, protobuf_type))
+
+
+# Writes policy_constants.h for use in Chrome OS.
+def _WriteChromeOSPolicyConstantsHeader(policies, os, f, riskTags):
emaxx 2017/03/02 16:56:15 nit: s/riskTags/risk_tags/ according to style guid
ljusten (tachyonic) 2017/03/16 23:45:27 Done.
+ f.write('#ifndef __BINDINGS_POLICY_CONSTANTS_H_\n'
+ '#define __BINDINGS_POLICY_CONSTANTS_H_\n\n')
+
+ # Forward declarations.
+ supported_user_policies = _GetSupportedUserPolicies(policies)
+ protobuf_types = _GetProtobufTypes(supported_user_policies)
+ f.write('namespace enterprise_management {\n'
+ 'class CloudPolicySettings;\n')
+ for protobuf_type in protobuf_types:
+ f.write('class %sPolicyProto;\n' % protobuf_type)
+ f.write('} // namespace enterprise_management\n\n')
+
+ f.write('namespace policy {\n\n')
+
+ # Policy keys.
+ f.write('// Registry key names for user and device policies.\n'
+ 'namespace key {\n\n')
+ for policy in policies:
+ f.write('extern const char k' + policy.name + '[];\n')
+ f.write('\n} // namespace key\n\n')
+
+ # User policy proto pointers, one struct for each protobuf type.
+ for protobuf_type in protobuf_types:
+ _WriteChromeOSPolicyAccessHeader(f, protobuf_type)
+
+ f.write('} // namespace policy\n\n'
+ '#endif // __BINDINGS_POLICY_CONSTANTS_H_\n')
+
+
+#------------------ Chrome OS policy constants source ------------------------#
+
+# Writes an array that contains the pointers to the mutable proto field for each
+# policy in |policies| of the given |protobuf_type|.
+def _WriteChromeOSPolicyAccessSource(policies, f, protobuf_type):
+ f.write('constexpr %sPolicyAccess k%sPolicyAccess[] = {\n'
+ % (protobuf_type, protobuf_type))
+ for policy in policies:
+ if policy.policy_protobuf_type == protobuf_type:
+ f.write(' {key::k%s,\n'
+ ' &em::CloudPolicySettings::mutable_%s},\n'
+ % (policy.name, policy.name.lower()))
+ # The list is nullptr-terminated.
+ f.write(' {nullptr, nullptr},\n'
+ '};\n\n')
+
+
+# Writes policy_constants.cc for use in Chrome OS.
+def _WriteChromeOSPolicyConstantsSource(policies, os, f, riskTags):
+ f.write('#include "bindings/cloud_policy.pb.h"\n'
+ '#include "bindings/policy_constants.h"\n\n'
+ 'namespace em = enterprise_management;\n\n'
+ 'namespace policy {\n\n')
+
+ # Policy keys.
+ f.write('namespace key {\n\n')
+ for policy in policies:
+ f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name))
+ f.write('\n} // namespace key\n\n')
+
+ # User policy proto pointers, one struct for each protobuf type.
+ supported_user_policies = _GetSupportedUserPolicies(policies)
+ protobuf_types = _GetProtobufTypes(supported_user_policies)
+ for protobuf_type in protobuf_types:
+ _WriteChromeOSPolicyAccessSource(supported_user_policies, f, protobuf_type)
+
+ f.write('} // namespace policy\n')
+
+#------------------ app restrictions -------------------------------#
def _WriteAppRestrictions(policies, os, f, riskTags):
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698