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..ac97058e6fdb3a6e6b5f2f5f026b1095fb756a0a |
--- /dev/null |
+++ b/third_party/WebKit/Source/bindings/scripts/generate_conditional_features.py |
@@ -0,0 +1,174 @@ |
+#!/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 |
+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), |
chasej
2017/07/11 20:12:31
I'm wondering if it would be better to have the co
iclelland
2017/07/12 17:23:20
I was definitely thinking about other kinds of con
|
+ "name": feature_name, |
+ "install_method": "install%s" % feature_name, |
+ "class": interface_info[1], |
+ "class_or_partial": interface_info[2] |
+ } |
+ install_functions.append(install_function) |
+ 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: |
+ # HACK WARNING: This is here becase the ServiceWorker interface |
+ # is actually a complete interface in modules/, but the Origin |
+ # Trial declarations end up on |
+ # ServiceWorkerGlobalScopeModulesConstructors.idl, which is a |
+ # partial interface declaration. TODO: Fix this by correctly |
+ # determining whether the interface where the install* methods |
+ # are declared is partial or not. |
+ if 'ServiceWorkerGlobalScope' in idl_filename: |
+ interface_read.is_partial = False |
+ # END OF HACK |
+ if interface_read.is_partial: |
+ includes.add('bindings/core/v8/V8%s.h' % name) |
+ includes.add('bindings/%s/v8/V8%sPartial.h' % (target_component, name)) |
+ else: |
+ includes.add('bindings/%s/v8/V8%s.h' % (target_component, name)) |
+ 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, |
+ "class": interface_info[1], |
+ "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()) |