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