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