| OLD | NEW |
| 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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 #------------------ policy constants header ------------------------# | 205 #------------------ policy constants header ------------------------# |
| 206 | 206 |
| 207 def _WritePolicyConstantHeader(policies, os, f): | 207 def _WritePolicyConstantHeader(policies, os, f): |
| 208 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 208 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| 209 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 209 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| 210 '\n' | 210 '\n' |
| 211 '#include <string>\n' | 211 '#include <string>\n' |
| 212 '\n' | 212 '\n' |
| 213 '#include "base/basictypes.h"\n' | 213 '#include "base/basictypes.h"\n' |
| 214 '#include "base/values.h"\n' | 214 '#include "base/values.h"\n' |
| 215 '#include "components/policy/core/common/policy_details.h"\n' |
| 215 '\n' | 216 '\n' |
| 216 'namespace policy {\n' | 217 'namespace policy {\n' |
| 217 '\n' | 218 '\n' |
| 218 'namespace internal {\n' | 219 'namespace internal {\n' |
| 219 'struct SchemaData;\n' | 220 'struct SchemaData;\n' |
| 220 '}\n\n') | 221 '}\n\n') |
| 221 | 222 |
| 222 if os == 'win': | 223 if os == 'win': |
| 223 f.write('// The windows registry path where Chrome policy ' | 224 f.write('// The windows registry path where Chrome policy ' |
| 224 'configuration resides.\n' | 225 'configuration resides.\n' |
| 225 'extern const wchar_t kRegistryChromePolicyKey[];\n') | 226 'extern const wchar_t kRegistryChromePolicyKey[];\n') |
| 226 | 227 |
| 227 f.write('// Lists metadata such as name, expected type and id for all\n' | 228 f.write('// Returns the PolicyDetails for |policy| if |policy| is a known\n' |
| 228 '// policies. Used to initialize ConfigurationPolicyProviders and\n' | 229 '// Chrome policy, otherwise returns NULL.\n' |
| 229 '// CloudExternalDataManagers.\n' | 230 'const PolicyDetails* GetChromePolicyDetails(' |
| 230 'struct PolicyDefinitionList {\n' | 231 'const std::string& policy);\n' |
| 231 ' struct Entry {\n' | |
| 232 ' const char* name;\n' | |
| 233 ' base::Value::Type value_type;\n' | |
| 234 ' bool device_policy;\n' | |
| 235 ' int id;\n' | |
| 236 ' size_t max_external_data_size;\n' | |
| 237 ' };\n' | |
| 238 '\n' | |
| 239 ' const Entry* begin;\n' | |
| 240 ' const Entry* end;\n' | |
| 241 '};\n' | |
| 242 '\n' | |
| 243 '// Returns true if the given policy is deprecated.\n' | |
| 244 'bool IsDeprecatedPolicy(const std::string& policy);\n' | |
| 245 '\n' | |
| 246 '// Returns the default policy definition list for Chrome.\n' | |
| 247 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n' | |
| 248 '\n' | 232 '\n' |
| 249 '// Returns the schema data of the Chrome policy schema.\n' | 233 '// Returns the schema data of the Chrome policy schema.\n' |
| 250 'const internal::SchemaData* GetChromeSchemaData();\n' | 234 'const internal::SchemaData* GetChromeSchemaData();\n' |
| 251 '\n') | 235 '\n') |
| 252 f.write('// Key names for the policy settings.\n' | 236 f.write('// Key names for the policy settings.\n' |
| 253 'namespace key {\n\n') | 237 'namespace key {\n\n') |
| 254 for policy in policies: | 238 for policy in policies: |
| 255 # TODO(joaodasilva): Include only supported policies in | 239 # TODO(joaodasilva): Include only supported policies in |
| 256 # configuration_policy_handler.cc and configuration_policy_handler_list.cc | 240 # configuration_policy_handler.cc and configuration_policy_handler_list.cc |
| 257 # so that these names can be conditional on 'policy.is_supported'. | 241 # so that these names can be conditional on 'policy.is_supported'. |
| 258 # http://crbug.com/223616 | 242 # http://crbug.com/223616 |
| 259 f.write('extern const char k' + policy.name + '[];\n') | 243 f.write('extern const char k' + policy.name + '[];\n') |
| 260 f.write('\n} // namespace key\n\n' | 244 f.write('\n} // namespace key\n\n' |
| 261 '} // namespace policy\n\n' | 245 '} // namespace policy\n\n' |
| 262 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') | 246 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') |
| 263 | 247 |
| 264 | 248 |
| 265 #------------------ policy constants source ------------------------# | 249 #------------------ policy constants source ------------------------# |
| 266 | 250 |
| 267 def _GetValueType(policy_type): | |
| 268 return policy_type if policy_type != 'TYPE_EXTERNAL' else 'TYPE_DICTIONARY' | |
| 269 | |
| 270 | |
| 271 # A mapping of the simple schema types to base::Value::Types. | 251 # A mapping of the simple schema types to base::Value::Types. |
| 272 SIMPLE_SCHEMA_NAME_MAP = { | 252 SIMPLE_SCHEMA_NAME_MAP = { |
| 273 'boolean': 'TYPE_BOOLEAN', | 253 'boolean': 'TYPE_BOOLEAN', |
| 274 'integer': 'TYPE_INTEGER', | 254 'integer': 'TYPE_INTEGER', |
| 275 'null' : 'TYPE_NULL', | 255 'null' : 'TYPE_NULL', |
| 276 'number' : 'TYPE_DOUBLE', | 256 'number' : 'TYPE_DOUBLE', |
| 277 'string' : 'TYPE_STRING', | 257 'string' : 'TYPE_STRING', |
| 278 } | 258 } |
| 279 | 259 |
| 280 | 260 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 # Properties must be sorted by name, for the binary search lookup. | 336 # Properties must be sorted by name, for the binary search lookup. |
| 357 # Note that |properties| must be evaluated immediately, so that all the | 337 # Note that |properties| must be evaluated immediately, so that all the |
| 358 # recursive calls to Generate() append the necessary child nodes; if | 338 # recursive calls to Generate() append the necessary child nodes; if |
| 359 # |properties| were a generator then this wouldn't work. | 339 # |properties| were a generator then this wouldn't work. |
| 360 sorted_properties = sorted(schema.get('properties', {}).items()) | 340 sorted_properties = sorted(schema.get('properties', {}).items()) |
| 361 properties = [ (self.GetString(key), self.Generate(schema, key)) | 341 properties = [ (self.GetString(key), self.Generate(schema, key)) |
| 362 for key, schema in sorted_properties ] | 342 for key, schema in sorted_properties ] |
| 363 begin = len(self.property_nodes) | 343 begin = len(self.property_nodes) |
| 364 self.property_nodes += properties | 344 self.property_nodes += properties |
| 365 end = len(self.property_nodes) | 345 end = len(self.property_nodes) |
| 346 if index == 0: |
| 347 self.root_properties_begin = begin |
| 348 self.root_properties_end = end |
| 366 | 349 |
| 367 extra = len(self.properties_nodes) | 350 extra = len(self.properties_nodes) |
| 368 self.properties_nodes.append((begin, end, additionalProperties, name)) | 351 self.properties_nodes.append((begin, end, additionalProperties, name)) |
| 369 | 352 |
| 370 # Set the right data at |index| now. | 353 # Set the right data at |index| now. |
| 371 self.schema_nodes[index] = ('TYPE_DICTIONARY', extra, name) | 354 self.schema_nodes[index] = ('TYPE_DICTIONARY', extra, name) |
| 372 return index | 355 return index |
| 373 else: | 356 else: |
| 374 assert False | 357 assert False |
| 375 | 358 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 398 f.write('};\n\n') | 381 f.write('};\n\n') |
| 399 | 382 |
| 400 f.write('const internal::SchemaData kChromeSchemaData = {\n' | 383 f.write('const internal::SchemaData kChromeSchemaData = {\n' |
| 401 ' kSchemas,\n' | 384 ' kSchemas,\n' |
| 402 ' kPropertyNodes,\n' | 385 ' kPropertyNodes,\n' |
| 403 ' kProperties,\n' | 386 ' kProperties,\n' |
| 404 '};\n\n') | 387 '};\n\n') |
| 405 | 388 |
| 406 | 389 |
| 407 def _WritePolicyConstantSource(policies, os, f): | 390 def _WritePolicyConstantSource(policies, os, f): |
| 408 f.write('#include "base/basictypes.h"\n' | 391 f.write('#include "policy/policy_constants.h"\n' |
| 392 '\n' |
| 393 '#include <algorithm>\n' |
| 394 '\n' |
| 409 '#include "base/logging.h"\n' | 395 '#include "base/logging.h"\n' |
| 410 '#include "components/policy/core/common/schema_internal.h"\n' | 396 '#include "components/policy/core/common/schema_internal.h"\n' |
| 411 '#include "policy/policy_constants.h"\n' | |
| 412 '\n' | 397 '\n' |
| 413 'namespace policy {\n' | 398 'namespace policy {\n' |
| 414 '\n' | 399 '\n' |
| 415 'namespace {\n' | 400 'namespace {\n' |
| 416 '\n') | 401 '\n') |
| 417 | 402 |
| 418 # Generate the Chrome schema. | 403 # Generate the Chrome schema. |
| 419 chrome_schema = { | 404 chrome_schema = { |
| 420 'type': 'object', | 405 'type': 'object', |
| 421 'properties': {}, | 406 'properties': {}, |
| 422 } | 407 } |
| 423 shared_strings = {} | 408 shared_strings = {} |
| 424 for policy in policies: | 409 for policy in policies: |
| 425 shared_strings[policy.name] = "key::k%s" % policy.name | 410 shared_strings[policy.name] = "key::k%s" % policy.name |
| 426 if policy.is_supported: | 411 if policy.is_supported: |
| 427 chrome_schema['properties'][policy.name] = policy.schema | 412 chrome_schema['properties'][policy.name] = policy.schema |
| 428 | 413 |
| 429 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') | 414 # Note: this list must be kept in sync with the known property list of the |
| 415 # Chrome schema, so that binary seaching in the PropertyNode array gets the |
| 416 # right index on this array as well. See the implementation of |
| 417 # GetChromePolicyDetails() below. |
| 418 f.write('const PolicyDetails kChromePolicyDetails[] = {\n' |
| 419 '// is_deprecated is_device_policy id max_external_data_size\n') |
| 430 for policy in policies: | 420 for policy in policies: |
| 431 if policy.is_supported: | 421 if policy.is_supported: |
| 432 f.write(' { key::k%s, base::Value::%s, %s, %s, %s },\n' % | 422 f.write(' { %-14s %-16s %3s, %24s },\n' % ( |
| 433 (policy.name, _GetValueType(policy.policy_type), | 423 'true,' if policy.is_deprecated else 'false,', |
| 434 'true' if policy.is_device_only else 'false', policy.id, | 424 'true,' if policy.is_device_only else 'false,', |
| 435 policy.max_size)) | 425 policy.id, |
| 426 policy.max_size)) |
| 436 f.write('};\n\n') | 427 f.write('};\n\n') |
| 437 | 428 |
| 438 f.write('const PolicyDefinitionList kChromePolicyList = {\n' | |
| 439 ' kEntries,\n' | |
| 440 ' kEntries + arraysize(kEntries),\n' | |
| 441 '};\n\n') | |
| 442 | |
| 443 has_deprecated_policies = any( | |
| 444 [p.is_supported and p.is_deprecated for p in policies]) | |
| 445 | |
| 446 if has_deprecated_policies: | |
| 447 f.write('// List of deprecated policies.\n' | |
| 448 'const char* kDeprecatedPolicyList[] = {\n') | |
| 449 for policy in policies: | |
| 450 if policy.is_supported and policy.is_deprecated: | |
| 451 f.write(' key::k%s,\n' % policy.name) | |
| 452 f.write('};\n\n') | |
| 453 | |
| 454 schema_generator = SchemaNodesGenerator(shared_strings) | 429 schema_generator = SchemaNodesGenerator(shared_strings) |
| 455 schema_generator.Generate(chrome_schema, 'root node') | 430 schema_generator.Generate(chrome_schema, 'root node') |
| 456 schema_generator.Write(f) | 431 schema_generator.Write(f) |
| 457 | 432 |
| 433 f.write('bool CompareKeys(const internal::PropertyNode& node,\n' |
| 434 ' const std::string& key) {\n' |
| 435 ' return node.key < key;\n' |
| 436 '}\n\n') |
| 437 |
| 458 f.write('} // namespace\n\n') | 438 f.write('} // namespace\n\n') |
| 459 | 439 |
| 460 if os == 'win': | 440 if os == 'win': |
| 461 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' | 441 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' |
| 462 'const wchar_t kRegistryChromePolicyKey[] = ' | 442 'const wchar_t kRegistryChromePolicyKey[] = ' |
| 463 'L"' + CHROME_POLICY_KEY + '";\n' | 443 'L"' + CHROME_POLICY_KEY + '";\n' |
| 464 '#else\n' | 444 '#else\n' |
| 465 'const wchar_t kRegistryChromePolicyKey[] = ' | 445 'const wchar_t kRegistryChromePolicyKey[] = ' |
| 466 'L"' + CHROMIUM_POLICY_KEY + '";\n' | 446 'L"' + CHROMIUM_POLICY_KEY + '";\n' |
| 467 '#endif\n\n') | 447 '#endif\n\n') |
| 468 | 448 |
| 469 f.write('bool IsDeprecatedPolicy(const std::string& policy) {\n') | |
| 470 if has_deprecated_policies: | |
| 471 # arraysize() doesn't work with empty arrays. | |
| 472 f.write(' for (size_t i = 0; i < arraysize(kDeprecatedPolicyList);' | |
| 473 ' ++i) {\n' | |
| 474 ' if (policy == kDeprecatedPolicyList[i])\n' | |
| 475 ' return true;\n' | |
| 476 ' }\n') | |
| 477 f.write(' return false;\n' | |
| 478 '}\n\n') | |
| 479 | |
| 480 f.write('const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n' | |
| 481 ' return &kChromePolicyList;\n' | |
| 482 '}\n\n') | |
| 483 | |
| 484 f.write('const internal::SchemaData* GetChromeSchemaData() {\n' | 449 f.write('const internal::SchemaData* GetChromeSchemaData() {\n' |
| 485 ' return &kChromeSchemaData;\n' | 450 ' return &kChromeSchemaData;\n' |
| 486 '}\n\n') | 451 '}\n\n') |
| 487 | 452 |
| 453 f.write('const PolicyDetails* GetChromePolicyDetails(' |
| 454 'const std::string& policy) {\n' |
| 455 ' // First index in kPropertyNodes of the Chrome policies.\n' |
| 456 ' static const int begin_index = %s;\n' |
| 457 ' // One-past-the-end of the Chrome policies in kPropertyNodes.\n' |
| 458 ' static const int end_index = %s;\n' % |
| 459 (schema_generator.root_properties_begin, |
| 460 schema_generator.root_properties_end)) |
| 461 f.write(' const internal::PropertyNode* begin =\n' |
| 462 ' kPropertyNodes + begin_index;\n' |
| 463 ' const internal::PropertyNode* end = kPropertyNodes + end_index;\n' |
| 464 ' const internal::PropertyNode* it =\n' |
| 465 ' std::lower_bound(begin, end, policy, CompareKeys);\n' |
| 466 ' if (it == end || it->key != policy)\n' |
| 467 ' return NULL;\n' |
| 468 ' // This relies on kPropertyNodes from begin_index to end_index\n' |
| 469 ' // having exactly the same policies (and in the same order) as\n' |
| 470 ' // kChromePolicyDetails, so that binary searching on the first\n' |
| 471 ' // gets the same results as a binary search on the second would.\n' |
| 472 ' // However, kPropertyNodes has the policy names and\n' |
| 473 ' // kChromePolicyDetails doesn\'t, so we obtain the index into\n' |
| 474 ' // the second array by searching the first to avoid duplicating\n' |
| 475 ' // the policy name pointers.\n' |
| 476 ' // Offsetting |it| from |begin| here obtains the index we\'re\n' |
| 477 ' // looking for.\n' |
| 478 ' size_t index = it - begin;\n' |
| 479 ' CHECK_LT(index, arraysize(kChromePolicyDetails));\n' |
| 480 ' return kChromePolicyDetails + index;\n' |
| 481 '}\n\n') |
| 482 |
| 488 f.write('namespace key {\n\n') | 483 f.write('namespace key {\n\n') |
| 489 for policy in policies: | 484 for policy in policies: |
| 490 # TODO(joaodasilva): Include only supported policies in | 485 # TODO(joaodasilva): Include only supported policies in |
| 491 # configuration_policy_handler.cc and configuration_policy_handler_list.cc | 486 # configuration_policy_handler.cc and configuration_policy_handler_list.cc |
| 492 # so that these names can be conditional on 'policy.is_supported'. | 487 # so that these names can be conditional on 'policy.is_supported'. |
| 493 # http://crbug.com/223616 | 488 # http://crbug.com/223616 |
| 494 f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name)) | 489 f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name)) |
| 495 f.write('\n} // namespace key\n\n' | 490 f.write('\n} // namespace key\n\n' |
| 496 '} // namespace policy\n') | 491 '} // namespace policy\n') |
| 497 | 492 |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 def _WriteCloudPolicyDecoder(policies, os, f): | 735 def _WriteCloudPolicyDecoder(policies, os, f): |
| 741 f.write(CPP_HEAD) | 736 f.write(CPP_HEAD) |
| 742 for policy in policies: | 737 for policy in policies: |
| 743 if policy.is_supported and not policy.is_device_only: | 738 if policy.is_supported and not policy.is_device_only: |
| 744 _WritePolicyCode(f, policy) | 739 _WritePolicyCode(f, policy) |
| 745 f.write(CPP_FOOT) | 740 f.write(CPP_FOOT) |
| 746 | 741 |
| 747 | 742 |
| 748 if __name__ == '__main__': | 743 if __name__ == '__main__': |
| 749 sys.exit(main()) | 744 sys.exit(main()) |
| OLD | NEW |