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

Side by Side Diff: chrome/renderer/resources/extensions/schema_utils.js

Issue 10535030: Allow updateArgumentsPostValidate to support callbacks and added / removed arguments. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // Routines used to validate and normalize arguments.
6
7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
8
9 // TODO(benwells): unit test this file.
10 // JSONSchemaValidator is not loaded in unit tests.
11 if (chromeHidden.JSONSchemaValidator) {
not at google - send to devlin 2012/06/07 03:15:04 Just throwing this out there, use it as you will.
benwells 2012/06/07 04:11:48 I like the latter. The unit test was testing event
12 var schemaValidator = new chromeHidden.JSONSchemaValidator();
13
14 // Validate arguments.
15 function validate(args, parameterSchemas) {
16 if (args.length > parameterSchemas.length)
17 throw new Error("Too many arguments.");
18 for (var i = 0; i < parameterSchemas.length; i++) {
19 if (i in args && args[i] !== null && args[i] !== undefined) {
20 schemaValidator.resetErrors();
21 schemaValidator.validate(args[i], parameterSchemas[i]);
22 if (schemaValidator.errors.length == 0)
23 continue;
24 var message = "Invalid value for argument " + (i + 1) + ". ";
25 for (var i = 0, err;
26 err = schemaValidator.errors[i]; i++) {
27 if (err.path) {
28 message += "Property '" + err.path + "': ";
29 }
30 message += err.message;
31 message = message.substring(0, message.length - 1);
32 message += ", ";
33 }
34 message = message.substring(0, message.length - 2);
35 message += ".";
36 throw new Error(message);
37 } else if (!parameterSchemas[i].optional) {
38 throw new Error("Parameter " + (i + 1) + " is required.");
39 }
40 }
41 };
42 }
43
44 // Generate all possible signatures for a given API function.
45 function getSignatures(parameterSchemas) {
46 if (parameterSchemas.length === 0)
47 return [[]];
48 var signatures = [];
49 var remaining = getSignatures(parameterSchemas.slice(1));
50 for (var i = 0; i < remaining.length; i++)
51 signatures.push([parameterSchemas[0]].concat(remaining[i]))
52 if (parameterSchemas[0].optional)
53 return signatures.concat(remaining);
54 return signatures;
55 };
56
57 // Return true if arguments match a given signature's schema.
58 function argumentsMatchSignature(args, candidateSignature) {
59 if (args.length != candidateSignature.length)
60 return false;
61 for (var i = 0; i < candidateSignature.length; i++) {
62 var argType = chromeHidden.JSONSchemaValidator.getType(args[i]);
63 if (!schemaValidator.isValidSchemaType(argType,
64 candidateSignature[i]))
65 return false;
66 }
67 return true;
68 };
69
70 // Finds the function signature for the given arguments.
71 function resolveSignature(args, definedSignature) {
72 var candidateSignatures = getSignatures(definedSignature);
73 for (var i = 0; i < candidateSignatures.length; i++) {
74 if (argumentsMatchSignature(args, candidateSignatures[i]))
75 return candidateSignatures[i];
76 }
77 return null;
78 };
79
80 // Returns a string representing the defined signature of the API function.
81 // Example return value for chrome.windows.getCurrent:
82 // "windows.getCurrent(optional object populate, function callback)"
83 function getParameterSignatureString(name, definedSignature) {
84 var getSchemaTypeString = function(schema) {
85 var schemaTypes = schemaValidator.getAllTypesForSchema(schema);
86 var typeName = schemaTypes.join(" or ") + " " + schema.name;
87 if (schema.optional)
88 return "optional " + typeName;
89 return typeName;
90 };
91 var typeNames = definedSignature.map(getSchemaTypeString);
92 return name + "(" + typeNames.join(", ") + ")";
93 };
94
95 // Returns a string representing a call to an API function.
96 // Example return value for call: chrome.windows.get(1, callback) is:
97 // "windows.get(int, function)"
98 function getArgumentSignatureString(name, args) {
99 var typeNames = args.map(chromeHidden.JSONSchemaValidator.getType);
100 return name + "(" + typeNames.join(", ") + ")";
101 };
102
103 // Finds the correct signature for the given arguments, then validates the
104 // arguments against that signature. Returns a 'normalized' arguments list
105 // where nulls are inserted where optional parameters were omitted.
106 // |args| is expected to be an array.
107 function normalizeArgumentsAndValidate(args, funDef) {
108 if (funDef.allowAmbiguousOptionalArguments) {
109 validate(args, funDef.definition.parameters);
110 return args;
111 }
112 var definedSignature = funDef.definition.parameters;
113 var resolvedSignature = resolveSignature(args, definedSignature);
114 if (!resolvedSignature)
115 throw new Error("Invocation of form " +
116 getArgumentSignatureString(funDef.name, args) +
117 " doesn't match definition " +
118 getParameterSignatureString(funDef.name, definedSignature));
119 validate(args, resolvedSignature);
120 var normalizedArgs = [];
121 var ai = 0;
122 for (var si = 0; si < definedSignature.length; si++) {
123 if (definedSignature[si] === resolvedSignature[ai])
124 normalizedArgs.push(args[ai++]);
125 else
126 normalizedArgs.push(null);
127 }
128 return normalizedArgs;
129 };
130
131 // Validates that a given schema for an API function is not ambiguous.
132 function isFunctionSignatureAmbiguous(functionDef) {
133 if (functionDef.allowAmbiguousOptionalArguments)
134 return false;
135 var signaturesAmbiguous = function(signature1, signature2) {
136 if (signature1.length != signature2.length)
137 return false;
138 for (var i = 0; i < signature1.length; i++) {
139 if (!schemaValidator.checkSchemaOverlap(
140 signature1[i], signature2[i]))
141 return false;
142 }
143 return true;
144 };
145 var candidateSignatures = getSignatures(functionDef.parameters);
146 for (var i = 0; i < candidateSignatures.length; i++) {
147 for (var j = i + 1; j < candidateSignatures.length; j++) {
148 if (signaturesAmbiguous(candidateSignatures[i], candidateSignatures[j]))
149 return true;
150 }
151 }
152 return false;
153 };
154
155 exports.isFunctionSignatureAmbiguous = isFunctionSignatureAmbiguous;
156 exports.normalizeArgumentsAndValidate = normalizeArgumentsAndValidate;
157 exports.schemaValidator = schemaValidator;
158 exports.validate = validate;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698