Don't preload rarely seen large images
[chromium-blink-merge.git] / components / policy / tools / generate_policy_source.py
blob62a37fbf5c798a5fa84d3bd7068db2027e196df7
1 #!/usr/bin/env python
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
4 # found in the LICENSE file.
6 '''python %prog [options] platform chromium_os_flag template
8 platform specifies which platform source is being generated for
9 and can be one of (win, mac, linux)
10 chromium_os_flag should be 1 if this is a Chromium OS build
11 template is the path to a .json policy template file.'''
13 from __future__ import with_statement
14 from functools import partial
15 import json
16 from optparse import OptionParser
17 import re
18 import sys
19 import textwrap
20 import types
21 from xml.sax.saxutils import escape as xml_escape
24 CHROME_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome'
25 CHROMIUM_POLICY_KEY = 'SOFTWARE\\\\Policies\\\\Chromium'
28 class PolicyDetails:
29 """Parses a policy template and caches all its details."""
31 # Maps policy types to a tuple with 4 other types:
32 # - the equivalent base::Value::Type or 'TYPE_EXTERNAL' if the policy
33 # references external data
34 # - the equivalent Protobuf field type
35 # - the name of one of the protobufs for shared policy types
36 # - the equivalent type in Android's App Restriction Schema
37 # TODO(joaodasilva): refactor the 'dict' type into a more generic 'json' type
38 # that can also be used to represent lists of other JSON objects.
39 TYPE_MAP = {
40 'dict': ('TYPE_DICTIONARY', 'string', 'String',
41 'string'),
42 'external': ('TYPE_EXTERNAL', 'string', 'String',
43 'invalid'),
44 'int': ('TYPE_INTEGER', 'int64', 'Integer',
45 'integer'),
46 'int-enum': ('TYPE_INTEGER', 'int64', 'Integer',
47 'choice'),
48 'list': ('TYPE_LIST', 'StringList', 'StringList',
49 'string'),
50 'main': ('TYPE_BOOLEAN', 'bool', 'Boolean',
51 'bool'),
52 'string': ('TYPE_STRING', 'string', 'String',
53 'string'),
54 'string-enum': ('TYPE_STRING', 'string', 'String',
55 'choice'),
56 'string-enum-list': ('TYPE_LIST', 'StringList', 'StringList',
57 'multi-select'),
60 class EnumItem:
61 def __init__(self, item):
62 self.caption = PolicyDetails._RemovePlaceholders(item['caption'])
63 self.value = item['value']
65 def __init__(self, policy, chrome_major_version, os, is_chromium_os):
66 self.id = policy['id']
67 self.name = policy['name']
68 features = policy.get('features', {})
69 self.can_be_recommended = features.get('can_be_recommended', False)
70 self.can_be_mandatory = features.get('can_be_mandatory', True)
71 self.is_deprecated = policy.get('deprecated', False)
72 self.is_device_only = policy.get('device_only', False)
73 self.schema = policy.get('schema', {})
74 self.has_enterprise_default = 'default_for_enterprise_users' in policy
75 if self.has_enterprise_default:
76 self.enterprise_default = policy['default_for_enterprise_users']
78 expected_platform = 'chrome_os' if is_chromium_os else os.lower()
79 self.platforms = []
80 for platform, version_range in [ p.split(':')
81 for p in policy['supported_on'] ]:
82 split_result = version_range.split('-')
83 if len(split_result) != 2:
84 raise RuntimeError('supported_on must have exactly one dash: "%s"' % p)
85 (version_min, version_max) = split_result
86 if version_min == '':
87 raise RuntimeError('supported_on must define a start version: "%s"' % p)
89 # Skip if the current Chromium version does not support the policy.
90 if (int(version_min) > chrome_major_version or
91 version_max != '' and int(version_max) < chrome_major_version):
92 continue
94 if platform.startswith('chrome.'):
95 platform_sub = platform[7:]
96 if platform_sub == '*':
97 self.platforms.extend(['win', 'mac', 'linux'])
98 else:
99 self.platforms.append(platform_sub)
100 else:
101 self.platforms.append(platform)
103 self.platforms.sort()
104 self.is_supported = expected_platform in self.platforms
106 if not PolicyDetails.TYPE_MAP.has_key(policy['type']):
107 raise NotImplementedError('Unknown policy type for %s: %s' %
108 (policy['name'], policy['type']))
109 self.policy_type, self.protobuf_type, self.policy_protobuf_type, \
110 self.restriction_type = PolicyDetails.TYPE_MAP[policy['type']]
111 self.schema = policy['schema']
113 self.desc = '\n'.join(
114 map(str.strip,
115 PolicyDetails._RemovePlaceholders(policy['desc']).splitlines()))
116 self.caption = PolicyDetails._RemovePlaceholders(policy['caption'])
117 self.max_size = policy.get('max_size', 0)
119 items = policy.get('items')
120 if items is None:
121 self.items = None
122 else:
123 self.items = [ PolicyDetails.EnumItem(entry) for entry in items ]
125 PH_PATTERN = re.compile('<ph[^>]*>([^<]*|[^<]*<ex>([^<]*)</ex>[^<]*)</ph>')
127 # Simplistic grit placeholder stripper.
128 @staticmethod
129 def _RemovePlaceholders(text):
130 result = ''
131 pos = 0
132 for m in PolicyDetails.PH_PATTERN.finditer(text):
133 result += text[pos:m.start(0)]
134 result += m.group(2) or m.group(1)
135 pos = m.end(0)
136 result += text[pos:]
137 return result
140 def ParseVersionFile(version_path):
141 major_version = None
142 for line in open(version_path, 'r').readlines():
143 key, val = line.rstrip('\r\n').split('=', 1)
144 if key == 'MAJOR':
145 major_version = val
146 break
147 if major_version is None:
148 raise RuntimeError('VERSION file does not contain major version.')
149 return major_version
152 def main():
153 parser = OptionParser(usage=__doc__)
154 parser.add_option('--pch', '--policy-constants-header', dest='header_path',
155 help='generate header file of policy constants',
156 metavar='FILE')
157 parser.add_option('--pcc', '--policy-constants-source', dest='source_path',
158 help='generate source file of policy constants',
159 metavar='FILE')
160 parser.add_option('--cpp', '--cloud-policy-protobuf',
161 dest='cloud_policy_proto_path',
162 help='generate cloud policy protobuf file',
163 metavar='FILE')
164 parser.add_option('--csp', '--chrome-settings-protobuf',
165 dest='chrome_settings_proto_path',
166 help='generate chrome settings protobuf file',
167 metavar='FILE')
168 parser.add_option('--cpd', '--cloud-policy-decoder',
169 dest='cloud_policy_decoder_path',
170 help='generate C++ code decoding the cloud policy protobuf',
171 metavar='FILE')
172 parser.add_option('--ard', '--app-restrictions-definition',
173 dest='app_restrictions_path',
174 help='generate an XML file as specified by '
175 'Android\'s App Restriction Schema',
176 metavar='FILE')
178 (opts, args) = parser.parse_args()
180 if len(args) != 4:
181 print('Please specify path to src/chrome/VERSION, platform, '
182 'chromium_os flag and input file as positional parameters.')
183 parser.print_help()
184 return 2
186 version_path = args[0]
187 os = args[1]
188 is_chromium_os = args[2] == '1'
189 template_file_name = args[3]
191 major_version = ParseVersionFile(version_path)
192 template_file_contents = _LoadJSONFile(template_file_name)
193 policy_details = [ PolicyDetails(policy, major_version, os, is_chromium_os)
194 for policy in _Flatten(template_file_contents) ]
195 sorted_policy_details = sorted(policy_details, key=lambda policy: policy.name)
197 def GenerateFile(path, writer, sorted=False, xml=False):
198 if path:
199 with open(path, 'w') as f:
200 _OutputGeneratedWarningHeader(f, template_file_name, xml)
201 writer(sorted and sorted_policy_details or policy_details, os, f)
203 GenerateFile(opts.header_path, _WritePolicyConstantHeader, sorted=True)
204 GenerateFile(opts.source_path, _WritePolicyConstantSource, sorted=True)
205 GenerateFile(opts.cloud_policy_proto_path, _WriteCloudPolicyProtobuf)
206 GenerateFile(opts.chrome_settings_proto_path, _WriteChromeSettingsProtobuf)
207 GenerateFile(opts.cloud_policy_decoder_path, _WriteCloudPolicyDecoder)
209 if os == 'android':
210 GenerateFile(opts.app_restrictions_path, _WriteAppRestrictions, xml=True)
212 return 0
215 #------------------ shared helpers ---------------------------------#
217 def _OutputGeneratedWarningHeader(f, template_file_path, xml_style):
218 left_margin = '//'
219 if xml_style:
220 left_margin = ' '
221 f.write('<?xml version="1.0" encoding="utf-8"?>\n'
222 '<!--\n')
223 else:
224 f.write('//\n')
226 f.write(left_margin + ' DO NOT MODIFY THIS FILE DIRECTLY!\n')
227 f.write(left_margin + ' IT IS GENERATED BY generate_policy_source.py\n')
228 f.write(left_margin + ' FROM ' + template_file_path + '\n')
230 if xml_style:
231 f.write('-->\n\n')
232 else:
233 f.write(left_margin + '\n\n')
236 COMMENT_WRAPPER = textwrap.TextWrapper()
237 COMMENT_WRAPPER.width = 80
238 COMMENT_WRAPPER.initial_indent = '// '
239 COMMENT_WRAPPER.subsequent_indent = '// '
240 COMMENT_WRAPPER.replace_whitespace = False
243 # Writes a comment, each line prefixed by // and wrapped to 80 spaces.
244 def _OutputComment(f, comment):
245 for line in comment.splitlines():
246 if len(line) == 0:
247 f.write('//')
248 else:
249 f.write(COMMENT_WRAPPER.fill(line))
250 f.write('\n')
253 # Returns an iterator over all the policies in |template_file_contents|.
254 def _Flatten(template_file_contents):
255 for policy in template_file_contents['policy_definitions']:
256 if policy['type'] == 'group':
257 for sub_policy in policy['policies']:
258 yield sub_policy
259 else:
260 yield policy
263 def _LoadJSONFile(json_file):
264 with open(json_file, 'r') as f:
265 text = f.read()
266 return eval(text)
269 #------------------ policy constants header ------------------------#
271 def _WritePolicyConstantHeader(policies, os, f):
272 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n'
273 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n'
274 '\n'
275 '#include <string>\n'
276 '\n'
277 '#include "base/basictypes.h"\n'
278 '#include "base/values.h"\n'
279 '#include "components/policy/core/common/policy_details.h"\n'
280 '#include "components/policy/core/common/policy_map.h"\n'
281 '\n'
282 'namespace policy {\n'
283 '\n'
284 'namespace internal {\n'
285 'struct SchemaData;\n'
286 '}\n\n')
288 if os == 'win':
289 f.write('// The windows registry path where Chrome policy '
290 'configuration resides.\n'
291 'extern const wchar_t kRegistryChromePolicyKey[];\n')
293 f.write('#if defined (OS_CHROMEOS)\n'
294 '// Sets default values for enterprise users.\n'
295 'void SetEnterpriseUsersDefaults(PolicyMap* policy_map);\n'
296 '#endif\n'
297 '\n'
298 '// Returns the PolicyDetails for |policy| if |policy| is a known\n'
299 '// Chrome policy, otherwise returns NULL.\n'
300 'const PolicyDetails* GetChromePolicyDetails('
301 'const std::string& policy);\n'
302 '\n'
303 '// Returns the schema data of the Chrome policy schema.\n'
304 'const internal::SchemaData* GetChromeSchemaData();\n'
305 '\n')
306 f.write('// Key names for the policy settings.\n'
307 'namespace key {\n\n')
308 for policy in policies:
309 # TODO(joaodasilva): Include only supported policies in
310 # configuration_policy_handler.cc and configuration_policy_handler_list.cc
311 # so that these names can be conditional on 'policy.is_supported'.
312 # http://crbug.com/223616
313 f.write('extern const char k' + policy.name + '[];\n')
314 f.write('\n} // namespace key\n\n'
315 '} // namespace policy\n\n'
316 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n')
319 #------------------ policy constants source ------------------------#
321 # A mapping of the simple schema types to base::Value::Types.
322 SIMPLE_SCHEMA_NAME_MAP = {
323 'boolean': 'TYPE_BOOLEAN',
324 'integer': 'TYPE_INTEGER',
325 'null' : 'TYPE_NULL',
326 'number' : 'TYPE_DOUBLE',
327 'string' : 'TYPE_STRING',
330 class SchemaNodesGenerator:
331 """Builds the internal structs to represent a JSON schema."""
333 def __init__(self, shared_strings):
334 """Creates a new generator.
336 |shared_strings| is a map of strings to a C expression that evaluates to
337 that string at runtime. This mapping can be used to reuse existing string
338 constants."""
339 self.shared_strings = shared_strings
340 self.schema_nodes = []
341 self.property_nodes = []
342 self.properties_nodes = []
343 self.restriction_nodes = []
344 self.int_enums = []
345 self.string_enums = []
346 self.simple_types = {
347 'boolean': None,
348 'integer': None,
349 'null': None,
350 'number': None,
351 'string': None,
353 self.stringlist_type = None
354 self.ranges = {}
355 self.id_map = {}
357 def GetString(self, s):
358 if s in self.shared_strings:
359 return self.shared_strings[s]
360 # Generate JSON escaped string, which is slightly different from desired
361 # C/C++ escaped string. Known differences includes unicode escaping format.
362 return json.dumps(s)
364 def AppendSchema(self, type, extra, comment=''):
365 index = len(self.schema_nodes)
366 self.schema_nodes.append((type, extra, comment))
367 return index
369 def AppendRestriction(self, first, second):
370 r = (str(first), str(second))
371 if not r in self.ranges:
372 self.ranges[r] = len(self.restriction_nodes)
373 self.restriction_nodes.append(r)
374 return self.ranges[r]
376 def GetSimpleType(self, name):
377 if self.simple_types[name] == None:
378 self.simple_types[name] = self.AppendSchema(
379 SIMPLE_SCHEMA_NAME_MAP[name],
381 'simple type: ' + name)
382 return self.simple_types[name]
384 def GetStringList(self):
385 if self.stringlist_type == None:
386 self.stringlist_type = self.AppendSchema(
387 'TYPE_LIST',
388 self.GetSimpleType('string'),
389 'simple type: stringlist')
390 return self.stringlist_type
392 def SchemaHaveRestriction(self, schema):
393 return any(keyword in schema for keyword in
394 ['minimum', 'maximum', 'enum', 'pattern'])
396 def IsConsecutiveInterval(self, seq):
397 sortedSeq = sorted(seq)
398 return all(sortedSeq[i] + 1 == sortedSeq[i + 1]
399 for i in xrange(len(sortedSeq) - 1))
401 def GetEnumIntegerType(self, schema, name):
402 assert all(type(x) == int for x in schema['enum'])
403 possible_values = schema['enum']
404 if self.IsConsecutiveInterval(possible_values):
405 index = self.AppendRestriction(max(possible_values), min(possible_values))
406 return self.AppendSchema('TYPE_INTEGER', index,
407 'integer with enumeration restriction (use range instead): %s' % name)
408 offset_begin = len(self.int_enums)
409 self.int_enums += possible_values
410 offset_end = len(self.int_enums)
411 return self.AppendSchema('TYPE_INTEGER',
412 self.AppendRestriction(offset_begin, offset_end),
413 'integer with enumeration restriction: %s' % name)
415 def GetEnumStringType(self, schema, name):
416 assert all(type(x) == str for x in schema['enum'])
417 offset_begin = len(self.string_enums)
418 self.string_enums += schema['enum']
419 offset_end = len(self.string_enums)
420 return self.AppendSchema('TYPE_STRING',
421 self.AppendRestriction(offset_begin, offset_end),
422 'string with enumeration restriction: %s' % name)
424 def GetEnumType(self, schema, name):
425 if len(schema['enum']) == 0:
426 raise RuntimeError('Empty enumeration in %s' % name)
427 elif schema['type'] == 'integer':
428 return self.GetEnumIntegerType(schema, name)
429 elif schema['type'] == 'string':
430 return self.GetEnumStringType(schema, name)
431 else:
432 raise RuntimeError('Unknown enumeration type in %s' % name)
434 def GetPatternType(self, schema, name):
435 if schema['type'] != 'string':
436 raise RuntimeError('Unknown pattern type in %s' % name)
437 pattern = schema['pattern']
438 # Try to compile the pattern to validate it, note that the syntax used
439 # here might be slightly different from re2.
440 # TODO(binjin): Add a python wrapper of re2 and use it here.
441 re.compile(pattern)
442 index = len(self.string_enums);
443 self.string_enums.append(pattern);
444 return self.AppendSchema('TYPE_STRING',
445 self.AppendRestriction(index, index),
446 'string with pattern restriction: %s' % name);
448 def GetRangedType(self, schema, name):
449 if schema['type'] != 'integer':
450 raise RuntimeError('Unknown ranged type in %s' % name)
451 min_value_set, max_value_set = False, False
452 if 'minimum' in schema:
453 min_value = int(schema['minimum'])
454 min_value_set = True
455 if 'maximum' in schema:
456 max_value = int(schema['minimum'])
457 max_value_set = True
458 if min_value_set and max_value_set and min_value > max_value:
459 raise RuntimeError('Invalid ranged type in %s' % name)
460 index = self.AppendRestriction(
461 str(max_value) if max_value_set else 'INT_MAX',
462 str(min_value) if min_value_set else 'INT_MIN')
463 return self.AppendSchema('TYPE_INTEGER',
464 index,
465 'integer with ranged restriction: %s' % name)
467 def Generate(self, schema, name):
468 """Generates the structs for the given schema.
470 |schema|: a valid JSON schema in a dictionary.
471 |name|: the name of the current node, for the generated comments."""
472 if schema.has_key('$ref'):
473 if schema.has_key('id'):
474 raise RuntimeError("Schemas with a $ref can't have an id")
475 if not isinstance(schema['$ref'], types.StringTypes):
476 raise RuntimeError("$ref attribute must be a string")
477 return schema['$ref']
478 if schema['type'] in self.simple_types:
479 if not self.SchemaHaveRestriction(schema):
480 # Simple types use shared nodes.
481 return self.GetSimpleType(schema['type'])
482 elif 'enum' in schema:
483 return self.GetEnumType(schema, name)
484 elif 'pattern' in schema:
485 return self.GetPatternType(schema, name)
486 else:
487 return self.GetRangedType(schema, name)
489 if schema['type'] == 'array':
490 # Special case for lists of strings, which is a common policy type.
491 # The 'type' may be missing if the schema has a '$ref' attribute.
492 if schema['items'].get('type', '') == 'string':
493 return self.GetStringList()
494 return self.AppendSchema('TYPE_LIST',
495 self.GenerateAndCollectID(schema['items'], 'items of ' + name))
496 elif schema['type'] == 'object':
497 # Reserve an index first, so that dictionaries come before their
498 # properties. This makes sure that the root node is the first in the
499 # SchemaNodes array.
500 index = self.AppendSchema('TYPE_DICTIONARY', -1)
502 if 'additionalProperties' in schema:
503 additionalProperties = self.GenerateAndCollectID(
504 schema['additionalProperties'],
505 'additionalProperties of ' + name)
506 else:
507 additionalProperties = -1
509 # Properties must be sorted by name, for the binary search lookup.
510 # Note that |properties| must be evaluated immediately, so that all the
511 # recursive calls to Generate() append the necessary child nodes; if
512 # |properties| were a generator then this wouldn't work.
513 sorted_properties = sorted(schema.get('properties', {}).items())
514 properties = [
515 (self.GetString(key), self.GenerateAndCollectID(subschema, key))
516 for key, subschema in sorted_properties ]
518 pattern_properties = []
519 for pattern, subschema in schema.get('patternProperties', {}).items():
520 pattern_properties.append((self.GetString(pattern),
521 self.GenerateAndCollectID(subschema, pattern)));
523 begin = len(self.property_nodes)
524 self.property_nodes += properties
525 end = len(self.property_nodes)
526 self.property_nodes += pattern_properties
527 pattern_end = len(self.property_nodes)
529 if index == 0:
530 self.root_properties_begin = begin
531 self.root_properties_end = end
533 extra = len(self.properties_nodes)
534 self.properties_nodes.append((begin, end, pattern_end,
535 additionalProperties, name))
537 # Set the right data at |index| now.
538 self.schema_nodes[index] = ('TYPE_DICTIONARY', extra, name)
539 return index
540 else:
541 assert False
543 def GenerateAndCollectID(self, schema, name):
544 """A wrapper of Generate(), will take the return value, check and add 'id'
545 attribute to self.id_map. The wrapper needs to be used for every call to
546 Generate().
548 index = self.Generate(schema, name)
549 if not schema.has_key('id'):
550 return index
551 id_str = schema['id']
552 if self.id_map.has_key(id_str):
553 raise RuntimeError('Duplicated id: ' + id_str)
554 self.id_map[id_str] = index
555 return index
557 def Write(self, f):
558 """Writes the generated structs to the given file.
560 |f| an open file to write to."""
561 f.write('const internal::SchemaNode kSchemas[] = {\n'
562 '// Type Extra\n')
563 for type, extra, comment in self.schema_nodes:
564 type += ','
565 f.write(' { base::Value::%-18s %3d }, // %s\n' % (type, extra, comment))
566 f.write('};\n\n')
568 if self.property_nodes:
569 f.write('const internal::PropertyNode kPropertyNodes[] = {\n'
570 '// Property #Schema\n')
571 for key, schema in self.property_nodes:
572 key += ','
573 f.write(' { %-50s %6d },\n' % (key, schema))
574 f.write('};\n\n')
576 if self.properties_nodes:
577 f.write('const internal::PropertiesNode kProperties[] = {\n'
578 '// Begin End PatternEnd Additional Properties\n')
579 for node in self.properties_nodes:
580 f.write(' { %5d, %5d, %10d, %5d }, // %s\n' % node)
581 f.write('};\n\n')
583 if self.restriction_nodes:
584 f.write('const internal::RestrictionNode kRestrictionNodes[] = {\n')
585 f.write('// FIRST, SECOND\n')
586 for first, second in self.restriction_nodes:
587 f.write(' {{ %-8s %4s}},\n' % (first + ',', second))
588 f.write('};\n\n')
590 if self.int_enums:
591 f.write('const int kIntegerEnumerations[] = {\n')
592 for possible_values in self.int_enums:
593 f.write(' %d,\n' % possible_values)
594 f.write('};\n\n')
596 if self.string_enums:
597 f.write('const char* kStringEnumerations[] = {\n')
598 for possible_values in self.string_enums:
599 f.write(' %s,\n' % self.GetString(possible_values))
600 f.write('};\n\n')
602 f.write('const internal::SchemaData kChromeSchemaData = {\n'
603 ' kSchemas,\n')
604 f.write(' kPropertyNodes,\n' if self.property_nodes else ' NULL,\n')
605 f.write(' kProperties,\n' if self.properties_nodes else ' NULL,\n')
606 f.write(' kRestrictionNodes,\n' if self.restriction_nodes else ' NULL,\n')
607 f.write(' kIntegerEnumerations,\n' if self.int_enums else ' NULL,\n')
608 f.write(' kStringEnumerations,\n' if self.string_enums else ' NULL,\n')
609 f.write('};\n\n')
611 def GetByID(self, id_str):
612 if not isinstance(id_str, types.StringTypes):
613 return id_str
614 if not self.id_map.has_key(id_str):
615 raise RuntimeError('Invalid $ref: ' + id_str)
616 return self.id_map[id_str]
618 def ResolveID(self, index, params):
619 return params[:index] + (self.GetByID(params[index]),) + params[index + 1:]
621 def ResolveReferences(self):
622 """Resolve reference mapping, required to be called after Generate()
624 After calling Generate(), the type of indices used in schema structures
625 might be either int or string. An int type suggests that it's a resolved
626 index, but for string type it's unresolved. Resolving a reference is as
627 simple as looking up for corresponding ID in self.id_map, and replace the
628 old index with the mapped index.
630 self.schema_nodes = map(partial(self.ResolveID, 1), self.schema_nodes)
631 self.property_nodes = map(partial(self.ResolveID, 1), self.property_nodes)
632 self.properties_nodes = map(partial(self.ResolveID, 3),
633 self.properties_nodes)
635 def _WritePolicyConstantSource(policies, os, f):
636 f.write('#include "policy/policy_constants.h"\n'
637 '\n'
638 '#include <algorithm>\n'
639 '#include <climits>\n'
640 '\n'
641 '#include "base/logging.h"\n'
642 '#include "components/policy/core/common/schema_internal.h"\n'
643 '\n'
644 'namespace policy {\n'
645 '\n'
646 'namespace {\n'
647 '\n')
649 # Generate the Chrome schema.
650 chrome_schema = {
651 'type': 'object',
652 'properties': {},
654 shared_strings = {}
655 for policy in policies:
656 shared_strings[policy.name] = "key::k%s" % policy.name
657 if policy.is_supported:
658 chrome_schema['properties'][policy.name] = policy.schema
660 # Note: this list must be kept in sync with the known property list of the
661 # Chrome schema, so that binary seaching in the PropertyNode array gets the
662 # right index on this array as well. See the implementation of
663 # GetChromePolicyDetails() below.
664 f.write('const PolicyDetails kChromePolicyDetails[] = {\n'
665 '// is_deprecated is_device_policy id max_external_data_size\n')
666 for policy in policies:
667 if policy.is_supported:
668 f.write(' { %-14s %-16s %3s, %24s },\n' % (
669 'true,' if policy.is_deprecated else 'false,',
670 'true,' if policy.is_device_only else 'false,',
671 policy.id,
672 policy.max_size))
673 f.write('};\n\n')
675 schema_generator = SchemaNodesGenerator(shared_strings)
676 schema_generator.GenerateAndCollectID(chrome_schema, 'root node')
677 schema_generator.ResolveReferences()
678 schema_generator.Write(f)
680 f.write('bool CompareKeys(const internal::PropertyNode& node,\n'
681 ' const std::string& key) {\n'
682 ' return node.key < key;\n'
683 '}\n\n')
685 f.write('} // namespace\n\n')
687 if os == 'win':
688 f.write('#if defined(GOOGLE_CHROME_BUILD)\n'
689 'const wchar_t kRegistryChromePolicyKey[] = '
690 'L"' + CHROME_POLICY_KEY + '";\n'
691 '#else\n'
692 'const wchar_t kRegistryChromePolicyKey[] = '
693 'L"' + CHROMIUM_POLICY_KEY + '";\n'
694 '#endif\n\n')
696 f.write('const internal::SchemaData* GetChromeSchemaData() {\n'
697 ' return &kChromeSchemaData;\n'
698 '}\n\n')
700 f.write('#if defined (OS_CHROMEOS)\n'
701 'void SetEnterpriseUsersDefaults(PolicyMap* policy_map) {\n')
703 for policy in policies:
704 if policy.has_enterprise_default:
705 if policy.policy_type == 'TYPE_BOOLEAN':
706 creation_expression = 'new base::FundamentalValue(%s)' %\
707 ('true' if policy.enterprise_default else 'false')
708 elif policy.policy_type == 'TYPE_INTEGER':
709 creation_expression = 'new base::FundamentalValue(%s)' %\
710 policy.enterprise_default
711 elif policy.policy_type == 'TYPE_STRING':
712 creation_expression = 'new base::StringValue("%s")' %\
713 policy.enterprise_default
714 else:
715 raise RuntimeError('Type %s of policy %s is not supported at '
716 'enterprise defaults' % (policy.policy_type,
717 policy.name))
718 f.write(' if (!policy_map->Get(key::k%s)) {\n'
719 ' policy_map->Set(key::k%s,\n'
720 ' POLICY_LEVEL_MANDATORY,\n'
721 ' POLICY_SCOPE_USER,\n'
722 ' %s,\n'
723 ' NULL);\n'
724 ' }\n' % (policy.name, policy.name, creation_expression))
726 f.write('}\n'
727 '#endif\n\n')
729 f.write('const PolicyDetails* GetChromePolicyDetails('
730 'const std::string& policy) {\n'
731 ' // First index in kPropertyNodes of the Chrome policies.\n'
732 ' static const int begin_index = %s;\n'
733 ' // One-past-the-end of the Chrome policies in kPropertyNodes.\n'
734 ' static const int end_index = %s;\n' %
735 (schema_generator.root_properties_begin,
736 schema_generator.root_properties_end))
737 f.write(' const internal::PropertyNode* begin =\n'
738 ' kPropertyNodes + begin_index;\n'
739 ' const internal::PropertyNode* end = kPropertyNodes + end_index;\n'
740 ' const internal::PropertyNode* it =\n'
741 ' std::lower_bound(begin, end, policy, CompareKeys);\n'
742 ' if (it == end || it->key != policy)\n'
743 ' return NULL;\n'
744 ' // This relies on kPropertyNodes from begin_index to end_index\n'
745 ' // having exactly the same policies (and in the same order) as\n'
746 ' // kChromePolicyDetails, so that binary searching on the first\n'
747 ' // gets the same results as a binary search on the second would.\n'
748 ' // However, kPropertyNodes has the policy names and\n'
749 ' // kChromePolicyDetails doesn\'t, so we obtain the index into\n'
750 ' // the second array by searching the first to avoid duplicating\n'
751 ' // the policy name pointers.\n'
752 ' // Offsetting |it| from |begin| here obtains the index we\'re\n'
753 ' // looking for.\n'
754 ' size_t index = it - begin;\n'
755 ' CHECK_LT(index, arraysize(kChromePolicyDetails));\n'
756 ' return kChromePolicyDetails + index;\n'
757 '}\n\n')
759 f.write('namespace key {\n\n')
760 for policy in policies:
761 # TODO(joaodasilva): Include only supported policies in
762 # configuration_policy_handler.cc and configuration_policy_handler_list.cc
763 # so that these names can be conditional on 'policy.is_supported'.
764 # http://crbug.com/223616
765 f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name))
766 f.write('\n} // namespace key\n\n'
767 '} // namespace policy\n')
770 #------------------ policy protobufs --------------------------------#
772 CHROME_SETTINGS_PROTO_HEAD = '''
773 syntax = "proto2";
775 option optimize_for = LITE_RUNTIME;
777 package enterprise_management;
779 // For StringList and PolicyOptions.
780 import "cloud_policy.proto";
785 CLOUD_POLICY_PROTO_HEAD = '''
786 syntax = "proto2";
788 option optimize_for = LITE_RUNTIME;
790 package enterprise_management;
792 message StringList {
793 repeated string entries = 1;
796 message PolicyOptions {
797 enum PolicyMode {
798 // The given settings are applied regardless of user choice.
799 MANDATORY = 0;
800 // The user may choose to override the given settings.
801 RECOMMENDED = 1;
802 // No policy value is present and the policy should be ignored.
803 UNSET = 2;
805 optional PolicyMode mode = 1 [default = MANDATORY];
808 message BooleanPolicyProto {
809 optional PolicyOptions policy_options = 1;
810 optional bool value = 2;
813 message IntegerPolicyProto {
814 optional PolicyOptions policy_options = 1;
815 optional int64 value = 2;
818 message StringPolicyProto {
819 optional PolicyOptions policy_options = 1;
820 optional string value = 2;
823 message StringListPolicyProto {
824 optional PolicyOptions policy_options = 1;
825 optional StringList value = 2;
831 # Field IDs [1..RESERVED_IDS] will not be used in the wrapping protobuf.
832 RESERVED_IDS = 2
835 def _WritePolicyProto(f, policy, fields):
836 _OutputComment(f, policy.caption + '\n\n' + policy.desc)
837 if policy.items is not None:
838 _OutputComment(f, '\nValid values:')
839 for item in policy.items:
840 _OutputComment(f, ' %s: %s' % (str(item.value), item.caption))
841 if policy.policy_type == 'TYPE_DICTIONARY':
842 _OutputComment(f, '\nValue schema:\n%s' %
843 json.dumps(policy.schema, sort_keys=True, indent=4,
844 separators=(',', ': ')))
845 _OutputComment(f, '\nSupported on: %s' % ', '.join(policy.platforms))
846 if policy.can_be_recommended and not policy.can_be_mandatory:
847 _OutputComment(f, '\nNote: this policy must have a RECOMMENDED ' +\
848 'PolicyMode set in PolicyOptions.')
849 f.write('message %sProto {\n' % policy.name)
850 f.write(' optional PolicyOptions policy_options = 1;\n')
851 f.write(' optional %s %s = 2;\n' % (policy.protobuf_type, policy.name))
852 f.write('}\n\n')
853 fields += [ ' optional %sProto %s = %s;\n' %
854 (policy.name, policy.name, policy.id + RESERVED_IDS) ]
857 def _WriteChromeSettingsProtobuf(policies, os, f):
858 f.write(CHROME_SETTINGS_PROTO_HEAD)
860 fields = []
861 f.write('// PBs for individual settings.\n\n')
862 for policy in policies:
863 # Note: this protobuf also gets the unsupported policies, since it's an
864 # exaustive list of all the supported user policies on any platform.
865 if not policy.is_device_only:
866 _WritePolicyProto(f, policy, fields)
868 f.write('// --------------------------------------------------\n'
869 '// Big wrapper PB containing the above groups.\n\n'
870 'message ChromeSettingsProto {\n')
871 f.write(''.join(fields))
872 f.write('}\n\n')
875 def _WriteCloudPolicyProtobuf(policies, os, f):
876 f.write(CLOUD_POLICY_PROTO_HEAD)
877 f.write('message CloudPolicySettings {\n')
878 for policy in policies:
879 if policy.is_supported and not policy.is_device_only:
880 f.write(' optional %sPolicyProto %s = %s;\n' %
881 (policy.policy_protobuf_type, policy.name,
882 policy.id + RESERVED_IDS))
883 f.write('}\n\n')
886 #------------------ protobuf decoder -------------------------------#
888 CPP_HEAD = '''
889 #include <limits>
890 #include <string>
892 #include "base/basictypes.h"
893 #include "base/callback.h"
894 #include "base/json/json_reader.h"
895 #include "base/logging.h"
896 #include "base/memory/scoped_ptr.h"
897 #include "base/memory/weak_ptr.h"
898 #include "base/values.h"
899 #include "components/policy/core/common/cloud/cloud_external_data_manager.h"
900 #include "components/policy/core/common/external_data_fetcher.h"
901 #include "components/policy/core/common/policy_map.h"
902 #include "policy/policy_constants.h"
903 #include "policy/proto/cloud_policy.pb.h"
905 using google::protobuf::RepeatedPtrField;
907 namespace policy {
909 namespace em = enterprise_management;
911 base::Value* DecodeIntegerValue(google::protobuf::int64 value) {
912 if (value < std::numeric_limits<int>::min() ||
913 value > std::numeric_limits<int>::max()) {
914 LOG(WARNING) << "Integer value " << value
915 << " out of numeric limits, ignoring.";
916 return NULL;
919 return new base::FundamentalValue(static_cast<int>(value));
922 base::ListValue* DecodeStringList(const em::StringList& string_list) {
923 base::ListValue* list_value = new base::ListValue;
924 RepeatedPtrField<std::string>::const_iterator entry;
925 for (entry = string_list.entries().begin();
926 entry != string_list.entries().end(); ++entry) {
927 list_value->AppendString(*entry);
929 return list_value;
932 base::Value* DecodeJson(const std::string& json) {
933 scoped_ptr<base::Value> root(
934 base::JSONReader::DeprecatedRead(json, base::JSON_ALLOW_TRAILING_COMMAS));
936 if (!root)
937 LOG(WARNING) << "Invalid JSON string, ignoring: " << json;
939 // Accept any Value type that parsed as JSON, and leave it to the handler to
940 // convert and check the concrete type.
941 return root.release();
944 void DecodePolicy(const em::CloudPolicySettings& policy,
945 base::WeakPtr<CloudExternalDataManager> external_data_manager,
946 PolicyMap* map) {
950 CPP_FOOT = '''}
952 } // namespace policy
956 def _CreateValue(type, arg):
957 if type == 'TYPE_BOOLEAN':
958 return 'new base::FundamentalValue(%s)' % arg
959 elif type == 'TYPE_INTEGER':
960 return 'DecodeIntegerValue(%s)' % arg
961 elif type == 'TYPE_STRING':
962 return 'new base::StringValue(%s)' % arg
963 elif type == 'TYPE_LIST':
964 return 'DecodeStringList(%s)' % arg
965 elif type == 'TYPE_DICTIONARY' or type == 'TYPE_EXTERNAL':
966 return 'DecodeJson(%s)' % arg
967 else:
968 raise NotImplementedError('Unknown type %s' % type)
971 def _CreateExternalDataFetcher(type, name):
972 if type == 'TYPE_EXTERNAL':
973 return 'new ExternalDataFetcher(external_data_manager, key::k%s)' % name
974 return 'NULL'
977 def _WritePolicyCode(f, policy):
978 membername = policy.name.lower()
979 proto_type = '%sPolicyProto' % policy.policy_protobuf_type
980 f.write(' if (policy.has_%s()) {\n' % membername)
981 f.write(' const em::%s& policy_proto = policy.%s();\n' %
982 (proto_type, membername))
983 f.write(' if (policy_proto.has_value()) {\n')
984 f.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n'
985 ' bool do_set = true;\n'
986 ' if (policy_proto.has_policy_options()) {\n'
987 ' do_set = false;\n'
988 ' switch(policy_proto.policy_options().mode()) {\n'
989 ' case em::PolicyOptions::MANDATORY:\n'
990 ' do_set = true;\n'
991 ' level = POLICY_LEVEL_MANDATORY;\n'
992 ' break;\n'
993 ' case em::PolicyOptions::RECOMMENDED:\n'
994 ' do_set = true;\n'
995 ' level = POLICY_LEVEL_RECOMMENDED;\n'
996 ' break;\n'
997 ' case em::PolicyOptions::UNSET:\n'
998 ' break;\n'
999 ' }\n'
1000 ' }\n'
1001 ' if (do_set) {\n')
1002 f.write(' base::Value* value = %s;\n' %
1003 (_CreateValue(policy.policy_type, 'policy_proto.value()')))
1004 # TODO(bartfab): |value| == NULL indicates that the policy value could not be
1005 # parsed successfully. Surface such errors in the UI.
1006 f.write(' if (value) {\n')
1007 f.write(' ExternalDataFetcher* external_data_fetcher = %s;\n' %
1008 _CreateExternalDataFetcher(policy.policy_type, policy.name))
1009 f.write(' map->Set(key::k%s, level, POLICY_SCOPE_USER,\n' %
1010 policy.name)
1011 f.write(' value, external_data_fetcher);\n'
1012 ' }\n'
1013 ' }\n'
1014 ' }\n'
1015 ' }\n')
1018 def _WriteCloudPolicyDecoder(policies, os, f):
1019 f.write(CPP_HEAD)
1020 for policy in policies:
1021 if policy.is_supported and not policy.is_device_only:
1022 _WritePolicyCode(f, policy)
1023 f.write(CPP_FOOT)
1026 def _WriteAppRestrictions(policies, os, f):
1028 def WriteRestrictionCommon(key):
1029 f.write(' <restriction\n'
1030 ' android:key="%s"\n' % key)
1031 f.write(' android:title="@string/%sTitle"\n' % key)
1032 f.write(' android:description="@string/%sDesc"\n' % key)
1034 def WriteItemsDefinition(key):
1035 f.write(' android:entries="@array/%sEntries"\n' % key)
1036 f.write(' android:entryValues="@array/%sValues"\n' % key)
1038 def WriteAppRestriction(policy):
1039 policy_name = policy.name
1040 WriteRestrictionCommon(policy_name)
1042 if policy.items is not None:
1043 WriteItemsDefinition(policy_name)
1045 f.write(' android:restrictionType="%s"/>' % policy.restriction_type)
1046 f.write('\n\n')
1048 # _WriteAppRestrictions body
1049 f.write('<restrictions xmlns:android="'
1050 'http://schemas.android.com/apk/res/android">\n\n')
1051 for policy in policies:
1052 if policy.is_supported and policy.restriction_type != 'invalid':
1053 WriteAppRestriction(policy)
1054 f.write('</restrictions>')
1056 if __name__ == '__main__':
1057 sys.exit(main())