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.pop("append", None) | |
468 prop_map.pop("wrap", None) | |
469 prop_map.pop("prep_req", None) | |
470 prop_map.pop("pass_type", None) | |
471 prop_map["proto_param"] = json_prop["name"] | |
472 prop_map["param"] = Uncamelcase(json_prop["name"]) | |
473 if public_to_value: | |
474 prop_map["var_name"] = "%s_" % prop_map["param"] | |
475 prop_map["cond_name"] = "has_%s_" % prop_map["param"] | |
476 else: | |
477 prop_map["var_name"] = "src.%s_" % prop_map["param"] | |
478 prop_map["cond_name"] = "src.has_%s_" % prop_map["param"] | |
479 ResolveType(json_prop, prop_map) | |
480 prop_map["declared_name"] = mapping["declared_name"] | |
481 methods.append(tmpl_struct_setter.substitute(prop_map)) | |
482 fields.append(tmpl_struct_field.substitute(prop_map)) | |
483 fields_init.append(tmpl_struct_field_init.substitute(prop_map)) | |
484 method_impls.append(tmpl_struct_setter_impl.substitute(prop_map)) | |
485 if json_prop.get("optional"): | |
486 wrap.append(tmpl_wrap_opt.substitute(prop_map)) | |
487 else: | |
488 dchecks.append(tmpl_dcheck.substitute(prop_map)); | |
489 wrap.append(prop_map["wrap"]) | |
490 | |
491 if public_to_value: | |
492 methods.append(struct_to_value) | |
493 method_impls.append(tmpl_struct_to_value_impl.substitute(mapping)) | |
494 | |
495 type_decls.append(tmpl_struct.substitute(mapping, | |
496 methods = "".join(methods), | |
497 fields = "".join(fields))) | |
498 fields_init_str = "" | |
499 if len(fields_init) > 0: | |
500 fields_init_str = "\n : " + (",\n ".join(fields_init)) | |
501 type_impls.append(tmpl_struct_impl.substitute(mapping, | |
502 fields = fields_init_str, | |
503 methods = "\n".join(method_impls))) | |
504 | |
505 if not public_to_value: | |
506 to_value.append(tmpl_to_value.substitute(mapping)) | |
507 to_value_impls.append(tmpl_to_value_impl.substitute(mapping, | |
508 dchecks = "".join(dchecks), | |
509 wrap = "".join(wrap))) | |
510 | |
511 def ResolveRef(json, mapping): | |
512 dot_pos = json["$ref"].find(".") | |
513 if dot_pos == -1: | |
514 domain_name = mapping["Domain"] | |
515 type_name = json["$ref"] | |
516 else: | |
517 domain_name = json["$ref"][:dot_pos] | |
518 type_name = json["$ref"][dot_pos + 1:] | |
519 json_type = types["%s.%s" % (domain_name, type_name)] | |
520 mapping["declared_name"] = Capitalize(type_name) | |
521 mapping["Domain"] = domain_name | |
522 mapping["domain"] = Decapitalize(domain_name) | |
523 mapping["param_type"] = tmpl_typename.substitute(mapping) | |
524 if json_type.get("enum"): | |
525 # TODO(vkuzkokov) Implement. Approximate template: | |
526 # namespace ${domain} { const char k${declared_name}${Value}; } | |
527 raise Exception("Named enumerations are not implemented") | |
528 ResolveType(json_type, mapping) | |
529 if not "___mark" in json_type: | |
530 json_type["___mark"] = True; | |
dgozman
2014/09/12 11:48:45
__mark -> __struct_declared
vkuzkokov
2014/09/12 14:22:52
Done.
| |
531 if (json_type.get("type") == "object") and ("properties" in json_type): | |
532 DeclareStruct(json_type["properties"], mapping) | |
533 else: | |
534 type_decls.append(tmpl_typedef.substitute(mapping)) | |
535 mapping["param_type"] = tmpl_typename.substitute(mapping) | |
536 | |
537 def ResolveArray(json, mapping): | |
538 items_map = mapping.copy() | |
539 ResolveType(json["items"], items_map) | |
540 mapping["param_type"] = "std::vector<%s>" % items_map["param_type"] | |
541 mapping["Type"] = "List" | |
542 mapping["wrap"] = tmpl_wrap_list.substitute(mapping, | |
543 append = items_map["append"]) | |
544 mapping["pass_type"] = "const %s&" % mapping["param_type"] | |
545 mapping["prep_req"] = tmpl_prep_req_list.substitute(mapping, | |
546 item_type = items_map["param_type"], | |
547 item_init = items_map["init"], | |
548 ItemType = items_map["Type"]) | |
549 | |
550 def ResolveObject(json, mapping): | |
551 mapping["Type"] = "Dictionary" | |
552 if "properties" in json: | |
553 if not "declared_name" in mapping: | |
554 mapping["declared_name"] = ("%s%s" % | |
555 (mapping["Command"], Capitalize(mapping["proto_param"]))) | |
556 mapping["param_type"] = tmpl_typename.substitute(mapping) | |
557 DeclareStruct(json["properties"], mapping) | |
558 mapping["append"] = tmpl_append_dict.substitute(mapping) | |
559 mapping["wrap"] = tmpl_wrap_dict.substitute(mapping) | |
560 mapping["pass_type"] = "const %s&" % mapping["param_type"] | |
561 else: | |
562 mapping["param_type"] = "base::DictionaryValue*" | |
563 mapping["append"] = tmpl_append_obj.substitute(mapping) | |
564 mapping["wrap"] = tmpl_wrap_obj.substitute(mapping) | |
565 | |
566 def ResolvePrimitive(json, mapping): | |
567 jsonrpc_type = json["type"] | |
568 if jsonrpc_type == "boolean": | |
569 mapping["param_type"] = "bool" | |
570 mapping["Type"] = "Boolean" | |
571 mapping["init"] = " = false" | |
572 elif jsonrpc_type == "integer": | |
573 mapping["param_type"] = "int" | |
574 mapping["Type"] = "Integer" | |
575 mapping["init"] = " = 0" | |
576 elif jsonrpc_type == "number": | |
577 mapping["param_type"] = "double" | |
578 mapping["Type"] = "Double" | |
579 mapping["init"] = " = 0.0" | |
580 elif jsonrpc_type == "string": | |
581 mapping["param_type"] = "std::string" | |
582 mapping["pass_type"] = "const std::string&" | |
583 mapping["Type"] = "String" | |
584 if "enum" in json: | |
585 values = [] | |
586 value_defs = [] | |
587 mapping["command_underscored"] = Uncamelcase(mapping["command"]) | |
588 mapping["Param"] = Capitalize(mapping["proto_param"]) | |
589 for enum_value in json["enum"]: | |
590 values.append(tmpl_enum_value.substitute(mapping, | |
591 Value = Capitalize(enum_value))) | |
592 value_defs.append(tmpl_enum_value_def.substitute(mapping, | |
593 value = enum_value, | |
594 Value = Capitalize(enum_value))) | |
595 type_decls.append(tmpl_enum.substitute(mapping, | |
596 values = "".join(values))) | |
597 type_impls.append(tmpl_enum.substitute(mapping, | |
598 values = "".join(value_defs))) | |
599 else: | |
600 raise Exception("Unknown type: %s" % json_type) | |
601 | |
602 | |
603 def ResolveType(json, mapping): | |
604 mapping["init"] = "" | |
605 if "$ref" in json: | |
606 ResolveRef(json, mapping) | |
607 elif "type" in json: | |
608 jsonrpc_type = json["type"] | |
609 if jsonrpc_type == "array": | |
610 ResolveArray(json, mapping) | |
611 elif jsonrpc_type == "object": | |
612 ResolveObject(json, mapping) | |
613 else: | |
614 ResolvePrimitive(json, mapping) | |
615 else: | |
616 raise Exception("Unknown type at %s.%s %s" % | |
617 (mapping["Domain"], mapping["command"], mapping["proto_param"])) | |
618 if not ("wrap" in mapping): | |
dgozman
2014/09/12 11:48:45
I think these default values for "wrap", "append",
vkuzkokov
2014/09/12 14:22:52
Done.
| |
619 mapping["wrap"] = tmpl_wrap.substitute(mapping) | |
620 if not ("append" in mapping): | |
621 mapping["append"] = tmpl_append.substitute(mapping) | |
622 if not ("prep_req" in mapping): | |
623 mapping["prep_req"] = tmpl_prep_req.substitute(mapping) | |
624 if not ("pass_type" in mapping): | |
625 mapping["pass_type"] = mapping["param_type"] | |
626 | |
627 handlers = [] | |
628 frontends = [] | |
629 getters = [] | |
630 friends = [] | |
631 callbacks = [] | |
632 fields = [] | |
633 | |
634 includes = [] | |
635 initializations = [] | |
636 client_detached = [] | |
637 set_rvh = [] | |
638 getter_impls = [] | |
639 callback_impls = [] | |
640 frontend_impls = [] | |
641 | |
642 for json_domain in json_api["domains"]: | |
643 domain_map = {} | |
644 domain_map["Domain"] = json_domain["domain"] | |
645 domain_map["domain"] = Decapitalize(json_domain["domain"]) | |
646 | |
647 events = [] | |
648 event_impls = [] | |
649 domain_has_commands = False | |
650 domain_has_events = False | |
651 | |
652 if "commands" in json_domain: | |
653 for json_command in json_domain["commands"]: | |
654 if (not ("handlers" in json_command) or | |
655 not ("browser" in json_command["handlers"])): | |
656 continue | |
657 domain_has_commands = True | |
658 | |
659 command_map = domain_map.copy() | |
660 command_map["command"] = json_command["name"] | |
661 command_map["Command"] = Capitalize(json_command["name"]) | |
662 | |
663 prep = [] | |
664 args = [] | |
665 | |
666 if "parameters" in json_command: | |
667 for json_param in json_command["parameters"]: | |
668 param_map = command_map.copy() | |
669 param_map["proto_param"] = json_param["name"] | |
670 param_map["param"] = Uncamelcase(json_param["name"]) | |
671 param_map["var_name"] = "in_%s" % param_map["param"] | |
672 | |
673 ResolveType(json_param, param_map) | |
674 if len(prep) == 0: | |
675 prep.append(params_prep) | |
676 if json_param.get("optional"): | |
677 if param_map["Type"] in ["List", "Dictionary"]: | |
678 # TODO(vkuzkokov) Implement transformation of base::ListValue | |
679 # to std::vector and base::DictonaryValue to struct. | |
680 raise Exception( | |
681 "Optional array and object parameters are not implemented") | |
682 prep.append(tmpl_prep_opt.substitute(param_map)) | |
683 args.append(tmpl_arg_opt.substitute(param_map)) | |
684 else: | |
685 prep.append(param_map["prep_req"]) | |
686 args.append(tmpl_arg_req.substitute(param_map)) | |
687 | |
688 if json_command.get("async"): | |
689 json_returns = [] | |
690 if "returns" in json_command: | |
691 json_returns = json_command["returns"] | |
692 command_map["declared_name"] = "%sResponse" % command_map["Command"] | |
693 DeclareStruct(json_returns, command_map) | |
694 # TODO(vkuzkokov) Pass async callback instance similar to how | |
695 # InspectorBackendDispatcher does it. This, however, will work | |
696 # only if Blink and Chrome are in the same repo. | |
697 args.append("command") | |
698 callback_impls.append(tmpl_callback_async_impl.substitute(command_map, | |
699 prep = "".join(prep), | |
700 args = ", ".join(args))) | |
701 else: | |
702 wrap = [] | |
703 if "returns" in json_command: | |
704 for json_param in json_command["returns"]: | |
705 param_map = command_map.copy() | |
706 param_map["proto_param"] = json_param["name"] | |
707 param_map["param"] = Uncamelcase(json_param["name"]) | |
708 param_map["var_name"] = "out_%s" % param_map["param"] | |
709 | |
710 if json_param.get("optional"): | |
711 # TODO(vkuzkokov) Implement Optional<T> for value types | |
712 raise Exception("Optional return values are not implemented") | |
713 ResolveType(json_param, param_map) | |
714 prep.append(tmpl_prep_output.substitute(param_map)) | |
715 args.append(tmpl_arg_output.substitute(param_map)) | |
716 wrap.append(param_map["wrap"]) | |
717 | |
718 callback_impls.append(tmpl_callback_impl.substitute(command_map, | |
719 prep = "".join(prep), | |
720 args = ", ".join(args), | |
721 wrap = "".join(wrap))) | |
722 | |
723 initializations.append(tmpl_register.substitute(command_map)) | |
724 callbacks.append(tmpl_callback.substitute(command_map)) | |
725 | |
726 if "events" in json_domain: | |
727 for json_event in json_domain["events"]: | |
728 if (not ("handlers" in json_event) or | |
729 not ("browser" in json_event["handlers"])): | |
730 continue | |
731 domain_has_events = True | |
732 | |
733 event_map = domain_map.copy() | |
734 event_map["command"] = json_event["name"] | |
735 event_map["Command"] = Capitalize(json_event["name"]) | |
736 | |
737 json_parameters = [] | |
738 if "parameters" in json_event: | |
739 json_parameters = json_event["parameters"] | |
740 event_map["declared_name"] = "%sParams" % event_map["Command"] | |
741 DeclareStruct(json_parameters, event_map); | |
742 | |
743 events.append(tmpl_event.substitute(event_map)) | |
744 event_impls.append(tmpl_event_impl.substitute(event_map)) | |
745 | |
746 if not (domain_has_commands or domain_has_events): | |
747 continue | |
748 handlers.append(tmpl_handler.substitute(domain_map)) | |
749 getters.append(tmpl_getter.substitute(domain_map)) | |
750 fields.append(tmpl_field.substitute(domain_map)) | |
751 includes.append(tmpl_include.substitute(domain_map)) | |
752 initializations.append(tmpl_init_handler.substitute(domain_map)) | |
753 client_detached.append(tmpl_client_detached.substitute(domain_map)) | |
754 set_rvh.append(tmpl_set_rvh.substitute(domain_map)) | |
755 getter_impls.append(tmpl_getter_impl.substitute(domain_map)) | |
756 if domain_has_events: | |
757 frontends.append(tmpl_frontend.substitute(domain_map, | |
758 events = "".join(events))) | |
759 friends.append(tmpl_friend.substitute(domain_map)) | |
760 initializations.append(tmpl_init_frontend.substitute(domain_map)) | |
761 frontend_impls.append(tmpl_frontend_impl.substitute(domain_map, | |
762 events = "\n".join(event_impls))) | |
763 | |
764 output_h_file = open(output_h_path, "w") | |
765 output_cc_file = open(output_cc_path, "w") | |
766 | |
767 output_h_file.write(template_h.substitute({}, | |
768 types = "\n".join(type_decls), | |
769 handlers = "\n".join(handlers), | |
770 getters = "".join(getters), | |
771 friends = "".join(friends), | |
772 callbacks = "".join(callbacks), | |
773 to_value = "".join(to_value), | |
774 fields = "".join(fields), | |
775 frontends = "\n".join(frontends))) | |
776 | |
777 output_cc_file.write(template_cc.substitute({}, | |
778 includes = "".join(includes), | |
779 initializations = "".join(initializations), | |
780 client_detached = "".join(client_detached), | |
781 set_rvh = "".join(set_rvh), | |
782 getters = "\n".join(getter_impls), | |
783 callbacks = "\n".join(callback_impls), | |
784 to_value = "\n".join(to_value_impls), | |
785 types = "\n".join(type_impls), | |
786 frontends = "\n".join(frontend_impls))) | |
OLD | NEW |