Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # pylint: disable=import-error,print-statement,relative-import | 5 # pylint: disable=import-error,print-statement,relative-import |
| 6 | 6 |
| 7 """Generates Blink Web Module bindings. | 7 """Generates Blink Web Module bindings. |
| 8 | 8 |
| 9 The Blink Web Module bindings provide a stable, IDL-generated interface for the | 9 The Blink Web Module bindings provide a stable, IDL-generated interface for the |
| 10 Web Modules. | 10 Web Modules. |
| 11 | 11 |
| 12 The Web Modules are the high-level services like Autofill, | 12 The Web Modules are the high-level services like Autofill, |
| 13 Autocomplete, Translate, Distiller, Phishing Detector, and others. Web Modules | 13 Autocomplete, Translate, Distiller, Phishing Detector, and others. Web Modules |
| 14 typically want to introspec the document and rendering infromation to implement | 14 typically want to introspec the document and rendering infromation to implement |
| 15 browser features. | 15 browser features. |
| 16 | 16 |
| 17 The bindings are meant to be as simple and as ephemeral as possible, mostly just | 17 The bindings are meant to be as simple and as ephemeral as possible, mostly just |
| 18 wrapping existing DOM classes. Their primary goal is to avoid leaking the actual | 18 wrapping existing DOM classes. Their primary goal is to avoid leaking the actual |
| 19 DOM classes to the Web Modules layer. | 19 DOM classes to the Web Modules layer. |
| 20 """ | 20 """ |
| 21 | 21 |
| 22 import os | 22 import os |
| 23 import posixpath | 23 import posixpath |
| 24 | 24 |
| 25 from code_generator import CodeGeneratorBase | 25 from code_generator import CodeGeneratorBase |
| 26 # TODO(dglazkov): Move TypedefResolver to code_generator.py | 26 # TODO(dglazkov): Move TypedefResolver to code_generator.py |
| 27 from code_generator_v8 import TypedefResolver | 27 from code_generator_v8 import TypedefResolver |
| 28 from v8_interface import interface_context | |
| 29 | 28 |
| 30 MODULE_PYNAME = os.path.splitext(os.path.basename(__file__))[0] + '.py' | 29 MODULE_PYNAME = os.path.splitext(os.path.basename(__file__))[0] + '.py' |
| 31 | 30 |
| 32 WEB_MODULE_IDL_ATTRIBUTE = 'WebModuleAPI' | 31 WEB_MODULE_IDL_ATTRIBUTE = 'WebModuleAPI' |
| 33 | 32 |
| 34 | 33 |
| 34 def includes_for_type(idl_type): | |
| 35 # TODO(dglazkov): Make this actually work. | |
| 36 name = idl_type.preprocessed_type.base_type | |
| 37 return set([name]) | |
| 38 | |
| 39 | |
| 40 def interface_context(idl_interface): | |
| 41 attributes = [] | |
| 42 methods = [] | |
| 43 includes = set() | |
| 44 for idl_attribute in idl_interface.attributes: | |
| 45 attributes.append(Attribute.create(idl_attribute)) | |
| 46 includes.update(includes_for_type(idl_attribute.idl_type)) | |
| 47 for idl_operation in idl_interface.operations: | |
| 48 if idl_operation.name: | |
| 49 methods.append(Method.create(idl_operation)) | |
| 50 return { | |
| 51 'code_generator': MODULE_PYNAME, | |
| 52 'class_name': idl_interface.name, | |
| 53 'cpp_includes': includes, | |
| 54 'attributes': attributes, | |
| 55 'methods': methods, | |
| 56 } | |
| 57 | |
| 58 | |
| 59 class Attribute(object): | |
| 60 def __init__(self, name, return_type): | |
| 61 self.name = name | |
| 62 self.return_type = return_type | |
| 63 | |
| 64 @staticmethod | |
| 65 def create(idl_attribute): | |
| 66 name = idl_attribute.name | |
| 67 return_type = idl_attribute.idl_type.preprocessed_type.base_type | |
| 68 return Attribute(name, return_type) | |
| 69 | |
| 70 | |
| 71 # TODO(dglazkov): Attribute and Method are both created off | |
| 72 # idl_definitions.TypedObject sub-types, so maybe use inheritance? | |
|
bashi
2016/11/13 23:33:58
I think it's a good idea to separate them from Typ
dglazkov
2016/11/19 22:14:33
SG. Will remove comment before landing.
| |
| 73 class Method(object): | |
| 74 def __init__(self, name, return_type): | |
| 75 self.name = name | |
| 76 self.return_type = return_type | |
| 77 | |
| 78 @staticmethod | |
| 79 def create(idl_operation): | |
| 80 name = idl_operation.name | |
| 81 return_type = idl_operation.idl_type.preprocessed_type.base_type | |
| 82 return Method(name, return_type) | |
| 83 | |
| 84 | |
| 35 class CodeGeneratorWebModule(CodeGeneratorBase): | 85 class CodeGeneratorWebModule(CodeGeneratorBase): |
| 36 def __init__(self, info_provider, cache_dir, output_dir): | 86 def __init__(self, info_provider, cache_dir, output_dir): |
| 37 CodeGeneratorBase.__init__(self, MODULE_PYNAME, info_provider, | 87 CodeGeneratorBase.__init__(self, MODULE_PYNAME, info_provider, |
| 38 cache_dir, output_dir) | 88 cache_dir, output_dir) |
| 39 self.typedef_resolver = TypedefResolver(info_provider) | 89 self.typedef_resolver = TypedefResolver(info_provider) |
| 40 | 90 |
| 41 def get_template(self): | 91 def get_template(self, file_extension): |
| 42 template_filename = 'web_module_interface.cpp.tmpl' | 92 template_filename = 'web_module_interface.%s.tmpl' % file_extension |
| 43 return self.jinja_env.get_template(template_filename) | 93 return self.jinja_env.get_template(template_filename) |
| 44 | 94 |
| 45 # TODO(dglazkov): Move to CodeGeneratorBase. | 95 # TODO(dglazkov): Move to CodeGeneratorBase. |
| 46 def output_paths(self, definition_name): | 96 def output_paths(self, definition_name): |
| 47 header_path = posixpath.join(self.output_dir, | 97 header_path = posixpath.join(self.output_dir, |
| 48 'Web%s.h' % definition_name) | 98 'Web%s.h' % definition_name) |
| 49 cpp_path = posixpath.join(self.output_dir, | 99 cpp_path = posixpath.join(self.output_dir, |
| 50 'Web%s.cpp' % definition_name) | 100 'Web%s.cpp' % definition_name) |
| 51 return header_path, cpp_path | 101 return header_path, cpp_path |
| 52 | 102 |
| 103 def generate_interface_code(self, interface): | |
| 104 # TODO(dglazkov): Implement callback interfaces. | |
| 105 # TODO(dglazkov): Make sure partial interfaces are handled. | |
| 106 if interface.is_callback or interface.is_partial: | |
| 107 raise ValueError("Partial or callback interfaces are not supported") | |
| 108 | |
| 109 template_context = interface_context(interface) | |
| 110 | |
| 111 cpp_template = self.get_template('cpp') | |
| 112 header_template = self.get_template('h') | |
| 113 cpp_text = cpp_template.render(template_context) | |
| 114 header_text = header_template.render(template_context) | |
| 115 header_path, cpp_path = self.output_paths(interface.name) | |
| 116 | |
| 117 return ( | |
| 118 (header_path, header_text), | |
| 119 (cpp_path, cpp_text) | |
| 120 ) | |
| 121 | |
| 53 def generate_code(self, definitions, definition_name): | 122 def generate_code(self, definitions, definition_name): |
| 54 self.typedef_resolver.resolve(definitions, definition_name) | 123 self.typedef_resolver.resolve(definitions, definition_name) |
| 55 header_path, cpp_path = self.output_paths(definition_name) | 124 header_path, cpp_path = self.output_paths(definition_name) |
| 56 | 125 |
| 57 template_context = {} | 126 template_context = {} |
| 58 # TODO(dglazkov): Implement dictionaries | 127 # TODO(dglazkov): Implement dictionaries |
| 59 if definition_name not in definitions.interfaces: | 128 if definition_name not in definitions.interfaces: |
| 60 return None | 129 return None |
| 61 | 130 |
| 62 interface = definitions.interfaces[definition_name] | 131 interface = definitions.interfaces[definition_name] |
| 63 if WEB_MODULE_IDL_ATTRIBUTE not in interface.extended_attributes: | 132 if WEB_MODULE_IDL_ATTRIBUTE not in interface.extended_attributes: |
| 64 return None | 133 return None |
| 65 | 134 |
| 66 # TODO(dglazkov): Implement callback interfaces. | 135 return self.generate_interface_code(interface) |
| 67 # TODO(dglazkov): Implement partial interfaces. | |
| 68 if interface.is_callback and interface.is_partial: | |
| 69 return None | |
| 70 | |
| 71 template_context = interface_context(interface, | |
| 72 definitions.interfaces) | |
| 73 template_context['code_generator'] = MODULE_PYNAME | |
| 74 template_context['class_name'] = definition_name | |
| 75 | |
| 76 cpp_template = self.get_template() | |
| 77 cpp_text = cpp_template.render(template_context) | |
| 78 | |
| 79 header_text = 'header' | |
| 80 return ( | |
| 81 (header_path, header_text), | |
| 82 (cpp_path, cpp_text) | |
| 83 ) | |
| OLD | NEW |