| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 require('json_schema'); | 5 require('json_schema'); |
| 6 require('event_bindings'); | 6 require('event_bindings'); |
| 7 var chrome = requireNative('chrome').GetChrome(); | 7 var chrome = requireNative('chrome').GetChrome(); |
| 8 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 8 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 9 var forEach = require('utils').forEach; | 9 var forEach = require('utils').forEach; |
| 10 var GetAvailability = requireNative('v8_context').GetAvailability; | 10 var GetAvailability = requireNative('v8_context').GetAvailability; |
| 11 var logging = requireNative('logging'); | 11 var logging = requireNative('logging'); |
| 12 var process = requireNative('process'); | 12 var process = requireNative('process'); |
| 13 var contextType = process.GetContextType(); | 13 var contextType = process.GetContextType(); |
| 14 var extensionId = process.GetExtensionId(); | 14 var extensionId = process.GetExtensionId(); |
| 15 var manifestVersion = process.GetManifestVersion(); | 15 var manifestVersion = process.GetManifestVersion(); |
| 16 var schemaRegistry = requireNative('schema_registry'); | 16 var schemaRegistry = requireNative('schema_registry'); |
| 17 var schemaUtils = require('schemaUtils'); | 17 var schemaUtils = require('schemaUtils'); |
| 18 var sendRequest = require('sendRequest').sendRequest; | |
| 19 var utils = require('utils'); | 18 var utils = require('utils'); |
| 20 var CHECK = requireNative('logging').CHECK; | 19 var CHECK = requireNative('logging').CHECK; |
| 20 var sendRequestHandler = require('sendRequest'); |
| 21 var sendRequest = sendRequestHandler.sendRequest; |
| 22 var logActivity = requireNative('activityLogger').LogActivity; |
| 21 | 23 |
| 22 // Stores the name and definition of each API function, with methods to | 24 // Stores the name and definition of each API function, with methods to |
| 23 // modify their behaviour (such as a custom way to handle requests to the | 25 // modify their behaviour (such as a custom way to handle requests to the |
| 24 // API, a custom callback, etc). | 26 // API, a custom callback, etc). |
| 25 function APIFunctions() { | 27 function APIFunctions(namespace) { |
| 26 this.apiFunctions_ = {}; | 28 this.apiFunctions_ = {}; |
| 27 this.unavailableApiFunctions_ = {}; | 29 this.unavailableApiFunctions_ = {}; |
| 30 this.namespace = namespace; |
| 28 } | 31 } |
| 29 | 32 |
| 30 APIFunctions.prototype.register = function(apiName, apiFunction) { | 33 APIFunctions.prototype.register = function(apiName, apiFunction) { |
| 31 this.apiFunctions_[apiName] = apiFunction; | 34 this.apiFunctions_[apiName] = apiFunction; |
| 32 }; | 35 }; |
| 33 | 36 |
| 34 // Registers a function as existing but not available, meaning that calls to | 37 // Registers a function as existing but not available, meaning that calls to |
| 35 // the set* methods that reference this function should be ignored rather | 38 // the set* methods that reference this function should be ignored rather |
| 36 // than throwing Errors. | 39 // than throwing Errors. |
| 37 APIFunctions.prototype.registerUnavailable = function(apiName) { | 40 APIFunctions.prototype.registerUnavailable = function(apiName) { |
| 38 this.unavailableApiFunctions_[apiName] = apiName; | 41 this.unavailableApiFunctions_[apiName] = apiName; |
| 39 }; | 42 }; |
| 40 | 43 |
| 41 APIFunctions.prototype.setHook_ = | 44 APIFunctions.prototype.setHook_ = |
| 42 function(apiName, propertyName, customizedFunction) { | 45 function(apiName, propertyName, customizedFunction) { |
| 43 if (this.unavailableApiFunctions_.hasOwnProperty(apiName)) | 46 if (this.unavailableApiFunctions_.hasOwnProperty(apiName)) |
| 44 return; | 47 return; |
| 45 if (!this.apiFunctions_.hasOwnProperty(apiName)) | 48 if (!this.apiFunctions_.hasOwnProperty(apiName)) |
| 46 throw new Error('Tried to set hook for unknown API "' + apiName + '"'); | 49 throw new Error('Tried to set hook for unknown API "' + apiName + '"'); |
| 47 this.apiFunctions_[apiName][propertyName] = customizedFunction; | 50 this.apiFunctions_[apiName][propertyName] = customizedFunction; |
| 48 }; | 51 }; |
| 49 | 52 |
| 50 APIFunctions.prototype.setHandleRequest = | 53 APIFunctions.prototype.setHandleRequest = |
| 51 function(apiName, customizedFunction) { | 54 function(apiName, customizedFunction) { |
| 52 return this.setHook_(apiName, 'handleRequest', customizedFunction); | 55 var prefix = this.namespace; |
| 56 return this.setHook_(apiName, 'handleRequest', |
| 57 function() { |
| 58 var ret = customizedFunction.apply(this, arguments); |
| 59 // Logs API calls to the Activity Log if it doesn't go through an |
| 60 // ExtensionFunction. |
| 61 if (!sendRequestHandler.getCalledSendRequest()) |
| 62 logActivity(extensionId, prefix + "." + apiName, |
| 63 Array.prototype.slice.call(arguments)); |
| 64 return ret; |
| 65 }); |
| 53 }; | 66 }; |
| 54 | 67 |
| 55 APIFunctions.prototype.setUpdateArgumentsPostValidate = | 68 APIFunctions.prototype.setUpdateArgumentsPostValidate = |
| 56 function(apiName, customizedFunction) { | 69 function(apiName, customizedFunction) { |
| 57 return this.setHook_( | 70 return this.setHook_( |
| 58 apiName, 'updateArgumentsPostValidate', customizedFunction); | 71 apiName, 'updateArgumentsPostValidate', customizedFunction); |
| 59 }; | 72 }; |
| 60 | 73 |
| 61 APIFunctions.prototype.setUpdateArgumentsPreValidate = | 74 APIFunctions.prototype.setUpdateArgumentsPreValidate = |
| 62 function(apiName, customizedFunction) { | 75 function(apiName, customizedFunction) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 CHECK(customType, jsModuleName + ' must export itself.'); | 141 CHECK(customType, jsModuleName + ' must export itself.'); |
| 129 customType.prototype = new CustomBindingsObject(); | 142 customType.prototype = new CustomBindingsObject(); |
| 130 customType.prototype.setSchema(type); | 143 customType.prototype.setSchema(type); |
| 131 return customType; | 144 return customType; |
| 132 } | 145 } |
| 133 | 146 |
| 134 var platform = getPlatform(); | 147 var platform = getPlatform(); |
| 135 | 148 |
| 136 function Binding(schema) { | 149 function Binding(schema) { |
| 137 this.schema_ = schema; | 150 this.schema_ = schema; |
| 138 this.apiFunctions_ = new APIFunctions(); | 151 this.apiFunctions_ = new APIFunctions(schema.namespace); |
| 139 this.customEvent_ = null; | 152 this.customEvent_ = null; |
| 140 this.customHooks_ = []; | 153 this.customHooks_ = []; |
| 141 }; | 154 }; |
| 142 | 155 |
| 143 Binding.create = function(apiName) { | 156 Binding.create = function(apiName) { |
| 144 return new Binding(schemaRegistry.GetSchema(apiName)); | 157 return new Binding(schemaRegistry.GetSchema(apiName)); |
| 145 }; | 158 }; |
| 146 | 159 |
| 147 Binding.prototype = { | 160 Binding.prototype = { |
| 148 // The API through which the ${api_name}_custom_bindings.js files customize | 161 // The API through which the ${api_name}_custom_bindings.js files customize |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 | 294 |
| 282 mod[functionDef.name] = (function() { | 295 mod[functionDef.name] = (function() { |
| 283 var args = Array.prototype.slice.call(arguments); | 296 var args = Array.prototype.slice.call(arguments); |
| 284 if (this.updateArgumentsPreValidate) | 297 if (this.updateArgumentsPreValidate) |
| 285 args = this.updateArgumentsPreValidate.apply(this, args); | 298 args = this.updateArgumentsPreValidate.apply(this, args); |
| 286 | 299 |
| 287 args = schemaUtils.normalizeArgumentsAndValidate(args, this); | 300 args = schemaUtils.normalizeArgumentsAndValidate(args, this); |
| 288 if (this.updateArgumentsPostValidate) | 301 if (this.updateArgumentsPostValidate) |
| 289 args = this.updateArgumentsPostValidate.apply(this, args); | 302 args = this.updateArgumentsPostValidate.apply(this, args); |
| 290 | 303 |
| 304 sendRequestHandler.clearCalledSendRequest(); |
| 305 |
| 291 var retval; | 306 var retval; |
| 292 if (this.handleRequest) { | 307 if (this.handleRequest) { |
| 293 retval = this.handleRequest.apply(this, args); | 308 retval = this.handleRequest.apply(this, args); |
| 294 } else { | 309 } else { |
| 295 var optArgs = { | 310 var optArgs = { |
| 296 customCallback: this.customCallback | 311 customCallback: this.customCallback |
| 297 }; | 312 }; |
| 298 retval = sendRequest(this.name, args, | 313 retval = sendRequest(this.name, args, |
| 299 this.definition.parameters, | 314 this.definition.parameters, |
| 300 optArgs); | 315 optArgs); |
| 301 } | 316 } |
| 317 sendRequestHandler.clearCalledSendRequest(); |
| 302 | 318 |
| 303 // Validate return value if defined - only in debug. | 319 // Validate return value if defined - only in debug. |
| 304 if (chromeHidden.validateCallbacks && | 320 if (chromeHidden.validateCallbacks && |
| 305 this.definition.returns) { | 321 this.definition.returns) { |
| 306 schemaUtils.validate([retval], [this.definition.returns]); | 322 schemaUtils.validate([retval], [this.definition.returns]); |
| 307 } | 323 } |
| 308 return retval; | 324 return retval; |
| 309 }).bind(apiFunction); | 325 }).bind(apiFunction); |
| 310 }, this); | 326 }, this); |
| 311 } | 327 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 }); | 411 }); |
| 396 }; | 412 }; |
| 397 | 413 |
| 398 addProperties(mod, schema); | 414 addProperties(mod, schema); |
| 399 this.runHooks_(mod); | 415 this.runHooks_(mod); |
| 400 return mod; | 416 return mod; |
| 401 } | 417 } |
| 402 }; | 418 }; |
| 403 | 419 |
| 404 exports.Binding = Binding; | 420 exports.Binding = Binding; |
| OLD | NEW |