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