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

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

Issue 12647017: Lazily require types when validating Extensions API calls (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more fixes 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 // ----------------------------------------------------------------------------- 5 // -----------------------------------------------------------------------------
6 // NOTE: If you change this file you need to touch renderer_resources.grd to 6 // NOTE: If you change this file you need to touch renderer_resources.grd to
7 // have your change take effect. 7 // have your change take effect.
8 // ----------------------------------------------------------------------------- 8 // -----------------------------------------------------------------------------
9 9
10 //============================================================================== 10 //==============================================================================
(...skipping 22 matching lines...) Expand all
33 // - null counts as 'unspecified' for optional values 33 // - null counts as 'unspecified' for optional values
34 // - added the 'choices' property, to allow specifying a list of possible types 34 // - added the 'choices' property, to allow specifying a list of possible types
35 // for a value 35 // for a value
36 // - by default an "object" typed schema does not allow additional properties. 36 // - by default an "object" typed schema does not allow additional properties.
37 // if present, "additionalProperties" is to be a schema against which all 37 // if present, "additionalProperties" is to be a schema against which all
38 // additional properties will be validated. 38 // additional properties will be validated.
39 //============================================================================== 39 //==============================================================================
40 40
41 // TODO(cduvall): Make this file not depend on chromeHidden. 41 // TODO(cduvall): Make this file not depend on chromeHidden.
42 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); 42 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
43 var loadRefDependency = require('utils').loadRefDependency; 43 var loadTypeSchema = require('utils').loadTypeSchema;
44 var CHECK = requireNative('logging').CHECK;
44 45
45 function isInstanceOfClass(instance, className) { 46 function isInstanceOfClass(instance, className) {
46 if (!instance) 47 if (!instance)
47 return false; 48 return false;
48 49
49 if (Object.prototype.toString.call(instance) == "[object " + className + "]") 50 if (Object.prototype.toString.call(instance) == "[object " + className + "]")
50 return true; 51 return true;
51 52
52 return isInstanceOfClass(Object.getPrototypeOf(instance), className); 53 return isInstanceOfClass(Object.getPrototypeOf(instance), className);
53 } 54 }
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 function(schema) { 170 function(schema) {
170 var schemaTypes = []; 171 var schemaTypes = [];
171 if (schema.type) 172 if (schema.type)
172 schemaTypes.push(schema.type); 173 schemaTypes.push(schema.type);
173 if (schema.choices) { 174 if (schema.choices) {
174 for (var i = 0; i < schema.choices.length; i++) { 175 for (var i = 0; i < schema.choices.length; i++) {
175 var choiceTypes = this.getAllTypesForSchema(schema.choices[i]); 176 var choiceTypes = this.getAllTypesForSchema(schema.choices[i]);
176 schemaTypes = schemaTypes.concat(choiceTypes); 177 schemaTypes = schemaTypes.concat(choiceTypes);
177 } 178 }
178 } 179 }
179 if (schema['$ref']) { 180 var ref = schema['$ref'];
180 var refTypes = this.getAllTypesForSchema(this.types[schema['$ref']]); 181 if (ref) {
181 schemaTypes = schemaTypes.concat(refTypes); 182 var type = this.getOrAddType(ref);
183 CHECK(type, 'Could not find type ' + ref);
184 schemaTypes = schemaTypes.concat(this.getAllTypesForSchema(type));
182 } 185 }
183 return schemaTypes; 186 return schemaTypes;
184 }; 187 };
185 188
189 chromeHidden.JSONSchemaValidator.prototype.getOrAddType = function(typeName) {
190 if (!this.types[typeName])
191 this.types[typeName] = loadTypeSchema(typeName);
192 return this.types[typeName];
193 };
194
186 /** 195 /**
187 * Returns true if |schema| would accept an argument of type |type|. 196 * Returns true if |schema| would accept an argument of type |type|.
188 */ 197 */
189 chromeHidden.JSONSchemaValidator.prototype.isValidSchemaType = 198 chromeHidden.JSONSchemaValidator.prototype.isValidSchemaType =
190 function(type, schema) { 199 function(type, schema) {
191 if (type == 'any') 200 if (type == 'any')
192 return true; 201 return true;
193 202
194 // TODO(kalman): I don't understand this code. How can type be "null"? 203 // TODO(kalman): I don't understand this code. How can type be "null"?
195 if (schema.optional && (type == "null" || type == "undefined")) 204 if (schema.optional && (type == "null" || type == "undefined"))
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 if (schema.id) 245 if (schema.id)
237 this.types[schema.id] = schema; 246 this.types[schema.id] = schema;
238 247
239 // If the schema has an extends property, the instance must validate against 248 // If the schema has an extends property, the instance must validate against
240 // that schema too. 249 // that schema too.
241 if (schema.extends) 250 if (schema.extends)
242 this.validate(instance, schema.extends, path); 251 this.validate(instance, schema.extends, path);
243 252
244 // If the schema has a $ref property, the instance must validate against 253 // If the schema has a $ref property, the instance must validate against
245 // that schema too. It must be present in this.types to be referenced. 254 // that schema too. It must be present in this.types to be referenced.
246 if (schema["$ref"]) { 255 var ref = schema["$ref"];
247 loadRefDependency(schema["$ref"]); 256 if (ref) {
248 if (!this.types[schema["$ref"]]) 257 if (!this.getOrAddType(ref))
249 this.addError(path, "unknownSchemaReference", [ schema["$ref"] ]); 258 this.addError(path, "unknownSchemaReference", [ ref ]);
250 else 259 else
251 this.validate(instance, this.types[schema["$ref"]], path) 260 this.validate(instance, this.getOrAddType(ref), path)
252 } 261 }
253 262
254 // If the schema has a choices property, the instance must validate against at 263 // If the schema has a choices property, the instance must validate against at
255 // least one of the items in that array. 264 // least one of the items in that array.
256 if (schema.choices) { 265 if (schema.choices) {
257 this.validateChoices(instance, schema, path); 266 this.validateChoices(instance, schema, path);
258 return; 267 return;
259 } 268 }
260 269
261 // If the schema has an enum property, the instance must be one of those 270 // If the schema has an enum property, the instance must be one of those
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 message: chromeHidden.JSONSchemaValidator.formatError(key, replacements) 518 message: chromeHidden.JSONSchemaValidator.formatError(key, replacements)
510 }); 519 });
511 }; 520 };
512 521
513 /** 522 /**
514 * Resets errors to an empty list so you can call 'validate' again. 523 * Resets errors to an empty list so you can call 'validate' again.
515 */ 524 */
516 chromeHidden.JSONSchemaValidator.prototype.resetErrors = function() { 525 chromeHidden.JSONSchemaValidator.prototype.resetErrors = function() {
517 this.errors = []; 526 this.errors = [];
518 }; 527 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698