| OLD | NEW |
| 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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 type.id + '.'); | 182 type.id + '.'); |
| 183 var customType = jsModule[jsModuleName]; | 183 var customType = jsModule[jsModuleName]; |
| 184 logging.CHECK(customType, jsModuleName + ' must export itself.'); | 184 logging.CHECK(customType, jsModuleName + ' must export itself.'); |
| 185 customType.prototype = new CustomBindingsObject(); | 185 customType.prototype = new CustomBindingsObject(); |
| 186 customType.prototype.setSchema(type); | 186 customType.prototype.setSchema(type); |
| 187 return customType; | 187 return customType; |
| 188 } | 188 } |
| 189 | 189 |
| 190 var platform = getPlatform(); | 190 var platform = getPlatform(); |
| 191 | 191 |
| 192 function Binding(schema) { | 192 function Binding(apiName) { |
| 193 this.schema_ = schema; | 193 this.apiName_ = apiName; |
| 194 this.apiFunctions_ = new APIFunctions(schema.namespace); | 194 this.apiFunctions_ = new APIFunctions(apiName); |
| 195 this.customEvent_ = null; | 195 this.customEvent_ = null; |
| 196 this.customHooks_ = []; | 196 this.customHooks_ = []; |
| 197 }; | 197 }; |
| 198 | 198 |
| 199 Binding.create = function(apiName) { | 199 Binding.create = function(apiName) { |
| 200 return new Binding(schemaRegistry.GetSchema(apiName)); | 200 return new Binding(apiName); |
| 201 }; | 201 }; |
| 202 | 202 |
| 203 Binding.prototype = { | 203 Binding.prototype = { |
| 204 // The API through which the ${api_name}_custom_bindings.js files customize | 204 // The API through which the ${api_name}_custom_bindings.js files customize |
| 205 // their API bindings beyond what can be generated. | 205 // their API bindings beyond what can be generated. |
| 206 // | 206 // |
| 207 // There are 2 types of customizations available: those which are required in | 207 // There are 2 types of customizations available: those which are required in |
| 208 // order to do the schema generation (registerCustomEvent and | 208 // order to do the schema generation (registerCustomEvent and |
| 209 // registerCustomType), and those which can only run after the bindings have | 209 // registerCustomType), and those which can only run after the bindings have |
| 210 // been generated (registerCustomHook). | 210 // been generated (registerCustomHook). |
| 211 | 211 |
| 212 // Registers a custom event type for the API identified by |namespace|. | 212 // Registers a custom event type for the API identified by |namespace|. |
| 213 // |event| is the event's constructor. | 213 // |event| is the event's constructor. |
| 214 registerCustomEvent: function(event) { | 214 registerCustomEvent: function(event) { |
| 215 this.customEvent_ = event; | 215 this.customEvent_ = event; |
| 216 }, | 216 }, |
| 217 | 217 |
| 218 // Registers a function |hook| to run after the schema for all APIs has been | 218 // Registers a function |hook| to run after the schema for all APIs has been |
| 219 // generated. The hook is passed as its first argument an "API" object to | 219 // generated. The hook is passed as its first argument an "API" object to |
| 220 // interact with, and second the current extension ID. See where | 220 // interact with, and second the current extension ID. See where |
| 221 // |customHooks| is used. | 221 // |customHooks| is used. |
| 222 registerCustomHook: function(fn) { | 222 registerCustomHook: function(fn) { |
| 223 $Array.push(this.customHooks_, fn); | 223 $Array.push(this.customHooks_, fn); |
| 224 }, | 224 }, |
| 225 | 225 |
| 226 // TODO(kalman/cduvall): Refactor this so |runHooks_| is not needed. | 226 // TODO(kalman/cduvall): Refactor this so |runHooks_| is not needed. |
| 227 runHooks_: function(api) { | 227 runHooks_: function(api, schema) { |
| 228 $Array.forEach(this.customHooks_, function(hook) { | 228 $Array.forEach(this.customHooks_, function(hook) { |
| 229 if (!isSchemaNodeSupported(this.schema_, platform, manifestVersion)) | 229 if (!isSchemaNodeSupported(schema, platform, manifestVersion)) |
| 230 return; | 230 return; |
| 231 | 231 |
| 232 if (!hook) | 232 if (!hook) |
| 233 return; | 233 return; |
| 234 | 234 |
| 235 hook({ | 235 hook({ |
| 236 apiFunctions: this.apiFunctions_, | 236 apiFunctions: this.apiFunctions_, |
| 237 schema: this.schema_, | 237 schema: schema, |
| 238 compiledApi: api | 238 compiledApi: api |
| 239 }, extensionId, contextType); | 239 }, extensionId, contextType); |
| 240 }, this); | 240 }, this); |
| 241 }, | 241 }, |
| 242 | 242 |
| 243 // Generates the bindings from |this.schema_| and integrates any custom | 243 // Generates the bindings from the schema for |this.apiName_| and integrates |
| 244 // bindings that might be present. | 244 // any custom bindings that might be present. |
| 245 generate: function() { | 245 generate: function() { |
| 246 var schema = this.schema_; | 246 // NB: It's important to load the schema during generation rather than |
| 247 // setting it beforehand so that we're more confident the schema we're |
| 248 // loading is real, and not one that was injected by a page intercepting |
| 249 // Binding.generate. |
| 250 // Additionally, since the schema is an object returned from a native |
| 251 // handler, its properties don't have the custom getters/setters that a page |
| 252 // may have put on Object.prototype. |
| 253 var schema = schemaRegistry.GetSchema(this.apiName_); |
| 247 | 254 |
| 248 function shouldCheckUnprivileged() { | 255 function shouldCheckUnprivileged() { |
| 249 var shouldCheck = 'unprivileged' in schema; | 256 var shouldCheck = 'unprivileged' in schema; |
| 250 if (shouldCheck) | 257 if (shouldCheck) |
| 251 return shouldCheck; | 258 return shouldCheck; |
| 252 | 259 |
| 253 $Array.forEach(['functions', 'events'], function(type) { | 260 $Array.forEach(['functions', 'events'], function(type) { |
| 254 if ($Object.hasOwnProperty(schema, type)) { | 261 if ($Object.hasOwnProperty(schema, type)) { |
| 255 $Array.forEach(schema[type], function(node) { | 262 $Array.forEach(schema[type], function(node) { |
| 256 if ('unprivileged' in node) | 263 if ('unprivileged' in node) |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 if (!success) { | 546 if (!success) { |
| 540 var availability = GetAvailability(schema.namespace); | 547 var availability = GetAvailability(schema.namespace); |
| 541 // If an API was available it should have been successfully generated. | 548 // If an API was available it should have been successfully generated. |
| 542 logging.DCHECK(!availability.is_available, | 549 logging.DCHECK(!availability.is_available, |
| 543 schema.namespace + ' was available but not generated'); | 550 schema.namespace + ' was available but not generated'); |
| 544 console.error('chrome.' + schema.namespace + ' is not available: ' + | 551 console.error('chrome.' + schema.namespace + ' is not available: ' + |
| 545 availability.message); | 552 availability.message); |
| 546 return; | 553 return; |
| 547 } | 554 } |
| 548 | 555 |
| 549 this.runHooks_(mod); | 556 this.runHooks_(mod, schema); |
| 550 return mod; | 557 return mod; |
| 551 } | 558 } |
| 552 }; | 559 }; |
| 553 | 560 |
| 554 exports.$set('Binding', Binding); | 561 exports.$set('Binding', Binding); |
| OLD | NEW |