OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 # | |
3 # Copyright 2017 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 # This script reads the global interface data collected by | |
8 # compute_interfaces_info_overall.py, and writes out the code which adds | |
9 # bindings for origin-trial-enabled features at runtime. | |
10 | |
11 # pylint: disable=W0403 | |
12 | |
13 import optparse | |
14 import os | |
15 import posixpath | |
16 import sys | |
17 from collections import defaultdict | |
18 | |
19 from code_generator import CodeGeneratorBase | |
20 from idl_reader import IdlReader | |
21 from utilities import create_component_info_provider, write_file, read_pickle_fi le, idl_filename_to_component | |
22 from v8_utilities import v8_class_name, v8_class_name_or_partial, uncapitalize | |
23 | |
24 # Make sure extension is .py, not .pyc or .pyo, so doesn't depend on caching | |
25 MODULE_PYNAME = os.path.splitext(os.path.basename(__file__))[0] + '.py' | |
26 | |
27 | |
28 def get_install_functions(interfaces, feature_names): | |
29 install_functions = [] | |
30 for interface_info in interfaces: | |
31 for feature_name in feature_names: | |
32 install_function = { | |
33 "condition": 'OriginTrials::%sEnabled' % uncapitalize(feature_na me), | |
34 "name": feature_name, | |
35 "install_method": "install%s" % feature_name, | |
36 "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.
| |
37 "class_or_partial": interface_info[2] | |
38 } | |
39 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.
| |
40 return install_functions | |
41 | |
42 | |
43 def get_conditional_feature_names_from_interface(interface): | |
44 feature_names = set() | |
45 if 'OriginTrialEnabled' in interface.extended_attributes and interface.is_pa rtial: | |
46 feature_names.add(interface.extended_attributes['OriginTrialEnabled']) | |
47 for operation in interface.operations: | |
48 if 'OriginTrialEnabled' in operation.extended_attributes: | |
49 feature_names.add(operation.extended_attributes['OriginTrialEnabled' ]) | |
50 for attribute in interface.attributes: | |
51 if 'OriginTrialEnabled' in attribute.extended_attributes: | |
52 feature_names.add(attribute.extended_attributes['OriginTrialEnabled' ]) | |
53 return list(feature_names) | |
54 | |
55 | |
56 class ConditionalFeaturesCodeGenerator(CodeGeneratorBase): | |
57 def __init__(self, generator_name, info_provider, global_objects, cache_dir, output_dir): | |
58 super(ConditionalFeaturesCodeGenerator, self).__init__(generator_name, i nfo_provider, cache_dir, output_dir) | |
59 self.global_objects = global_objects | |
60 | |
61 def generate_code(self, reader, idl_filenames, header_template, cpp_template , target_component): | |
62 context = {} | |
63 context['header_includes'] = [] | |
64 | |
65 includes = set([ | |
66 "core/context_features/ContextFeatureSettings.h", | |
67 "core/dom/ExecutionContext.h", | |
68 "core/frame/Frame.h", | |
69 "core/origin_trials/OriginTrials.h", | |
70 "platform/bindings/ConditionalFeatures.h", | |
71 "platform/bindings/ScriptState.h", | |
72 ]) | |
73 # TODO(iclelland): Remove the need to explicitly include this; it is | |
74 # here because the ContextFeatureSettings code needs it. | |
75 includes.add("bindings/core/v8/V8Window.h") | |
76 | |
77 features_for_type = defaultdict(set) | |
78 types_for_feature = defaultdict(set) | |
79 | |
80 for idl_filename in idl_filenames: | |
81 interfaces_read = reader.read_idl_file(idl_filename).interfaces | |
82 assert len(interfaces_read) == 1 | |
83 name, interface_read = interfaces_read.items()[0] | |
84 feature_names = get_conditional_feature_names_from_interface(interfa ce_read) | |
85 if feature_names: | |
86 if interface_read.is_partial: | |
87 # For partial interfaces, we need to generate different | |
88 # includes if the parent interface is in a different | |
89 # component. | |
90 parent_component = idl_filename_to_component( | |
91 self.info_provider.interfaces_info[name].get('full_path' )) | |
92 if interface_read.is_partial and target_component != parent_comp onent: | |
93 includes.add('bindings/%s/v8/V8%s.h' % (parent_component, na me)) | |
94 includes.add('bindings/%s/v8/V8%sPartial.h' % (target_compon ent, name)) | |
95 else: | |
96 includes.add('bindings/%s/v8/V8%s.h' % (target_component, na me)) | |
97 # If this is a partial interface in the same component as | |
98 # its parent, then treat it as a non-partial interface. | |
99 interface_read.is_partial = False | |
100 interface_info = (name, v8_class_name(interface_read), v8_class_ name_or_partial(interface_read)) | |
101 for feature_name in feature_names: | |
102 features_for_type[interface_info].add(feature_name) | |
103 types_for_feature[feature_name].add(interface_info) | |
104 | |
105 context['installers_by_interface'] = [ | |
106 {"name": interface_info[0], | |
107 "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
| |
108 "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.
| |
109 "installers": get_install_functions([interface_info], feature_names )} | |
110 for interface_info, feature_names in features_for_type.items()] | |
111 context['installers_by_interface'].sort(key=lambda x: x['name']) | |
112 | |
113 context['installers_by_feature'] = [ | |
114 {"name": feature_name, | |
115 "installers": get_install_functions(interfaces, [feature_name])} | |
116 for feature_name, interfaces in types_for_feature.items()] | |
117 context['installers_by_feature'].sort(key=lambda x: x['name']) | |
118 | |
119 context['includes'] = sorted(includes) | |
120 return self.render_template( | |
121 [], | |
122 self.jinja_env.get_template(header_template), | |
123 self.jinja_env.get_template(cpp_template), | |
124 context, target_component) | |
125 | |
126 | |
127 def parse_options(): | |
128 parser = optparse.OptionParser() | |
129 parser.add_option('--cache-directory', | |
130 help='cache directory, defaults to output directory') | |
131 parser.add_option('--output-directory') | |
132 parser.add_option('--info-dir') | |
133 parser.add_option('--target-component', | |
134 type='choice', | |
135 choices=['Core', 'Modules'], | |
136 help='target component to generate code') | |
137 parser.add_option('--idl-files-list') | |
138 parser.add_option('--global-objects-file') | |
139 | |
140 options, _ = parser.parse_args() | |
141 if options.output_directory is None: | |
142 parser.error('Must specify output directory using --output-directory.') | |
143 return options | |
144 | |
145 | |
146 def main(): | |
147 options = parse_options() | |
148 info_provider = create_component_info_provider( | |
149 os.path.normpath(options.info_dir), options.target_component.lower()) | |
150 | |
151 idl_filenames = map(str.strip, open(options.idl_files_list)) | |
152 reader = IdlReader(info_provider.interfaces_info, options.cache_directory) | |
153 cg = ConditionalFeaturesCodeGenerator(MODULE_PYNAME, info_provider, | |
154 read_pickle_file(options.global_object s_file), | |
155 options.cache_directory, | |
156 options.output_directory) | |
157 | |
158 header_text, cpp_text = cg.generate_code( | |
159 reader, idl_filenames, | |
160 "ConditionalFeaturesFor%s.h.tmpl" % options.target_component, | |
161 "ConditionalFeaturesFor%s.cpp.tmpl" % options.target_component, | |
162 options.target_component.lower()) | |
163 header_path = posixpath.join(options.output_directory, | |
164 "ConditionalFeaturesFor%s.h" % options.target_c omponent) | |
165 cpp_path = posixpath.join(options.output_directory, | |
166 "ConditionalFeaturesFor%s.cpp" % options.target_co mponent) | |
167 write_file(header_text, header_path) | |
168 write_file(cpp_text, cpp_path) | |
169 return 0 | |
170 | |
171 | |
172 if __name__ == '__main__': | |
173 sys.exit(main()) | |
OLD | NEW |