summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/resources
diff options
context:
space:
mode:
authorrafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-12 21:41:15 +0000
committerrafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-12 21:41:15 +0000
commitdb30527a4f5017945dca3db5cf6cb5e1d589387e (patch)
tree22a0c41d138300aa72a7d441620d6dad3eed0440 /chrome/renderer/resources
parent99432806a06b73301e4723de2d4b6b12aad63a21 (diff)
downloadchromium_src-db30527a4f5017945dca3db5cf6cb5e1d589387e.zip
chromium_src-db30527a4f5017945dca3db5cf6cb5e1d589387e.tar.gz
chromium_src-db30527a4f5017945dca3db5cf6cb5e1d589387e.tar.bz2
JsonSchema support for declaring "object" constructors.
This adds support for an optional declaration of "isInstanceOf" on "type":"object". If present, the candidate object will be tested for js:instanceof window[schema.isInstanceOf]. Several api functions are modified to take advantage of declaring their "class". Also, support added to docs for modules, methods & events to declare "nodocs":"true" to be excluded from the docs. Also, check to warn on an attempt to use cygwin python to generate docs. BUG=27213 TEST=NONE Review URL: http://codereview.chromium.org/389005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31834 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/resources')
-rw-r--r--chrome/renderer/resources/extension_process_bindings.js44
-rw-r--r--chrome/renderer/resources/json_schema.js40
2 files changed, 54 insertions, 30 deletions
diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js
index 6a0584b..85b65ea 100644
--- a/chrome/renderer/resources/extension_process_bindings.js
+++ b/chrome/renderer/resources/extension_process_bindings.js
@@ -316,23 +316,22 @@ var chrome = chrome || {};
apiFunctions[apiFunction.name] = apiFunction;
module[functionDef.name] = bind(apiFunction, function() {
- // If the function is marked with a custom handler, then bypass
- // validation of the arguments. This flag is required for
- // extensions taking DOM objects as arguments. DOM objects
- // cannot be described using the type system in extension_api.json,
- // and so cannot be validated.
- // A good practice is to extract the primitive data needed to
- // service the request, and then invoke validate against that data.
- // See popup.show(...) for an example.
- if (!functionDef.customHandler) {
- chromeHidden.validate(arguments, this.definition.parameters);
- }
-
+ chromeHidden.validate(arguments, this.definition.parameters);
+
+ var retval;
if (this.handleRequest)
- return this.handleRequest.apply(this, arguments);
+ retval = this.handleRequest.apply(this, arguments);
else
- return sendRequest(this.name, arguments,
+ retval = sendRequest(this.name, arguments,
this.definition.parameters);
+
+ // Validate return value if defined - only in debug.
+ if (chromeHidden.validateCallbacks &&
+ chromeHidden.validate &&
+ this.definition.returns) {
+ chromeHidden.validate([retval], [this.definition.returns]);
+ }
+ return retval;
});
});
}
@@ -420,20 +419,9 @@ var chrome = chrome || {};
apiFunctions["experimental.popup.show"].handleRequest =
function(url, showDetails, callback) {
- if (!showDetails || !showDetails.relativeTo) {
- throw new Error("showDetails.relativeTo argument missing.");
- }
-
- var position = getAbsoluteRect(showDetails.relativeTo);
- var popUpInfo = {
- "url": url
- };
- var domAnchor = position;
- var modifiedArgs = [popUpInfo, domAnchor, callback];
- chromeHidden.validate(modifiedArgs, this.definition.parameters);
-
- return sendRequest(this.name, modifiedArgs,
- this.definition.parameters);
+ var internalArgs = [url, getAbsoluteRect(showDetails.relativeTo),
+ callback];
+ return sendRequest(this.name, internalArgs, this.definition.parameters);
}
apiFunctions["experimental.extension.getPopupView"].handleRequest =
diff --git a/chrome/renderer/resources/json_schema.js b/chrome/renderer/resources/json_schema.js
index 750f89d..6d0ef53 100644
--- a/chrome/renderer/resources/json_schema.js
+++ b/chrome/renderer/resources/json_schema.js
@@ -33,7 +33,9 @@
// - null counts as 'unspecified' for optional values
// - added the 'choices' property, to allow specifying a list of possible types
// for a value
-// - made additionalProperties default to false
+// - by default an "object" typed schema does not allow additional properties.
+// if present, "additionalProperties" is to be a schema against which all
+// additional properties will be validated.
//==============================================================================
(function() {
@@ -77,7 +79,8 @@ chromeHidden.JSONSchemaValidator.messages = {
invalidChoice: "Value does not match any valid type choices.",
invalidPropertyType: "Missing property type.",
schemaRequired: "Schema value required.",
- unknownSchemaReference: "Unknown schema reference: *."
+ unknownSchemaReference: "Unknown schema reference: *.",
+ notInstance: "Object must be an instance of *."
};
/**
@@ -272,6 +275,39 @@ chromeHidden.JSONSchemaValidator.prototype.validateObject = function(
}
}
+ // If "instanceof" property is set, check that this object inherits from
+ // the specified constructor (function).
+ if (schema.isInstanceOf) {
+ var isInstance = function() {
+ var constructor = this[schema.isInstanceOf];
+ if (constructor) {
+ return (instance instanceof constructor);
+ }
+
+ // Special-case constructors that can not always be found on the global
+ // object, but for which we to allow validation.
+ var allowedNamedConstructors = {
+ "DOMWindow": true,
+ "ImageData": true
+ }
+ if (!allowedNamedConstructors[schema.isInstanceOf]) {
+ throw "Attempt to validate against an instance ctor that could not be" +
+ "found: " + schema.isInstanceOf;
+ }
+ return (schema.isInstanceOf == instance.constructor.name)
+ }();
+
+ if (!isInstance)
+ this.addError(propPath, "notInstance", [schema.isInstanceOf]);
+ }
+
+ // Exit early from additional property check if "type":"any" is defined.
+ if (schema.additionalProperties &&
+ schema.additionalProperties.type &&
+ schema.additionalProperties.type == "any") {
+ return;
+ }
+
// By default, additional properties are not allowed on instance objects. This
// can be overridden by setting the additionalProperties property to a schema
// which any additional properties must validate against.