| 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 // This script contains privileged chrome extension related javascript APIs. | 5 // This script contains privileged chrome extension related javascript APIs. |
| 6 // It is loaded by pages whose URL has the chrome-extension protocol. | 6 // It is loaded by pages whose URL has the chrome-extension protocol. |
| 7 | 7 |
| 8 // TODO(battre): cleanup the usage of packages everywhere, as described here |
| 9 // http://codereview.chromium.org/10392008/diff/38/chrome/renderer/resources/e
xtensions/schema_generated_bindings.js |
| 10 |
| 8 require('json_schema'); | 11 require('json_schema'); |
| 9 require('event_bindings'); | 12 var events = require('event_bindings'); |
| 10 var GetExtensionAPIDefinition = | 13 var GetExtensionAPIDefinition = |
| 11 requireNative('apiDefinitions').GetExtensionAPIDefinition; | 14 requireNative('apiDefinitions').GetExtensionAPIDefinition; |
| 12 var sendRequest = require('sendRequest').sendRequest; | 15 var sendRequest = require('sendRequest').sendRequest; |
| 16 var utils = require('utils'); |
| 13 | 17 |
| 14 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 18 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 15 | 19 |
| 16 // The object to generate the bindings for "internal" APIs in, so that | 20 // The object to generate the bindings for "internal" APIs in, so that |
| 17 // extensions can't directly call them (without access to chromeHidden), | 21 // extensions can't directly call them (without access to chromeHidden), |
| 18 // but are still needed for internal mechanisms of extensions (e.g. events). | 22 // but are still needed for internal mechanisms of extensions (e.g. events). |
| 19 // | 23 // |
| 20 // This is distinct to the "*Private" APIs which are controlled via | 24 // This is distinct to the "*Private" APIs which are controlled via |
| 21 // having strict permissions and aren't generated *anywhere* unless needed. | 25 // having strict permissions and aren't generated *anywhere* unless needed. |
| 22 var internalAPIs = {}; | 26 var internalAPIs = {}; |
| 23 chromeHidden.internalAPIs = internalAPIs; | 27 chromeHidden.internalAPIs = internalAPIs; |
| 24 | 28 |
| 25 function forEach(dict, f) { | |
| 26 for (key in dict) { | |
| 27 if (dict.hasOwnProperty(key)) | |
| 28 f(key, dict[key]); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 // Validate arguments. | 29 // Validate arguments. |
| 33 var schemaValidator = new chromeHidden.JSONSchemaValidator(); | 30 var schemaValidator = new chromeHidden.JSONSchemaValidator(); |
| 34 chromeHidden.validate = function(args, parameterSchemas) { | 31 chromeHidden.validate = function(args, parameterSchemas) { |
| 35 if (args.length > parameterSchemas.length) | 32 if (args.length > parameterSchemas.length) |
| 36 throw new Error("Too many arguments."); | 33 throw new Error("Too many arguments."); |
| 37 | 34 |
| 38 for (var i = 0; i < parameterSchemas.length; i++) { | 35 for (var i = 0; i < parameterSchemas.length; i++) { |
| 39 if (i in args && args[i] !== null && args[i] !== undefined) { | 36 if (i in args && args[i] !== null && args[i] !== undefined) { |
| 40 schemaValidator.resetErrors(); | 37 schemaValidator.resetErrors(); |
| 41 schemaValidator.validate(args[i], parameterSchemas[i]); | 38 schemaValidator.validate(args[i], parameterSchemas[i]); |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 var apiDefinitions = GetExtensionAPIDefinition(); | 350 var apiDefinitions = GetExtensionAPIDefinition(); |
| 354 | 351 |
| 355 // Read api definitions and setup api functions in the chrome namespace. | 352 // Read api definitions and setup api functions in the chrome namespace. |
| 356 // TODO(rafaelw): Consider defining a json schema for an api definition | 353 // TODO(rafaelw): Consider defining a json schema for an api definition |
| 357 // and validating either here, in a unit_test or both. | 354 // and validating either here, in a unit_test or both. |
| 358 // TODO(rafaelw): Handle synchronous functions. | 355 // TODO(rafaelw): Handle synchronous functions. |
| 359 // TODO(rafaelw): Consider providing some convenient override points | 356 // TODO(rafaelw): Consider providing some convenient override points |
| 360 // for api functions that wish to insert themselves into the call. | 357 // for api functions that wish to insert themselves into the call. |
| 361 var platform = getPlatform(); | 358 var platform = getPlatform(); |
| 362 | 359 |
| 360 // Inform implementation in event.js about schema of events namespace. |
| 361 var eventsSchema = utils.lookup(apiDefinitions, 'namespace', 'events'); |
| 362 if (eventsSchema) |
| 363 events.storeFunctionSchemes(eventsSchema); |
| 364 |
| 363 apiDefinitions.forEach(function(apiDef) { | 365 apiDefinitions.forEach(function(apiDef) { |
| 364 // TODO(kalman): Remove this, or refactor schema_generated_bindings.js so | 366 // TODO(kalman): Remove this, or refactor schema_generated_bindings.js so |
| 365 // that it isn't necessary. For now, chrome.app is entirely handwritten. | 367 // that it isn't necessary. For now, chrome.app is entirely handwritten. |
| 366 if (apiDef.namespace === 'app') | 368 if (apiDef.namespace === 'app') |
| 367 return; | 369 return; |
| 368 | 370 |
| 369 if (!isSchemaNodeSupported(apiDef, platform, manifestVersion)) | 371 if (!isSchemaNodeSupported(apiDef, platform, manifestVersion)) |
| 370 return; | 372 return; |
| 371 | 373 |
| 372 // See comment on internalAPIs at the top. | 374 // See comment on internalAPIs at the top. |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 if (!isSchemaAccessAllowed(eventDef)) { | 490 if (!isSchemaAccessAllowed(eventDef)) { |
| 489 addUnprivilegedAccessGetter(mod, eventDef.name); | 491 addUnprivilegedAccessGetter(mod, eventDef.name); |
| 490 return; | 492 return; |
| 491 } | 493 } |
| 492 | 494 |
| 493 var eventName = apiDef.namespace + "." + eventDef.name; | 495 var eventName = apiDef.namespace + "." + eventDef.name; |
| 494 var customEvent = customEvents[apiDef.namespace]; | 496 var customEvent = customEvents[apiDef.namespace]; |
| 495 if (customEvent) { | 497 if (customEvent) { |
| 496 mod[eventDef.name] = new customEvent( | 498 mod[eventDef.name] = new customEvent( |
| 497 eventName, eventDef.parameters, eventDef.extraParameters, | 499 eventName, eventDef.parameters, eventDef.extraParameters, |
| 498 eventDef.options); | 500 eventDef.options, eventsSchema); |
| 499 } else if (eventDef.anonymous) { | 501 } else if (eventDef.anonymous) { |
| 500 mod[eventDef.name] = new chrome.Event(); | 502 mod[eventDef.name] = new chrome.Event(); |
| 501 } else { | 503 } else { |
| 502 mod[eventDef.name] = new chrome.Event( | 504 mod[eventDef.name] = new chrome.Event( |
| 503 eventName, eventDef.parameters, eventDef.options); | 505 eventName, eventDef.parameters, eventDef.options); |
| 504 } | 506 } |
| 505 }); | 507 }); |
| 506 } | 508 } |
| 507 | 509 |
| 508 function addProperties(m, parentDef) { | 510 function addProperties(m, parentDef) { |
| 509 var properties = parentDef.properties; | 511 var properties = parentDef.properties; |
| 510 if (!properties) | 512 if (!properties) |
| 511 return; | 513 return; |
| 512 | 514 |
| 513 forEach(properties, function(propertyName, propertyDef) { | 515 utils.forEach(properties, function(propertyName, propertyDef) { |
| 514 if (propertyName in m) | 516 if (propertyName in m) |
| 515 return; // TODO(kalman): be strict like functions/events somehow. | 517 return; // TODO(kalman): be strict like functions/events somehow. |
| 516 if (!isSchemaNodeSupported(propertyDef, platform, manifestVersion)) | 518 if (!isSchemaNodeSupported(propertyDef, platform, manifestVersion)) |
| 517 return; | 519 return; |
| 518 if (!isSchemaAccessAllowed(propertyDef)) { | 520 if (!isSchemaAccessAllowed(propertyDef)) { |
| 519 addUnprivilegedAccessGetter(m, propertyName); | 521 addUnprivilegedAccessGetter(m, propertyName); |
| 520 return; | 522 return; |
| 521 } | 523 } |
| 522 | 524 |
| 523 var value = propertyDef.value; | 525 var value = propertyDef.value; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 // beginInstallWithManifest2. | 585 // beginInstallWithManifest2. |
| 584 // See http://crbug.com/100242 | 586 // See http://crbug.com/100242 |
| 585 if (chrome.webstorePrivate) { | 587 if (chrome.webstorePrivate) { |
| 586 chrome.webstorePrivate.beginInstallWithManifest2 = | 588 chrome.webstorePrivate.beginInstallWithManifest2 = |
| 587 chrome.webstorePrivate.beginInstallWithManifest3; | 589 chrome.webstorePrivate.beginInstallWithManifest3; |
| 588 } | 590 } |
| 589 | 591 |
| 590 if (chrome.test) | 592 if (chrome.test) |
| 591 chrome.test.getApiDefinitions = GetExtensionAPIDefinition; | 593 chrome.test.getApiDefinitions = GetExtensionAPIDefinition; |
| 592 }); | 594 }); |
| OLD | NEW |