| 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 class Method(object): |
| 72 def __init__(self, name, return_type): |
| 73 self.name = name |
| 74 self.return_type = return_type |
| 75 |
| 76 @staticmethod |
| 77 def create(idl_operation): |
| 78 name = idl_operation.name |
| 79 return_type = idl_operation.idl_type.preprocessed_type.base_type |
| 80 return Method(name, return_type) |
| 81 |
| 82 |
| 35 class CodeGeneratorWebModule(CodeGeneratorBase): | 83 class CodeGeneratorWebModule(CodeGeneratorBase): |
| 36 def __init__(self, info_provider, cache_dir, output_dir): | 84 def __init__(self, info_provider, cache_dir, output_dir): |
| 37 CodeGeneratorBase.__init__(self, MODULE_PYNAME, info_provider, | 85 CodeGeneratorBase.__init__(self, MODULE_PYNAME, info_provider, |
| 38 cache_dir, output_dir) | 86 cache_dir, output_dir) |
| 39 self.typedef_resolver = TypedefResolver(info_provider) | 87 self.typedef_resolver = TypedefResolver(info_provider) |
| 40 | 88 |
| 41 def get_template(self): | 89 def get_template(self, file_extension): |
| 42 template_filename = 'web_module_interface.cpp.tmpl' | 90 template_filename = 'web_module_interface.%s.tmpl' % file_extension |
| 43 return self.jinja_env.get_template(template_filename) | 91 return self.jinja_env.get_template(template_filename) |
| 44 | 92 |
| 45 # TODO(dglazkov): Move to CodeGeneratorBase. | 93 # TODO(dglazkov): Move to CodeGeneratorBase. |
| 46 def output_paths(self, definition_name): | 94 def output_paths(self, definition_name): |
| 47 header_path = posixpath.join(self.output_dir, | 95 header_path = posixpath.join(self.output_dir, |
| 48 'Web%s.h' % definition_name) | 96 'Web%s.h' % definition_name) |
| 49 cpp_path = posixpath.join(self.output_dir, | 97 cpp_path = posixpath.join(self.output_dir, |
| 50 'Web%s.cpp' % definition_name) | 98 'Web%s.cpp' % definition_name) |
| 51 return header_path, cpp_path | 99 return header_path, cpp_path |
| 52 | 100 |
| 101 def generate_interface_code(self, interface): |
| 102 # TODO(dglazkov): Implement callback interfaces. |
| 103 # TODO(dglazkov): Make sure partial interfaces are handled. |
| 104 if interface.is_callback or interface.is_partial: |
| 105 raise ValueError("Partial or callback interfaces are not supported") |
| 106 |
| 107 template_context = interface_context(interface) |
| 108 |
| 109 cpp_template = self.get_template('cpp') |
| 110 header_template = self.get_template('h') |
| 111 cpp_text = cpp_template.render(template_context) |
| 112 header_text = header_template.render(template_context) |
| 113 header_path, cpp_path = self.output_paths(interface.name) |
| 114 |
| 115 return ( |
| 116 (header_path, header_text), |
| 117 (cpp_path, cpp_text) |
| 118 ) |
| 119 |
| 53 def generate_code(self, definitions, definition_name): | 120 def generate_code(self, definitions, definition_name): |
| 54 self.typedef_resolver.resolve(definitions, definition_name) | 121 self.typedef_resolver.resolve(definitions, definition_name) |
| 55 header_path, cpp_path = self.output_paths(definition_name) | 122 header_path, cpp_path = self.output_paths(definition_name) |
| 56 | 123 |
| 57 template_context = {} | 124 template_context = {} |
| 58 # TODO(dglazkov): Implement dictionaries | 125 # TODO(dglazkov): Implement dictionaries |
| 59 if definition_name not in definitions.interfaces: | 126 if definition_name not in definitions.interfaces: |
| 60 return None | 127 return None |
| 61 | 128 |
| 62 interface = definitions.interfaces[definition_name] | 129 interface = definitions.interfaces[definition_name] |
| 63 if WEB_MODULE_IDL_ATTRIBUTE not in interface.extended_attributes: | 130 if WEB_MODULE_IDL_ATTRIBUTE not in interface.extended_attributes: |
| 64 return None | 131 return None |
| 65 | 132 |
| 66 # TODO(dglazkov): Implement callback interfaces. | 133 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 |