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