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
16 from optparse
import OptionParser
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'
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.
40 'dict': ('TYPE_DICTIONARY', 'string', 'String',
42 'external': ('TYPE_EXTERNAL', 'string', 'String',
44 'int': ('TYPE_INTEGER', 'int64', 'Integer',
46 'int-enum': ('TYPE_INTEGER', 'int64', 'Integer',
48 'list': ('TYPE_LIST', 'StringList', 'StringList',
50 'main': ('TYPE_BOOLEAN', 'bool', 'Boolean',
52 'string': ('TYPE_STRING', 'string', 'String',
54 'string-enum': ('TYPE_STRING', 'string', 'String',
56 'string-enum-list': ('TYPE_LIST', 'StringList', 'StringList',
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()
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
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
):
94 if platform
.startswith('chrome.'):
95 platform_sub
= platform
[7:]
96 if platform_sub
== '*':
97 self
.platforms
.extend(['win', 'mac', 'linux'])
99 self
.platforms
.append(platform_sub
)
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(
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')
123 self
.items
= [ PolicyDetails
.EnumItem(entry
) for entry
in items
]
125 PH_PATTERN
= re
.compile('<ph[^>]*>([^<]*|[^<]*<ex>([^<]*)</ex>[^<]*)</ph>')
127 # Simplistic grit placeholder stripper.
129 def _RemovePlaceholders(text
):
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)
140 def ParseVersionFile(version_path
):
142 for line
in open(version_path
, 'r').readlines():
143 key
, val
= line
.rstrip('\r\n').split('=', 1)
147 if major_version
is None:
148 raise RuntimeError('VERSION file does not contain major version.')
153 parser
= OptionParser(usage
=__doc__
)
154 parser
.add_option('--pch', '--policy-constants-header', dest
='header_path',
155 help='generate header file of policy constants',
157 parser
.add_option('--pcc', '--policy-constants-source', dest
='source_path',
158 help='generate source file of policy constants',
160 parser
.add_option('--cpp', '--cloud-policy-protobuf',
161 dest
='cloud_policy_proto_path',
162 help='generate cloud policy protobuf file',
164 parser
.add_option('--csp', '--chrome-settings-protobuf',
165 dest
='chrome_settings_proto_path',
166 help='generate chrome settings protobuf 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',
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',
178 (opts
, args
) = parser
.parse_args()
181 print('Please specify path to src/chrome/VERSION, platform, '
182 'chromium_os flag and input file as positional parameters.')
186 version_path
= args
[0]
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):
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
)
210 GenerateFile(opts
.app_restrictions_path
, _WriteAppRestrictions
, xml
=True)
215 #------------------ shared helpers ---------------------------------#
217 def _OutputGeneratedWarningHeader(f
, template_file_path
, xml_style
):
221 f
.write('<?xml version="1.0" encoding="utf-8"?>\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')
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():
249 f
.write(COMMENT_WRAPPER
.fill(line
))
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']:
263 def _LoadJSONFile(json_file
):
264 with
open(json_file
, 'r') as f
:
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'
275 '#include <string>\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'
282 'namespace policy {\n'
284 'namespace internal {\n'
285 'struct SchemaData;\n'
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'
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'
303 '// Returns the schema data of the Chrome policy schema.\n'
304 'const internal::SchemaData* GetChromeSchemaData();\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
339 self
.shared_strings
= shared_strings
340 self
.schema_nodes
= []
341 self
.property_nodes
= []
342 self
.properties_nodes
= []
343 self
.restriction_nodes
= []
345 self
.string_enums
= []
346 self
.simple_types
= {
353 self
.stringlist_type
= None
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.
364 def AppendSchema(self
, type, extra
, comment
=''):
365 index
= len(self
.schema_nodes
)
366 self
.schema_nodes
.append((type, extra
, comment
))
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(
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
)
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.
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'])
455 if 'maximum' in schema
:
456 max_value
= int(schema
['minimum'])
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',
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
)
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
500 index
= self
.AppendSchema('TYPE_DICTIONARY', -1)
502 if 'additionalProperties' in schema
:
503 additionalProperties
= self
.GenerateAndCollectID(
504 schema
['additionalProperties'],
505 'additionalProperties of ' + name
)
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())
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
)
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
)
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
548 index
= self
.Generate(schema
, name
)
549 if not schema
.has_key('id'):
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
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'
563 for type, extra
, comment
in self
.schema_nodes
:
565 f
.write(' { base::Value::%-18s %3d }, // %s\n' % (type, extra
, comment
))
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
:
573 f
.write(' { %-50s %6d },\n' % (key
, schema
))
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
)
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
))
591 f
.write('const int kIntegerEnumerations[] = {\n')
592 for possible_values
in self
.int_enums
:
593 f
.write(' %d,\n' % possible_values
)
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
))
602 f
.write('const internal::SchemaData kChromeSchemaData = {\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')
611 def GetByID(self
, id_str
):
612 if not isinstance(id_str
, types
.StringTypes
):
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'
638 '#include <algorithm>\n'
639 '#include <climits>\n'
641 '#include "base/logging.h"\n'
642 '#include "components/policy/core/common/policy_types.h"\n'
643 '#include "components/policy/core/common/schema_internal.h"\n'
645 'namespace policy {\n'
650 # Generate the Chrome schema.
656 for policy
in policies
:
657 shared_strings
[policy
.name
] = "key::k%s" % policy
.name
658 if policy
.is_supported
:
659 chrome_schema
['properties'][policy
.name
] = policy
.schema
661 # Note: this list must be kept in sync with the known property list of the
662 # Chrome schema, so that binary seaching in the PropertyNode array gets the
663 # right index on this array as well. See the implementation of
664 # GetChromePolicyDetails() below.
665 f
.write('const PolicyDetails kChromePolicyDetails[] = {\n'
666 '// is_deprecated is_device_policy id max_external_data_size\n')
667 for policy
in policies
:
668 if policy
.is_supported
:
669 f
.write(' { %-14s %-16s %3s, %24s },\n' % (
670 'true,' if policy
.is_deprecated
else 'false,',
671 'true,' if policy
.is_device_only
else 'false,',
676 schema_generator
= SchemaNodesGenerator(shared_strings
)
677 schema_generator
.GenerateAndCollectID(chrome_schema
, 'root node')
678 schema_generator
.ResolveReferences()
679 schema_generator
.Write(f
)
681 f
.write('bool CompareKeys(const internal::PropertyNode& node,\n'
682 ' const std::string& key) {\n'
683 ' return node.key < key;\n'
686 f
.write('} // namespace\n\n')
689 f
.write('#if defined(GOOGLE_CHROME_BUILD)\n'
690 'const wchar_t kRegistryChromePolicyKey[] = '
691 'L"' + CHROME_POLICY_KEY
+ '";\n'
693 'const wchar_t kRegistryChromePolicyKey[] = '
694 'L"' + CHROMIUM_POLICY_KEY
+ '";\n'
697 f
.write('const internal::SchemaData* GetChromeSchemaData() {\n'
698 ' return &kChromeSchemaData;\n'
701 f
.write('#if defined (OS_CHROMEOS)\n'
702 'void SetEnterpriseUsersDefaults(PolicyMap* policy_map) {\n')
704 for policy
in policies
:
705 if policy
.has_enterprise_default
:
706 if policy
.policy_type
== 'TYPE_BOOLEAN':
707 creation_expression
= 'new base::FundamentalValue(%s)' %\
708 ('true' if policy
.enterprise_default
else 'false')
709 elif policy
.policy_type
== 'TYPE_INTEGER':
710 creation_expression
= 'new base::FundamentalValue(%s)' %\
711 policy
.enterprise_default
712 elif policy
.policy_type
== 'TYPE_STRING':
713 creation_expression
= 'new base::StringValue("%s")' %\
714 policy
.enterprise_default
716 raise RuntimeError('Type %s of policy %s is not supported at '
717 'enterprise defaults' % (policy
.policy_type
,
719 f
.write(' if (!policy_map->Get(key::k%s)) {\n'
720 ' policy_map->Set(key::k%s,\n'
721 ' POLICY_LEVEL_MANDATORY,\n'
722 ' POLICY_SCOPE_USER,\n'
723 ' POLICY_SOURCE_ENTERPRISE_DEFAULT,\n'
726 ' }\n' % (policy
.name
, policy
.name
, creation_expression
))
731 f
.write('const PolicyDetails* GetChromePolicyDetails('
732 'const std::string& policy) {\n'
733 ' // First index in kPropertyNodes of the Chrome policies.\n'
734 ' static const int begin_index = %s;\n'
735 ' // One-past-the-end of the Chrome policies in kPropertyNodes.\n'
736 ' static const int end_index = %s;\n' %
737 (schema_generator
.root_properties_begin
,
738 schema_generator
.root_properties_end
))
739 f
.write(' const internal::PropertyNode* begin =\n'
740 ' kPropertyNodes + begin_index;\n'
741 ' const internal::PropertyNode* end = kPropertyNodes + end_index;\n'
742 ' const internal::PropertyNode* it =\n'
743 ' std::lower_bound(begin, end, policy, CompareKeys);\n'
744 ' if (it == end || it->key != policy)\n'
746 ' // This relies on kPropertyNodes from begin_index to end_index\n'
747 ' // having exactly the same policies (and in the same order) as\n'
748 ' // kChromePolicyDetails, so that binary searching on the first\n'
749 ' // gets the same results as a binary search on the second would.\n'
750 ' // However, kPropertyNodes has the policy names and\n'
751 ' // kChromePolicyDetails doesn\'t, so we obtain the index into\n'
752 ' // the second array by searching the first to avoid duplicating\n'
753 ' // the policy name pointers.\n'
754 ' // Offsetting |it| from |begin| here obtains the index we\'re\n'
756 ' size_t index = it - begin;\n'
757 ' CHECK_LT(index, arraysize(kChromePolicyDetails));\n'
758 ' return kChromePolicyDetails + index;\n'
761 f
.write('namespace key {\n\n')
762 for policy
in policies
:
763 # TODO(joaodasilva): Include only supported policies in
764 # configuration_policy_handler.cc and configuration_policy_handler_list.cc
765 # so that these names can be conditional on 'policy.is_supported'.
766 # http://crbug.com/223616
767 f
.write('const char k{name}[] = "{name}";\n'.format(name
=policy
.name
))
768 f
.write('\n} // namespace key\n\n'
769 '} // namespace policy\n')
772 #------------------ policy protobufs --------------------------------#
774 CHROME_SETTINGS_PROTO_HEAD
= '''
777 option optimize_for = LITE_RUNTIME;
779 package enterprise_management;
781 // For StringList and PolicyOptions.
782 import "cloud_policy.proto";
787 CLOUD_POLICY_PROTO_HEAD
= '''
790 option optimize_for = LITE_RUNTIME;
792 package enterprise_management;
795 repeated string entries = 1;
798 message PolicyOptions {
800 // The given settings are applied regardless of user choice.
802 // The user may choose to override the given settings.
804 // No policy value is present and the policy should be ignored.
807 optional PolicyMode mode = 1 [default = MANDATORY];
810 message BooleanPolicyProto {
811 optional PolicyOptions policy_options = 1;
812 optional bool value = 2;
815 message IntegerPolicyProto {
816 optional PolicyOptions policy_options = 1;
817 optional int64 value = 2;
820 message StringPolicyProto {
821 optional PolicyOptions policy_options = 1;
822 optional string value = 2;
825 message StringListPolicyProto {
826 optional PolicyOptions policy_options = 1;
827 optional StringList value = 2;
833 # Field IDs [1..RESERVED_IDS] will not be used in the wrapping protobuf.
837 def _WritePolicyProto(f
, policy
, fields
):
838 _OutputComment(f
, policy
.caption
+ '\n\n' + policy
.desc
)
839 if policy
.items
is not None:
840 _OutputComment(f
, '\nValid values:')
841 for item
in policy
.items
:
842 _OutputComment(f
, ' %s: %s' % (str(item
.value
), item
.caption
))
843 if policy
.policy_type
== 'TYPE_DICTIONARY':
844 _OutputComment(f
, '\nValue schema:\n%s' %
845 json
.dumps(policy
.schema
, sort_keys
=True, indent
=4,
846 separators
=(',', ': ')))
847 _OutputComment(f
, '\nSupported on: %s' % ', '.join(policy
.platforms
))
848 if policy
.can_be_recommended
and not policy
.can_be_mandatory
:
849 _OutputComment(f
, '\nNote: this policy must have a RECOMMENDED ' +\
850 'PolicyMode set in PolicyOptions.')
851 f
.write('message %sProto {\n' % policy
.name
)
852 f
.write(' optional PolicyOptions policy_options = 1;\n')
853 f
.write(' optional %s %s = 2;\n' % (policy
.protobuf_type
, policy
.name
))
855 fields
+= [ ' optional %sProto %s = %s;\n' %
856 (policy
.name
, policy
.name
, policy
.id + RESERVED_IDS
) ]
859 def _WriteChromeSettingsProtobuf(policies
, os
, f
):
860 f
.write(CHROME_SETTINGS_PROTO_HEAD
)
863 f
.write('// PBs for individual settings.\n\n')
864 for policy
in policies
:
865 # Note: This protobuf also gets the unsupported policies, since it's an
866 # exhaustive list of all the supported user policies on any platform.
867 if not policy
.is_device_only
:
868 _WritePolicyProto(f
, policy
, fields
)
870 f
.write('// --------------------------------------------------\n'
871 '// Big wrapper PB containing the above groups.\n\n'
872 'message ChromeSettingsProto {\n')
873 f
.write(''.join(fields
))
877 def _WriteCloudPolicyProtobuf(policies
, os
, f
):
878 f
.write(CLOUD_POLICY_PROTO_HEAD
)
879 f
.write('message CloudPolicySettings {\n')
880 for policy
in policies
:
881 if policy
.is_supported
and not policy
.is_device_only
:
882 f
.write(' optional %sPolicyProto %s = %s;\n' %
883 (policy
.policy_protobuf_type
, policy
.name
,
884 policy
.id + RESERVED_IDS
))
888 #------------------ protobuf decoder -------------------------------#
894 #include "base/basictypes.h"
895 #include "base/callback.h"
896 #include "base/json/json_reader.h"
897 #include "base/logging.h"
898 #include "base/memory/scoped_ptr.h"
899 #include "base/memory/weak_ptr.h"
900 #include "base/values.h"
901 #include "components/policy/core/common/cloud/cloud_external_data_manager.h"
902 #include "components/policy/core/common/external_data_fetcher.h"
903 #include "components/policy/core/common/policy_map.h"
904 #include "components/policy/core/common/policy_types.h"
905 #include "policy/policy_constants.h"
906 #include "policy/proto/cloud_policy.pb.h"
908 using google::protobuf::RepeatedPtrField;
912 namespace em = enterprise_management;
914 base::Value* DecodeIntegerValue(google::protobuf::int64 value) {
915 if (value < std::numeric_limits<int>::min() ||
916 value > std::numeric_limits<int>::max()) {
917 LOG(WARNING) << "Integer value " << value
918 << " out of numeric limits, ignoring.";
922 return new base::FundamentalValue(static_cast<int>(value));
925 base::ListValue* DecodeStringList(const em::StringList& string_list) {
926 base::ListValue* list_value = new base::ListValue;
927 RepeatedPtrField<std::string>::const_iterator entry;
928 for (entry = string_list.entries().begin();
929 entry != string_list.entries().end(); ++entry) {
930 list_value->AppendString(*entry);
935 base::Value* DecodeJson(const std::string& json) {
936 scoped_ptr<base::Value> root =
937 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS);
940 LOG(WARNING) << "Invalid JSON string, ignoring: " << json;
942 // Accept any Value type that parsed as JSON, and leave it to the handler to
943 // convert and check the concrete type.
944 return root.release();
947 void DecodePolicy(const em::CloudPolicySettings& policy,
948 base::WeakPtr<CloudExternalDataManager> external_data_manager,
955 } // namespace policy
959 def _CreateValue(type, arg
):
960 if type == 'TYPE_BOOLEAN':
961 return 'new base::FundamentalValue(%s)' % arg
962 elif type == 'TYPE_INTEGER':
963 return 'DecodeIntegerValue(%s)' % arg
964 elif type == 'TYPE_STRING':
965 return 'new base::StringValue(%s)' % arg
966 elif type == 'TYPE_LIST':
967 return 'DecodeStringList(%s)' % arg
968 elif type == 'TYPE_DICTIONARY' or type == 'TYPE_EXTERNAL':
969 return 'DecodeJson(%s)' % arg
971 raise NotImplementedError('Unknown type %s' % type)
974 def _CreateExternalDataFetcher(type, name
):
975 if type == 'TYPE_EXTERNAL':
976 return 'new ExternalDataFetcher(external_data_manager, key::k%s)' % name
980 def _WritePolicyCode(f
, policy
):
981 membername
= policy
.name
.lower()
982 proto_type
= '%sPolicyProto' % policy
.policy_protobuf_type
983 f
.write(' if (policy.has_%s()) {\n' % membername
)
984 f
.write(' const em::%s& policy_proto = policy.%s();\n' %
985 (proto_type
, membername
))
986 f
.write(' if (policy_proto.has_value()) {\n')
987 f
.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n'
988 ' bool do_set = true;\n'
989 ' if (policy_proto.has_policy_options()) {\n'
991 ' switch(policy_proto.policy_options().mode()) {\n'
992 ' case em::PolicyOptions::MANDATORY:\n'
994 ' level = POLICY_LEVEL_MANDATORY;\n'
996 ' case em::PolicyOptions::RECOMMENDED:\n'
998 ' level = POLICY_LEVEL_RECOMMENDED;\n'
1000 ' case em::PolicyOptions::UNSET:\n'
1005 f
.write(' base::Value* value = %s;\n' %
1006 (_CreateValue(policy
.policy_type
, 'policy_proto.value()')))
1007 # TODO(bartfab): |value| == NULL indicates that the policy value could not be
1008 # parsed successfully. Surface such errors in the UI.
1009 f
.write(' if (value) {\n')
1010 f
.write(' ExternalDataFetcher* external_data_fetcher = %s;\n' %
1011 _CreateExternalDataFetcher(policy
.policy_type
, policy
.name
))
1012 f
.write(' map->Set(key::k%s, \n' % policy
.name
)
1013 f
.write(' level, \n'
1014 ' POLICY_SCOPE_USER, \n'
1015 ' POLICY_SOURCE_CLOUD, \n'
1017 ' external_data_fetcher);\n'
1024 def _WriteCloudPolicyDecoder(policies
, os
, f
):
1026 for policy
in policies
:
1027 if policy
.is_supported
and not policy
.is_device_only
:
1028 _WritePolicyCode(f
, policy
)
1032 def _WriteAppRestrictions(policies
, os
, f
):
1034 def WriteRestrictionCommon(key
):
1035 f
.write(' <restriction\n'
1036 ' android:key="%s"\n' % key
)
1037 f
.write(' android:title="@string/%sTitle"\n' % key
)
1038 f
.write(' android:description="@string/%sDesc"\n' % key
)
1040 def WriteItemsDefinition(key
):
1041 f
.write(' android:entries="@array/%sEntries"\n' % key
)
1042 f
.write(' android:entryValues="@array/%sValues"\n' % key
)
1044 def WriteAppRestriction(policy
):
1045 policy_name
= policy
.name
1046 WriteRestrictionCommon(policy_name
)
1048 if policy
.items
is not None:
1049 WriteItemsDefinition(policy_name
)
1051 f
.write(' android:restrictionType="%s"/>' % policy
.restriction_type
)
1054 # _WriteAppRestrictions body
1055 f
.write('<restrictions xmlns:android="'
1056 'http://schemas.android.com/apk/res/android">\n\n')
1057 for policy
in policies
:
1058 if policy
.is_supported
and policy
.restriction_type
!= 'invalid':
1059 WriteAppRestriction(policy
)
1060 f
.write('</restrictions>')
1062 if __name__
== '__main__':