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, render_template | 25 from code_generator import CodeGeneratorBase, render_template |
| 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 name_style_converter import NameStyleConverter | |
| 28 | 29 |
| 29 MODULE_PYNAME = os.path.splitext(os.path.basename(__file__))[0] + '.py' | 30 MODULE_PYNAME = os.path.splitext(os.path.basename(__file__))[0] + '.py' |
| 30 | 31 |
| 31 WEB_MODULE_IDL_ATTRIBUTE = 'WebModuleAPI' | 32 WEB_MODULE_IDL_ATTRIBUTE = 'WebModuleAPI' |
| 32 STRING_INCLUDE_PATH = 'wtf/text/WTFString.h' | 33 STRING_INCLUDE_PATH = 'wtf/text/WTFString.h' |
| 33 | 34 |
| 34 def interface_context(idl_interface): | 35 def interface_context(idl_interface): |
| 35 builder = InterfaceContextBuilder(MODULE_PYNAME, TypeResolver()) | 36 builder = InterfaceContextBuilder(MODULE_PYNAME, TypeResolver()) |
| 36 builder.set_class_name(idl_interface.name) | 37 builder.set_class_name(idl_interface.name) |
| 37 builder.set_inheritance(idl_interface.parent) | 38 builder.set_inheritance(idl_interface.parent) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 # type that can be used directly in the jinja2 template. | 75 # type that can be used directly in the jinja2 template. |
| 75 return idl_definition.idl_type.base_type | 76 return idl_definition.idl_type.base_type |
| 76 | 77 |
| 77 | 78 |
| 78 class InterfaceContextBuilder(object): | 79 class InterfaceContextBuilder(object): |
| 79 def __init__(self, code_generator, type_resolver): | 80 def __init__(self, code_generator, type_resolver): |
| 80 self.result = {'code_generator': code_generator} | 81 self.result = {'code_generator': code_generator} |
| 81 self.type_resolver = type_resolver | 82 self.type_resolver = type_resolver |
| 82 | 83 |
| 83 def set_class_name(self, class_name): | 84 def set_class_name(self, class_name): |
| 84 self.result['class_name'] = class_name | 85 converter = NameStyleConverter(class_name) |
| 86 self.result['class_name'] = converter.to_all_cases() | |
| 85 | 87 |
| 86 def set_inheritance(self, base_interface): | 88 def set_inheritance(self, base_interface): |
| 87 if base_interface is None: | 89 if base_interface is None: |
| 88 return | 90 return |
| 89 self.result['inherits_expression'] = ' : public %s' % base_interface | 91 self.result['inherits_expression'] = ' : public %s' % base_interface |
| 90 self._ensure_set('cpp_includes').update( | 92 self._ensure_set('cpp_includes').update( |
| 91 self.type_resolver.includes_from_interface(base_interface)) | 93 self.type_resolver.includes_from_interface(base_interface)) |
| 92 | 94 |
| 93 def _ensure_set(self, name): | 95 def _ensure_set(self, name): |
| 94 return self.result.setdefault(name, set()) | 96 return self.result.setdefault(name, set()) |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 class CodeGeneratorWebModule(CodeGeneratorBase): | 135 class CodeGeneratorWebModule(CodeGeneratorBase): |
| 134 def __init__(self, info_provider, cache_dir, output_dir): | 136 def __init__(self, info_provider, cache_dir, output_dir): |
| 135 CodeGeneratorBase.__init__(self, MODULE_PYNAME, info_provider, | 137 CodeGeneratorBase.__init__(self, MODULE_PYNAME, info_provider, |
| 136 cache_dir, output_dir) | 138 cache_dir, output_dir) |
| 137 self.typedef_resolver = TypedefResolver(info_provider) | 139 self.typedef_resolver = TypedefResolver(info_provider) |
| 138 | 140 |
| 139 def get_template(self, file_extension): | 141 def get_template(self, file_extension): |
| 140 template_filename = 'web_module_interface.%s.tmpl' % file_extension | 142 template_filename = 'web_module_interface.%s.tmpl' % file_extension |
| 141 return self.jinja_env.get_template(template_filename) | 143 return self.jinja_env.get_template(template_filename) |
| 142 | 144 |
| 143 # TODO(dglazkov): Move to CodeGeneratorBase. | 145 def generate_file(self, template_context, file_extension): |
| 144 def output_paths(self, definition_name): | 146 template = self.get_template(file_extension) |
| 145 header_path = posixpath.join(self.output_dir, | 147 path = posixpath.join( |
| 146 'Web%s.h' % definition_name) | 148 self.output_dir, |
| 147 cpp_path = posixpath.join(self.output_dir, | 149 '%s.%s' % (template_context['class_name']['hacker'], |
| 148 'Web%s.cpp' % definition_name) | 150 file_extension)) |
| 149 return header_path, cpp_path | 151 text = render_template(template, template_context) |
| 152 return (path, text) | |
| 150 | 153 |
| 151 def generate_interface_code(self, interface): | 154 def generate_interface_code(self, interface): |
| 152 # TODO(dglazkov): Implement callback interfaces. | 155 # TODO(dglazkov): Implement callback interfaces. |
| 153 # TODO(dglazkov): Make sure partial interfaces are handled. | 156 # TODO(dglazkov): Make sure partial interfaces are handled. |
| 154 if interface.is_callback or interface.is_partial: | 157 if interface.is_callback or interface.is_partial: |
| 155 raise ValueError('Partial or callback interfaces are not supported') | 158 raise ValueError('Partial or callback interfaces are not supported') |
| 156 | 159 |
| 157 template_context = interface_context(interface) | 160 template_context = interface_context(interface) |
| 158 | 161 |
| 159 cpp_template = self.get_template('cpp') | |
| 160 header_template = self.get_template('h') | |
| 161 cpp_text = render_template(cpp_template, template_context) | |
| 162 header_text = render_template(header_template, template_context) | |
| 163 header_path, cpp_path = self.output_paths(interface.name) | |
| 164 | |
| 165 return ( | 162 return ( |
| 166 (header_path, header_text), | 163 self.generate_file(template_context, 'h'), |
| 167 (cpp_path, cpp_text) | 164 self.generate_file(template_context, 'cc') |
|
haraken
2017/02/09 01:00:11
Is it intentional that you use .cc instead of .cpp
dglazkov
2017/02/09 04:15:28
Yep, trying to match the Chromium style for these
| |
| 168 ) | 165 ) |
| 169 | 166 |
| 170 def generate_code(self, definitions, definition_name): | 167 def generate_code(self, definitions, definition_name): |
| 171 self.typedef_resolver.resolve(definitions, definition_name) | 168 self.typedef_resolver.resolve(definitions, definition_name) |
| 172 header_path, cpp_path = self.output_paths(definition_name) | |
| 173 | 169 |
| 174 template_context = {} | |
| 175 # TODO(dglazkov): Implement dictionaries | 170 # TODO(dglazkov): Implement dictionaries |
| 176 if definition_name not in definitions.interfaces: | 171 if definition_name not in definitions.interfaces: |
| 177 return None | 172 return None |
| 178 | 173 |
| 179 interface = definitions.interfaces[definition_name] | 174 interface = definitions.interfaces[definition_name] |
| 180 if WEB_MODULE_IDL_ATTRIBUTE not in interface.extended_attributes: | 175 if WEB_MODULE_IDL_ATTRIBUTE not in interface.extended_attributes: |
| 181 return None | 176 return None |
| 182 | 177 |
| 183 return self.generate_interface_code(interface) | 178 return self.generate_interface_code(interface) |
| OLD | NEW |