Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 require('json_schema'); | |
| 6 var schemaRegistry = requireNative('schema_registry'); | |
| 7 var sendRequest = require('sendRequest').sendRequest; | |
| 8 var utils = require('utils'); | |
| 9 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | |
| 10 var chrome = requireNative('chrome').GetChrome(); | |
| 11 var schemaUtils = require('schemaUtils'); | |
| 12 var process = requireNative('process'); | |
| 13 var manifestVersion = process.GetManifestVersion(); | |
| 14 var extensionId = process.GetExtensionId(); | |
| 15 var contextType = process.GetContextType(); | |
| 16 | |
| 17 // TODO(cduvall): take out. | |
| 18 var DCHECK = requireNative('logging').DCHECK; | |
|
not at google - send to devlin
2013/01/24 21:10:12
DCHECK is badass, why do you want to take it out?
cduvall
2013/01/24 22:15:15
I just put in in for debugging.
| |
| 19 | |
| 20 // The object to generate the bindings for "internal" APIs in, so that | |
| 21 // extensions can't directly call them (without access to chromeHidden), | |
| 22 // but are still needed for internal mechanisms of extensions (e.g. events). | |
| 23 // | |
| 24 // This is distinct to the "*Private" APIs which are controlled via | |
| 25 // having strict permissions and aren't generated *anywhere* unless needed. | |
| 26 var internalAPIs = {}; | |
| 27 chromeHidden.internalAPIs = internalAPIs; | |
|
not at google - send to devlin
2013/01/24 21:10:12
With the work you're doing it seems possible to im
cduvall
2013/01/24 22:15:15
Put in TODO on me.
| |
| 28 | |
| 29 // Stores the name and definition of each API function, with methods to | |
| 30 // modify their behaviour (such as a custom way to handle requests to the | |
| 31 // API, a custom callback, etc). | |
| 32 function APIFunctions() { | |
| 33 this._apiFunctions = {}; | |
| 34 this._unavailableApiFunctions = {}; | |
| 35 } | |
| 36 APIFunctions.prototype.register = function(apiName, apiFunction) { | |
| 37 this._apiFunctions[apiName] = apiFunction; | |
| 38 }; | |
| 39 // Registers a function as existing but not available, meaning that calls to | |
| 40 // the set* methods that reference this function should be ignored rather | |
| 41 // than throwing Errors. | |
| 42 APIFunctions.prototype.registerUnavailable = function(apiName) { | |
| 43 this._unavailableApiFunctions[apiName] = apiName; | |
| 44 }; | |
| 45 APIFunctions.prototype._setHook = | |
| 46 function(apiName, propertyName, customizedFunction) { | |
| 47 if (this._unavailableApiFunctions.hasOwnProperty(apiName)) | |
| 48 return; | |
| 49 if (!this._apiFunctions.hasOwnProperty(apiName)) | |
| 50 throw new Error('Tried to set hook for unknown API "' + apiName + '"'); | |
| 51 this._apiFunctions[apiName][propertyName] = customizedFunction; | |
| 52 }; | |
| 53 APIFunctions.prototype.setHandleRequest = | |
| 54 function(apiName, customizedFunction) { | |
| 55 return this._setHook(apiName, 'handleRequest', customizedFunction); | |
| 56 }; | |
| 57 APIFunctions.prototype.setUpdateArgumentsPostValidate = | |
| 58 function(apiName, customizedFunction) { | |
| 59 return this._setHook( | |
| 60 apiName, 'updateArgumentsPostValidate', customizedFunction); | |
| 61 }; | |
| 62 APIFunctions.prototype.setUpdateArgumentsPreValidate = | |
| 63 function(apiName, customizedFunction) { | |
| 64 return this._setHook( | |
| 65 apiName, 'updateArgumentsPreValidate', customizedFunction); | |
| 66 }; | |
| 67 APIFunctions.prototype.setCustomCallback = | |
| 68 function(apiName, customizedFunction) { | |
| 69 return this._setHook(apiName, 'customCallback', customizedFunction); | |
| 70 }; | |
| 71 | |
| 72 var apiFunctions = new APIFunctions(); | |
| 73 | |
| 74 // Wraps the calls to the set* methods of APIFunctions with the namespace of | |
| 75 // an API, and validates that all calls to set* methods aren't prefixed with | |
| 76 // a namespace. | |
| 77 // | |
| 78 // For example, if constructed with 'browserAction', a call to | |
| 79 // handleRequest('foo') will be transformed into | |
| 80 // handleRequest('browserAction.foo'). | |
| 81 // | |
| 82 // Likewise, if a call to handleRequest is called with 'browserAction.foo', | |
| 83 // it will throw an error. | |
| 84 // | |
| 85 // These help with isolating custom bindings from each other. | |
| 86 function NamespacedAPIFunctions(namespace, delegate) { | |
| 87 var self = this; | |
| 88 function wrap(methodName) { | |
| 89 self[methodName] = function(apiName, customizedFunction) { | |
| 90 var prefix = namespace + '.'; | |
| 91 if (apiName.indexOf(prefix) === 0) { | |
| 92 throw new Error(methodName + ' called with "' + apiName + | |
| 93 '" which has a "' + prefix + '" prefix. ' + | |
| 94 'This is unnecessary and must be left out.'); | |
| 95 } | |
| 96 return delegate[methodName].call(delegate, | |
| 97 prefix + apiName, customizedFunction); | |
| 98 }; | |
| 99 } | |
| 100 | |
| 101 wrap('contains'); | |
| 102 wrap('setHandleRequest'); | |
| 103 wrap('setUpdateArgumentsPostValidate'); | |
| 104 wrap('setUpdateArgumentsPreValidate'); | |
| 105 wrap('setCustomCallback'); | |
| 106 } | |
| 107 | |
| 108 function CustomBindingsObject() { | |
| 109 } | |
| 110 CustomBindingsObject.prototype.setSchema = function(schema) { | |
| 111 // The functions in the schema are in list form, so we move them into a | |
| 112 // dictionary for easier access. | |
| 113 var self = this; | |
| 114 self.functionSchemas = {}; | |
| 115 schema.functions.forEach(function(f) { | |
| 116 self.functionSchemas[f.name] = { | |
| 117 name: f.name, | |
| 118 definition: f | |
| 119 } | |
| 120 }); | |
| 121 }; | |
| 122 | |
| 123 // Get the platform from navigator.appVersion. | |
| 124 function getPlatform() { | |
| 125 var platforms = [ | |
| 126 [/CrOS Touch/, "chromeos touch"], | |
| 127 [/CrOS/, "chromeos"], | |
| 128 [/Linux/, "linux"], | |
| 129 [/Mac/, "mac"], | |
| 130 [/Win/, "win"], | |
| 131 ]; | |
| 132 | |
| 133 for (var i = 0; i < platforms.length; i++) { | |
| 134 if (platforms[i][0].test(navigator.appVersion)) { | |
| 135 return platforms[i][1]; | |
| 136 } | |
| 137 } | |
| 138 return "unknown"; | |
| 139 } | |
| 140 | |
| 141 function isPlatformSupported(schemaNode, platform) { | |
| 142 return !schemaNode.platforms || | |
| 143 schemaNode.platforms.indexOf(platform) > -1; | |
| 144 } | |
| 145 | |
| 146 function isManifestVersionSupported(schemaNode, manifestVersion) { | |
| 147 return !schemaNode.maximumManifestVersion || | |
| 148 manifestVersion <= schemaNode.maximumManifestVersion; | |
| 149 } | |
| 150 | |
| 151 function isSchemaNodeSupported(schemaNode, platform, manifestVersion) { | |
| 152 return isPlatformSupported(schemaNode, platform) && | |
| 153 isManifestVersionSupported(schemaNode, manifestVersion); | |
| 154 } | |
| 155 | |
| 156 var platform = getPlatform(); | |
| 157 | |
| 158 function generate(apiDef, eventType, types) { | |
| 159 var customEvent = eventType || null; | |
| 160 var customTypes = types || {}; | |
| 161 | |
| 162 if (!isSchemaNodeSupported(apiDef, platform, manifestVersion)) | |
| 163 return; | |
| 164 | |
| 165 // See comment on internalAPIs at the top. | |
| 166 var mod = apiDef.internal ? internalAPIs : {}; | |
| 167 | |
| 168 var namespaces = apiDef.namespace.split('.'); | |
| 169 for (var index = 0, name; name = namespaces[index]; index++) { | |
| 170 mod[name] = mod[name] || {}; | |
| 171 mod = mod[name]; | |
| 172 } | |
| 173 | |
| 174 // Add types to global schemaValidator | |
| 175 if (apiDef.types) { | |
| 176 apiDef.types.forEach(function(t) { | |
| 177 if (!isSchemaNodeSupported(t, platform, manifestVersion)) | |
| 178 return; | |
| 179 | |
| 180 schemaUtils.schemaValidator.addTypes(t); | |
| 181 if (t.type == 'object' && customTypes[t.id]) { | |
| 182 var parts = t.id.split("."); | |
| 183 customTypes[t.id].prototype.setSchema(t); | |
| 184 mod[parts[parts.length - 1]] = customTypes[t.id]; | |
| 185 } | |
| 186 }); | |
| 187 } | |
| 188 | |
| 189 // Returns whether access to the content of a schema should be denied, | |
| 190 // based on the presence of "unprivileged" and whether this is an | |
| 191 // extension process (versus e.g. a content script). | |
| 192 function isSchemaAccessAllowed(itemSchema) { | |
| 193 return (contextType == 'BLESSED_EXTENSION') || | |
| 194 apiDef.unprivileged || | |
| 195 itemSchema.unprivileged; | |
| 196 } | |
| 197 | |
| 198 // Adds a getter that throws an access denied error to object |mod| | |
| 199 // for property |name|. | |
| 200 function addUnprivilegedAccessGetter(mod, name) { | |
| 201 mod.__defineGetter__(name, function() { | |
| 202 throw new Error( | |
| 203 '"' + name + '" can only be used in extension processes. See ' + | |
| 204 'the content scripts documentation for more details.'); | |
| 205 }); | |
| 206 } | |
| 207 | |
| 208 // Setup Functions. | |
| 209 if (apiDef.functions) { | |
| 210 apiDef.functions.forEach(function(functionDef) { | |
| 211 if (functionDef.name in mod) { | |
| 212 throw new Error('Function ' + functionDef.name + | |
| 213 ' already defined in ' + apiDef.namespace); | |
| 214 } | |
| 215 | |
| 216 var apiFunctionName = apiDef.namespace + "." + functionDef.name; | |
| 217 | |
| 218 if (!isSchemaNodeSupported(functionDef, platform, manifestVersion)) { | |
| 219 apiFunctions.registerUnavailable(apiFunctionName); | |
| 220 return; | |
| 221 } | |
| 222 if (!isSchemaAccessAllowed(functionDef)) { | |
| 223 apiFunctions.registerUnavailable(apiFunctionName); | |
| 224 addUnprivilegedAccessGetter(mod, functionDef.name); | |
| 225 return; | |
| 226 } | |
| 227 | |
| 228 var apiFunction = {}; | |
| 229 apiFunction.definition = functionDef; | |
| 230 apiFunction.name = apiFunctionName; | |
| 231 | |
| 232 // TODO(aa): It would be best to run this in a unit test, but in order | |
| 233 // to do that we would need to better factor this code so that it | |
| 234 // doesn't depend on so much v8::Extension machinery. | |
| 235 if (chromeHidden.validateAPI && | |
| 236 schemaUtils.isFunctionSignatureAmbiguous( | |
| 237 apiFunction.definition)) { | |
| 238 throw new Error( | |
| 239 apiFunction.name + ' has ambiguous optional arguments. ' + | |
| 240 'To implement custom disambiguation logic, add ' + | |
| 241 '"allowAmbiguousOptionalArguments" to the function\'s schema.'); | |
| 242 } | |
| 243 | |
| 244 apiFunctions.register(apiFunction.name, apiFunction); | |
| 245 | |
| 246 mod[functionDef.name] = (function() { | |
| 247 var args = Array.prototype.slice.call(arguments); | |
| 248 if (this.updateArgumentsPreValidate) | |
| 249 args = this.updateArgumentsPreValidate.apply(this, args); | |
| 250 | |
| 251 args = schemaUtils.normalizeArgumentsAndValidate(args, this); | |
| 252 if (this.updateArgumentsPostValidate) | |
| 253 args = this.updateArgumentsPostValidate.apply(this, args); | |
| 254 | |
| 255 var retval; | |
| 256 if (this.handleRequest) { | |
| 257 retval = this.handleRequest.apply(this, args); | |
| 258 } else { | |
| 259 var optArgs = { | |
| 260 customCallback: this.customCallback | |
| 261 }; | |
| 262 retval = sendRequest(this.name, args, | |
| 263 this.definition.parameters, | |
| 264 optArgs); | |
| 265 } | |
| 266 | |
| 267 // Validate return value if defined - only in debug. | |
| 268 if (chromeHidden.validateCallbacks && | |
| 269 this.definition.returns) { | |
| 270 schemaUtils.validate([retval], [this.definition.returns]); | |
| 271 } | |
| 272 return retval; | |
| 273 }).bind(apiFunction); | |
| 274 }); | |
| 275 } | |
| 276 | |
| 277 // Setup Events | |
| 278 if (apiDef.events) { | |
| 279 apiDef.events.forEach(function(eventDef) { | |
| 280 if (eventDef.name in mod) { | |
| 281 throw new Error('Event ' + eventDef.name + | |
| 282 ' already defined in ' + apiDef.namespace); | |
| 283 } | |
| 284 if (!isSchemaNodeSupported(eventDef, platform, manifestVersion)) | |
| 285 return; | |
| 286 if (!isSchemaAccessAllowed(eventDef)) { | |
| 287 addUnprivilegedAccessGetter(mod, eventDef.name); | |
| 288 return; | |
| 289 } | |
| 290 | |
| 291 var eventName = apiDef.namespace + "." + eventDef.name; | |
| 292 var options = eventDef.options || {}; | |
| 293 | |
| 294 if (eventDef.filters && eventDef.filters.length > 0) | |
| 295 options.supportsFilters = true; | |
| 296 | |
| 297 if (customEvent) { | |
| 298 mod[eventDef.name] = new customEvent( | |
| 299 eventName, eventDef.parameters, eventDef.extraParameters, | |
| 300 options); | |
| 301 } else if (eventDef.anonymous) { | |
| 302 mod[eventDef.name] = new chrome.Event(); | |
| 303 } else { | |
| 304 mod[eventDef.name] = new chrome.Event( | |
| 305 eventName, eventDef.parameters, options); | |
| 306 } | |
| 307 }); | |
| 308 } | |
| 309 | |
| 310 function addProperties(m, parentDef) { | |
| 311 var properties = parentDef.properties; | |
| 312 if (!properties) | |
| 313 return; | |
| 314 | |
| 315 utils.forEach(properties, function(propertyName, propertyDef) { | |
| 316 if (propertyName in m) | |
| 317 return; // TODO(kalman): be strict like functions/events somehow. | |
| 318 if (!isSchemaNodeSupported(propertyDef, platform, manifestVersion)) | |
| 319 return; | |
| 320 if (!isSchemaAccessAllowed(propertyDef)) { | |
| 321 addUnprivilegedAccessGetter(m, propertyName); | |
| 322 return; | |
| 323 } | |
| 324 | |
| 325 var value = propertyDef.value; | |
| 326 if (value) { | |
| 327 // Values may just have raw types as defined in the JSON, such | |
| 328 // as "WINDOW_ID_NONE": { "value": -1 }. We handle this here. | |
| 329 // TODO(kalman): enforce that things with a "value" property can't | |
| 330 // define their own types. | |
| 331 var type = propertyDef.type || typeof(value); | |
| 332 if (type === 'integer' || type === 'number') { | |
| 333 value = parseInt(value); | |
| 334 } else if (type === 'boolean') { | |
| 335 value = value === "true"; | |
| 336 } else if (propertyDef["$ref"]) { | |
| 337 var refParts = propertyDef["$ref"].split("."); | |
| 338 if (refParts.length > 1) { | |
| 339 var constructor = utils.loadRefDependency( | |
| 340 propertyDef["$ref"])[refParts[refParts.length - 1]]; | |
| 341 } else { | |
| 342 var constructor = customTypes[propertyDef["$ref"]]; | |
| 343 } | |
| 344 if (!constructor) | |
| 345 throw new Error("No custom binding for " + propertyDef["$ref"]); | |
| 346 var args = value; | |
| 347 // For an object propertyDef, |value| is an array of constructor | |
| 348 // arguments, but we want to pass the arguments directly (i.e. | |
| 349 // not as an array), so we have to fake calling |new| on the | |
| 350 // constructor. | |
| 351 value = { __proto__: constructor.prototype }; | |
| 352 constructor.apply(value, args); | |
| 353 // Recursively add properties. | |
| 354 addProperties(value, propertyDef); | |
| 355 } else if (type === 'object') { | |
| 356 // Recursively add properties. | |
| 357 addProperties(value, propertyDef); | |
| 358 } else if (type !== 'string') { | |
| 359 throw new Error("NOT IMPLEMENTED (extension_api.json error): " + | |
| 360 "Cannot parse values for type \"" + type + "\""); | |
| 361 } | |
| 362 m[propertyName] = value; | |
| 363 } | |
| 364 }); | |
| 365 } | |
| 366 | |
| 367 addProperties(mod, apiDef); | |
| 368 if (!apiDef.internal) | |
| 369 return mod; | |
|
not at google - send to devlin
2013/01/24 21:10:12
Yeah - so we could do internal APIs like so:
Rath
cduvall
2013/01/24 22:15:15
Yeah, I'll probably need to refactor a little to g
| |
| 370 }; | |
| 371 | |
| 372 function Bindings(apiName) { | |
| 373 this._schema = schemaRegistry.GetSchema(apiName); | |
| 374 this._customEvent = null; | |
| 375 this._customTypes = {}; | |
| 376 this._customHooks = []; | |
| 377 | |
| 378 // The API through which the ${api_name}_custom_bindings.js files customize | |
| 379 // their API bindings beyond what can be generated. | |
| 380 // | |
| 381 // There are 2 types of customizations available: those which are required in | |
| 382 // order to do the schema generation (registerCustomEvent and | |
| 383 // registerCustomType), and those which can only run after the bindings have | |
| 384 // been generated (registerCustomHook). | |
| 385 // | |
| 386 | |
| 387 // Registers a custom type referenced via "$ref" fields in the API schema | |
| 388 // JSON. | |
| 389 this.registerCustomType = function(typeName, customTypeFactory) { | |
|
not at google - send to devlin
2013/01/24 21:10:12
This isn't the idiomatic way to put class methods
cduvall
2013/01/24 22:15:15
Done.
| |
| 390 var customType = customTypeFactory(); | |
| 391 customType.prototype = new CustomBindingsObject(); | |
| 392 this._customTypes[typeName] = customType; | |
| 393 }; | |
| 394 | |
| 395 // Registers a custom event type for the API identified by |namespace|. | |
| 396 // |event| is the event's constructor. | |
| 397 this.registerCustomEvent = function(event) { | |
| 398 this._customEvent = event; | |
| 399 }; | |
| 400 | |
| 401 // TODO(cduvall): MAKE THESE HAPPEN | |
| 402 // Registers a function |hook| to run after the schema for all APIs has been | |
| 403 // generated. The hook is passed as its first argument an "API" object to | |
| 404 // interact with, and second the current extension ID. See where | |
| 405 // |customHooks| is used. | |
| 406 this.registerCustomHook = function(fn) { | |
| 407 this._customHooks.push(fn); | |
| 408 }; | |
| 409 | |
| 410 this._runHooks = function(api) { | |
| 411 this._customHooks.forEach(function(hook) { | |
| 412 if (!isSchemaNodeSupported(this._schema, platform, manifestVersion)) | |
| 413 return; | |
| 414 | |
| 415 if (!hook) | |
| 416 return; | |
| 417 | |
| 418 hook({ | |
| 419 apiFunctions: new NamespacedAPIFunctions(this._schema.namespace, | |
| 420 apiFunctions), | |
| 421 schema: this._schema, | |
| 422 compiledApi: api | |
| 423 }, extensionId, contextType); | |
| 424 }, this); | |
| 425 }; | |
| 426 | |
| 427 this.generate = function() { | |
| 428 var bindings = generate(this._schema, this._customEvent, this._customTypes); | |
| 429 this._runHooks(bindings); | |
| 430 return bindings; | |
| 431 }; | |
| 432 } | |
| 433 | |
| 434 exports.generate = generate; | |
| 435 exports.Bindings = Bindings; | |
| 436 | |
| 437 // TODO: copy in the "Run non-declarative custom hooks" part? | |
| OLD | NEW |