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; | 18 var sendRequest = require('sendRequest').sendRequest; |
19 var utils = require('utils'); | 19 var utils = require('utils'); |
20 var logActivity = requireNative('activityLogger').LogActivity; | |
20 | 21 |
21 // Stores the name and definition of each API function, with methods to | 22 // 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 | 23 // modify their behaviour (such as a custom way to handle requests to the |
23 // API, a custom callback, etc). | 24 // API, a custom callback, etc). |
24 function APIFunctions() { | 25 function APIFunctions(namespace) { |
25 this.apiFunctions_ = {}; | 26 this.apiFunctions_ = {}; |
26 this.unavailableApiFunctions_ = {}; | 27 this.unavailableApiFunctions_ = {}; |
28 this.namespace = namespace; | |
27 } | 29 } |
28 | 30 |
29 APIFunctions.prototype.register = function(apiName, apiFunction) { | 31 APIFunctions.prototype.register = function(apiName, apiFunction) { |
30 this.apiFunctions_[apiName] = apiFunction; | 32 this.apiFunctions_[apiName] = apiFunction; |
31 }; | 33 }; |
32 | 34 |
33 // Registers a function as existing but not available, meaning that calls to | 35 // Registers a function as existing but not available, meaning that calls to |
34 // the set* methods that reference this function should be ignored rather | 36 // the set* methods that reference this function should be ignored rather |
35 // than throwing Errors. | 37 // than throwing Errors. |
36 APIFunctions.prototype.registerUnavailable = function(apiName) { | 38 APIFunctions.prototype.registerUnavailable = function(apiName) { |
37 this.unavailableApiFunctions_[apiName] = apiName; | 39 this.unavailableApiFunctions_[apiName] = apiName; |
38 }; | 40 }; |
39 | 41 |
40 APIFunctions.prototype.setHook_ = | 42 APIFunctions.prototype.setHook_ = |
41 function(apiName, propertyName, customizedFunction) { | 43 function(apiName, propertyName, customizedFunction) { |
42 if (this.unavailableApiFunctions_.hasOwnProperty(apiName)) | 44 if (this.unavailableApiFunctions_.hasOwnProperty(apiName)) |
43 return; | 45 return; |
44 if (!this.apiFunctions_.hasOwnProperty(apiName)) | 46 if (!this.apiFunctions_.hasOwnProperty(apiName)) |
45 throw new Error('Tried to set hook for unknown API "' + apiName + '"'); | 47 throw new Error('Tried to set hook for unknown API "' + apiName + '"'); |
46 this.apiFunctions_[apiName][propertyName] = customizedFunction; | 48 this.apiFunctions_[apiName][propertyName] = customizedFunction; |
47 }; | 49 }; |
48 | 50 |
51 // Logs API calls to the Activity Log. However we do *not* want to log API | |
52 // calls that use ExtensionFunctions. Set usesExtensionFunction=true if your | |
53 // API call uses an ExtensionFunction (i.e., invokes sendRequest). | |
49 APIFunctions.prototype.setHandleRequest = | 54 APIFunctions.prototype.setHandleRequest = |
50 function(apiName, customizedFunction) { | 55 function(apiName, customizedFunction, usesExtensionFunction) { |
51 return this.setHook_(apiName, 'handleRequest', customizedFunction); | 56 if (usesExtensionFunction) { |
57 return this.setHook_(apiName, 'handleRequest', customizedFunction); | |
58 } else { | |
59 var prefix = this.namespace; | |
60 return this.setHook_(apiName, 'handleRequest', | |
61 function() { | |
62 logActivity(extensionId, prefix + "." + apiName, | |
63 Array.prototype.slice.call(arguments)); | |
64 return customizedFunction.apply(this, arguments); | |
65 }); | |
66 } | |
52 }; | 67 }; |
53 | 68 |
54 APIFunctions.prototype.setUpdateArgumentsPostValidate = | 69 APIFunctions.prototype.setUpdateArgumentsPostValidate = |
55 function(apiName, customizedFunction) { | 70 function(apiName, customizedFunction) { |
56 return this.setHook_( | 71 return this.setHook_( |
57 apiName, 'updateArgumentsPostValidate', customizedFunction); | 72 apiName, 'updateArgumentsPostValidate', customizedFunction); |
58 }; | 73 }; |
59 | 74 |
60 APIFunctions.prototype.setUpdateArgumentsPreValidate = | 75 APIFunctions.prototype.setUpdateArgumentsPreValidate = |
61 function(apiName, customizedFunction) { | 76 function(apiName, customizedFunction) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 | 129 |
115 function isSchemaNodeSupported(schemaNode, platform, manifestVersion) { | 130 function isSchemaNodeSupported(schemaNode, platform, manifestVersion) { |
116 return isPlatformSupported(schemaNode, platform) && | 131 return isPlatformSupported(schemaNode, platform) && |
117 isManifestVersionSupported(schemaNode, manifestVersion); | 132 isManifestVersionSupported(schemaNode, manifestVersion); |
118 } | 133 } |
119 | 134 |
120 var platform = getPlatform(); | 135 var platform = getPlatform(); |
121 | 136 |
122 function Binding(schema) { | 137 function Binding(schema) { |
123 this.schema_ = schema; | 138 this.schema_ = schema; |
124 this.apiFunctions_ = new APIFunctions(); | 139 this.apiFunctions_ = new APIFunctions(schema.namespace); |
125 this.customEvent_ = null; | 140 this.customEvent_ = null; |
126 this.customTypes_ = {}; | 141 this.customTypes_ = {}; |
127 this.customHooks_ = []; | 142 this.customHooks_ = []; |
128 }; | 143 }; |
129 | 144 |
130 Binding.create = function(apiName) { | 145 Binding.create = function(apiName) { |
131 return new Binding(schemaRegistry.GetSchema(apiName)); | 146 return new Binding(schemaRegistry.GetSchema(apiName)); |
132 }; | 147 }; |
133 | 148 |
134 Binding.prototype = { | 149 Binding.prototype = { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 this.apiFunctions_.register(functionDef.name, apiFunction); | 293 this.apiFunctions_.register(functionDef.name, apiFunction); |
279 | 294 |
280 mod[functionDef.name] = (function() { | 295 mod[functionDef.name] = (function() { |
281 var args = Array.prototype.slice.call(arguments); | 296 var args = Array.prototype.slice.call(arguments); |
282 if (this.updateArgumentsPreValidate) | 297 if (this.updateArgumentsPreValidate) |
283 args = this.updateArgumentsPreValidate.apply(this, args); | 298 args = this.updateArgumentsPreValidate.apply(this, args); |
284 | 299 |
285 args = schemaUtils.normalizeArgumentsAndValidate(args, this); | 300 args = schemaUtils.normalizeArgumentsAndValidate(args, this); |
286 if (this.updateArgumentsPostValidate) | 301 if (this.updateArgumentsPostValidate) |
287 args = this.updateArgumentsPostValidate.apply(this, args); | 302 args = this.updateArgumentsPostValidate.apply(this, args); |
288 | 303 |
Matt Perry
2013/03/15 17:51:07
(clear global here)
felt
2013/03/15 23:26:15
Done.
| |
289 var retval; | 304 var retval; |
290 if (this.handleRequest) { | 305 if (this.handleRequest) { |
291 retval = this.handleRequest.apply(this, args); | 306 retval = this.handleRequest.apply(this, args); |
292 } else { | 307 } else { |
293 var optArgs = { | 308 var optArgs = { |
294 customCallback: this.customCallback | 309 customCallback: this.customCallback |
295 }; | 310 }; |
296 retval = sendRequest(this.name, args, | 311 retval = sendRequest(this.name, args, |
297 this.definition.parameters, | 312 this.definition.parameters, |
298 optArgs); | 313 optArgs); |
299 } | 314 } |
300 | 315 |
Matt Perry
2013/03/15 17:51:07
(clear global here)
felt
2013/03/15 23:26:15
Done.
| |
301 // Validate return value if defined - only in debug. | 316 // Validate return value if defined - only in debug. |
302 if (chromeHidden.validateCallbacks && | 317 if (chromeHidden.validateCallbacks && |
303 this.definition.returns) { | 318 this.definition.returns) { |
304 schemaUtils.validate([retval], [this.definition.returns]); | 319 schemaUtils.validate([retval], [this.definition.returns]); |
305 } | 320 } |
306 return retval; | 321 return retval; |
307 }).bind(apiFunction); | 322 }).bind(apiFunction); |
308 }, this); | 323 }, this); |
309 } | 324 } |
310 | 325 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 }); | 414 }); |
400 }; | 415 }; |
401 | 416 |
402 addProperties(mod, schema); | 417 addProperties(mod, schema); |
403 this.runHooks_(mod); | 418 this.runHooks_(mod); |
404 return mod; | 419 return mod; |
405 } | 420 } |
406 }; | 421 }; |
407 | 422 |
408 exports.Binding = Binding; | 423 exports.Binding = Binding; |
OLD | NEW |