Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5663)

Unified Diff: chrome/renderer/resources/extensions/schema_generated_bindings.js

Issue 10535030: Allow updateArgumentsPostValidate to support callbacks and added / removed arguments. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Proper syntax Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/renderer/resources/extensions/event.js ('k') | chrome/renderer/resources/extensions/schema_utils.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/renderer/resources/extensions/schema_generated_bindings.js
diff --git a/chrome/renderer/resources/extensions/schema_generated_bindings.js b/chrome/renderer/resources/extensions/schema_generated_bindings.js
index 29700bb5be8ed89282a9db4c0649934faef987e6..11fee33ed5dc8854df7e203aa66c3c952fc39a17 100644
--- a/chrome/renderer/resources/extensions/schema_generated_bindings.js
+++ b/chrome/renderer/resources/extensions/schema_generated_bindings.js
@@ -16,6 +16,7 @@
var utils = require('utils');
var isDevChannel = requireNative('channel').IsDevChannel;
var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
+ var schemaUtils = require('schemaUtils');
// The object to generate the bindings for "internal" APIs in, so that
// extensions can't directly call them (without access to chromeHidden),
@@ -26,156 +27,6 @@
var internalAPIs = {};
chromeHidden.internalAPIs = internalAPIs;
- // Validate arguments.
- var schemaValidator = new chromeHidden.JSONSchemaValidator();
- chromeHidden.validate = function(args, parameterSchemas) {
- if (args.length > parameterSchemas.length)
- throw new Error("Too many arguments.");
-
- for (var i = 0; i < parameterSchemas.length; i++) {
- if (i in args && args[i] !== null && args[i] !== undefined) {
- schemaValidator.resetErrors();
- schemaValidator.validate(args[i], parameterSchemas[i]);
- if (schemaValidator.errors.length == 0)
- continue;
-
- var message = "Invalid value for argument " + (i + 1) + ". ";
- for (var i = 0, err; err = schemaValidator.errors[i]; i++) {
- if (err.path) {
- message += "Property '" + err.path + "': ";
- }
- message += err.message;
- message = message.substring(0, message.length - 1);
- message += ", ";
- }
- message = message.substring(0, message.length - 2);
- message += ".";
-
- throw new Error(message);
- } else if (!parameterSchemas[i].optional) {
- throw new Error("Parameter " + (i + 1) + " is required.");
- }
- }
- };
-
- // Generate all possible signatures for a given API function.
- function getSignatures(parameterSchemas) {
- if (parameterSchemas.length === 0)
- return [[]];
-
- var signatures = [];
- var remaining = getSignatures(parameterSchemas.slice(1));
- for (var i = 0; i < remaining.length; i++)
- signatures.push([parameterSchemas[0]].concat(remaining[i]))
-
- if (parameterSchemas[0].optional)
- return signatures.concat(remaining);
- return signatures;
- };
-
- // Return true if arguments match a given signature's schema.
- function argumentsMatchSignature(args, candidateSignature) {
- if (args.length != candidateSignature.length)
- return false;
-
- for (var i = 0; i < candidateSignature.length; i++) {
- var argType = chromeHidden.JSONSchemaValidator.getType(args[i]);
- if (!schemaValidator.isValidSchemaType(argType, candidateSignature[i]))
- return false;
- }
- return true;
- };
-
- // Finds the function signature for the given arguments.
- function resolveSignature(args, definedSignature) {
- var candidateSignatures = getSignatures(definedSignature);
- for (var i = 0; i < candidateSignatures.length; i++) {
- if (argumentsMatchSignature(args, candidateSignatures[i]))
- return candidateSignatures[i];
- }
- return null;
- };
-
- // Returns a string representing the defined signature of the API function.
- // Example return value for chrome.windows.getCurrent:
- // "windows.getCurrent(optional object populate, function callback)"
- function getParameterSignatureString(name, definedSignature) {
- var getSchemaTypeString = function(schema) {
- var schemaTypes = schemaValidator.getAllTypesForSchema(schema);
- var typeName = schemaTypes.join(" or ") + " " + schema.name;
- if (schema.optional)
- return "optional " + typeName;
- return typeName;
- };
-
- var typeNames = definedSignature.map(getSchemaTypeString);
- return name + "(" + typeNames.join(", ") + ")";
- };
-
- // Returns a string representing a call to an API function.
- // Example return value for call: chrome.windows.get(1, callback) is:
- // "windows.get(int, function)"
- function getArgumentSignatureString(name, args) {
- var typeNames = args.map(chromeHidden.JSONSchemaValidator.getType);
- return name + "(" + typeNames.join(", ") + ")";
- };
-
- // Finds the correct signature for the given arguments, then validates the
- // arguments against that signature. Returns a 'normalized' arguments list
- // where nulls are inserted where optional parameters were omitted.
- function normalizeArgumentsAndValidate(args, funDef) {
- if (funDef.allowAmbiguousOptionalArguments) {
- chromeHidden.validate(args, funDef.definition.parameters);
- return args;
- }
-
- var definedSignature = funDef.definition.parameters;
- var resolvedSignature = resolveSignature(args, definedSignature);
- if (!resolvedSignature)
- throw new Error("Invocation of form " +
- getArgumentSignatureString(funDef.name, args) +
- " doesn't match definition " +
- getParameterSignatureString(funDef.name, definedSignature));
-
- chromeHidden.validate(args, resolvedSignature);
-
- var normalizedArgs = [];
- var ai = 0;
- for (var si = 0; si < definedSignature.length; si++) {
- if (definedSignature[si] === resolvedSignature[ai])
- normalizedArgs.push(args[ai++]);
- else
- normalizedArgs.push(null);
- }
- return normalizedArgs;
- };
-
- // Validates that a given schema for an API function is not ambiguous.
- function isFunctionSignatureAmbiguous(functionDef) {
- if (functionDef.allowAmbiguousOptionalArguments)
- return false;
-
- var signaturesAmbiguous = function(signature1, signature2) {
- if (signature1.length != signature2.length)
- return false;
-
- for (var i = 0; i < signature1.length; i++) {
- if (!schemaValidator.checkSchemaOverlap(signature1[i], signature2[i]))
- return false;
- }
- return true;
- };
-
- var candidateSignatures = getSignatures(functionDef.parameters);
- for (var i = 0; i < candidateSignatures.length; i++) {
- for (var j = i + 1; j < candidateSignatures.length; j++) {
- if (signaturesAmbiguous(candidateSignatures[i], candidateSignatures[j]))
- return true;
- }
- }
- return false;
- };
-
// Stores the name and definition of each API function, with methods to
// modify their behaviour (such as a custom way to handle requests to the
// API, a custom callback, etc).
@@ -295,9 +146,12 @@
// The functions in the schema are in list form, so we move them into a
// dictionary for easier access.
var self = this;
- self.parameters = {};
+ self.functionSchemas = {};
schema.functions.forEach(function(f) {
- self.parameters[f.name] = f.parameters;
+ self.functionSchemas[f.name] = {
+ name: f.name,
+ definition: f
+ }
});
};
@@ -390,7 +244,7 @@
if (!isSchemaNodeSupported(t, platform, manifestVersion))
return;
- schemaValidator.addTypes(t);
+ schemaUtils.schemaValidator.addTypes(t);
if (t.type == 'object' && customTypes[t.id]) {
customTypes[t.id].prototype.setSchema(t);
}
@@ -444,7 +298,8 @@
// to do that we would need to better factor this code so that it
// doesn't depend on so much v8::Extension machinery.
if (chromeHidden.validateAPI &&
- isFunctionSignatureAmbiguous(apiFunction.definition)) {
+ schemaUtils.isFunctionSignatureAmbiguous(
+ apiFunction.definition)) {
throw new Error(
apiFunction.name + ' has ambiguous optional arguments. ' +
'To implement custom disambiguation logic, add ' +
@@ -458,7 +313,7 @@
if (this.updateArgumentsPreValidate)
args = this.updateArgumentsPreValidate.apply(this, args);
- args = normalizeArgumentsAndValidate(args, this);
+ args = schemaUtils.normalizeArgumentsAndValidate(args, this);
if (this.updateArgumentsPostValidate)
args = this.updateArgumentsPostValidate.apply(this, args);
@@ -473,9 +328,8 @@
// Validate return value if defined - only in debug.
if (chromeHidden.validateCallbacks &&
- chromeHidden.validate &&
this.definition.returns) {
- chromeHidden.validate([retval], [this.definition.returns]);
+ schemaUtils.validate([retval], [this.definition.returns]);
}
return retval;
}).bind(apiFunction);
« no previous file with comments | « chrome/renderer/resources/extensions/event.js ('k') | chrome/renderer/resources/extensions/schema_utils.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698