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

Side by Side Diff: components/policy/tools/generate_policy_source.py

Issue 2726103004: Generate code for encoding cloud policy protobufs (Closed)
Patch Set: 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 metavar='FILE') 196 metavar='FILE')
197 parser.add_option('--ard', '--app-restrictions-definition', 197 parser.add_option('--ard', '--app-restrictions-definition',
198 dest='app_restrictions_path', 198 dest='app_restrictions_path',
199 help='generate an XML file as specified by ' 199 help='generate an XML file as specified by '
200 'Android\'s App Restriction Schema', 200 'Android\'s App Restriction Schema',
201 metavar='FILE') 201 metavar='FILE')
202 parser.add_option('--rth', '--risk-tag-header', 202 parser.add_option('--rth', '--risk-tag-header',
203 dest='risk_header_path', 203 dest='risk_header_path',
204 help='generate header file for policy risk tags', 204 help='generate header file for policy risk tags',
205 metavar='FILE') 205 metavar='FILE')
206 parser.add_option('--crospch', '--cros-policy-constants-header',
207 dest='cros_constants_header_path',
208 help='generate header file of policy constants for use in '
209 'Chrome OS',
210 metavar='FILE')
211 parser.add_option('--crospcc', '--cros-policy-constants-source',
212 dest='cros_constants_source_path',
213 help='generate source file of policy constants for use in '
214 'Chrome OS',
215 metavar='FILE')
206 (opts, args) = parser.parse_args() 216 (opts, args) = parser.parse_args()
207 217
208 if len(args) != 4: 218 if len(args) != 4:
209 print('Please specify path to src/chrome/VERSION, platform, ' 219 print('Please specify path to src/chrome/VERSION, platform, '
210 'chromium_os flag and input file as positional parameters.') 220 'chromium_os flag and input file as positional parameters.')
211 parser.print_help() 221 parser.print_help()
212 return 2 222 return 2
213 223
214 version_path = args[0] 224 version_path = args[0]
215 os = args[1] 225 os = args[1]
(...skipping 28 matching lines...) Expand all
244 GenerateFile(opts.cloud_policy_full_runtime_proto_path, 254 GenerateFile(opts.cloud_policy_full_runtime_proto_path,
245 _WriteCloudPolicyFullRuntimeProtobuf) 255 _WriteCloudPolicyFullRuntimeProtobuf)
246 if opts.chrome_settings_proto_path: 256 if opts.chrome_settings_proto_path:
247 GenerateFile(opts.chrome_settings_proto_path, _WriteChromeSettingsProtobuf) 257 GenerateFile(opts.chrome_settings_proto_path, _WriteChromeSettingsProtobuf)
248 if opts.cloud_policy_decoder_path: 258 if opts.cloud_policy_decoder_path:
249 GenerateFile(opts.cloud_policy_decoder_path, _WriteCloudPolicyDecoder) 259 GenerateFile(opts.cloud_policy_decoder_path, _WriteCloudPolicyDecoder)
250 260
251 if os == 'android' and opts.app_restrictions_path: 261 if os == 'android' and opts.app_restrictions_path:
252 GenerateFile(opts.app_restrictions_path, _WriteAppRestrictions, xml=True) 262 GenerateFile(opts.app_restrictions_path, _WriteAppRestrictions, xml=True)
253 263
264 # Generated code for Chrome OS (unused in Chromium).
265 if opts.cros_constants_header_path:
266 GenerateFile(opts.cros_constants_header_path,
267 _WriteChromeOSPolicyConstantsHeader, sorted=True)
268 if opts.cros_constants_source_path:
269 GenerateFile(opts.cros_constants_source_path,
270 _WriteChromeOSPolicyConstantsSource, sorted=True)
271
254 return 0 272 return 0
255 273
256 274
257 #------------------ shared helpers ---------------------------------# 275 #------------------ shared helpers ---------------------------------#
258 276
259 def _OutputGeneratedWarningHeader(f, template_file_path, xml_style): 277 def _OutputGeneratedWarningHeader(f, template_file_path, xml_style):
260 left_margin = '//' 278 left_margin = '//'
261 if xml_style: 279 if xml_style:
262 left_margin = ' ' 280 left_margin = ' '
263 f.write('<?xml version="1.0" encoding="utf-8"?>\n' 281 f.write('<?xml version="1.0" encoding="utf-8"?>\n'
(...skipping 827 matching lines...) Expand 10 before | Expand all | Expand 10 after
1091 for policy in policies: 1109 for policy in policies:
1092 if policy.is_supported and not policy.is_device_only: 1110 if policy.is_supported and not policy.is_device_only:
1093 f.write(' optional %sPolicyProto %s = %s;\n' % 1111 f.write(' optional %sPolicyProto %s = %s;\n' %
1094 (policy.policy_protobuf_type, policy.name, 1112 (policy.policy_protobuf_type, policy.name,
1095 policy.id + RESERVED_IDS)) 1113 policy.id + RESERVED_IDS))
1096 f.write('}\n\n') 1114 f.write('}\n\n')
1097 1115
1098 1116
1099 #------------------ protobuf decoder -------------------------------# 1117 #------------------ protobuf decoder -------------------------------#
1100 1118
1101 CPP_HEAD = ''' 1119 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.
1102 #include <limits> 1120 #include <limits>
1103 #include <memory> 1121 #include <memory>
1104 #include <utility> 1122 #include <utility>
1105 #include <string> 1123 #include <string>
1106 1124
1107 #include "base/callback.h" 1125 #include "base/callback.h"
1108 #include "base/json/json_reader.h" 1126 #include "base/json/json_reader.h"
1109 #include "base/logging.h" 1127 #include "base/logging.h"
1110 #include "base/memory/ptr_util.h" 1128 #include "base/memory/ptr_util.h"
1111 #include "base/memory/weak_ptr.h" 1129 #include "base/memory/weak_ptr.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1155 // convert and check the concrete type. 1173 // convert and check the concrete type.
1156 return root; 1174 return root;
1157 } 1175 }
1158 1176
1159 void DecodePolicy(const em::CloudPolicySettings& policy, 1177 void DecodePolicy(const em::CloudPolicySettings& policy,
1160 base::WeakPtr<CloudExternalDataManager> external_data_manager, 1178 base::WeakPtr<CloudExternalDataManager> external_data_manager,
1161 PolicyMap* map) { 1179 PolicyMap* map) {
1162 ''' 1180 '''
1163 1181
1164 1182
1165 CPP_FOOT = '''} 1183 POLICY_DECODER_CPP_FOOT = '''}
1166 1184
1167 } // namespace policy 1185 } // namespace policy
1168 ''' 1186 '''
1169 1187
1170 1188
1171 def _CreateValue(type, arg): 1189 def _CreateValue(type, arg):
1172 if type == 'Type::BOOLEAN': 1190 if type == 'Type::BOOLEAN':
1173 return 'new base::FundamentalValue(%s)' % arg 1191 return 'new base::FundamentalValue(%s)' % arg
1174 elif type == 'Type::INTEGER': 1192 elif type == 'Type::INTEGER':
1175 return 'DecodeIntegerValue(%s)' % arg 1193 return 'DecodeIntegerValue(%s)' % arg
1176 elif type == 'Type::STRING': 1194 elif type == 'Type::STRING':
1177 return 'new base::StringValue(%s)' % arg 1195 return 'new base::StringValue(%s)' % arg
1178 elif type == 'Type::LIST': 1196 elif type == 'Type::LIST':
1179 return 'DecodeStringList(%s)' % arg 1197 return 'DecodeStringList(%s)' % arg
1180 elif type == 'Type::DICTIONARY' or type == 'TYPE_EXTERNAL': 1198 elif type == 'Type::DICTIONARY' or type == 'TYPE_EXTERNAL':
1181 return 'DecodeJson(%s)' % arg 1199 return 'DecodeJson(%s)' % arg
1182 else: 1200 else:
1183 raise NotImplementedError('Unknown type %s' % type) 1201 raise NotImplementedError('Unknown type %s' % type)
1184 1202
1185 1203
1186 def _CreateExternalDataFetcher(type, name): 1204 def _CreateExternalDataFetcher(type, name):
1187 if type == 'TYPE_EXTERNAL': 1205 if type == 'TYPE_EXTERNAL':
1188 return 'new ExternalDataFetcher(external_data_manager, key::k%s)' % name 1206 return 'new ExternalDataFetcher(external_data_manager, key::k%s)' % name
1189 return 'nullptr' 1207 return 'nullptr'
1190 1208
1191 1209
1192 def _WritePolicyCode(f, policy): 1210 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.
1193 membername = policy.name.lower() 1211 membername = policy.name.lower()
1194 proto_type = '%sPolicyProto' % policy.policy_protobuf_type 1212 proto_type = '%sPolicyProto' % policy.policy_protobuf_type
1195 f.write(' if (policy.has_%s()) {\n' % membername) 1213 f.write(' if (policy.has_%s()) {\n' % membername)
1196 f.write(' const em::%s& policy_proto = policy.%s();\n' % 1214 f.write(' const em::%s& policy_proto = policy.%s();\n' %
1197 (proto_type, membername)) 1215 (proto_type, membername))
1198 f.write(' if (policy_proto.has_value()) {\n') 1216 f.write(' if (policy_proto.has_value()) {\n')
1199 f.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n' 1217 f.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n'
1200 ' bool do_set = true;\n' 1218 ' bool do_set = true;\n'
1201 ' if (policy_proto.has_policy_options()) {\n' 1219 ' if (policy_proto.has_policy_options()) {\n'
1202 ' do_set = false;\n' 1220 ' do_set = false;\n'
(...skipping 25 matching lines...) Expand all
1228 ' POLICY_SOURCE_CLOUD, \n' 1246 ' POLICY_SOURCE_CLOUD, \n'
1229 ' std::move(value), \n' 1247 ' std::move(value), \n'
1230 ' std::move(external_data_fetcher));\n' 1248 ' std::move(external_data_fetcher));\n'
1231 ' }\n' 1249 ' }\n'
1232 ' }\n' 1250 ' }\n'
1233 ' }\n' 1251 ' }\n'
1234 ' }\n') 1252 ' }\n')
1235 1253
1236 1254
1237 def _WriteCloudPolicyDecoder(policies, os, f, riskTags): 1255 def _WriteCloudPolicyDecoder(policies, os, f, riskTags):
1238 f.write(CPP_HEAD) 1256 f.write(POLICY_DECODER_CPP_HEAD)
1239 for policy in policies: 1257 for policy in policies:
1240 if policy.is_supported and not policy.is_device_only: 1258 if policy.is_supported and not policy.is_device_only:
1241 _WritePolicyCode(f, policy) 1259 _WritePolicyDecoderCode(f, policy)
1242 f.write(CPP_FOOT) 1260 f.write(POLICY_DECODER_CPP_FOOT)
1243 1261
1244 1262
1263 #------------------ 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.
1264
1265 # Returns a list of supported user policies by filtering |policies|.
1266 def _GetSupportedUserPolicies(policies):
1267 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.
1268 not policy.is_device_only, policies)
1269
1270
1271 # Returns the set of all policy.policy_protobuf_type strings from |policies|.
1272 def _GetProtobufTypes(policies):
1273 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.
1274 for policy in policies:
1275 protobuf_types.add(policy.policy_protobuf_type)
1276 return protobuf_types
1277
1278
1279 # Writes the definition of an array that contains the pointers to the mutable
1280 # proto field for each policy in |policies| of the given |protobuf_type|.
1281 def _WriteChromeOSPolicyAccessHeader(f, protobuf_type):
1282 f.write('// Access to the mutable protobuf function of all supported '
1283 '%s user\n// policies.\n' % protobuf_type.lower())
1284 f.write('struct %sPolicyAccess {\n'
1285 ' const char* policy_key;\n'
1286 ' enterprise_management::%sPolicyProto*\n'
1287 ' (enterprise_management::CloudPolicySettings::'
1288 '*mutable_proto_ptr)();\n'
1289 '};\n' % (protobuf_type, protobuf_type))
1290 f.write('extern const %sPolicyAccess k%sPolicyAccess[];\n\n'
1291 % (protobuf_type, protobuf_type))
1292
1293
1294 # Writes policy_constants.h for use in Chrome OS.
1295 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.
1296 f.write('#ifndef __BINDINGS_POLICY_CONSTANTS_H_\n'
1297 '#define __BINDINGS_POLICY_CONSTANTS_H_\n\n')
1298
1299 # Forward declarations.
1300 supported_user_policies = _GetSupportedUserPolicies(policies)
1301 protobuf_types = _GetProtobufTypes(supported_user_policies)
1302 f.write('namespace enterprise_management {\n'
1303 'class CloudPolicySettings;\n')
1304 for protobuf_type in protobuf_types:
1305 f.write('class %sPolicyProto;\n' % protobuf_type)
1306 f.write('} // namespace enterprise_management\n\n')
1307
1308 f.write('namespace policy {\n\n')
1309
1310 # Policy keys.
1311 f.write('// Registry key names for user and device policies.\n'
1312 'namespace key {\n\n')
1313 for policy in policies:
1314 f.write('extern const char k' + policy.name + '[];\n')
1315 f.write('\n} // namespace key\n\n')
1316
1317 # User policy proto pointers, one struct for each protobuf type.
1318 for protobuf_type in protobuf_types:
1319 _WriteChromeOSPolicyAccessHeader(f, protobuf_type)
1320
1321 f.write('} // namespace policy\n\n'
1322 '#endif // __BINDINGS_POLICY_CONSTANTS_H_\n')
1323
1324
1325 #------------------ Chrome OS policy constants source ------------------------#
1326
1327 # Writes an array that contains the pointers to the mutable proto field for each
1328 # policy in |policies| of the given |protobuf_type|.
1329 def _WriteChromeOSPolicyAccessSource(policies, f, protobuf_type):
1330 f.write('constexpr %sPolicyAccess k%sPolicyAccess[] = {\n'
1331 % (protobuf_type, protobuf_type))
1332 for policy in policies:
1333 if policy.policy_protobuf_type == protobuf_type:
1334 f.write(' {key::k%s,\n'
1335 ' &em::CloudPolicySettings::mutable_%s},\n'
1336 % (policy.name, policy.name.lower()))
1337 # The list is nullptr-terminated.
1338 f.write(' {nullptr, nullptr},\n'
1339 '};\n\n')
1340
1341
1342 # Writes policy_constants.cc for use in Chrome OS.
1343 def _WriteChromeOSPolicyConstantsSource(policies, os, f, riskTags):
1344 f.write('#include "bindings/cloud_policy.pb.h"\n'
1345 '#include "bindings/policy_constants.h"\n\n'
1346 'namespace em = enterprise_management;\n\n'
1347 'namespace policy {\n\n')
1348
1349 # Policy keys.
1350 f.write('namespace key {\n\n')
1351 for policy in policies:
1352 f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name))
1353 f.write('\n} // namespace key\n\n')
1354
1355 # User policy proto pointers, one struct for each protobuf type.
1356 supported_user_policies = _GetSupportedUserPolicies(policies)
1357 protobuf_types = _GetProtobufTypes(supported_user_policies)
1358 for protobuf_type in protobuf_types:
1359 _WriteChromeOSPolicyAccessSource(supported_user_policies, f, protobuf_type)
1360
1361 f.write('} // namespace policy\n')
1362
1363
1364 #------------------ app restrictions -------------------------------#
1365
1245 def _WriteAppRestrictions(policies, os, f, riskTags): 1366 def _WriteAppRestrictions(policies, os, f, riskTags):
1246 1367
1247 def WriteRestrictionCommon(key): 1368 def WriteRestrictionCommon(key):
1248 f.write(' <restriction\n' 1369 f.write(' <restriction\n'
1249 ' android:key="%s"\n' % key) 1370 ' android:key="%s"\n' % key)
1250 f.write(' android:title="@string/%sTitle"\n' % key) 1371 f.write(' android:title="@string/%sTitle"\n' % key)
1251 f.write(' android:description="@string/%sDesc"\n' % key) 1372 f.write(' android:description="@string/%sDesc"\n' % key)
1252 1373
1253 def WriteItemsDefinition(key): 1374 def WriteItemsDefinition(key):
1254 f.write(' android:entries="@array/%sEntries"\n' % key) 1375 f.write(' android:entries="@array/%sEntries"\n' % key)
(...skipping 13 matching lines...) Expand all
1268 f.write('<restrictions xmlns:android="' 1389 f.write('<restrictions xmlns:android="'
1269 'http://schemas.android.com/apk/res/android">\n\n') 1390 'http://schemas.android.com/apk/res/android">\n\n')
1270 for policy in policies: 1391 for policy in policies:
1271 if (policy.is_supported and policy.restriction_type != 'invalid' and 1392 if (policy.is_supported and policy.restriction_type != 'invalid' and
1272 not policy.is_deprecated and not policy.is_future): 1393 not policy.is_deprecated and not policy.is_future):
1273 WriteAppRestriction(policy) 1394 WriteAppRestriction(policy)
1274 f.write('</restrictions>') 1395 f.write('</restrictions>')
1275 1396
1276 if __name__ == '__main__': 1397 if __name__ == '__main__':
1277 sys.exit(main()) 1398 sys.exit(main())
OLDNEW
« 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