Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(731)

Side by Side Diff: extensions/renderer/resources/binding.js

Issue 1864353002: [Extensions] Make Binding.generate() load the schema directly (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 var Event = require('event_bindings').Event; 5 var Event = require('event_bindings').Event;
6 var forEach = require('utils').forEach; 6 var forEach = require('utils').forEach;
7 var GetAvailability = requireNative('v8_context').GetAvailability; 7 var GetAvailability = requireNative('v8_context').GetAvailability;
8 var exceptionHandler = require('uncaught_exception_handler'); 8 var exceptionHandler = require('uncaught_exception_handler');
9 var lastError = require('lastError'); 9 var lastError = require('lastError');
10 var logActivity = requireNative('activityLogger'); 10 var logActivity = requireNative('activityLogger');
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 type.id + '.'); 169 type.id + '.');
170 var customType = jsModule[jsModuleName]; 170 var customType = jsModule[jsModuleName];
171 logging.CHECK(customType, jsModuleName + ' must export itself.'); 171 logging.CHECK(customType, jsModuleName + ' must export itself.');
172 customType.prototype = new CustomBindingsObject(); 172 customType.prototype = new CustomBindingsObject();
173 customType.prototype.setSchema(type); 173 customType.prototype.setSchema(type);
174 return customType; 174 return customType;
175 } 175 }
176 176
177 var platform = getPlatform(); 177 var platform = getPlatform();
178 178
179 function Binding(schema) { 179 function Binding(apiName) {
180 this.schema_ = schema; 180 this.apiName_ = apiName;
181 this.apiFunctions_ = new APIFunctions(schema.namespace); 181 this.apiFunctions_ = new APIFunctions(apiName);
182 this.customEvent_ = null; 182 this.customEvent_ = null;
183 this.customHooks_ = []; 183 this.customHooks_ = [];
184 }; 184 };
185 185
186 Binding.create = function(apiName) { 186 Binding.create = function(apiName) {
187 return new Binding(schemaRegistry.GetSchema(apiName)); 187 return new Binding(apiName);
188 }; 188 };
189 189
190 Binding.prototype = { 190 Binding.prototype = {
191 // The API through which the ${api_name}_custom_bindings.js files customize 191 // The API through which the ${api_name}_custom_bindings.js files customize
192 // their API bindings beyond what can be generated. 192 // their API bindings beyond what can be generated.
193 // 193 //
194 // There are 2 types of customizations available: those which are required in 194 // There are 2 types of customizations available: those which are required in
195 // order to do the schema generation (registerCustomEvent and 195 // order to do the schema generation (registerCustomEvent and
196 // registerCustomType), and those which can only run after the bindings have 196 // registerCustomType), and those which can only run after the bindings have
197 // been generated (registerCustomHook). 197 // been generated (registerCustomHook).
198 198
199 // Registers a custom event type for the API identified by |namespace|. 199 // Registers a custom event type for the API identified by |namespace|.
200 // |event| is the event's constructor. 200 // |event| is the event's constructor.
201 registerCustomEvent: function(event) { 201 registerCustomEvent: function(event) {
202 this.customEvent_ = event; 202 this.customEvent_ = event;
203 }, 203 },
204 204
205 // Registers a function |hook| to run after the schema for all APIs has been 205 // Registers a function |hook| to run after the schema for all APIs has been
206 // generated. The hook is passed as its first argument an "API" object to 206 // generated. The hook is passed as its first argument an "API" object to
207 // interact with, and second the current extension ID. See where 207 // interact with, and second the current extension ID. See where
208 // |customHooks| is used. 208 // |customHooks| is used.
209 registerCustomHook: function(fn) { 209 registerCustomHook: function(fn) {
210 $Array.push(this.customHooks_, fn); 210 $Array.push(this.customHooks_, fn);
211 }, 211 },
212 212
213 // TODO(kalman/cduvall): Refactor this so |runHooks_| is not needed. 213 // TODO(kalman/cduvall): Refactor this so |runHooks_| is not needed.
214 runHooks_: function(api) { 214 runHooks_: function(api, schema) {
215 $Array.forEach(this.customHooks_, function(hook) { 215 $Array.forEach(this.customHooks_, function(hook) {
216 if (!isSchemaNodeSupported(this.schema_, platform, manifestVersion)) 216 if (!isSchemaNodeSupported(schema, platform, manifestVersion))
217 return; 217 return;
218 218
219 if (!hook) 219 if (!hook)
220 return; 220 return;
221 221
222 hook({ 222 hook({
223 apiFunctions: this.apiFunctions_, 223 apiFunctions: this.apiFunctions_,
224 schema: this.schema_, 224 schema: schema,
225 compiledApi: api 225 compiledApi: api
226 }, extensionId, contextType); 226 }, extensionId, contextType);
227 }, this); 227 }, this);
228 }, 228 },
229 229
230 // Generates the bindings from |this.schema_| and integrates any custom 230 // Generates the bindings from the schema for |this.apiName_| and integrates
231 // bindings that might be present. 231 // any custom bindings that might be present.
232 generate: function() { 232 generate: function() {
233 var schema = this.schema_; 233 // NB: It's important to load the schema during generation rather than
234 // setting it beforehand so that we're more confident the schema we're
235 // loading is real, and not one that was injected by a page intercepting
236 // Binding.generate.
237 // Additionally, since the schema is an object returned from a native
238 // handler, it's properties don't have the custom getters/setters that
asargent_no_longer_on_chrome 2016/04/11 18:13:44 grammar nazi: this should be "its" instead of "it'
Devlin 2016/04/11 19:10:03 whoops, fixed.
239 // a page may have put on Object.prototype.
240 var schema = schemaRegistry.GetSchema(this.apiName_);
234 241
235 function shouldCheckUnprivileged() { 242 function shouldCheckUnprivileged() {
236 var shouldCheck = 'unprivileged' in schema; 243 var shouldCheck = 'unprivileged' in schema;
237 if (shouldCheck) 244 if (shouldCheck)
238 return shouldCheck; 245 return shouldCheck;
239 246
240 $Array.forEach(['functions', 'events'], function(type) { 247 $Array.forEach(['functions', 'events'], function(type) {
241 if ($Object.hasOwnProperty(schema, type)) { 248 if ($Object.hasOwnProperty(schema, type)) {
242 $Array.forEach(schema[type], function(node) { 249 $Array.forEach(schema[type], function(node) {
243 if ('unprivileged' in node) 250 if ('unprivileged' in node)
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 if (!success) { 519 if (!success) {
513 var availability = GetAvailability(schema.namespace); 520 var availability = GetAvailability(schema.namespace);
514 // If an API was available it should have been successfully generated. 521 // If an API was available it should have been successfully generated.
515 logging.DCHECK(!availability.is_available, 522 logging.DCHECK(!availability.is_available,
516 schema.namespace + ' was available but not generated'); 523 schema.namespace + ' was available but not generated');
517 console.error('chrome.' + schema.namespace + ' is not available: ' + 524 console.error('chrome.' + schema.namespace + ' is not available: ' +
518 availability.message); 525 availability.message);
519 return; 526 return;
520 } 527 }
521 528
522 this.runHooks_(mod); 529 this.runHooks_(mod, schema);
523 return mod; 530 return mod;
524 } 531 }
525 }; 532 };
526 533
527 exports.$set('Binding', Binding); 534 exports.$set('Binding', Binding);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698