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

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

Issue 12647017: Lazily require types when validating Extensions API calls (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 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
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 // Routines used to validate and normalize arguments. 5 // Routines used to validate and normalize arguments.
6 6
7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); 7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
8 var chrome = requireNative('chrome').GetChrome(); 8 var chrome = requireNative('chrome').GetChrome();
9 var loadRefType = require('utils').loadRefType;
9 10
10 // TODO(benwells): unit test this file. 11 // TODO(benwells): unit test this file.
11 // JSONSchemaValidator is not loaded in unit tests. 12 // JSONSchemaValidator is not loaded in unit tests.
12 var validate; 13 var validate;
13 if (chromeHidden.JSONSchemaValidator) { 14 if (chromeHidden.JSONSchemaValidator) {
14 var schemaValidator = new chromeHidden.JSONSchemaValidator(); 15 var schemaValidator = new chromeHidden.JSONSchemaValidator();
15 16
16 // Validate arguments. 17 // Validate arguments.
17 validate = function(args, parameterSchemas) { 18 validate = function(args, parameterSchemas) {
18 if (args.length > parameterSchemas.length) 19 if (args.length > parameterSchemas.length)
(...skipping 24 matching lines...) Expand all
43 } 44 }
44 }; 45 };
45 } else { 46 } else {
46 validate = function() {}; 47 validate = function() {};
47 } 48 }
48 49
49 // Generate all possible signatures for a given API function. 50 // Generate all possible signatures for a given API function.
50 function getSignatures(parameterSchemas) { 51 function getSignatures(parameterSchemas) {
51 if (parameterSchemas.length === 0) 52 if (parameterSchemas.length === 0)
52 return [[]]; 53 return [[]];
54 for (var i = 0; i < parameterSchemas.length; i++) {
55 if ('$ref' in parameterSchemas[i]) {
56 var type = loadRefType(parameterSchemas[i]['$ref']);
57 type.name = parameterSchemas[i].name;
58 parameterSchemas[i] = type;
59 }
60 }
not at google - send to devlin 2013/03/21 00:01:13 is this necessary, the schema validator knows how
cduvall 2013/03/21 00:59:51 Hmm, it seemed necessary when I did it, but now I
53 var signatures = []; 61 var signatures = [];
54 var remaining = getSignatures(parameterSchemas.slice(1)); 62 var remaining = getSignatures(parameterSchemas.slice(1));
55 for (var i = 0; i < remaining.length; i++) 63 for (var i = 0; i < remaining.length; i++)
56 signatures.push([parameterSchemas[0]].concat(remaining[i])) 64 signatures.push([parameterSchemas[0]].concat(remaining[i]));
57 if (parameterSchemas[0].optional) 65 if (parameterSchemas[0].optional)
58 return signatures.concat(remaining); 66 return signatures.concat(remaining);
59 return signatures; 67 return signatures;
60 }; 68 };
61 69
62 // Return true if arguments match a given signature's schema. 70 // Return true if arguments match a given signature's schema.
63 function argumentsMatchSignature(args, candidateSignature) { 71 function argumentsMatchSignature(args, candidateSignature) {
64 if (args.length != candidateSignature.length) 72 if (args.length != candidateSignature.length)
65 return false; 73 return false;
66 for (var i = 0; i < candidateSignature.length; i++) { 74 for (var i = 0; i < candidateSignature.length; i++) {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 return true; 162 return true;
155 } 163 }
156 } 164 }
157 return false; 165 return false;
158 }; 166 };
159 167
160 exports.isFunctionSignatureAmbiguous = isFunctionSignatureAmbiguous; 168 exports.isFunctionSignatureAmbiguous = isFunctionSignatureAmbiguous;
161 exports.normalizeArgumentsAndValidate = normalizeArgumentsAndValidate; 169 exports.normalizeArgumentsAndValidate = normalizeArgumentsAndValidate;
162 exports.schemaValidator = schemaValidator; 170 exports.schemaValidator = schemaValidator;
163 exports.validate = validate; 171 exports.validate = validate;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698