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

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

Issue 2726103004: Generate code for encoding cloud policy protobufs (Closed)
Patch Set: Incorporated emaxx's comments. Created 3 years, 9 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 ffde6a8db2206a67adcb515143f79089926f8853..8f3c40c49b82d62cbe7253b736b4a36ce3329f74 100755
--- a/components/policy/tools/generate_policy_source.py
+++ b/components/policy/tools/generate_policy_source.py
@@ -207,6 +207,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:
@@ -222,11 +232,11 @@ def main():
major_version = ParseVersionFile(version_path)
template_file_contents = _LoadJSONFile(template_file_name)
- riskTags = RiskTags(template_file_contents)
+ risk_tags = RiskTags(template_file_contents)
policy_details = [ PolicyDetails(policy, major_version, os, is_chromium_os,
- riskTags.GetValidTags())
+ risk_tags.GetValidTags())
for policy in _Flatten(template_file_contents) ]
- riskTags.ComputeMaxTags(policy_details)
+ risk_tags.ComputeMaxTags(policy_details)
sorted_policy_details = sorted(policy_details, key=lambda policy: policy.name)
def GenerateFile(path, writer, sorted=False, xml=False):
@@ -234,7 +244,7 @@ def main():
with open(path, 'w') as f:
_OutputGeneratedWarningHeader(f, template_file_name, xml)
writer(sorted and sorted_policy_details or policy_details,
- os, f, riskTags)
+ os, f, risk_tags)
if opts.header_path:
GenerateFile(opts.header_path, _WritePolicyConstantHeader, sorted=True)
@@ -258,6 +268,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
@@ -317,7 +335,7 @@ def _LoadJSONFile(json_file):
#------------------ policy constants header ------------------------#
-def _WritePolicyConstantHeader(policies, os, f, riskTags):
+def _WritePolicyConstantHeader(policies, os, f, risk_tags):
f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n'
'#define CHROME_COMMON_POLICY_CONSTANTS_H_\n'
'\n'
@@ -706,7 +724,7 @@ def _GenerateDefaultValue(value):
return setup, 'std::move(default_value)'
return [], None
-def _WritePolicyConstantSource(policies, os, f, riskTags):
+def _WritePolicyConstantSource(policies, os, f, risk_tags):
f.write('#include "components/policy/policy_constants.h"\n'
'\n'
'#include <algorithm>\n'
@@ -747,7 +765,7 @@ def _WritePolicyConstantSource(policies, os, f, riskTags):
'true,' if policy.is_device_only else 'false,',
policy.id,
policy.max_size,
- riskTags.ToInitString(policy.tags)))
+ risk_tags.ToInitString(policy.tags)))
f.write('};\n\n')
schema_generator = SchemaNodesGenerator(shared_strings)
@@ -851,7 +869,7 @@ def _WritePolicyConstantSource(policies, os, f, riskTags):
'} // namespace policy\n')
-#------------------ policy risk tag header ------------------------#
+#------------------ policy risk tag header -------------------------#
class RiskTags(object):
'''Generates files and strings to translate the parsed risk tags.'''
@@ -908,7 +926,7 @@ class RiskTags(object):
self.enum_for_tag[tag['name']] = "RISK_TAG_" + \
tag['name'].replace("-","_").upper()
-def _WritePolicyRiskTagHeader(policies, os, f, riskTags):
+def _WritePolicyRiskTagHeader(policies, os, f, risk_tags):
f.write('#ifndef CHROME_COMMON_POLICY_RISK_TAG_H_\n'
'#define CHROME_COMMON_POLICY_RISK_TAG_H_\n'
'\n'
@@ -921,18 +939,18 @@ def _WritePolicyRiskTagHeader(policies, os, f, riskTags):
'// impact.\n'
'// The explanation of the single tags is stated in\n'
'// policy_templates.json within the \'risk_tag_definitions\' tag.'
- '\n' + riskTags.GenerateEnum() + '\n'
+ '\n' + risk_tags.GenerateEnum() + '\n'
'// This constant describes how many risk tags were used by the\n'
'// policy which uses the most risk tags. \n'
'const size_t kMaxRiskTagCount = ' + \
- riskTags.GetMaxTags() + ';\n'
+ risk_tags.GetMaxTags() + ';\n'
'\n'
'} // namespace policy\n'
'\n'
'#endif // CHROME_COMMON_POLICY_RISK_TAG_H_'
'\n')
-#------------------ policy protobufs --------------------------------#
+#------------------ policy protobufs -------------------------------#
CHROME_SETTINGS_PROTO_HEAD = '''
syntax = "proto2";
@@ -1019,7 +1037,7 @@ def _WritePolicyProto(f, policy, fields):
(policy.name, policy.name, policy.id + RESERVED_IDS) ]
-def _WriteChromeSettingsProtobuf(policies, os, f, riskTags):
+def _WriteChromeSettingsProtobuf(policies, os, f, risk_tags):
f.write(CHROME_SETTINGS_PROTO_HEAD)
fields = []
f.write('// PBs for individual settings.\n\n')
@@ -1036,7 +1054,7 @@ def _WriteChromeSettingsProtobuf(policies, os, f, riskTags):
f.write('}\n\n')
-def _WriteChromeSettingsFullRuntimeProtobuf(policies, os, f, riskTags):
+def _WriteChromeSettingsFullRuntimeProtobuf(policies, os, f, risk_tags):
# For full runtime, disable LITE_RUNTIME switch and import full runtime
# version of cloud_policy.proto.
f.write(CHROME_SETTINGS_PROTO_HEAD.replace(
@@ -1060,7 +1078,7 @@ def _WriteChromeSettingsFullRuntimeProtobuf(policies, os, f, riskTags):
f.write('}\n\n')
-def _WriteCloudPolicyProtobuf(policies, os, f, riskTags):
+def _WriteCloudPolicyProtobuf(policies, os, f, risk_tags):
f.write(CLOUD_POLICY_PROTO_HEAD)
f.write('message CloudPolicySettings {\n')
for policy in policies:
@@ -1071,7 +1089,7 @@ def _WriteCloudPolicyProtobuf(policies, os, f, riskTags):
f.write('}\n\n')
-def _WriteCloudPolicyFullRuntimeProtobuf(policies, os, f, riskTags):
+def _WriteCloudPolicyFullRuntimeProtobuf(policies, os, f, risk_tags):
# For full runtime, disable LITE_RUNTIME switch
f.write(CLOUD_POLICY_PROTO_HEAD.replace(
"option optimize_for = LITE_RUNTIME;",
@@ -1087,7 +1105,7 @@ def _WriteCloudPolicyFullRuntimeProtobuf(policies, os, f, riskTags):
#------------------ protobuf decoder -------------------------------#
-CPP_HEAD = '''
+CLOUD_POLICY_DECODER_CPP_HEAD = '''
#include <limits>
#include <memory>
#include <utility>
@@ -1151,7 +1169,7 @@ void DecodePolicy(const em::CloudPolicySettings& policy,
'''
-CPP_FOOT = '''}
+CLOUD_POLICY_DECODER_CPP_FOOT = '''}
} // namespace policy
'''
@@ -1178,7 +1196,7 @@ def _CreateExternalDataFetcher(type, name):
return 'nullptr'
-def _WritePolicyCode(f, policy):
+def _WriteCloudPolicyDecoderCode(f, policy):
membername = policy.name.lower()
proto_type = '%sPolicyProto' % policy.policy_protobuf_type
f.write(' if (policy.has_%s()) {\n' % membername)
@@ -1223,15 +1241,115 @@ def _WritePolicyCode(f, policy):
' }\n')
-def _WriteCloudPolicyDecoder(policies, os, f, riskTags):
- f.write(CPP_HEAD)
+def _WriteCloudPolicyDecoder(policies, os, f, risk_tags):
+ f.write(CLOUD_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)
+ _WriteCloudPolicyDecoderCode(f, policy)
+ f.write(CLOUD_POLICY_DECODER_CPP_FOOT)
+
+
+#------------------ Chrome OS policy constants header --------------#
+
+# Returns a list of supported user policies by filtering |policies|.
+def _GetSupportedUserPolicies(policies):
+ return filter(lambda policy: policy.is_supported and
+ not policy.is_device_only, policies)
+
+
+# Returns the set of all policy.policy_protobuf_type strings from |policies|.
+def _GetProtobufTypes(policies):
+ return set(policy.policy_protobuf_type for policy in policies)
+
+
+# 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, risk_tags):
+ 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, risk_tags):
+ 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):
+def _WriteAppRestrictions(policies, os, f, risk_tags):
def WriteRestrictionCommon(key):
f.write(' <restriction\n'
« 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