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