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