| 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 15 matching lines...) Expand all Loading... |
| 26 'int-enum': 'TYPE_INTEGER', | 26 'int-enum': 'TYPE_INTEGER', |
| 27 'list': 'TYPE_LIST', | 27 'list': 'TYPE_LIST', |
| 28 'main': 'TYPE_BOOLEAN', | 28 'main': 'TYPE_BOOLEAN', |
| 29 'string': 'TYPE_STRING', | 29 'string': 'TYPE_STRING', |
| 30 'string-enum': 'TYPE_STRING', | 30 'string-enum': 'TYPE_STRING', |
| 31 } | 31 } |
| 32 | 32 |
| 33 | 33 |
| 34 def main(): | 34 def main(): |
| 35 parser = OptionParser(usage=__doc__) | 35 parser = OptionParser(usage=__doc__) |
| 36 parser.add_option("--pch", "--policy-constants-header", dest="header_path", | 36 parser.add_option('--pch', '--policy-constants-header', dest='header_path', |
| 37 help="generate header file of policy constants", | 37 help='generate header file of policy constants', |
| 38 metavar="FILE") | 38 metavar='FILE') |
| 39 parser.add_option("--pcc", "--policy-constants-source", dest="source_path", | 39 parser.add_option('--pcc', '--policy-constants-source', dest='source_path', |
| 40 help="generate source file of policy constants", | 40 help='generate source file of policy constants', |
| 41 metavar="FILE") | 41 metavar='FILE') |
| 42 parser.add_option("--pth", "--policy-type-header", dest="type_path", | 42 parser.add_option('--ppb', '--policy-protobuf', dest='proto_path', |
| 43 help="generate header file for policy type enumeration", | 43 help='generate cloud policy protobuf file', |
| 44 metavar="FILE") | 44 metavar='FILE') |
| 45 parser.add_option("--ppb", "--policy-protobuf", dest="proto_path", | 45 parser.add_option('--ppd', '--protobuf-decoder', dest='decoder_path', |
| 46 help="generate cloud policy protobuf file", | 46 help='generate C++ code decoding the policy protobuf', |
| 47 metavar="FILE") | 47 metavar='FILE') |
| 48 parser.add_option("--ppd", "--protobuf-decoder", dest="decoder_path", | |
| 49 help="generate C++ code decoding the policy protobuf", | |
| 50 metavar="FILE") | |
| 51 | 48 |
| 52 (opts, args) = parser.parse_args() | 49 (opts, args) = parser.parse_args() |
| 53 | 50 |
| 54 if len(args) != 3: | 51 if len(args) != 3: |
| 55 print "exactly platform, chromium_os flag and input file must be specified." | 52 print 'exactly platform, chromium_os flag and input file must be specified.' |
| 56 parser.print_help() | 53 parser.print_help() |
| 57 return 2 | 54 return 2 |
| 58 template_file_contents = _LoadJSONFile(args[2]) | 55 template_file_contents = _LoadJSONFile(args[2]) |
| 59 if opts.header_path is not None: | 56 if opts.header_path is not None: |
| 60 _WritePolicyConstantHeader(template_file_contents, args, opts) | 57 _WritePolicyConstantHeader(template_file_contents, args, opts) |
| 61 if opts.source_path is not None: | 58 if opts.source_path is not None: |
| 62 _WritePolicyConstantSource(template_file_contents, args, opts) | 59 _WritePolicyConstantSource(template_file_contents, args, opts) |
| 63 if opts.type_path is not None: | |
| 64 _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts) | |
| 65 if opts.proto_path is not None: | 60 if opts.proto_path is not None: |
| 66 _WriteProtobuf(template_file_contents, args, opts.proto_path) | 61 _WriteProtobuf(template_file_contents, args, opts.proto_path) |
| 67 if opts.decoder_path is not None: | 62 if opts.decoder_path is not None: |
| 68 _WriteProtobufParser(template_file_contents, args, opts.decoder_path) | 63 _WriteProtobufParser(template_file_contents, args, opts.decoder_path) |
| 69 return 0 | 64 return 0 |
| 70 | 65 |
| 71 | 66 |
| 72 #------------------ shared helpers ---------------------------------# | 67 #------------------ shared helpers ---------------------------------# |
| 73 def _OutputGeneratedWarningForC(f, template_file_path): | 68 def _OutputGeneratedWarningForC(f, template_file_path): |
| 74 f.write('//\n' | 69 f.write('//\n' |
| 75 '// DO NOT MODIFY THIS FILE DIRECTLY!\n' | 70 '// DO NOT MODIFY THIS FILE DIRECTLY!\n' |
| 76 '// IT IS GENERATED BY generate_policy_source.py\n' | 71 '// IT IS GENERATED BY generate_policy_source.py\n' |
| 77 '// FROM ' + template_file_path + '\n' | 72 '// FROM ' + template_file_path + '\n' |
| 78 '//\n\n') | 73 '//\n\n') |
| 79 | 74 |
| 80 | 75 |
| 81 # Returns a tuple with details about the given policy: | 76 # Returns a tuple with details about the given policy: |
| 82 # (name, type, list_of_platforms, is_deprecated, is_device_policy) | 77 # (name, type, list_of_platforms, is_deprecated, is_device_policy) |
| 83 def _GetPolicyDetails(policy): | 78 def _GetPolicyDetails(policy): |
| 84 name = policy['name'] | 79 name = policy['name'] |
| 85 if not TYPE_MAP.has_key(policy['type']): | 80 if not TYPE_MAP.has_key(policy['type']): |
| 86 print "Unknown policy type for %s: %s" % (name, policy['type']) | 81 print 'Unknown policy type for %s: %s' % (name, policy['type']) |
| 87 sys.exit(3) | 82 sys.exit(3) |
| 88 vtype = TYPE_MAP[policy['type']] | 83 vtype = TYPE_MAP[policy['type']] |
| 89 # platforms is a list of "chrome", "chrome_os" and/or "chrome_frame". | 84 # platforms is a list of 'chrome', 'chrome_os' and/or 'chrome_frame'. |
| 90 platforms = [ x.split(':')[0] for x in policy['supported_on'] ] | 85 platforms = [ x.split(':')[0] for x in policy['supported_on'] ] |
| 91 is_deprecated = policy.get('deprecated', False) | 86 is_deprecated = policy.get('deprecated', False) |
| 92 return (name, vtype, platforms, is_deprecated) | 87 return (name, vtype, platforms, is_deprecated) |
| 93 | 88 |
| 94 | 89 |
| 95 def _GetPolicyList(template_file_contents): | 90 def _GetPolicyList(template_file_contents): |
| 96 policies = [] | 91 policies = [] |
| 97 for policy in template_file_contents['policy_definitions']: | 92 for policy in template_file_contents['policy_definitions']: |
| 98 if policy['type'] == 'group': | 93 if policy['type'] == 'group': |
| 99 for sub_policy in policy['policies']: | 94 for sub_policy in policy['policies']: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 115 in _GetPolicyList(template_file_contents)] | 110 in _GetPolicyList(template_file_contents)] |
| 116 | 111 |
| 117 | 112 |
| 118 def _GetDeprecatedPolicyList(template_file_contents): | 113 def _GetDeprecatedPolicyList(template_file_contents): |
| 119 return [name for (name, _, _, is_deprecated) | 114 return [name for (name, _, _, is_deprecated) |
| 120 in _GetPolicyList(template_file_contents) | 115 in _GetPolicyList(template_file_contents) |
| 121 if is_deprecated] | 116 if is_deprecated] |
| 122 | 117 |
| 123 | 118 |
| 124 def _LoadJSONFile(json_file): | 119 def _LoadJSONFile(json_file): |
| 125 with open(json_file, "r") as f: | 120 with open(json_file, 'r') as f: |
| 126 text = f.read() | 121 text = f.read() |
| 127 return eval(text) | 122 return eval(text) |
| 128 | 123 |
| 129 | 124 |
| 130 #------------------ policy constants header ------------------------# | 125 #------------------ policy constants header ------------------------# |
| 131 def _WritePolicyConstantHeader(template_file_contents, args, opts): | 126 def _WritePolicyConstantHeader(template_file_contents, args, opts): |
| 132 os = args[0] | 127 os = args[0] |
| 133 with open(opts.header_path, "w") as f: | 128 with open(opts.header_path, 'w') as f: |
| 134 _OutputGeneratedWarningForC(f, args[2]) | 129 _OutputGeneratedWarningForC(f, args[2]) |
| 135 | 130 |
| 136 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 131 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| 137 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 132 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| 138 '#pragma once\n' | 133 '#pragma once\n' |
| 139 '\n' | 134 '\n' |
| 135 '#include <string>\n' |
| 136 '\n' |
| 140 '#include "base/values.h"\n' | 137 '#include "base/values.h"\n' |
| 141 '#include "policy/configuration_policy_type.h"\n' | |
| 142 '\n' | 138 '\n' |
| 143 'namespace policy {\n\n') | 139 'namespace policy {\n\n') |
| 144 | 140 |
| 145 if os == "win": | 141 if os == 'win': |
| 146 f.write('// The windows registry path where mandatory policy ' | 142 f.write('// The windows registry path where mandatory policy ' |
| 147 'configuration resides.\n' | 143 'configuration resides.\n' |
| 148 'extern const wchar_t kRegistryMandatorySubKey[];\n' | 144 'extern const wchar_t kRegistryMandatorySubKey[];\n' |
| 149 '// The windows registry path where recommended policy ' | 145 '// The windows registry path where recommended policy ' |
| 150 'configuration resides.\n' | 146 'configuration resides.\n' |
| 151 'extern const wchar_t kRegistryRecommendedSubKey[];\n\n') | 147 'extern const wchar_t kRegistryRecommendedSubKey[];\n\n') |
| 152 | 148 |
| 153 f.write('// Lists policy types mapped to their names and expected types.\n' | 149 f.write('// Lists policy types mapped to their names and expected types.\n' |
| 154 '// Used to initialize ConfigurationPolicyProviders.\n' | 150 '// Used to initialize ConfigurationPolicyProviders.\n' |
| 155 'struct PolicyDefinitionList {\n' | 151 'struct PolicyDefinitionList {\n' |
| 156 ' struct Entry {\n' | 152 ' struct Entry {\n' |
| 157 ' ConfigurationPolicyType policy_type;\n' | 153 ' const char* name;\n' |
| 158 ' base::Value::Type value_type;\n' | 154 ' base::Value::Type value_type;\n' |
| 159 ' const char* name;\n' | |
| 160 ' };\n' | 155 ' };\n' |
| 161 '\n' | 156 '\n' |
| 162 ' const Entry* begin;\n' | 157 ' const Entry* begin;\n' |
| 163 ' const Entry* end;\n' | 158 ' const Entry* end;\n' |
| 164 '};\n' | 159 '};\n' |
| 165 '\n' | 160 '\n' |
| 166 '// Gets the policy name for the given policy type.\n' | |
| 167 'const char* GetPolicyName(ConfigurationPolicyType type);\n' | |
| 168 '\n' | |
| 169 '// Returns true if the given policy is deprecated.\n' | 161 '// Returns true if the given policy is deprecated.\n' |
| 170 'bool IsDeprecatedPolicy(ConfigurationPolicyType type);\n' | 162 'bool IsDeprecatedPolicy(const std::string& policy);\n' |
| 171 '\n' | 163 '\n' |
| 172 '// Returns the default policy definition list for Chrome.\n' | 164 '// Returns the default policy definition list for Chrome.\n' |
| 173 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n') | 165 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n') |
| 174 f.write('// Key names for the policy settings.\n' | 166 f.write('// Key names for the policy settings.\n' |
| 175 'namespace key {\n\n') | 167 'namespace key {\n\n') |
| 176 for policy_name in _GetPolicyNameList(template_file_contents): | 168 for policy_name in _GetPolicyNameList(template_file_contents): |
| 177 f.write('extern const char k' + policy_name + '[];\n') | 169 f.write('extern const char k' + policy_name + '[];\n') |
| 178 f.write('\n} // namespace key\n\n' | 170 f.write('\n} // namespace key\n\n' |
| 179 '} // namespace policy\n\n' | 171 '} // namespace policy\n\n' |
| 180 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') | 172 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') |
| 181 | 173 |
| 182 | 174 |
| 183 #------------------ policy constants source ------------------------# | 175 #------------------ policy constants source ------------------------# |
| 184 def _WritePolicyConstantSource(template_file_contents, args, opts): | 176 def _WritePolicyConstantSource(template_file_contents, args, opts): |
| 185 os = args[0] | 177 os = args[0] |
| 186 is_chromium_os = args[1] == "1" | 178 is_chromium_os = args[1] == '1' |
| 187 platform = None | 179 platform = None |
| 188 platform_wildcard = None | 180 platform_wildcard = None |
| 189 if is_chromium_os: | 181 if is_chromium_os: |
| 190 platform = 'chrome_os' | 182 platform = 'chrome_os' |
| 191 else: | 183 else: |
| 192 platform = 'chrome.' + os.lower() | 184 platform = 'chrome.' + os.lower() |
| 193 platform_wildcard = 'chrome.*' | 185 platform_wildcard = 'chrome.*' |
| 194 with open(opts.source_path, "w") as f: | 186 with open(opts.source_path, 'w') as f: |
| 195 _OutputGeneratedWarningForC(f, args[2]) | 187 _OutputGeneratedWarningForC(f, args[2]) |
| 196 | 188 |
| 197 f.write('#include "base/basictypes.h"\n' | 189 f.write('#include "base/basictypes.h"\n' |
| 198 '#include "base/logging.h"\n' | 190 '#include "base/logging.h"\n' |
| 199 '#include "policy/policy_constants.h"\n' | 191 '#include "policy/policy_constants.h"\n' |
| 200 '\n' | 192 '\n' |
| 201 'namespace policy {\n\n') | 193 'namespace policy {\n\n') |
| 202 | 194 |
| 203 f.write('namespace {\n\n') | 195 f.write('namespace {\n\n') |
| 204 | 196 |
| 205 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') | 197 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') |
| 206 policy_list = _GetChromePolicyList(template_file_contents) | 198 policy_list = _GetChromePolicyList(template_file_contents) |
| 207 for (name, platforms, vtype) in policy_list: | 199 for (name, platforms, vtype) in policy_list: |
| 208 if (platform in platforms) or (platform_wildcard in platforms): | 200 if (platform in platforms) or (platform_wildcard in platforms): |
| 209 f.write(' { kPolicy%s, Value::%s, key::k%s },\n' % (name, vtype, name)) | 201 f.write(' { key::k%s, Value::%s },\n' % (name, vtype)) |
| 210 f.write('};\n\n') | 202 f.write('};\n\n') |
| 211 | 203 |
| 212 f.write('const PolicyDefinitionList kChromePolicyList = {\n' | 204 f.write('const PolicyDefinitionList kChromePolicyList = {\n' |
| 213 ' kEntries,\n' | 205 ' kEntries,\n' |
| 214 ' kEntries + arraysize(kEntries),\n' | 206 ' kEntries + arraysize(kEntries),\n' |
| 215 '};\n\n') | 207 '};\n\n') |
| 216 | 208 |
| 217 f.write('// Maps a policy-type enum value to the policy name.\n' | 209 f.write('// List of deprecated policies.\n' |
| 218 'const char* kPolicyNameMap[] = {\n') | 210 'const char* kDeprecatedPolicyList[] = {\n') |
| 219 for name in _GetPolicyNameList(template_file_contents): | 211 for name in _GetDeprecatedPolicyList(template_file_contents): |
| 220 f.write(' key::k%s,\n' % name) | 212 f.write(' key::k%s,\n' % name) |
| 221 f.write('};\n\n') | 213 f.write('};\n\n') |
| 222 | 214 |
| 223 f.write('// List of deprecated policies.\n' | |
| 224 'const ConfigurationPolicyType kDeprecatedPolicyList[] = {\n') | |
| 225 for name in _GetDeprecatedPolicyList(template_file_contents): | |
| 226 f.write(' kPolicy%s,\n' % name) | |
| 227 f.write('};\n\n') | |
| 228 | |
| 229 f.write('} // namespace\n\n') | 215 f.write('} // namespace\n\n') |
| 230 | 216 |
| 231 if os == "win": | 217 if os == 'win': |
| 232 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' | 218 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' |
| 233 'const wchar_t kRegistryMandatorySubKey[] = ' | 219 'const wchar_t kRegistryMandatorySubKey[] = ' |
| 234 'L"' + CHROME_MANDATORY_SUBKEY + '";\n' | 220 'L"' + CHROME_MANDATORY_SUBKEY + '";\n' |
| 235 'const wchar_t kRegistryRecommendedSubKey[] = ' | 221 'const wchar_t kRegistryRecommendedSubKey[] = ' |
| 236 'L"' + CHROME_RECOMMENDED_SUBKEY + '";\n' | 222 'L"' + CHROME_RECOMMENDED_SUBKEY + '";\n' |
| 237 '#else\n' | 223 '#else\n' |
| 238 'const wchar_t kRegistryMandatorySubKey[] = ' | 224 'const wchar_t kRegistryMandatorySubKey[] = ' |
| 239 'L"' + CHROMIUM_MANDATORY_SUBKEY + '";\n' | 225 'L"' + CHROMIUM_MANDATORY_SUBKEY + '";\n' |
| 240 'const wchar_t kRegistryRecommendedSubKey[] = ' | 226 'const wchar_t kRegistryRecommendedSubKey[] = ' |
| 241 'L"' + CHROMIUM_RECOMMENDED_SUBKEY + '";\n' | 227 'L"' + CHROMIUM_RECOMMENDED_SUBKEY + '";\n' |
| 242 '#endif\n\n') | 228 '#endif\n\n') |
| 243 | 229 |
| 244 f.write('const char* GetPolicyName(ConfigurationPolicyType type) {\n' | 230 f.write('bool IsDeprecatedPolicy(const std::string& policy) {\n' |
| 245 ' CHECK(type >= 0 && ' | |
| 246 'static_cast<size_t>(type) < arraysize(kPolicyNameMap));\n' | |
| 247 ' return kPolicyNameMap[type];\n' | |
| 248 '}\n' | |
| 249 '\n' | |
| 250 'bool IsDeprecatedPolicy(ConfigurationPolicyType type) {\n' | |
| 251 ' for (size_t i = 0; i < arraysize(kDeprecatedPolicyList);' | 231 ' for (size_t i = 0; i < arraysize(kDeprecatedPolicyList);' |
| 252 ' ++i) {\n' | 232 ' ++i) {\n' |
| 253 ' if (kDeprecatedPolicyList[i] == type)\n' | 233 ' if (policy == kDeprecatedPolicyList[i])\n' |
| 254 ' return true;\n' | 234 ' return true;\n' |
| 255 ' }\n' | 235 ' }\n' |
| 256 ' return false;\n' | 236 ' return false;\n' |
| 257 '}\n' | 237 '}\n' |
| 258 '\n' | 238 '\n' |
| 259 'const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n' | 239 'const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n' |
| 260 ' return &kChromePolicyList;\n' | 240 ' return &kChromePolicyList;\n' |
| 261 '}\n\n') | 241 '}\n\n') |
| 262 | 242 |
| 263 f.write('namespace key {\n\n') | 243 f.write('namespace key {\n\n') |
| 264 for policy_name in _GetPolicyNameList(template_file_contents): | 244 for policy_name in _GetPolicyNameList(template_file_contents): |
| 265 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) | 245 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) |
| 266 f.write('\n} // namespace key\n\n' | 246 f.write('\n} // namespace key\n\n' |
| 267 '} // namespace policy\n') | 247 '} // namespace policy\n') |
| 268 | 248 |
| 269 | 249 |
| 270 #------------------ policy type enumeration header -----------------# | |
| 271 def _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts): | |
| 272 with open(opts.type_path, "w") as f: | |
| 273 _OutputGeneratedWarningForC(f, args[2]) | |
| 274 f.write('#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' | |
| 275 '#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' | |
| 276 '#pragma once\n' | |
| 277 '\n' | |
| 278 'namespace policy {\n' | |
| 279 '\n' | |
| 280 'enum ConfigurationPolicyType {\n') | |
| 281 for policy_name in _GetPolicyNameList(template_file_contents): | |
| 282 f.write(' kPolicy' + policy_name + ",\n") | |
| 283 f.write('};\n\n' | |
| 284 '} // namespace policy\n\n' | |
| 285 '#endif // CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n') | |
| 286 | |
| 287 | |
| 288 #------------------ policy protobuf --------------------------------# | 250 #------------------ policy protobuf --------------------------------# |
| 289 PROTO_HEAD = ''' | 251 PROTO_HEAD = ''' |
| 290 syntax = "proto2"; | 252 syntax = "proto2"; |
| 291 | 253 |
| 292 option optimize_for = LITE_RUNTIME; | 254 option optimize_for = LITE_RUNTIME; |
| 293 | 255 |
| 294 package enterprise_management; | 256 package enterprise_management; |
| 295 | 257 |
| 296 message StringList { | 258 message StringList { |
| 297 repeated string entries = 1; | 259 repeated string entries = 1; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 f.write('// --------------------------------------------------\n' | 320 f.write('// --------------------------------------------------\n' |
| 359 '// Big wrapper PB containing the above groups.\n\n' | 321 '// Big wrapper PB containing the above groups.\n\n' |
| 360 'message CloudPolicySettings {\n') | 322 'message CloudPolicySettings {\n') |
| 361 f.write(''.join(fields)) | 323 f.write(''.join(fields)) |
| 362 f.write('}\n\n') | 324 f.write('}\n\n') |
| 363 | 325 |
| 364 | 326 |
| 365 #------------------ protobuf decoder -------------------------------# | 327 #------------------ protobuf decoder -------------------------------# |
| 366 CPP_HEAD = ''' | 328 CPP_HEAD = ''' |
| 367 #include <limits> | 329 #include <limits> |
| 368 #include <map> | |
| 369 #include <string> | 330 #include <string> |
| 370 | 331 |
| 371 #include "base/logging.h" | 332 #include "base/logging.h" |
| 372 #include "base/values.h" | 333 #include "base/values.h" |
| 373 #include "chrome/browser/policy/configuration_policy_provider.h" | |
| 374 #include "chrome/browser/policy/policy_map.h" | 334 #include "chrome/browser/policy/policy_map.h" |
| 375 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 335 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 376 #include "policy/configuration_policy_type.h" | 336 #include "policy/policy_constants.h" |
| 377 | 337 |
| 378 using google::protobuf::RepeatedPtrField; | 338 using google::protobuf::RepeatedPtrField; |
| 379 | 339 |
| 380 namespace policy { | 340 namespace policy { |
| 381 | 341 |
| 382 namespace em = enterprise_management; | 342 namespace em = enterprise_management; |
| 383 | 343 |
| 384 Value* DecodeIntegerValue(google::protobuf::int64 value) { | 344 Value* DecodeIntegerValue(google::protobuf::int64 value) { |
| 385 if (value < std::numeric_limits<int>::min() || | 345 if (value < std::numeric_limits<int>::min() || |
| 386 value > std::numeric_limits<int>::max()) { | 346 value > std::numeric_limits<int>::max()) { |
| 387 LOG(WARNING) << "Integer value " << value | 347 LOG(WARNING) << "Integer value " << value |
| 388 << " out of numeric limits, ignoring."; | 348 << " out of numeric limits, ignoring."; |
| 389 return NULL; | 349 return NULL; |
| 390 } | 350 } |
| 391 | 351 |
| 392 return Value::CreateIntegerValue(static_cast<int>(value)); | 352 return Value::CreateIntegerValue(static_cast<int>(value)); |
| 393 } | 353 } |
| 394 | 354 |
| 395 ListValue* DecodeStringList(const em::StringList& string_list) { | 355 ListValue* DecodeStringList(const em::StringList& string_list) { |
| 396 ListValue* list_value = new ListValue; | 356 ListValue* list_value = new ListValue; |
| 397 RepeatedPtrField<std::string>::const_iterator entry; | 357 RepeatedPtrField<std::string>::const_iterator entry; |
| 398 for (entry = string_list.entries().begin(); | 358 for (entry = string_list.entries().begin(); |
| 399 entry != string_list.entries().end(); ++entry) { | 359 entry != string_list.entries().end(); ++entry) { |
| 400 list_value->Append(Value::CreateStringValue(*entry)); | 360 list_value->Append(Value::CreateStringValue(*entry)); |
| 401 } | 361 } |
| 402 return list_value; | 362 return list_value; |
| 403 } | 363 } |
| 404 | 364 |
| 405 void DecodePolicy(const em::CloudPolicySettings& policy, | 365 void DecodePolicy(const em::CloudPolicySettings& policy, PolicyMap* map) { |
| 406 PolicyMap* mandatory, PolicyMap* recommended) { | |
| 407 DCHECK(mandatory); | |
| 408 DCHECK(recommended); | |
| 409 ''' | 366 ''' |
| 410 | 367 |
| 411 | 368 |
| 412 CPP_FOOT = '''} | 369 CPP_FOOT = '''} |
| 413 | 370 |
| 414 } // namespace policy | 371 } // namespace policy |
| 415 ''' | 372 ''' |
| 416 | 373 |
| 417 | 374 |
| 418 def _CreateValue(type, arg): | 375 def _CreateValue(type, arg): |
| 419 if type == 'main': | 376 if type == 'main': |
| 420 return "Value::CreateBooleanValue(%s)" % arg | 377 return "Value::CreateBooleanValue(%s)" % arg |
| 421 elif type in ('int', 'int-enum'): | 378 elif type in ('int', 'int-enum'): |
| 422 return "DecodeIntegerValue(%s)" % arg | 379 return "DecodeIntegerValue(%s)" % arg |
| 423 elif type in ('string', 'string-enum'): | 380 elif type in ('string', 'string-enum'): |
| 424 return "Value::CreateStringValue(%s)" % arg | 381 return "Value::CreateStringValue(%s)" % arg |
| 425 elif type == 'list': | 382 elif type == 'list': |
| 426 return "DecodeStringList(%s)" % arg | 383 return "DecodeStringList(%s)" % arg |
| 427 elif type == 'dict': | 384 elif type == 'dict': |
| 428 # TODO(joaodasilva): decode 'dict' types. http://crbug.com/108997 | 385 # TODO(joaodasilva): decode 'dict' types. http://crbug.com/108997 |
| 429 return "new DictionaryValue()" | 386 return "new DictionaryValue()" |
| 430 else: | 387 else: |
| 431 raise NotImplementedError() | 388 raise NotImplementedError() |
| 432 | 389 |
| 433 | 390 |
| 434 def _WritePolicyCode(file, policy): | 391 def _WritePolicyCode(file, policy): |
| 435 if policy.get('device_only', False): | 392 if policy.get('device_only', False): |
| 436 return | 393 return |
| 437 membername = policy['name'].lower() | 394 membername = policy['name'].lower() |
| 438 proto_type = "%sProto" % policy['name'] | 395 proto_type = '%sProto' % policy['name'] |
| 439 proto_name = "%s_proto" % membername | 396 proto_name = '%s_proto' % membername |
| 440 file.write(' if (policy.has_%s()) {\n' % membername) | 397 file.write(' if (policy.has_%s()) {\n' % membername) |
| 441 file.write(' const em::%s& %s = policy.%s();\n' % | 398 file.write(' const em::%s& %s = policy.%s();\n' % |
| 442 (proto_type, proto_name, membername)) | 399 (proto_type, proto_name, membername)) |
| 443 file.write(' if (%s.has_%s()) {\n' % (proto_name, membername)) | 400 file.write(' if (%s.has_%s()) {\n' % (proto_name, membername)) |
| 444 file.write(' PolicyMap* destination = mandatory;\n' | 401 file.write(' PolicyLevel level = POLICY_LEVEL_MANDATORY;\n' |
| 402 ' bool do_set = true;\n' |
| 445 ' if (%s.has_policy_options()) {\n' | 403 ' if (%s.has_policy_options()) {\n' |
| 404 ' do_set = false;\n' |
| 446 ' switch(%s.policy_options().mode()) {\n' % | 405 ' switch(%s.policy_options().mode()) {\n' % |
| 447 (proto_name, proto_name)) | 406 (proto_name, proto_name)) |
| 448 file.write(' case em::PolicyOptions::MANDATORY:\n' | 407 file.write(' case em::PolicyOptions::MANDATORY:\n' |
| 449 ' destination = mandatory;\n' | 408 ' do_set = true;\n' |
| 409 ' level = POLICY_LEVEL_MANDATORY;\n' |
| 450 ' break;\n' | 410 ' break;\n' |
| 451 ' case em::PolicyOptions::RECOMMENDED:\n' | 411 ' case em::PolicyOptions::RECOMMENDED:\n' |
| 452 ' destination = recommended;\n' | 412 ' do_set = true;\n' |
| 413 ' level = POLICY_LEVEL_RECOMMENDED;\n' |
| 453 ' break;\n' | 414 ' break;\n' |
| 454 ' case em::PolicyOptions::UNSET:\n' | 415 ' case em::PolicyOptions::UNSET:\n' |
| 455 ' default:\n' | |
| 456 ' destination = NULL;\n' | |
| 457 ' break;\n' | 416 ' break;\n' |
| 458 ' }\n' | 417 ' }\n' |
| 459 ' }\n' | 418 ' }\n' |
| 460 ' if (destination) {\n') | 419 ' if (do_set) {\n') |
| 461 file.write(' Value* value = %s;\n' % | 420 file.write(' Value* value = %s;\n' % |
| 462 (_CreateValue(policy['type'], | 421 (_CreateValue(policy['type'], |
| 463 '%s.%s()' % (proto_name, membername)))) | 422 '%s.%s()' % (proto_name, membername)))) |
| 464 file.write(' destination->Set(kPolicy%s, value);\n' % policy['name']) | 423 file.write(' map->Set(key::k%s, level, POLICY_SCOPE_USER, value);\n' % |
| 424 policy['name']) |
| 465 file.write(' }\n' | 425 file.write(' }\n' |
| 466 ' }\n' | 426 ' }\n' |
| 467 ' }\n') | 427 ' }\n') |
| 468 | 428 |
| 469 | 429 |
| 470 def _WriteProtobufParser(template_file_contents, args, outfilepath): | 430 def _WriteProtobufParser(template_file_contents, args, outfilepath): |
| 471 with open(outfilepath, 'w') as f: | 431 with open(outfilepath, 'w') as f: |
| 472 _OutputGeneratedWarningForC(f, args[2]) | 432 _OutputGeneratedWarningForC(f, args[2]) |
| 473 f.write(CPP_HEAD) | 433 f.write(CPP_HEAD) |
| 474 for policy in template_file_contents['policy_definitions']: | 434 for policy in template_file_contents['policy_definitions']: |
| 475 if policy['type'] == 'group': | 435 if policy['type'] == 'group': |
| 476 for sub_policy in policy['policies']: | 436 for sub_policy in policy['policies']: |
| 477 _WritePolicyCode(f, sub_policy) | 437 _WritePolicyCode(f, sub_policy) |
| 478 else: | 438 else: |
| 479 _WritePolicyCode(f, policy) | 439 _WritePolicyCode(f, policy) |
| 480 f.write(CPP_FOOT) | 440 f.write(CPP_FOOT) |
| 481 | 441 |
| 482 | 442 |
| 483 if __name__ == '__main__': | 443 if __name__ == '__main__': |
| 484 sys.exit(main()) | 444 sys.exit(main()) |
| OLD | NEW |