| 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'); |
| 19 var sendRequestHandler = require('sendRequest'); |
| 20 var sendRequest = sendRequestHandler.sendRequest; |
| 21 var logActivity = requireNative('activityLogger').LogActivity; |
| 20 | 22 |
| 21 // Stores the name and definition of each API function, with methods to | 23 // Stores the name and definition of each API function, with methods to |
| 22 // modify their behaviour (such as a custom way to handle requests to the | 24 // modify their behaviour (such as a custom way to handle requests to the |
| 23 // API, a custom callback, etc). | 25 // API, a custom callback, etc). |
| 24 function APIFunctions() { | 26 function APIFunctions(namespace) { |
| 25 this.apiFunctions_ = {}; | 27 this.apiFunctions_ = {}; |
| 26 this.unavailableApiFunctions_ = {}; | 28 this.unavailableApiFunctions_ = {}; |
| 29 this.namespace = namespace; |
| 27 } | 30 } |
| 28 | 31 |
| 29 APIFunctions.prototype.register = function(apiName, apiFunction) { | 32 APIFunctions.prototype.register = function(apiName, apiFunction) { |
| 30 this.apiFunctions_[apiName] = apiFunction; | 33 this.apiFunctions_[apiName] = apiFunction; |
| 31 }; | 34 }; |
| 32 | 35 |
| 33 // Registers a function as existing but not available, meaning that calls to | 36 // Registers a function as existing but not available, meaning that calls to |
| 34 // the set* methods that reference this function should be ignored rather | 37 // the set* methods that reference this function should be ignored rather |
| 35 // than throwing Errors. | 38 // than throwing Errors. |
| 36 APIFunctions.prototype.registerUnavailable = function(apiName) { | 39 APIFunctions.prototype.registerUnavailable = function(apiName) { |
| 37 this.unavailableApiFunctions_[apiName] = apiName; | 40 this.unavailableApiFunctions_[apiName] = apiName; |
| 38 }; | 41 }; |
| 39 | 42 |
| 40 APIFunctions.prototype.setHook_ = | 43 APIFunctions.prototype.setHook_ = |
| 41 function(apiName, propertyName, customizedFunction) { | 44 function(apiName, propertyName, customizedFunction) { |
| 42 if (this.unavailableApiFunctions_.hasOwnProperty(apiName)) | 45 if (this.unavailableApiFunctions_.hasOwnProperty(apiName)) |
| 43 return; | 46 return; |
| 44 if (!this.apiFunctions_.hasOwnProperty(apiName)) | 47 if (!this.apiFunctions_.hasOwnProperty(apiName)) |
| 45 throw new Error('Tried to set hook for unknown API "' + apiName + '"'); | 48 throw new Error('Tried to set hook for unknown API "' + apiName + '"'); |
| 46 this.apiFunctions_[apiName][propertyName] = customizedFunction; | 49 this.apiFunctions_[apiName][propertyName] = customizedFunction; |
| 47 }; | 50 }; |
| 48 | 51 |
| 49 APIFunctions.prototype.setHandleRequest = | 52 APIFunctions.prototype.setHandleRequest = |
| 50 function(apiName, customizedFunction) { | 53 function(apiName, customizedFunction) { |
| 51 return this.setHook_(apiName, 'handleRequest', customizedFunction); | 54 var prefix = this.namespace; |
| 55 return this.setHook_(apiName, 'handleRequest', |
| 56 function() { |
| 57 var ret = customizedFunction.apply(this, arguments); |
| 58 // Logs API calls to the Activity Log if it doesn't go through an |
| 59 // ExtensionFunction. |
| 60 if (!sendRequestHandler.getCalledSendRequest()) |
| 61 logActivity(extensionId, prefix + "." + apiName, |
| 62 Array.prototype.slice.call(arguments)); |
| 63 return ret; |
| 64 }); |
| 52 }; | 65 }; |
| 53 | 66 |
| 54 APIFunctions.prototype.setUpdateArgumentsPostValidate = | 67 APIFunctions.prototype.setUpdateArgumentsPostValidate = |
| 55 function(apiName, customizedFunction) { | 68 function(apiName, customizedFunction) { |
| 56 return this.setHook_( | 69 return this.setHook_( |
| 57 apiName, 'updateArgumentsPostValidate', customizedFunction); | 70 apiName, 'updateArgumentsPostValidate', customizedFunction); |
| 58 }; | 71 }; |
| 59 | 72 |
| 60 APIFunctions.prototype.setUpdateArgumentsPreValidate = | 73 APIFunctions.prototype.setUpdateArgumentsPreValidate = |
| 61 function(apiName, customizedFunction) { | 74 function(apiName, customizedFunction) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 | 127 |
| 115 function isSchemaNodeSupported(schemaNode, platform, manifestVersion) { | 128 function isSchemaNodeSupported(schemaNode, platform, manifestVersion) { |
| 116 return isPlatformSupported(schemaNode, platform) && | 129 return isPlatformSupported(schemaNode, platform) && |
| 117 isManifestVersionSupported(schemaNode, manifestVersion); | 130 isManifestVersionSupported(schemaNode, manifestVersion); |
| 118 } | 131 } |
| 119 | 132 |
| 120 var platform = getPlatform(); | 133 var platform = getPlatform(); |
| 121 | 134 |
| 122 function Binding(schema) { | 135 function Binding(schema) { |
| 123 this.schema_ = schema; | 136 this.schema_ = schema; |
| 124 this.apiFunctions_ = new APIFunctions(); | 137 this.apiFunctions_ = new APIFunctions(schema.namespace); |
| 125 this.customEvent_ = null; | 138 this.customEvent_ = null; |
| 126 this.customTypes_ = {}; | 139 this.customTypes_ = {}; |
| 127 this.customHooks_ = []; | 140 this.customHooks_ = []; |
| 128 }; | 141 }; |
| 129 | 142 |
| 130 Binding.create = function(apiName) { | 143 Binding.create = function(apiName) { |
| 131 return new Binding(schemaRegistry.GetSchema(apiName)); | 144 return new Binding(schemaRegistry.GetSchema(apiName)); |
| 132 }; | 145 }; |
| 133 | 146 |
| 134 Binding.prototype = { | 147 Binding.prototype = { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 function addUnprivilegedAccessGetter(mod, name) { | 249 function addUnprivilegedAccessGetter(mod, name) { |
| 237 mod.__defineGetter__(name, function() { | 250 mod.__defineGetter__(name, function() { |
| 238 throw new Error( | 251 throw new Error( |
| 239 '"' + name + '" can only be used in extension processes. See ' + | 252 '"' + name + '" can only be used in extension processes. See ' + |
| 240 'the content scripts documentation for more details.'); | 253 'the content scripts documentation for more details.'); |
| 241 }); | 254 }); |
| 242 } | 255 } |
| 243 | 256 |
| 244 // Setup Functions. | 257 // Setup Functions. |
| 245 if (schema.functions) { | 258 if (schema.functions) { |
| 259 var doubleLogging = "abcdefg"; |
| 246 forEach(schema.functions, function(i, functionDef) { | 260 forEach(schema.functions, function(i, functionDef) { |
| 247 if (functionDef.name in mod) { | 261 if (functionDef.name in mod) { |
| 248 throw new Error('Function ' + functionDef.name + | 262 throw new Error('Function ' + functionDef.name + |
| 249 ' already defined in ' + schema.namespace); | 263 ' already defined in ' + schema.namespace); |
| 250 } | 264 } |
| 251 | 265 |
| 252 if (!isSchemaNodeSupported(functionDef, platform, manifestVersion)) { | 266 if (!isSchemaNodeSupported(functionDef, platform, manifestVersion)) { |
| 253 this.apiFunctions_.registerUnavailable(functionDef.name); | 267 this.apiFunctions_.registerUnavailable(functionDef.name); |
| 254 return; | 268 return; |
| 255 } | 269 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 279 | 293 |
| 280 mod[functionDef.name] = (function() { | 294 mod[functionDef.name] = (function() { |
| 281 var args = Array.prototype.slice.call(arguments); | 295 var args = Array.prototype.slice.call(arguments); |
| 282 if (this.updateArgumentsPreValidate) | 296 if (this.updateArgumentsPreValidate) |
| 283 args = this.updateArgumentsPreValidate.apply(this, args); | 297 args = this.updateArgumentsPreValidate.apply(this, args); |
| 284 | 298 |
| 285 args = schemaUtils.normalizeArgumentsAndValidate(args, this); | 299 args = schemaUtils.normalizeArgumentsAndValidate(args, this); |
| 286 if (this.updateArgumentsPostValidate) | 300 if (this.updateArgumentsPostValidate) |
| 287 args = this.updateArgumentsPostValidate.apply(this, args); | 301 args = this.updateArgumentsPostValidate.apply(this, args); |
| 288 | 302 |
| 303 sendRequestHandler.clearCalledSendRequest(); |
| 304 |
| 289 var retval; | 305 var retval; |
| 290 if (this.handleRequest) { | 306 if (this.handleRequest) { |
| 291 retval = this.handleRequest.apply(this, args); | 307 retval = this.handleRequest.apply(this, args); |
| 292 } else { | 308 } else { |
| 293 var optArgs = { | 309 var optArgs = { |
| 294 customCallback: this.customCallback | 310 customCallback: this.customCallback |
| 295 }; | 311 }; |
| 296 retval = sendRequest(this.name, args, | 312 retval = sendRequest(this.name, args, |
| 297 this.definition.parameters, | 313 this.definition.parameters, |
| 298 optArgs); | 314 optArgs); |
| 299 } | 315 } |
| 316 sendRequestHandler.clearCalledSendRequest(); |
| 300 | 317 |
| 301 // Validate return value if defined - only in debug. | 318 // Validate return value if defined - only in debug. |
| 302 if (chromeHidden.validateCallbacks && | 319 if (chromeHidden.validateCallbacks && |
| 303 this.definition.returns) { | 320 this.definition.returns) { |
| 304 schemaUtils.validate([retval], [this.definition.returns]); | 321 schemaUtils.validate([retval], [this.definition.returns]); |
| 305 } | 322 } |
| 306 return retval; | 323 return retval; |
| 307 }).bind(apiFunction); | 324 }).bind(apiFunction); |
| 308 }, this); | 325 }, this); |
| 309 } | 326 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 }); | 416 }); |
| 400 }; | 417 }; |
| 401 | 418 |
| 402 addProperties(mod, schema); | 419 addProperties(mod, schema); |
| 403 this.runHooks_(mod); | 420 this.runHooks_(mod); |
| 404 return mod; | 421 return mod; |
| 405 } | 422 } |
| 406 }; | 423 }; |
| 407 | 424 |
| 408 exports.Binding = Binding; | 425 exports.Binding = Binding; |
| OLD | NEW |