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

Side by Side Diff: chrome/tools/build/generate_policy_source.py

Issue 24407003: Generate the Chrome policy schema at compile time. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/policy/browser_policy_connector.cc ('k') | 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 class EnumItem: 46 class EnumItem:
47 def __init__(self, item): 47 def __init__(self, item):
48 self.caption = PolicyDetails._RemovePlaceholders(item['caption']) 48 self.caption = PolicyDetails._RemovePlaceholders(item['caption'])
49 self.value = item['value'] 49 self.value = item['value']
50 50
51 def __init__(self, policy, os, is_chromium_os): 51 def __init__(self, policy, os, is_chromium_os):
52 self.id = policy['id'] 52 self.id = policy['id']
53 self.name = policy['name'] 53 self.name = policy['name']
54 self.is_deprecated = policy.get('deprecated', False) 54 self.is_deprecated = policy.get('deprecated', False)
55 self.is_device_only = policy.get('device_only', False) 55 self.is_device_only = policy.get('device_only', False)
56 self.schema = policy.get('schema', {})
56 57
57 if is_chromium_os: 58 if is_chromium_os:
58 expected_platform = 'chrome_os' 59 expected_platform = 'chrome_os'
59 else: 60 else:
60 expected_platform = os.lower() 61 expected_platform = os.lower()
61 62
62 self.platforms = [] 63 self.platforms = []
63 for platform, version in [ p.split(':') for p in policy['supported_on'] ]: 64 for platform, version in [ p.split(':') for p in policy['supported_on'] ]:
64 if not version.endswith('-'): 65 if not version.endswith('-'):
65 continue 66 continue
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 210
210 def _WritePolicyConstantHeader(policies, os, f): 211 def _WritePolicyConstantHeader(policies, os, f):
211 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' 212 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n'
212 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' 213 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n'
213 '\n' 214 '\n'
214 '#include <string>\n' 215 '#include <string>\n'
215 '\n' 216 '\n'
216 '#include "base/basictypes.h"\n' 217 '#include "base/basictypes.h"\n'
217 '#include "base/values.h"\n' 218 '#include "base/values.h"\n'
218 '\n' 219 '\n'
219 'namespace policy {\n\n') 220 'namespace policy {\n'
221 '\n'
222 'namespace internal {\n'
223 'struct SchemaNode;\n'
224 '}\n\n')
220 225
221 if os == 'win': 226 if os == 'win':
222 f.write('// The windows registry path where Chrome policy ' 227 f.write('// The windows registry path where Chrome policy '
223 'configuration resides.\n' 228 'configuration resides.\n'
224 'extern const wchar_t kRegistryChromePolicyKey[];\n') 229 'extern const wchar_t kRegistryChromePolicyKey[];\n')
225 230
226 f.write('// Lists metadata such as name, expected type and id for all\n' 231 f.write('// Lists metadata such as name, expected type and id for all\n'
227 '// policies. Used to initialize ConfigurationPolicyProviders and\n' 232 '// policies. Used to initialize ConfigurationPolicyProviders and\n'
228 '// CloudExternalDataManagers.\n' 233 '// CloudExternalDataManagers.\n'
229 'struct PolicyDefinitionList {\n' 234 'struct PolicyDefinitionList {\n'
230 ' struct Entry {\n' 235 ' struct Entry {\n'
231 ' const char* name;\n' 236 ' const char* name;\n'
232 ' base::Value::Type value_type;\n' 237 ' base::Value::Type value_type;\n'
233 ' bool device_policy;\n' 238 ' bool device_policy;\n'
234 ' int id;\n' 239 ' int id;\n'
235 ' size_t max_external_data_size;\n' 240 ' size_t max_external_data_size;\n'
236 ' };\n' 241 ' };\n'
237 '\n' 242 '\n'
238 ' const Entry* begin;\n' 243 ' const Entry* begin;\n'
239 ' const Entry* end;\n' 244 ' const Entry* end;\n'
240 '};\n' 245 '};\n'
241 '\n' 246 '\n'
242 '// Returns true if the given policy is deprecated.\n' 247 '// Returns true if the given policy is deprecated.\n'
243 'bool IsDeprecatedPolicy(const std::string& policy);\n' 248 'bool IsDeprecatedPolicy(const std::string& policy);\n'
244 '\n' 249 '\n'
245 '// Returns the default policy definition list for Chrome.\n' 250 '// Returns the default policy definition list for Chrome.\n'
246 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n') 251 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n'
252 '\n'
253 '// Returns the root node of the Chrome policy schema.\n'
254 'const internal::SchemaNode* GetChromeSchemaNode();\n'
255 '\n')
247 f.write('// Key names for the policy settings.\n' 256 f.write('// Key names for the policy settings.\n'
248 'namespace key {\n\n') 257 'namespace key {\n\n')
249 for policy in policies: 258 for policy in policies:
250 # TODO(joaodasilva): Include only supported policies in 259 # TODO(joaodasilva): Include only supported policies in
251 # configuration_policy_handler.cc and configuration_policy_handler_list.cc 260 # configuration_policy_handler.cc and configuration_policy_handler_list.cc
252 # so that these names can be conditional on 'policy.is_supported'. 261 # so that these names can be conditional on 'policy.is_supported'.
253 # http://crbug.com/223616 262 # http://crbug.com/223616
254 f.write('extern const char k' + policy.name + '[];\n') 263 f.write('extern const char k' + policy.name + '[];\n')
255 f.write('\n} // namespace key\n\n' 264 f.write('\n} // namespace key\n\n'
256 '} // namespace policy\n\n' 265 '} // namespace policy\n\n'
257 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') 266 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n')
258 267
259 268
260 #------------------ policy constants source ------------------------# 269 #------------------ policy constants source ------------------------#
261 270
262 def _GetValueType(policy_type): 271 def _GetValueType(policy_type):
263 return policy_type if policy_type != 'TYPE_EXTERNAL' else 'TYPE_DICTIONARY' 272 return policy_type if policy_type != 'TYPE_EXTERNAL' else 'TYPE_DICTIONARY'
264 273
265 274
275 # Map simple schema types to the address of a static SchemaNode that
276 # represents that type.
277 SIMPLE_SCHEMA_NAME_MAP = {
278 'boolean': '&kSchema_boolean',
279 'integer': '&kSchema_integer',
280 'null' : '&kSchema_null',
281 'number' : '&kSchema_number',
282 'string' : '&kSchema_string',
283 }
284
285
286 # Generates SchemaNodes, PropertyNodes and PropertiesNodes for |schema| and
287 # its sub-schemas, and writes them to |f|.
288 # |prefix| is prepended to the generated variable names.
289 # |shared_keys_map| is optional. If present, then property names are looked up
290 # there to reuse shared strings. Otherwise a new static C string is generated.
291 # Returns an expression that evaluates to a SchemaNode*.
292 def _GenerateSchema(f, schema, prefix, shared_keys_map={}):
293 # Simple types use the shared structures.
294 if schema['type'] in SIMPLE_SCHEMA_NAME_MAP:
295 return SIMPLE_SCHEMA_NAME_MAP[schema['type']]
296
297 if schema['type'] == 'array':
298 # Special case for lists of strings, which is a common policy type.
299 if schema['items']['type'] == 'string':
300 return '&kSchema_stringlist'
301 value_type = 'TYPE_LIST'
302 extra = _GenerateSchema(f, schema['items'], prefix + '_items')
303 elif schema['type'] == 'object':
304 value_type = 'TYPE_DICTIONARY'
305
306 if 'additionalProperties' in schema:
307 additionalProperties = _GenerateSchema(
308 f, schema['additionalProperties'], prefix + '_additionalProperties')
309 else:
310 additionalProperties = 'NULL'
311
312 properties = []
313 sorted_properties = sorted(schema.get('properties', {}).items())
314 for property_name, property_schema in sorted_properties:
315 schema_ptr = _GenerateSchema(
316 f, property_schema, prefix + '_property_' + property_name)
317 properties.append((property_name, schema_ptr))
318
319 f.write('const PropertyNode %s_propertyNodes[] = {\n' % prefix)
320 for key, ptr in properties:
321 f.write(' { %s, %s },\n' % (shared_keys_map.get(key, '"%s"' % key), ptr))
322 f.write('};\n\n')
323
324 f.write('const PropertiesNode %s_properties = {\n'
325 ' %s_propertyNodes,\n'
326 ' %s_propertyNodes + ARRAYSIZE_UNSAFE(%s_propertyNodes),\n'
327 ' %s,\n'
328 '};\n\n' % (prefix, prefix, prefix, prefix, additionalProperties))
329
330 extra = '&%s_properties' % prefix
331
332 f.write('const SchemaNode %s = {\n'
333 ' base::Value::%s,\n'
334 ' %s,\n'
335 '};\n\n' % (prefix, value_type, extra))
336
337 return '&%s' % prefix
338
339
266 def _WritePolicyConstantSource(policies, os, f): 340 def _WritePolicyConstantSource(policies, os, f):
267 f.write('#include "base/basictypes.h"\n' 341 f.write('#include "base/basictypes.h"\n'
268 '#include "base/logging.h"\n' 342 '#include "base/logging.h"\n'
343 '#include "components/policy/core/common/schema_internal.h"\n'
269 '#include "policy/policy_constants.h"\n' 344 '#include "policy/policy_constants.h"\n'
270 '\n' 345 '\n'
271 'namespace policy {\n\n') 346 'namespace policy {\n\n')
272 347
273 f.write('namespace {\n\n') 348 f.write('namespace {\n\n')
274 349
275 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') 350 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n')
276 for policy in policies: 351 for policy in policies:
277 if policy.is_supported: 352 if policy.is_supported:
278 f.write(' { key::k%s, base::Value::%s, %s, %s, %s },\n' % 353 f.write(' { key::k%s, base::Value::%s, %s, %s, %s },\n' %
(...skipping 13 matching lines...) Expand all
292 if has_deprecated_policies: 367 if has_deprecated_policies:
293 f.write('// List of deprecated policies.\n' 368 f.write('// List of deprecated policies.\n'
294 'const char* kDeprecatedPolicyList[] = {\n') 369 'const char* kDeprecatedPolicyList[] = {\n')
295 for policy in policies: 370 for policy in policies:
296 if policy.is_supported and policy.is_deprecated: 371 if policy.is_supported and policy.is_deprecated:
297 f.write(' key::k%s,\n' % policy.name) 372 f.write(' key::k%s,\n' % policy.name)
298 f.write('};\n\n') 373 f.write('};\n\n')
299 374
300 f.write('} // namespace\n\n') 375 f.write('} // namespace\n\n')
301 376
377 f.write('namespace internal {\n'
378 'namespace {\n\n')
379
380 # Write shared SchemaNodes for the simple types, and the string list.
381 for node in [ ('boolean', 'BOOLEAN', 'NULL'),
382 ('integer', 'INTEGER', 'NULL'),
383 ('null', 'NULL', 'NULL'),
384 ('number', 'DOUBLE', 'NULL'),
385 ('string', 'STRING', 'NULL'),
386 ('stringlist', 'LIST', '&kSchema_string') ]:
387 f.write('const SchemaNode kSchema_%s = {\n'
388 ' base::Value::TYPE_%s,\n'
389 ' %s,\n'
390 '};\n\n' % node)
391
392 # Generate the Chrome schema.
393 chrome_schema = {
394 'type': 'object',
395 'properties': {},
396 }
397 shared_keys_map = {}
398 for policy in policies:
399 if policy.is_supported:
400 chrome_schema['properties'][policy.name] = policy.schema
401 shared_keys_map[policy.name] = 'key::k%s' % policy.name
402
403 # And write it.
404 _GenerateSchema(f, chrome_schema, 'kSchema', shared_keys_map)
405
406 f.write('} // namespace\n'
407 '} // namespace internal\n\n')
408
302 if os == 'win': 409 if os == 'win':
303 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' 410 f.write('#if defined(GOOGLE_CHROME_BUILD)\n'
304 'const wchar_t kRegistryChromePolicyKey[] = ' 411 'const wchar_t kRegistryChromePolicyKey[] = '
305 'L"' + CHROME_POLICY_KEY + '";\n' 412 'L"' + CHROME_POLICY_KEY + '";\n'
306 '#else\n' 413 '#else\n'
307 'const wchar_t kRegistryChromePolicyKey[] = ' 414 'const wchar_t kRegistryChromePolicyKey[] = '
308 'L"' + CHROMIUM_POLICY_KEY + '";\n' 415 'L"' + CHROMIUM_POLICY_KEY + '";\n'
309 '#endif\n\n') 416 '#endif\n\n')
310 417
311 f.write('bool IsDeprecatedPolicy(const std::string& policy) {\n') 418 f.write('bool IsDeprecatedPolicy(const std::string& policy) {\n')
312 if has_deprecated_policies: 419 if has_deprecated_policies:
313 # arraysize() doesn't work with empty arrays. 420 # arraysize() doesn't work with empty arrays.
314 f.write(' for (size_t i = 0; i < arraysize(kDeprecatedPolicyList);' 421 f.write(' for (size_t i = 0; i < arraysize(kDeprecatedPolicyList);'
315 ' ++i) {\n' 422 ' ++i) {\n'
316 ' if (policy == kDeprecatedPolicyList[i])\n' 423 ' if (policy == kDeprecatedPolicyList[i])\n'
317 ' return true;\n' 424 ' return true;\n'
318 ' }\n') 425 ' }\n')
319 f.write(' return false;\n' 426 f.write(' return false;\n'
320 '}\n\n') 427 '}\n\n')
321 428
322 f.write('const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n' 429 f.write('const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n'
323 ' return &kChromePolicyList;\n' 430 ' return &kChromePolicyList;\n'
324 '}\n\n') 431 '}\n\n')
325 432
433 f.write('const internal::SchemaNode* GetChromeSchemaNode() {\n'
434 ' return &internal::kSchema;\n'
435 '}\n\n')
436
326 f.write('namespace key {\n\n') 437 f.write('namespace key {\n\n')
327 for policy in policies: 438 for policy in policies:
328 # TODO(joaodasilva): Include only supported policies in 439 # TODO(joaodasilva): Include only supported policies in
329 # configuration_policy_handler.cc and configuration_policy_handler_list.cc 440 # configuration_policy_handler.cc and configuration_policy_handler_list.cc
330 # so that these names can be conditional on 'policy.is_supported'. 441 # so that these names can be conditional on 'policy.is_supported'.
331 # http://crbug.com/223616 442 # http://crbug.com/223616
332 f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name)) 443 f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name))
333 f.write('\n} // namespace key\n\n' 444 f.write('\n} // namespace key\n\n'
334 '} // namespace policy\n') 445 '} // namespace policy\n')
335 446
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 def _WriteCloudPolicyDecoder(policies, os, f): 689 def _WriteCloudPolicyDecoder(policies, os, f):
579 f.write(CPP_HEAD) 690 f.write(CPP_HEAD)
580 for policy in policies: 691 for policy in policies:
581 if policy.is_supported and not policy.is_device_only: 692 if policy.is_supported and not policy.is_device_only:
582 _WritePolicyCode(f, policy) 693 _WritePolicyCode(f, policy)
583 f.write(CPP_FOOT) 694 f.write(CPP_FOOT)
584 695
585 696
586 if __name__ == '__main__': 697 if __name__ == '__main__':
587 sys.exit(main()) 698 sys.exit(main())
OLDNEW
« no previous file with comments | « chrome/browser/policy/browser_policy_connector.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698