Chromium Code Reviews| Index: third_party/WebKit/Source/bindings/scripts/generate_conditional_features.py |
| diff --git a/third_party/WebKit/Source/bindings/scripts/generate_conditional_features.py b/third_party/WebKit/Source/bindings/scripts/generate_conditional_features.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..1f28ca4d876bb38094236e9ed7145bd0bbec54d7 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/bindings/scripts/generate_conditional_features.py |
| @@ -0,0 +1,173 @@ |
| +#!/usr/bin/python |
| +# |
| +# Copyright 2017 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +# This script reads the global interface data collected by |
| +# compute_interfaces_info_overall.py, and writes out the code which adds |
| +# bindings for origin-trial-enabled features at runtime. |
| + |
| +# pylint: disable=W0403 |
| + |
| +import optparse |
| +import os |
| +import posixpath |
| +import sys |
| +from collections import defaultdict |
| + |
| +from code_generator import CodeGeneratorBase |
| +from idl_reader import IdlReader |
| +from utilities import create_component_info_provider, write_file, read_pickle_file, idl_filename_to_component |
| +from v8_utilities import v8_class_name, v8_class_name_or_partial, uncapitalize |
| + |
| +# Make sure extension is .py, not .pyc or .pyo, so doesn't depend on caching |
| +MODULE_PYNAME = os.path.splitext(os.path.basename(__file__))[0] + '.py' |
| + |
| + |
| +def get_install_functions(interfaces, feature_names): |
| + install_functions = [] |
| + for interface_info in interfaces: |
| + for feature_name in feature_names: |
| + install_function = { |
| + "condition": 'OriginTrials::%sEnabled' % uncapitalize(feature_name), |
| + "name": feature_name, |
| + "install_method": "install%s" % feature_name, |
| + "class": interface_info[1], |
|
Yuki
2017/07/14 03:30:07
s/class/v8_class/
v8_interface.py name them "v8_c
iclelland
2017/07/14 18:25:05
Done.
|
| + "class_or_partial": interface_info[2] |
| + } |
| + install_functions.append(install_function) |
|
Yuki
2017/07/14 03:30:07
nit: You can use the list-comprehension style, and
iclelland
2017/07/14 18:25:05
Done.
|
| + return install_functions |
| + |
| + |
| +def get_conditional_feature_names_from_interface(interface): |
| + feature_names = set() |
| + if 'OriginTrialEnabled' in interface.extended_attributes and interface.is_partial: |
| + feature_names.add(interface.extended_attributes['OriginTrialEnabled']) |
| + for operation in interface.operations: |
| + if 'OriginTrialEnabled' in operation.extended_attributes: |
| + feature_names.add(operation.extended_attributes['OriginTrialEnabled']) |
| + for attribute in interface.attributes: |
| + if 'OriginTrialEnabled' in attribute.extended_attributes: |
| + feature_names.add(attribute.extended_attributes['OriginTrialEnabled']) |
| + return list(feature_names) |
| + |
| + |
| +class ConditionalFeaturesCodeGenerator(CodeGeneratorBase): |
| + def __init__(self, generator_name, info_provider, global_objects, cache_dir, output_dir): |
| + super(ConditionalFeaturesCodeGenerator, self).__init__(generator_name, info_provider, cache_dir, output_dir) |
| + self.global_objects = global_objects |
| + |
| + def generate_code(self, reader, idl_filenames, header_template, cpp_template, target_component): |
| + context = {} |
| + context['header_includes'] = [] |
| + |
| + includes = set([ |
| + "core/context_features/ContextFeatureSettings.h", |
| + "core/dom/ExecutionContext.h", |
| + "core/frame/Frame.h", |
| + "core/origin_trials/OriginTrials.h", |
| + "platform/bindings/ConditionalFeatures.h", |
| + "platform/bindings/ScriptState.h", |
| + ]) |
| + # TODO(iclelland): Remove the need to explicitly include this; it is |
| + # here because the ContextFeatureSettings code needs it. |
| + includes.add("bindings/core/v8/V8Window.h") |
| + |
| + features_for_type = defaultdict(set) |
| + types_for_feature = defaultdict(set) |
| + |
| + for idl_filename in idl_filenames: |
| + interfaces_read = reader.read_idl_file(idl_filename).interfaces |
| + assert len(interfaces_read) == 1 |
| + name, interface_read = interfaces_read.items()[0] |
| + feature_names = get_conditional_feature_names_from_interface(interface_read) |
| + if feature_names: |
| + if interface_read.is_partial: |
| + # For partial interfaces, we need to generate different |
| + # includes if the parent interface is in a different |
| + # component. |
| + parent_component = idl_filename_to_component( |
| + self.info_provider.interfaces_info[name].get('full_path')) |
| + if interface_read.is_partial and target_component != parent_component: |
| + includes.add('bindings/%s/v8/V8%s.h' % (parent_component, name)) |
| + includes.add('bindings/%s/v8/V8%sPartial.h' % (target_component, name)) |
| + else: |
| + includes.add('bindings/%s/v8/V8%s.h' % (target_component, name)) |
| + # If this is a partial interface in the same component as |
| + # its parent, then treat it as a non-partial interface. |
| + interface_read.is_partial = False |
| + interface_info = (name, v8_class_name(interface_read), v8_class_name_or_partial(interface_read)) |
| + for feature_name in feature_names: |
| + features_for_type[interface_info].add(feature_name) |
| + types_for_feature[feature_name].add(interface_info) |
| + |
| + context['installers_by_interface'] = [ |
| + {"name": interface_info[0], |
| + "is_global": interface_info[0] in self.global_objects, |
|
Yuki
2017/07/14 03:30:07
You can tell if an interface is global or not by l
iclelland
2017/07/14 18:25:05
The trouble here is that in many cases, the origin
|
| + "class": interface_info[1], |
|
Yuki
2017/07/14 03:30:07
nit: s/class/v8_class/
We have two kind of class
iclelland
2017/07/14 18:25:05
Fair point. Done.
|
| + "installers": get_install_functions([interface_info], feature_names)} |
| + for interface_info, feature_names in features_for_type.items()] |
| + context['installers_by_interface'].sort(key=lambda x: x['name']) |
| + |
| + context['installers_by_feature'] = [ |
| + {"name": feature_name, |
| + "installers": get_install_functions(interfaces, [feature_name])} |
| + for feature_name, interfaces in types_for_feature.items()] |
| + context['installers_by_feature'].sort(key=lambda x: x['name']) |
| + |
| + context['includes'] = sorted(includes) |
| + return self.render_template( |
| + [], |
| + self.jinja_env.get_template(header_template), |
| + self.jinja_env.get_template(cpp_template), |
| + context, target_component) |
| + |
| + |
| +def parse_options(): |
| + parser = optparse.OptionParser() |
| + parser.add_option('--cache-directory', |
| + help='cache directory, defaults to output directory') |
| + parser.add_option('--output-directory') |
| + parser.add_option('--info-dir') |
| + parser.add_option('--target-component', |
| + type='choice', |
| + choices=['Core', 'Modules'], |
| + help='target component to generate code') |
| + parser.add_option('--idl-files-list') |
| + parser.add_option('--global-objects-file') |
| + |
| + options, _ = parser.parse_args() |
| + if options.output_directory is None: |
| + parser.error('Must specify output directory using --output-directory.') |
| + return options |
| + |
| + |
| +def main(): |
| + options = parse_options() |
| + info_provider = create_component_info_provider( |
| + os.path.normpath(options.info_dir), options.target_component.lower()) |
| + |
| + idl_filenames = map(str.strip, open(options.idl_files_list)) |
| + reader = IdlReader(info_provider.interfaces_info, options.cache_directory) |
| + cg = ConditionalFeaturesCodeGenerator(MODULE_PYNAME, info_provider, |
| + read_pickle_file(options.global_objects_file), |
| + options.cache_directory, |
| + options.output_directory) |
| + |
| + header_text, cpp_text = cg.generate_code( |
| + reader, idl_filenames, |
| + "ConditionalFeaturesFor%s.h.tmpl" % options.target_component, |
| + "ConditionalFeaturesFor%s.cpp.tmpl" % options.target_component, |
| + options.target_component.lower()) |
| + header_path = posixpath.join(options.output_directory, |
| + "ConditionalFeaturesFor%s.h" % options.target_component) |
| + cpp_path = posixpath.join(options.output_directory, |
| + "ConditionalFeaturesFor%s.cpp" % options.target_component) |
| + write_file(header_text, header_path) |
| + write_file(cpp_text, cpp_path) |
| + return 0 |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main()) |