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