OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 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. |
| 5 |
| 6 import sys |
| 7 import string |
| 8 import json |
| 9 |
| 10 input_json_path = sys.argv[1] |
| 11 output_cc_path = sys.argv[2] |
| 12 output_h_path = sys.argv[3] |
| 13 |
| 14 header = """\ |
| 15 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 16 // Use of this source code is governed by a BSD-style license that can be |
| 17 // found in the LICENSE file. |
| 18 |
| 19 // THIS FILE IS AUTOGENERATED. DO NOT EDIT. |
| 20 // Generated by |
| 21 // content/public/browser/devtools_protocol_handler_generator.py from |
| 22 // third_party/WebKit/Source/devtools/protocol.json |
| 23 """ |
| 24 |
| 25 template_h = string.Template(header + """\ |
| 26 |
| 27 #ifndef CONTENT_BROWSER_DEVTOOLS_PROTOCOL_DEVTOOLS_PROTOCOL_HANDLER_IMPL_H_ |
| 28 #define CONTENT_BROWSER_DEVTOOLS_PROTOCOL_DEVTOOLS_PROTOCOL_HANDLER_IMPL_H_ |
| 29 |
| 30 #include "content/browser/devtools/devtools_protocol.h" |
| 31 #include "content/browser/devtools/protocol/devtools_protocol_client.h" |
| 32 |
| 33 namespace content { |
| 34 |
| 35 class DevToolsProtocolHandlerImpl; |
| 36 |
| 37 namespace devtools { |
| 38 |
| 39 ${types}\ |
| 40 |
| 41 } // namespace devtools |
| 42 |
| 43 class DevToolsProtocolHandlerImpl : public DevToolsProtocol::Handler { |
| 44 public: |
| 45 typedef DevToolsProtocolClient::Response Response; |
| 46 typedef DevToolsProtocolClient::ResponseStatus ResponseStatus; |
| 47 |
| 48 DevToolsProtocolHandlerImpl(); |
| 49 virtual ~DevToolsProtocolHandlerImpl(); |
| 50 |
| 51 ${setters}\ |
| 52 |
| 53 private: |
| 54 ${friends}\ |
| 55 |
| 56 ${methods}\ |
| 57 |
| 58 ${fields}\ |
| 59 }; |
| 60 |
| 61 } // namespace content |
| 62 |
| 63 #endif // CONTENT_BROWSER_DEVTOOLS_PROTOCOL_DEVTOOLS_PROTOCOL_HANDLER_IMPL_H_ |
| 64 """) |
| 65 |
| 66 tmpl_typedef = string.Template("""\ |
| 67 namespace ${domain} { |
| 68 typedef ${param_type} ${declared_name}; |
| 69 } // namespace ${domain} |
| 70 """) |
| 71 |
| 72 tmpl_struct = string.Template("""\ |
| 73 namespace ${domain} { |
| 74 struct ${declared_name} { |
| 75 public: |
| 76 ${declared_name}(); |
| 77 |
| 78 ${methods}\ |
| 79 |
| 80 private: |
| 81 friend class ::content::DevToolsProtocolHandlerImpl; |
| 82 |
| 83 ${fields}\ |
| 84 }; |
| 85 } // namespace ${domain} |
| 86 """) |
| 87 |
| 88 tmpl_struct_setter = string.Template("""\ |
| 89 void set_${param}(${pass_type} ${param}); |
| 90 """) |
| 91 |
| 92 tmpl_struct_field = string.Template("""\ |
| 93 ${param_type} ${param}_; |
| 94 bool has_${param}_; |
| 95 """) |
| 96 |
| 97 tmpl_enum = string.Template("""\ |
| 98 namespace ${domain} { |
| 99 namespace ${command_underscored} { |
| 100 ${values}\ |
| 101 } // namespace ${command_underscored} |
| 102 } // namespace ${domain} |
| 103 """) |
| 104 |
| 105 tmpl_enum_value = string.Template("""\ |
| 106 extern const char k${Param}${Value}[]; |
| 107 """) |
| 108 |
| 109 tmpl_enum_value_def = string.Template("""\ |
| 110 const char k${Param}${Value}[] = "${value}"; |
| 111 """) |
| 112 |
| 113 tmpl_handler = string.Template("""\ |
| 114 namespace ${domain} { |
| 115 class ${Domain}Handler; |
| 116 } // namespace domain |
| 117 """) |
| 118 |
| 119 tmpl_client = string.Template("""\ |
| 120 namespace ${domain} { |
| 121 class Client : public DevToolsProtocolClient { |
| 122 public: |
| 123 Client(const EventCallback& event_callback, |
| 124 const ResponseCallback& response_callback); |
| 125 virtual ~Client(); |
| 126 |
| 127 ${methods}\ |
| 128 }; |
| 129 } // namespace ${domain} |
| 130 """) |
| 131 |
| 132 tmpl_event = string.Template("""\ |
| 133 void ${Command}( |
| 134 const ${Command}Params& params); |
| 135 """) |
| 136 |
| 137 tmpl_response = string.Template("""\ |
| 138 void Send${Command}Response( |
| 139 scoped_refptr<DevToolsProtocol::Command> command, |
| 140 const ${Command}Response& params); |
| 141 """) |
| 142 |
| 143 tmpl_setter = string.Template("""\ |
| 144 void Set${Domain}Handler( |
| 145 devtools::${domain}::${Domain}Handler* ${domain}_handler); |
| 146 """) |
| 147 |
| 148 tmpl_friend = string.Template("""\ |
| 149 friend class devtools::${domain}::Client; |
| 150 """) |
| 151 |
| 152 tmpl_callback = string.Template("""\ |
| 153 scoped_refptr<DevToolsProtocol::Response> |
| 154 On${Domain}${Command}( |
| 155 scoped_refptr<DevToolsProtocol::Command> command); |
| 156 """) |
| 157 |
| 158 tmpl_to_value = string.Template("""\ |
| 159 static base::DictionaryValue* ToValue( |
| 160 const devtools::${domain}::${declared_name}& src); |
| 161 """) |
| 162 |
| 163 tmpl_field = string.Template("""\ |
| 164 devtools::${domain}::${Domain}Handler* ${domain}_handler_; |
| 165 """) |
| 166 |
| 167 template_cc = string.Template(header + """\ |
| 168 |
| 169 #include "content/browser/devtools/protocol/devtools_protocol_handler_impl.h" |
| 170 |
| 171 #include "base/bind.h" |
| 172 ${includes}\ |
| 173 |
| 174 namespace content { |
| 175 |
| 176 DevToolsProtocolHandlerImpl::DevToolsProtocolHandlerImpl() |
| 177 : ${fields_init} { |
| 178 } |
| 179 |
| 180 DevToolsProtocolHandlerImpl::~DevToolsProtocolHandlerImpl() { |
| 181 } |
| 182 |
| 183 namespace { |
| 184 |
| 185 typedef DevToolsProtocolClient::ResponseStatus ResponseStatus; |
| 186 |
| 187 bool CreateCommonResponse( |
| 188 scoped_refptr<DevToolsProtocol::Command> command, |
| 189 const DevToolsProtocolClient::Response& response, |
| 190 scoped_refptr<DevToolsProtocol::Response>* protocol_response) { |
| 191 switch (response.status()) { |
| 192 case ResponseStatus::RESPONSE_STATUS_FALLTHROUGH: |
| 193 *protocol_response = NULL; |
| 194 break; |
| 195 case ResponseStatus::RESPONSE_STATUS_OK: |
| 196 return false; |
| 197 case ResponseStatus::RESPONSE_STATUS_INVALID_PARAMS: |
| 198 *protocol_response = command->InvalidParamResponse(response.message()); |
| 199 break; |
| 200 case ResponseStatus::RESPONSE_STATUS_INTERNAL_ERROR: |
| 201 *protocol_response = command->InternalErrorResponse(response.message()); |
| 202 break; |
| 203 case ResponseStatus::RESPONSE_STATUS_SERVER_ERROR: |
| 204 *protocol_response = command->ServerErrorResponse(response.message()); |
| 205 break; |
| 206 } |
| 207 return true; |
| 208 } |
| 209 |
| 210 } // namespace |
| 211 |
| 212 ${methods}\ |
| 213 |
| 214 namespace devtools { |
| 215 |
| 216 ${types}\ |
| 217 |
| 218 } // namespace devtools |
| 219 |
| 220 } // namespace content |
| 221 """) |
| 222 |
| 223 tmpl_include = string.Template("""\ |
| 224 #include "content/browser/devtools/protocol/${domain}_handler.h" |
| 225 """) |
| 226 |
| 227 tmpl_field_init = string.Template("${domain}_handler_(NULL)") |
| 228 |
| 229 tmpl_setter_impl = string.Template("""\ |
| 230 void DevToolsProtocolHandlerImpl::Set${Domain}Handler( |
| 231 devtools::${domain}::${Domain}Handler* ${domain}_handler) { |
| 232 DCHECK(!${domain}_handler_); |
| 233 ${domain}_handler_ = ${domain}_handler; |
| 234 ${initializations}\ |
| 235 } |
| 236 """) |
| 237 |
| 238 tmpl_register = string.Template("""\ |
| 239 RegisterCommandHandler( |
| 240 "${Domain}.${command}", |
| 241 base::Bind( |
| 242 &DevToolsProtocolHandlerImpl::On${Domain}${Command}, |
| 243 base::Unretained(this))); |
| 244 """) |
| 245 |
| 246 tmpl_init_client = string.Template("""\ |
| 247 ${domain}_handler_->SetClient(make_scoped_ptr( |
| 248 new devtools::${domain}::Client( |
| 249 base::Bind(&DevToolsProtocolHandlerImpl::SendNotification, |
| 250 base::Unretained(this)), |
| 251 base::Bind(&DevToolsProtocolHandlerImpl::SendAsyncResponse, |
| 252 base::Unretained(this))))); |
| 253 """) |
| 254 |
| 255 tmpl_callback_impl = string.Template("""\ |
| 256 scoped_refptr<DevToolsProtocol::Response> |
| 257 DevToolsProtocolHandlerImpl::On${Domain}${Command}( |
| 258 scoped_refptr<DevToolsProtocol::Command> command) { |
| 259 ${prep}\ |
| 260 Response response = ${domain}_handler_->${Command}(${args}); |
| 261 scoped_refptr<DevToolsProtocol::Response> protocol_response; |
| 262 if (CreateCommonResponse(command, response, &protocol_response)) |
| 263 return protocol_response; |
| 264 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 265 ${wrap}\ |
| 266 return command->SuccessResponse(dict); |
| 267 } |
| 268 """) |
| 269 |
| 270 tmpl_callback_async_impl = string.Template("""\ |
| 271 scoped_refptr<DevToolsProtocol::Response> |
| 272 DevToolsProtocolHandlerImpl::On${Domain}${Command}( |
| 273 scoped_refptr<DevToolsProtocol::Command> command) { |
| 274 ${prep}\ |
| 275 return ${domain}_handler_->${Command}(${args}); |
| 276 } |
| 277 """) |
| 278 |
| 279 params_prep = """\ |
| 280 base::DictionaryValue* params = command->params(); |
| 281 """ |
| 282 |
| 283 tmpl_prep_req = string.Template("""\ |
| 284 ${param_type} in_${param}${init}; |
| 285 if (!params || |
| 286 !params->Get${Type}("${proto_param}", &in_${param})) |
| 287 return command->InvalidParamResponse("${proto_param}"); |
| 288 """) |
| 289 |
| 290 tmpl_prep_req_list = string.Template("""\ |
| 291 base::ListValue* list_${param} = NULL; |
| 292 if (!params || !params->GetList("${proto_param}", &list_${param})) |
| 293 return command->InvalidParamResponse("${proto_param}"); |
| 294 ${param_type} in_${param}; |
| 295 for (base::ListValue::const_iterator it = |
| 296 list_${param}->begin(); it != list_${param}->end(); ++it) { |
| 297 ${item_type} item${item_init}; |
| 298 if (!(*it)->GetAs${ItemType}(&item)) |
| 299 return command->InvalidParamResponse("${proto_param}"); |
| 300 in_${param}.push_back(item); |
| 301 } |
| 302 """) |
| 303 |
| 304 tmpl_prep_opt = string.Template("""\ |
| 305 ${param_type} in_${param}${init}; |
| 306 bool ${param}_found = params && params->Get${Type}( |
| 307 "${proto_param}", |
| 308 &in_${param}); |
| 309 """) |
| 310 |
| 311 tmpl_prep_output = string.Template("""\ |
| 312 ${param_type} out_${param}${init}; |
| 313 """) |
| 314 |
| 315 tmpl_arg_req = string.Template("in_${param}") |
| 316 |
| 317 tmpl_arg_opt = string.Template( |
| 318 "${param}_found ?\n &in_${param} : NULL") |
| 319 |
| 320 tmpl_arg_output = string.Template("&out_${param}") |
| 321 |
| 322 tmpl_to_value_impl = string.Template("""\ |
| 323 // static |
| 324 base::DictionaryValue* DevToolsProtocolHandlerImpl::ToValue( |
| 325 const devtools::${domain}::${declared_name}& src) { |
| 326 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 327 ${dchecks}\ |
| 328 ${wrap}\ |
| 329 return dict; |
| 330 } |
| 331 """) |
| 332 |
| 333 tmpl_dcheck = string.Template("""\ |
| 334 DCHECK(${cond_name}); |
| 335 """) |
| 336 |
| 337 tmpl_struct_impl = string.Template("""\ |
| 338 namespace ${domain} { |
| 339 |
| 340 ${declared_name}::${declared_name}()${fields} { |
| 341 } |
| 342 |
| 343 ${methods}\ |
| 344 |
| 345 } // namespace ${domain} |
| 346 """) |
| 347 |
| 348 tmpl_struct_field_init = string.Template("has_${param}_(false)") |
| 349 |
| 350 tmpl_struct_setter_impl = string.Template("""\ |
| 351 void ${declared_name}::set_${param}( |
| 352 ${pass_type} ${param}) { |
| 353 ${param}_ = ${param}; |
| 354 has_${param}_ = true; |
| 355 } |
| 356 """) |
| 357 |
| 358 tmpl_client_impl = string.Template("""\ |
| 359 namespace ${domain} { |
| 360 |
| 361 Client::Client(const EventCallback& event_callback, |
| 362 const ResponseCallback& response_callback) |
| 363 : DevToolsProtocolClient(event_callback, response_callback) { |
| 364 } |
| 365 |
| 366 Client::~Client() { |
| 367 } |
| 368 |
| 369 ${methods}\ |
| 370 |
| 371 } // namespace ${domain} |
| 372 """) |
| 373 |
| 374 tmpl_event_impl = string.Template("""\ |
| 375 void Client::${Command}( |
| 376 const ${Command}Params& params) { |
| 377 SendNotification("${Domain}.${command}", |
| 378 DevToolsProtocolHandlerImpl::ToValue(params)); |
| 379 } |
| 380 """) |
| 381 |
| 382 tmpl_response_impl = string.Template("""\ |
| 383 void Client::Send${Command}Response( |
| 384 scoped_refptr<DevToolsProtocol::Command> command, |
| 385 const ${Command}Response& params) { |
| 386 SendAsyncResponse( |
| 387 command->SuccessResponse(DevToolsProtocolHandlerImpl::ToValue(params))); |
| 388 } |
| 389 """) |
| 390 |
| 391 tmpl_wrap = string.Template("""\ |
| 392 dict->Set${Type}("${proto_param}", ${var_name}); |
| 393 """) |
| 394 |
| 395 tmpl_wrap_dict = string.Template("""\ |
| 396 dict->Set("${proto_param}", |
| 397 DevToolsProtocolHandlerImpl::ToValue(${var_name})); |
| 398 """) |
| 399 |
| 400 tmpl_wrap_obj = string.Template("""\ |
| 401 dict->Set("${proto_param}", ${var_name}); |
| 402 """) |
| 403 |
| 404 tmpl_wrap_list = string.Template("""\ |
| 405 base::ListValue* list_${param} = new base::ListValue(); |
| 406 for (${param_type}::const_iterator it = |
| 407 ${var_name}.begin(); it != ${var_name}.end(); ++it) { |
| 408 ${append}\ |
| 409 } |
| 410 dict->Set("${proto_param}", list_${param}); |
| 411 """) |
| 412 |
| 413 tmpl_append = string.Template("""\ |
| 414 list_${param}->Append${Type}(*it); |
| 415 """) |
| 416 |
| 417 tmpl_append_dict = string.Template("""\ |
| 418 list_${param}->Append(DevToolsProtocolHandlerImpl::ToValue(*it)); |
| 419 """) |
| 420 |
| 421 tmpl_append_obj = string.Template("""\ |
| 422 list_${param}->Append(*it); |
| 423 """) |
| 424 |
| 425 tmpl_wrap_opt = string.Template("""\ |
| 426 if (${cond_name}) |
| 427 dict->Set${Type}("${proto_param}", ${var_name}); |
| 428 """) |
| 429 |
| 430 tmpl_typename = string.Template("devtools::${domain}::${declared_name}") |
| 431 |
| 432 def Capitalize(s): |
| 433 return s[:1].upper() + s[1:] |
| 434 |
| 435 def Decapitalize(s): |
| 436 return s.lower() |
| 437 |
| 438 def Uncamelcase(s): |
| 439 result = "" |
| 440 for i, c in enumerate(s): |
| 441 if c.isupper(): |
| 442 if (i > 0) and ((i < len(s)-1) and s[i+1].islower() or s[i-1].islower()): |
| 443 result += "_" |
| 444 result += c.lower() |
| 445 else: |
| 446 result += c |
| 447 return result |
| 448 |
| 449 types = {} |
| 450 json_api = json.loads(open(input_json_path, "r").read()) |
| 451 type_decls = [] |
| 452 type_impls = [] |
| 453 handler_methods = [] |
| 454 handler_method_impls = [] |
| 455 |
| 456 for json_domain in json_api["domains"]: |
| 457 if "types" in json_domain: |
| 458 for json_type in json_domain["types"]: |
| 459 types["%s.%s" % (json_domain["domain"], json_type["id"])] = json_type |
| 460 |
| 461 def DeclareStruct(json_properties, mapping): |
| 462 methods = [] |
| 463 fields = [] |
| 464 fields_init = [] |
| 465 method_impls = [] |
| 466 dchecks = [] |
| 467 wrap = [] |
| 468 for json_prop in json_properties: |
| 469 prop_map = mapping.copy() |
| 470 prop_map["proto_param"] = json_prop["name"] |
| 471 prop_map["param"] = Uncamelcase(json_prop["name"]) |
| 472 prop_map["var_name"] = "src.%s_" % prop_map["param"] |
| 473 prop_map["cond_name"] = "src.has_%s_" % prop_map["param"] |
| 474 ResolveType(json_prop, prop_map) |
| 475 prop_map["declared_name"] = mapping["declared_name"] |
| 476 methods.append(tmpl_struct_setter.substitute(prop_map)) |
| 477 fields.append(tmpl_struct_field.substitute(prop_map)) |
| 478 fields_init.append(tmpl_struct_field_init.substitute(prop_map)) |
| 479 method_impls.append(tmpl_struct_setter_impl.substitute(prop_map)) |
| 480 if json_prop.get("optional"): |
| 481 if param_map["Type"] in ["List", "Dictionary"]: |
| 482 # TODO(vkuzkokov) Implement. |
| 483 raise Exception( |
| 484 "Optional array and object properties are not implemented") |
| 485 wrap.append(tmpl_wrap_opt.substitute(prop_map)) |
| 486 else: |
| 487 dchecks.append(tmpl_dcheck.substitute(prop_map)); |
| 488 if not "wrap" in prop_map: |
| 489 raise Exception("Arrays of arrays are not implemented") |
| 490 wrap.append(prop_map["wrap"]) |
| 491 |
| 492 type_decls.append(tmpl_struct.substitute(mapping, |
| 493 methods = "".join(methods), |
| 494 fields = "".join(fields))) |
| 495 fields_init_str = "" |
| 496 if len(fields_init) > 0: |
| 497 fields_init_str = "\n : " + (",\n ".join(fields_init)) |
| 498 type_impls.append(tmpl_struct_impl.substitute(mapping, |
| 499 fields = fields_init_str, |
| 500 methods = "\n".join(method_impls))) |
| 501 handler_methods.append(tmpl_to_value.substitute(mapping)) |
| 502 handler_method_impls.append(tmpl_to_value_impl.substitute(mapping, |
| 503 dchecks = "".join(dchecks), |
| 504 wrap = "".join(wrap))) |
| 505 |
| 506 def ResolveRef(json, mapping): |
| 507 dot_pos = json["$ref"].find(".") |
| 508 if dot_pos == -1: |
| 509 domain_name = mapping["Domain"] |
| 510 type_name = json["$ref"] |
| 511 else: |
| 512 domain_name = json["$ref"][:dot_pos] |
| 513 type_name = json["$ref"][dot_pos + 1:] |
| 514 json_type = types["%s.%s" % (domain_name, type_name)] |
| 515 mapping["declared_name"] = Capitalize(type_name) |
| 516 mapping["Domain"] = domain_name |
| 517 mapping["domain"] = Decapitalize(domain_name) |
| 518 mapping["param_type"] = tmpl_typename.substitute(mapping) |
| 519 if json_type.get("enum"): |
| 520 # TODO(vkuzkokov) Implement. Approximate template: |
| 521 # namespace ${domain} { const char k${declared_name}${Value}; } |
| 522 raise Exception("Named enumerations are not implemented") |
| 523 ResolveType(json_type, mapping) |
| 524 if not "___struct_declared" in json_type: |
| 525 json_type["___struct_declared"] = True; |
| 526 if (json_type.get("type") == "object") and ("properties" in json_type): |
| 527 DeclareStruct(json_type["properties"], mapping) |
| 528 else: |
| 529 type_decls.append(tmpl_typedef.substitute(mapping)) |
| 530 mapping["param_type"] = tmpl_typename.substitute(mapping) |
| 531 |
| 532 def ResolveArray(json, mapping): |
| 533 items_map = mapping.copy() |
| 534 ResolveType(json["items"], items_map) |
| 535 mapping["param_type"] = "std::vector<%s>" % items_map["param_type"] |
| 536 mapping["Type"] = "List" |
| 537 if "append" in items_map: |
| 538 mapping["wrap"] = tmpl_wrap_list.substitute(mapping, |
| 539 append = items_map["append"]) |
| 540 mapping["pass_type"] = "const %s&" % mapping["param_type"] |
| 541 mapping["prep_req"] = tmpl_prep_req_list.substitute(mapping, |
| 542 item_type = items_map["param_type"], |
| 543 item_init = items_map["init"], |
| 544 ItemType = items_map["Type"]) |
| 545 # TODO(vkuzkokov) mapping["append"]: template for array of arrays. |
| 546 |
| 547 def ResolveObject(json, mapping): |
| 548 mapping["Type"] = "Dictionary" |
| 549 if "properties" in json: |
| 550 if not "declared_name" in mapping: |
| 551 mapping["declared_name"] = ("%s%s" % |
| 552 (mapping["Command"], Capitalize(mapping["proto_param"]))) |
| 553 mapping["param_type"] = tmpl_typename.substitute(mapping) |
| 554 DeclareStruct(json["properties"], mapping) |
| 555 mapping["append"] = tmpl_append_dict.substitute(mapping) |
| 556 mapping["wrap"] = tmpl_wrap_dict.substitute(mapping) |
| 557 mapping["pass_type"] = "const %s&" % mapping["param_type"] |
| 558 else: |
| 559 mapping["param_type"] = "base::DictionaryValue*" |
| 560 mapping["append"] = tmpl_append_obj.substitute(mapping) |
| 561 mapping["wrap"] = tmpl_wrap_obj.substitute(mapping) |
| 562 mapping["pass_type"] = mapping["param_type"] |
| 563 |
| 564 def ResolvePrimitive(json, mapping): |
| 565 jsonrpc_type = json["type"] |
| 566 if jsonrpc_type == "boolean": |
| 567 mapping["param_type"] = "bool" |
| 568 mapping["Type"] = "Boolean" |
| 569 mapping["init"] = " = false" |
| 570 elif jsonrpc_type == "integer": |
| 571 mapping["param_type"] = "int" |
| 572 mapping["Type"] = "Integer" |
| 573 mapping["init"] = " = 0" |
| 574 elif jsonrpc_type == "number": |
| 575 mapping["param_type"] = "double" |
| 576 mapping["Type"] = "Double" |
| 577 mapping["init"] = " = 0.0" |
| 578 elif jsonrpc_type == "string": |
| 579 mapping["param_type"] = "std::string" |
| 580 mapping["pass_type"] = "const std::string&" |
| 581 mapping["Type"] = "String" |
| 582 if "enum" in json: |
| 583 values = [] |
| 584 value_defs = [] |
| 585 mapping["command_underscored"] = Uncamelcase(mapping["command"]) |
| 586 mapping["Param"] = Capitalize(mapping["proto_param"]) |
| 587 for enum_value in json["enum"]: |
| 588 values.append(tmpl_enum_value.substitute(mapping, |
| 589 Value = Capitalize(enum_value))) |
| 590 value_defs.append(tmpl_enum_value_def.substitute(mapping, |
| 591 value = enum_value, |
| 592 Value = Capitalize(enum_value))) |
| 593 type_decls.append(tmpl_enum.substitute(mapping, |
| 594 values = "".join(values))) |
| 595 type_impls.append(tmpl_enum.substitute(mapping, |
| 596 values = "".join(value_defs))) |
| 597 else: |
| 598 raise Exception("Unknown type: %s" % json_type) |
| 599 mapping["wrap"] = tmpl_wrap.substitute(mapping) |
| 600 mapping["append"] = tmpl_append.substitute(mapping) |
| 601 mapping["prep_req"] = tmpl_prep_req.substitute(mapping) |
| 602 if jsonrpc_type != "string": |
| 603 mapping["pass_type"] = mapping["param_type"] |
| 604 |
| 605 def ResolveType(json, mapping): |
| 606 mapping["init"] = "" |
| 607 if "$ref" in json: |
| 608 ResolveRef(json, mapping) |
| 609 elif "type" in json: |
| 610 jsonrpc_type = json["type"] |
| 611 if jsonrpc_type == "array": |
| 612 ResolveArray(json, mapping) |
| 613 elif jsonrpc_type == "object": |
| 614 ResolveObject(json, mapping) |
| 615 else: |
| 616 ResolvePrimitive(json, mapping) |
| 617 else: |
| 618 raise Exception("Unknown type at %s.%s %s" % |
| 619 (mapping["Domain"], mapping["command"], mapping["proto_param"])) |
| 620 |
| 621 setters = [] |
| 622 friends = [] |
| 623 fields = [] |
| 624 |
| 625 includes = [] |
| 626 fields_init = [] |
| 627 |
| 628 for json_domain in json_api["domains"]: |
| 629 domain_map = {} |
| 630 domain_map["Domain"] = json_domain["domain"] |
| 631 domain_map["domain"] = Decapitalize(json_domain["domain"]) |
| 632 |
| 633 initializations = [] |
| 634 client_methods = [] |
| 635 client_method_impls = [] |
| 636 domain_empty = True |
| 637 domain_needs_client = False |
| 638 |
| 639 if "commands" in json_domain: |
| 640 for json_command in json_domain["commands"]: |
| 641 if (not ("handlers" in json_command) or |
| 642 not ("browser" in json_command["handlers"])): |
| 643 continue |
| 644 domain_empty = False |
| 645 |
| 646 command_map = domain_map.copy() |
| 647 command_map["command"] = json_command["name"] |
| 648 command_map["Command"] = Capitalize(json_command["name"]) |
| 649 |
| 650 prep = [] |
| 651 args = [] |
| 652 |
| 653 if "parameters" in json_command: |
| 654 for json_param in json_command["parameters"]: |
| 655 param_map = command_map.copy() |
| 656 param_map["proto_param"] = json_param["name"] |
| 657 param_map["param"] = Uncamelcase(json_param["name"]) |
| 658 param_map["var_name"] = "in_%s" % param_map["param"] |
| 659 |
| 660 ResolveType(json_param, param_map) |
| 661 if len(prep) == 0: |
| 662 prep.append(params_prep) |
| 663 if json_param.get("optional"): |
| 664 if param_map["Type"] in ["List", "Dictionary"]: |
| 665 # TODO(vkuzkokov) Implement transformation of base::ListValue |
| 666 # to std::vector and base::DictonaryValue to struct. |
| 667 raise Exception( |
| 668 "Optional array and object parameters are not implemented") |
| 669 prep.append(tmpl_prep_opt.substitute(param_map)) |
| 670 args.append(tmpl_arg_opt.substitute(param_map)) |
| 671 else: |
| 672 prep.append(param_map["prep_req"]) |
| 673 args.append(tmpl_arg_req.substitute(param_map)) |
| 674 |
| 675 if json_command.get("async"): |
| 676 domain_needs_client = True |
| 677 json_returns = [] |
| 678 if "returns" in json_command: |
| 679 json_returns = json_command["returns"] |
| 680 command_map["declared_name"] = "%sResponse" % command_map["Command"] |
| 681 DeclareStruct(json_returns, command_map) |
| 682 # TODO(vkuzkokov) Pass async callback instance similar to how |
| 683 # InspectorBackendDispatcher does it. This, however, can work |
| 684 # only if Blink and Chrome are in the same repo. |
| 685 args.append("command") |
| 686 handler_method_impls.append( |
| 687 tmpl_callback_async_impl.substitute(command_map, |
| 688 prep = "".join(prep), |
| 689 args = "\n " + ",\n ".join(args))) |
| 690 client_methods.append(tmpl_response.substitute(command_map)) |
| 691 client_method_impls.append(tmpl_response_impl.substitute(command_map)) |
| 692 else: |
| 693 wrap = [] |
| 694 if "returns" in json_command: |
| 695 for json_param in json_command["returns"]: |
| 696 param_map = command_map.copy() |
| 697 param_map["proto_param"] = json_param["name"] |
| 698 param_map["param"] = Uncamelcase(json_param["name"]) |
| 699 param_map["var_name"] = "out_%s" % param_map["param"] |
| 700 |
| 701 if json_param.get("optional"): |
| 702 # TODO(vkuzkokov) Implement Optional<T> for value types. |
| 703 raise Exception("Optional return values are not implemented") |
| 704 ResolveType(json_param, param_map) |
| 705 prep.append(tmpl_prep_output.substitute(param_map)) |
| 706 args.append(tmpl_arg_output.substitute(param_map)) |
| 707 if not "wrap" in param_map: |
| 708 raise Exception("Arrays of arrays are not implemented") |
| 709 wrap.append(param_map["wrap"]) |
| 710 |
| 711 args_str = "" |
| 712 if len(args) > 0: |
| 713 args_str = "\n " + ",\n ".join(args) |
| 714 handler_method_impls.append(tmpl_callback_impl.substitute(command_map, |
| 715 prep = "".join(prep), |
| 716 args = args_str, |
| 717 wrap = "".join(wrap))) |
| 718 |
| 719 initializations.append(tmpl_register.substitute(command_map)) |
| 720 handler_methods.append(tmpl_callback.substitute(command_map)) |
| 721 |
| 722 if "events" in json_domain: |
| 723 for json_event in json_domain["events"]: |
| 724 if (not ("handlers" in json_event) or |
| 725 not ("browser" in json_event["handlers"])): |
| 726 continue |
| 727 domain_empty = False |
| 728 domain_needs_client = True |
| 729 |
| 730 event_map = domain_map.copy() |
| 731 event_map["command"] = json_event["name"] |
| 732 event_map["Command"] = Capitalize(json_event["name"]) |
| 733 |
| 734 json_parameters = [] |
| 735 if "parameters" in json_event: |
| 736 json_parameters = json_event["parameters"] |
| 737 event_map["declared_name"] = "%sParams" % event_map["Command"] |
| 738 DeclareStruct(json_parameters, event_map); |
| 739 |
| 740 client_methods.append(tmpl_event.substitute(event_map)) |
| 741 client_method_impls.append(tmpl_event_impl.substitute(event_map)) |
| 742 |
| 743 if domain_empty: |
| 744 continue |
| 745 type_decls.append(tmpl_handler.substitute(domain_map)) |
| 746 setters.append(tmpl_setter.substitute(domain_map)) |
| 747 fields.append(tmpl_field.substitute(domain_map)) |
| 748 includes.append(tmpl_include.substitute(domain_map)) |
| 749 fields_init.append(tmpl_field_init.substitute(domain_map)) |
| 750 if domain_needs_client: |
| 751 type_decls.append(tmpl_client.substitute(domain_map, |
| 752 methods = "".join(client_methods))) |
| 753 friends.append(tmpl_friend.substitute(domain_map)) |
| 754 initializations.append(tmpl_init_client.substitute(domain_map)) |
| 755 type_impls.append(tmpl_client_impl.substitute(domain_map, |
| 756 methods = "\n".join(client_method_impls))) |
| 757 handler_method_impls.append(tmpl_setter_impl.substitute(domain_map, |
| 758 initializations = "".join(initializations))) |
| 759 |
| 760 |
| 761 output_h_file = open(output_h_path, "w") |
| 762 output_cc_file = open(output_cc_path, "w") |
| 763 |
| 764 output_h_file.write(template_h.substitute({}, |
| 765 types = "\n".join(type_decls), |
| 766 setters = "".join(setters), |
| 767 friends = "".join(friends), |
| 768 methods = "".join(handler_methods), |
| 769 fields = "".join(fields))) |
| 770 output_h_file.close() |
| 771 |
| 772 output_cc_file.write(template_cc.substitute({}, |
| 773 includes = "".join(sorted(includes)), |
| 774 fields_init = ",\n ".join(fields_init), |
| 775 methods = "\n".join(handler_method_impls), |
| 776 types = "\n".join(type_impls))) |
| 777 output_cc_file.close() |
OLD | NEW |