Chromium Code Reviews| 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 ${setters}\ | |
| 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 tmpl_struct_field = string.Template("""\ | |
| 102 ${param_type} ${param}_; | |
| 103 bool has_${param}_; | |
| 104 """) | |
| 105 | |
| 106 tmpl_handler = string.Template("""\ | |
| 107 namespace ${domain} { | |
| 108 class ${Domain}Handler; | |
| 109 } // namespace domain | |
| 110 """) | |
| 111 | |
| 112 tmpl_frontend = string.Template("""\ | |
| 113 namespace ${domain} { | |
| 114 class Frontend : public DevToolsProtocolFrontend { | |
| 115 public: | |
| 116 Frontend(const EventCallback& event_callback); | |
| 117 virtual ~Frontend(); | |
| 118 | |
| 119 ${events}\ | |
| 120 }; | |
| 121 } // namespace ${domain} | |
| 122 """) | |
| 123 | |
| 124 tmpl_event = string.Template("""\ | |
| 125 void ${Event}($params); | |
| 126 """) | |
| 127 | |
| 128 tmpl_event_param = string.Template("${pass_type} ${param}") | |
| 129 | |
| 130 tmpl_getter = string.Template("""\ | |
| 131 devtools::${domain}::${Domain}Handler* ${domain}(); | |
| 132 """) | |
| 133 | |
| 134 tmpl_friend = string.Template("""\ | |
| 135 friend class devtools::${domain}::Frontend; | |
| 136 """) | |
| 137 | |
| 138 tmpl_callback = string.Template("""\ | |
| 139 scoped_refptr<DevToolsProtocol::Response> On${Domain}${Command}( | |
| 140 scoped_refptr<DevToolsProtocol::Command> command); | |
| 141 """) | |
| 142 | |
| 143 tmpl_to_value = string.Template("""\ | |
| 144 static base::Value* ToValue(${pass_type} src); | |
| 145 """) | |
| 146 | |
| 147 tmpl_field = string.Template("""\ | |
| 148 scoped_ptr<devtools::${domain}::${Domain}Handler> ${domain}_handler_; | |
| 149 """) | |
| 150 | |
| 151 template_cc = string.Template(header + """\ | |
| 152 | |
| 153 #include "content/browser/devtools/protocol/devtools_protocol_handler_impl.h" | |
| 154 | |
| 155 #include "base/bind.h" | |
| 156 ${includes}\ | |
| 157 | |
| 158 namespace content { | |
| 159 | |
| 160 DevToolsProtocolHandlerImpl::DevToolsProtocolHandlerImpl() { | |
| 161 ${initializations}\ | |
| 162 } | |
| 163 | |
| 164 DevToolsProtocolHandlerImpl::~DevToolsProtocolHandlerImpl() { | |
| 165 } | |
| 166 | |
| 167 void DevToolsProtocolHandlerImpl::OnClientDetached() { | |
| 168 ${client_detached}\ | |
| 169 } | |
| 170 | |
| 171 void DevToolsProtocolHandlerImpl::SetRenderViewHost(RenderViewHostImpl* host) { | |
| 172 ${set_rvh}\ | |
| 173 } | |
| 174 | |
| 175 ${getters}\ | |
| 176 | |
| 177 ${callbacks}\ | |
| 178 | |
| 179 ${to_value}\ | |
| 180 | |
| 181 namespace devtools { | |
| 182 | |
| 183 ${types}\ | |
| 184 | |
| 185 ${frontends}\ | |
| 186 | |
| 187 } // namespace devtools | |
| 188 | |
| 189 } // namespace content | |
| 190 """) | |
| 191 | |
| 192 tmpl_include = string.Template("""\ | |
| 193 #include "content/browser/devtools/protocol/${domain}_handler.h" | |
| 194 """) | |
| 195 | |
| 196 tmpl_register = string.Template("""\ | |
| 197 RegisterCommandHandler( | |
| 198 "${Domain}.${command}", | |
| 199 base::Bind(&DevToolsProtocolHandlerImpl::On${Domain}${Command}, | |
| 200 base::Unretained(this))); | |
| 201 """) | |
| 202 | |
| 203 tmpl_init_handler = string.Template("""\ | |
| 204 ${domain}_handler_.reset(new devtools::${domain}::${Domain}Handler()); | |
| 205 """) | |
| 206 | |
| 207 tmpl_init_frontend = string.Template("""\ | |
| 208 ${domain}_handler_->SetFrontend(make_scoped_ptr( | |
| 209 new devtools::${domain}::Frontend( | |
| 210 base::Bind(&DevToolsProtocolHandlerImpl::SendNotification, | |
| 211 base::Unretained(this))))); | |
| 212 """) | |
| 213 | |
| 214 tmpl_client_detached = string.Template("""\ | |
| 215 ${domain}_handler_->OnClientDetached(); | |
| 216 """) | |
| 217 | |
| 218 tmpl_set_rvh = string.Template("""\ | |
| 219 ${domain}_handler_->SetRenderViewHost(host); | |
| 220 """) | |
| 221 | |
| 222 tmpl_getter_impl = string.Template("""\ | |
| 223 devtools::${domain}::${Domain}Handler* | |
| 224 DevToolsProtocolHandlerImpl::${domain}() { | |
| 225 return ${domain}_handler_.get(); | |
| 226 } | |
| 227 """) | |
| 228 | |
| 229 tmpl_callback_impl = string.Template("""\ | |
| 230 scoped_refptr<DevToolsProtocol::Response> | |
| 231 DevToolsProtocolHandlerImpl::On${Domain}${Command}( | |
| 232 scoped_refptr<DevToolsProtocol::Command> command) { | |
| 233 ${prep}\ | |
| 234 Response response = ${domain}_handler_->${Command}(${args}); | |
| 235 switch (response.status()) { | |
| 236 case ResponseStatus::RESPONSE_STATUS_FALLTHROUGH: | |
| 237 return NULL; | |
| 238 case ResponseStatus::RESPONSE_STATUS_OK: | |
| 239 break; | |
| 240 case ResponseStatus::RESPONSE_STATUS_INVALID_PARAMS: | |
| 241 return command->InvalidParamResponse(response.message()); | |
| 242 case ResponseStatus::RESPONSE_STATUS_INTERNAL_ERROR: | |
| 243 return command->InternalErrorResponse(response.message()); | |
| 244 case ResponseStatus::RESPONSE_STATUS_SERVER_ERROR: | |
| 245 return command->ServerErrorResponse(response.message()); | |
| 246 } | |
| 247 base::DictionaryValue* dict = new base::DictionaryValue(); | |
| 248 ${wrap}\ | |
| 249 return command->SuccessResponse(dict); | |
| 250 } | |
| 251 """) | |
| 252 | |
| 253 params_prep = """\ | |
| 254 base::DictionaryValue* params = command->params(); | |
| 255 if (!params) | |
| 256 return command->NoSuchMethodErrorResponse(); | |
| 257 """ | |
| 258 | |
| 259 tmpl_prep_req = string.Template("""\ | |
| 260 ${param_type} in_${param}${init}; | |
| 261 if (!params->Get${Type}("${proto_param}", &in_${param})) | |
| 262 return command->InvalidParamResponse("${proto_param}"); | |
| 263 """) | |
| 264 | |
| 265 tmpl_prep_req_list = string.Template("""\ | |
| 266 base::ListValue* list_${param} = NULL; | |
| 267 if (!params->GetList("${proto_param}", &list_${param})) | |
| 268 return command->InvalidParamResponse("${proto_param}"); | |
| 269 ${param_type} in_${param}; | |
| 270 for (base::ListValue::const_iterator it = list_${param}->begin(); | |
| 271 it != list_${param}->end(); ++it) { | |
| 272 ${item_type} item${item_init}; | |
| 273 if (!(*it)->GetAs${ItemType}(&item)) | |
| 274 return command->InvalidParamResponse("${proto_param}"); | |
| 275 in_${param}.push_back(item); | |
| 276 } | |
| 277 """) | |
| 278 | |
| 279 tmpl_prep_opt = string.Template("""\ | |
| 280 ${param_type} in_${param}${init}; | |
| 281 bool ${param}_found = params->Get${Type}("${proto_param}", &in_${param}); | |
| 282 """) | |
| 283 | |
| 284 tmpl_prep_output = string.Template("""\ | |
| 285 ${param_type} out_${param}${init}; | |
| 286 """) | |
| 287 | |
| 288 tmpl_arg_req = string.Template("in_${param}") | |
| 289 | |
| 290 tmpl_arg_opt = string.Template("${param}_found ? &in_${param} : NULL") | |
| 291 | |
| 292 tmpl_arg_output = string.Template("&out_${param}") | |
| 293 | |
| 294 tmpl_to_value_impl = string.Template("""\ | |
| 295 // static | |
| 296 base::Value* DevToolsProtocolHandlerImpl::ToValue(${pass_type} src) { | |
| 297 base::DictionaryValue* dict = new base::DictionaryValue(); | |
| 298 ${dchecks}\ | |
| 299 ${wrap}\ | |
| 300 return dict; | |
| 301 } | |
| 302 """) | |
| 303 | |
| 304 tmpl_dcheck = string.Template("""\ | |
| 305 DCHECK(src.has_${param}_); | |
| 306 """) | |
| 307 | |
| 308 tmpl_struct_impl = string.Template("""\ | |
| 309 namespace ${domain} { | |
| 310 | |
| 311 ${declared_name}::${declared_name}() | |
| 312 : ${fields} { | |
| 313 } | |
| 314 | |
| 315 ${setters}\ | |
| 316 | |
| 317 } // namespace ${domain} | |
| 318 """) | |
| 319 | |
| 320 tmpl_struct_field_init = string.Template("has_${param}_(false)") | |
| 321 | |
| 322 tmpl_struct_setter_impl = string.Template("""\ | |
| 323 void ${declared_name}::set_${param}(${pass_type} ${param}) { | |
| 324 ${param}_ = ${param}; | |
| 325 has_${param}_ = true; | |
| 326 } | |
| 327 """) | |
| 328 | |
| 329 tmpl_frontend_impl = string.Template("""\ | |
| 330 namespace ${domain} { | |
| 331 | |
| 332 Frontend::Frontend(const EventCallback& event_callback) | |
| 333 : DevToolsProtocolFrontend(event_callback) { | |
| 334 } | |
| 335 | |
| 336 Frontend::~Frontend() { | |
| 337 } | |
| 338 | |
| 339 ${events}\ | |
| 340 | |
| 341 } // namespace ${domain} | |
| 342 """) | |
| 343 | |
| 344 tmpl_event_impl = string.Template("""\ | |
| 345 void Frontend::${Event}(${params}) { | |
| 346 base::DictionaryValue* dict = new base::DictionaryValue(); | |
| 347 ${wrap}\ | |
| 348 SendNotification("${Domain}.${event}", dict); | |
| 349 } | |
| 350 """) | |
| 351 | |
| 352 tmpl_wrap = string.Template("""\ | |
| 353 dict->Set${Type}("${proto_param}", ${var_name}); | |
| 354 """) | |
| 355 | |
| 356 tmpl_wrap_dict = string.Template("""\ | |
| 357 dict->Set("${proto_param}", | |
| 358 DevToolsProtocolHandlerImpl::ToValue(${var_name})); | |
| 359 """) | |
| 360 | |
| 361 tmpl_wrap_obj = string.Template("""\ | |
| 362 dict->Set("${proto_param}", ${var_name}); | |
| 363 """) | |
| 364 | |
| 365 tmpl_wrap_list = string.Template("""\ | |
| 366 base::ListValue* list_${param} = new base::ListValue(); | |
| 367 for (${param_type}::const_iterator it = ${var_name}.begin(); | |
| 368 it != ${var_name}.end(); ++it) { | |
| 369 ${append}\ | |
| 370 } | |
| 371 dict->Set("${proto_param}", list_${param}); | |
| 372 """) | |
| 373 | |
| 374 tmpl_append = string.Template("""\ | |
| 375 list_${param}->Append${Type}(*it); | |
| 376 """) | |
| 377 | |
| 378 tmpl_append_dict = string.Template("""\ | |
| 379 list_${param}->Append(DevToolsProtocolHandlerImpl::ToValue(*it)); | |
| 380 """) | |
| 381 | |
| 382 tmpl_append_obj = string.Template("""\ | |
| 383 list_${param}->Append(*it); | |
| 384 """) | |
| 385 | |
| 386 tmpl_wrap_opt = string.Template("""\ | |
| 387 if (${cond_name}) | |
| 388 dict->Set${Type}("${proto_param}", ${var_name}); | |
| 389 """) | |
| 390 | |
| 391 tmpl_typename = string.Template("devtools::${domain}::${declared_name}") | |
| 392 | |
| 393 def Capitalize(s): | |
| 394 return s[:1].upper() + s[1:] | |
| 395 | |
| 396 def Decapitalize(s): | |
| 397 return s.lower() | |
| 398 | |
| 399 def Uncamelcase(s): | |
| 400 result = "" | |
| 401 for i, c in enumerate(s): | |
| 402 if c.isupper(): | |
| 403 if (i < len(s)-1) and s[i+1].islower() or (i > 0) and s[i-1].islower(): | |
|
dgozman
2014/09/11 13:43:22
Does this work for "ContentOfHTMLFile"? We should
vkuzkokov
2014/09/11 17:19:20
_content_of_html_file. Fixed to remove underscore
| |
| 404 result += "_" | |
| 405 result += c.lower() | |
| 406 else: | |
| 407 result += c | |
| 408 return result | |
| 409 | |
| 410 types = {} | |
| 411 json_api = json.loads(open(input_json_path, "r").read()) | |
| 412 type_decls = [] | |
| 413 type_impls = [] | |
| 414 to_value = [] | |
| 415 to_value_impls = [] | |
| 416 | |
| 417 for json_domain in json_api["domains"]: | |
| 418 if "types" in json_domain: | |
| 419 for json_type in json_domain["types"]: | |
| 420 types["%s.%s" % (json_domain["domain"], json_type["id"])] = json_type | |
| 421 | |
| 422 def DeclareStruct(json, mapping): | |
| 423 setters = [] | |
| 424 fields = [] | |
| 425 fields_init = [] | |
| 426 setter_impls = [] | |
| 427 dchecks = [] | |
| 428 wrap = [] | |
| 429 if "properties" in json: | |
|
dgozman
2014/09/11 13:43:22
How there may be no "properties" here?
vkuzkokov
2014/09/11 17:19:20
API changed. No I pass json["properties"] for type
| |
| 430 for json_prop in json["properties"]: | |
| 431 prop_map = mapping.copy() | |
| 432 prop_map.pop("append", None) | |
| 433 prop_map.pop("wrap", None) | |
| 434 prop_map.pop("prep_req", None) | |
| 435 prop_map.pop("pass_type", None) | |
| 436 prop_map["proto_param"] = json_prop["name"] | |
| 437 prop_map["param"] = Uncamelcase(json_prop["name"]) | |
| 438 prop_map["var_name"] = "src.%s_" % prop_map["param"] | |
| 439 ResolveType(json_prop, prop_map) | |
| 440 prop_map["declared_name"] = mapping["declared_name"] | |
| 441 setters.append(tmpl_struct_setter.substitute(prop_map)) | |
| 442 fields.append(tmpl_struct_field.substitute(prop_map)) | |
| 443 fields_init.append(tmpl_struct_field_init.substitute(prop_map)) | |
| 444 setter_impls.append(tmpl_struct_setter_impl.substitute(prop_map)) | |
| 445 if json_prop.get("optional"): | |
| 446 wrap.append(tmpl_wrap_opt.substitute(prop_map, | |
| 447 cond_name = "src.has_%s_" % prop_map["param"])) | |
| 448 else: | |
| 449 dchecks.append(tmpl_dcheck.substitute(prop_map)); | |
| 450 wrap.append(prop_map["wrap"]) | |
| 451 type_decls.append(tmpl_struct.substitute(mapping, | |
| 452 setters = "".join(setters), | |
| 453 fields = "".join(fields))) | |
| 454 type_impls.append(tmpl_struct_impl.substitute(mapping, | |
| 455 fields = ",\n ".join(fields_init), | |
| 456 setters = "\n".join(setter_impls))) | |
| 457 to_value.append(tmpl_to_value.substitute(mapping)) | |
| 458 to_value_impls.append(tmpl_to_value_impl.substitute(mapping, | |
| 459 dchecks = "".join(dchecks), | |
| 460 wrap = "".join(wrap))) | |
| 461 | |
| 462 def ResolveType(json, mapping): | |
| 463 mapping["init"] = "" | |
| 464 if "$ref" in json: | |
| 465 dot_pos = json["$ref"].find(".") | |
| 466 if dot_pos == -1: | |
| 467 domain_name = mapping["Domain"] | |
| 468 type_name = json["$ref"] | |
| 469 else: | |
| 470 domain_name = json["$ref"][:dot_pos] | |
| 471 type_name = json["$ref"][dot_pos + 1:] | |
| 472 json_type = types["%s.%s" % (domain_name, type_name)] | |
| 473 mapping["declared_name"] = Capitalize(type_name) | |
| 474 mapping["param_type"] = tmpl_typename.substitute(mapping) | |
| 475 ResolveType(json_type, mapping) | |
| 476 if not "___mark" in json_type: | |
| 477 json_type["___mark"] = True; | |
| 478 if (json_type.get("type") == "object") and ("properties" in json_type): | |
| 479 DeclareStruct(json_type, mapping) | |
| 480 else: | |
| 481 type_decls.append(tmpl_typedef.substitute(mapping)) | |
| 482 mapping["param_type"] = tmpl_typename.substitute(mapping) | |
| 483 elif "type" in json: | |
|
dgozman
2014/09/11 13:43:22
I'd split this function into ResolveRef, ResolveAr
vkuzkokov
2014/09/11 17:19:20
Done.
| |
| 484 json_type = json["type"] | |
| 485 if json_type == "array": | |
| 486 items_map = mapping.copy() | |
| 487 ResolveType(json["items"], items_map) | |
| 488 mapping["param_type"] = "std::vector<%s>" % items_map["param_type"] | |
| 489 mapping["Type"] = "List" | |
| 490 mapping["wrap"] = tmpl_wrap_list.substitute(mapping, | |
| 491 append = items_map["append"]) | |
| 492 mapping["prep_req"] = tmpl_prep_req_list.substitute(mapping, | |
| 493 item_type = items_map["param_type"], | |
| 494 item_init = items_map["init"], | |
| 495 ItemType = items_map["Type"]) | |
| 496 elif json_type == "boolean": | |
| 497 mapping["param_type"] = "bool" | |
| 498 mapping["Type"] = "Boolean" | |
| 499 mapping["init"] = " = false" | |
| 500 elif json_type == "integer": | |
| 501 mapping["param_type"] = "int" | |
| 502 mapping["Type"] = "Integer" | |
| 503 mapping["init"] = " = 0" | |
| 504 elif json_type == "number": | |
| 505 mapping["param_type"] = "double" | |
| 506 mapping["Type"] = "Double" | |
| 507 mapping["init"] = " = 0.0" | |
| 508 elif json_type == "object": | |
| 509 mapping["Type"] = "Dictionary" | |
| 510 if "properties" in json: | |
| 511 if not "declared_name" in mapping: | |
| 512 if "Command" in mapping: | |
| 513 subdomain = mapping["Command"] | |
| 514 elif "Event" in mapping: | |
| 515 subdomain = mapping["Event"] | |
| 516 else: | |
| 517 raise Exception("Unrecognized subdomain") | |
| 518 mapping["declared_name"] = ("%s%s" % | |
| 519 (subdomain, Capitalize(mapping["proto_param"]))) | |
| 520 mapping["param_type"] = tmpl_typename.substitute(mapping) | |
| 521 DeclareStruct(json_type, mapping) | |
|
dgozman
2014/09/11 13:43:22
This call looks strange: json_type is not a json,
vkuzkokov
2014/09/11 17:19:20
Fixed.
| |
| 522 mapping["append"] = tmpl_append_dict.substitute(mapping) | |
| 523 mapping["wrap"] = tmpl_wrap_dict.substitute(mapping) | |
| 524 mapping["pass_type"] = "const %s&" % mapping["param_type"] | |
| 525 else: | |
| 526 mapping["param_type"] = "base::DictionaryValue*" | |
| 527 mapping["append"] = tmpl_append_obj.substitute(mapping) | |
| 528 mapping["wrap"] = tmpl_wrap_obj.substitute(mapping) | |
| 529 elif json_type == "string": | |
| 530 mapping["param_type"] = "std::string" | |
| 531 mapping["pass_type"] = "const std::string&" | |
| 532 mapping["Type"] = "String" | |
| 533 else: | |
| 534 raise Exception("Unknown type: %s" % json_type) | |
| 535 else: | |
| 536 raise Exception("Unknown type at %s.%s %s" % | |
| 537 (mapping["Domain"], mapping["command"], mapping["proto_param"])) | |
| 538 if not ("wrap" in mapping): | |
| 539 mapping["wrap"] = tmpl_wrap.substitute(mapping) | |
| 540 if not ("append" in mapping): | |
| 541 mapping["append"] = tmpl_append.substitute(mapping) | |
| 542 if not ("prep_req" in mapping): | |
| 543 mapping["prep_req"] = tmpl_prep_req.substitute(mapping) | |
| 544 if not ("pass_type" in mapping): | |
| 545 mapping["pass_type"] = mapping["param_type"] | |
| 546 | |
| 547 handlers = [] | |
| 548 frontends = [] | |
| 549 getters = [] | |
| 550 friends = [] | |
| 551 callbacks = [] | |
| 552 fields = [] | |
| 553 | |
| 554 includes = [] | |
| 555 initializations = [] | |
| 556 client_detached = [] | |
| 557 set_rvh = [] | |
| 558 getter_impls = [] | |
| 559 callback_impls = [] | |
| 560 frontend_impls = [] | |
| 561 | |
| 562 for json_domain in json_api["domains"]: | |
| 563 domain_map = {} | |
| 564 domain_map["Domain"] = json_domain["domain"] | |
| 565 domain_map["domain"] = Decapitalize(json_domain["domain"]) | |
| 566 | |
| 567 events = [] | |
| 568 event_impls = [] | |
| 569 domain_has_commands = False | |
| 570 domain_has_events = False | |
| 571 | |
| 572 if "commands" in json_domain: | |
| 573 for json_command in json_domain["commands"]: | |
| 574 if (not ("handlers" in json_command) or | |
| 575 not ("browser" in json_command["handlers"])): | |
| 576 continue | |
| 577 domain_has_commands = True | |
| 578 | |
| 579 command_map = domain_map.copy() | |
| 580 command_map["command"] = json_command["name"] | |
| 581 command_map["Command"] = Capitalize(json_command["name"]) | |
| 582 | |
| 583 prep = [] | |
| 584 args = [] | |
| 585 wrap = [] | |
| 586 | |
| 587 if "parameters" in json_command: | |
| 588 for json_param in json_command["parameters"]: | |
| 589 param_map = command_map.copy() | |
| 590 param_map["proto_param"] = json_param["name"] | |
| 591 param_map["param"] = Uncamelcase(json_param["name"]) | |
| 592 param_map["var_name"] = "in_%s" % param_map["param"] | |
| 593 | |
| 594 ResolveType(json_param, param_map) | |
| 595 if len(prep) == 0: | |
| 596 prep.append(params_prep) | |
| 597 if json_param.get("optional"): | |
| 598 if param_map["Type"] in ["List", "Dictionary"]: | |
| 599 # TODO(vkuzkokov) implement transformation of base::ListValue | |
| 600 # to std::vector and base::DictonaryValue to struct. | |
| 601 raise Exception( | |
| 602 "Optional array and object parameters are not implemented") | |
| 603 prep.append(tmpl_prep_opt.substitute(param_map)) | |
| 604 args.append(tmpl_arg_opt.substitute(param_map)) | |
| 605 else: | |
| 606 prep.append(param_map["prep_req"]) | |
| 607 args.append(tmpl_arg_req.substitute(param_map)) | |
| 608 | |
| 609 if "returns" in json_command: | |
| 610 for json_param in json_command["returns"]: | |
| 611 param_map = command_map.copy() | |
| 612 param_map["proto_param"] = json_param["name"] | |
| 613 param_map["param"] = Uncamelcase(json_param["name"]) | |
| 614 param_map["var_name"] = "out_%s" % param_map["param"] | |
| 615 | |
| 616 if json_param.get("optional"): | |
| 617 # TODO(vkuzkokov) implement Optional<T> for value types | |
| 618 raise Exception("Optional return values are not implemented") | |
| 619 ResolveType(json_param, param_map) | |
| 620 prep.append(tmpl_prep_output.substitute(param_map)) | |
| 621 args.append(tmpl_arg_output.substitute(param_map)) | |
| 622 wrap.append(param_map["wrap"]) | |
| 623 | |
| 624 initializations.append(tmpl_register.substitute(command_map)) | |
| 625 callbacks.append(tmpl_callback.substitute(command_map)) | |
| 626 callback_impls.append(tmpl_callback_impl.substitute(command_map, | |
| 627 prep = "".join(prep), | |
| 628 args = ", ".join(args), | |
| 629 wrap = "".join(wrap))) | |
| 630 | |
| 631 if "events" in json_domain: | |
| 632 for json_event in json_domain["events"]: | |
| 633 if (not ("handlers" in json_event) or | |
| 634 not ("browser" in json_event["handlers"])): | |
| 635 continue | |
| 636 domain_has_events = True | |
| 637 | |
| 638 event_map = domain_map.copy() | |
| 639 event_map["event"] = json_event["name"] | |
| 640 event_map["Event"] = Capitalize(json_event["name"]) | |
| 641 | |
| 642 params = [] | |
| 643 wrap = [] | |
| 644 | |
| 645 if "parameters" in json_event: | |
| 646 for json_param in json_event["parameters"]: | |
| 647 param_map = event_map.copy() | |
| 648 param_map["proto_param"] = json_param["name"] | |
| 649 param_map["param"] = Uncamelcase(json_param["name"]) | |
| 650 param_map["var_name"] = param_map["param"] | |
| 651 | |
| 652 if json_param.get("optional"): | |
| 653 # TODO(vkuzkokov) force passing by pointer and check for NULL | |
| 654 raise Exception("Optional event params are not implemented") | |
| 655 ResolveType(json_param, param_map) | |
| 656 params.append(tmpl_event_param.substitute(param_map)) | |
| 657 wrap.append(param_map["wrap"]) | |
| 658 | |
| 659 event_map["params"] = ", ".join(params) | |
| 660 events.append(tmpl_event.substitute(event_map)) | |
| 661 event_impls.append(tmpl_event_impl.substitute(event_map, | |
| 662 wrap = "".join(wrap))) | |
| 663 | |
| 664 if not (domain_has_commands or domain_has_events): | |
| 665 continue | |
| 666 handlers.append(tmpl_handler.substitute(domain_map)) | |
| 667 getters.append(tmpl_getter.substitute(domain_map)) | |
| 668 fields.append(tmpl_field.substitute(domain_map)) | |
| 669 includes.append(tmpl_include.substitute(domain_map)) | |
| 670 initializations.append(tmpl_init_handler.substitute(domain_map)) | |
| 671 client_detached.append(tmpl_client_detached.substitute(domain_map)) | |
| 672 set_rvh.append(tmpl_set_rvh.substitute(domain_map)) | |
| 673 getter_impls.append(tmpl_getter_impl.substitute(domain_map)) | |
| 674 if domain_has_events: | |
| 675 frontends.append(tmpl_frontend.substitute(domain_map, | |
| 676 events = "".join(events))) | |
| 677 friends.append(tmpl_friend.substitute(domain_map)) | |
| 678 initializations.append(tmpl_init_frontend.substitute(domain_map)) | |
| 679 frontend_impls.append(tmpl_frontend_impl.substitute(domain_map, | |
| 680 events = "\n".join(event_impls))) | |
| 681 | |
| 682 output_h_file = open(output_h_path, "w") | |
| 683 output_cc_file = open(output_cc_path, "w") | |
| 684 | |
| 685 output_h_file.write(template_h.substitute({}, | |
| 686 types = "\n".join(type_decls), | |
| 687 handlers = "".join(handlers), | |
| 688 getters = "".join(getters), | |
| 689 friends = "".join(friends), | |
| 690 callbacks = "".join(callbacks), | |
| 691 to_value = "".join(to_value), | |
| 692 fields = "".join(fields), | |
| 693 frontends = "\n".join(frontends))) | |
| 694 | |
| 695 output_cc_file.write(template_cc.substitute({}, | |
| 696 includes = "".join(includes), | |
| 697 initializations = "".join(initializations), | |
| 698 client_detached = "".join(client_detached), | |
| 699 set_rvh = "".join(set_rvh), | |
| 700 getters = "\n".join(getter_impls), | |
| 701 callbacks = "\n".join(callback_impls), | |
| 702 to_value = "\n".join(to_value_impls), | |
| 703 types = "\n".join(type_impls), | |
| 704 frontends = "\n".join(frontend_impls))) | |
| OLD | NEW |