Commit 08768f75 authored by hansmuller's avatar hansmuller Committed by Commit bot

Integrate Mojo JS validation bindings with the Router

Instad of generating exported validateFooRequest(),
validateFooResponse() methods, a pointer to a validation
functions is added to the generated Stub and Proxy class
prototypes. For example, given an interface Foo, with
client interface FooClient:

  FooStub.prototype.validators = [validateFooRequest];
  FooProxy.prototype.validators = [validateFooResponse];

Note that if interface Foo has no callbacks, then
FooProxy.prototype.validators is an empty array.

The router validates incoming messages using these functions
and validateMessageHeader().

BUG=407181

Review URL: https://codereview.chromium.org/488173006

Cr-Commit-Position: refs/heads/master@{#292270}
parent fcf8643d
......@@ -11,6 +11,15 @@ define("mojo/public/js/bindings/connection", [
this.remote = new remoteFactory(this.router_);
this.local = new localFactory(this.remote);
this.router_.setIncomingReceiver(this.local);
var validateRequest = localFactory.prototype.validator;
var validateResponse = remoteFactory.prototype.validator;
var payloadValidators = [];
if (validateRequest)
payloadValidators.push(validateRequest);
if (validateResponse)
payloadValidators.push(validateResponse);
this.router_.setPayloadValidators(payloadValidators);
}
Connection.prototype.close = function() {
......
......@@ -13,6 +13,7 @@ define("mojo/public/js/bindings/router", [
this.incomingReceiver_ = null;
this.nextRequestID_ = 0;
this.responders_ = {};
this.payloadValidators_ = [];
this.connector_.setIncomingReceiver({
accept: this.handleIncomingMessage_.bind(this),
......@@ -56,13 +57,21 @@ define("mojo/public/js/bindings/router", [
this.incomingReceiver_ = receiver;
};
Router.prototype.setPayloadValidators = function(payloadValidators) {
this.payloadValidators_ = payloadValidators;
};
Router.prototype.encounteredError = function() {
return this.connector_.encounteredError();
};
Router.prototype.handleIncomingMessage_ = function(message) {
var v = new validator.Validator(message);
if (v.validateMessageHeader() !== validator.validationError.NONE)
var noError = validator.validationError.NONE;
var messageValidator = new validator.Validator(message);
var err = messageValidator.validateMessageHeader();
for (var i = 0; err === noError && i < this.payloadValidators_.length; ++i)
err = this.payloadValidators_[i](messageValidator);
if (err !== noError)
this.close();
if (message.expectsResponse()) {
......
......@@ -227,19 +227,17 @@ define([
function testConformanceMessageValidation() {
testMessageValidation("conformance_", [
testInterface.validateConformanceTestInterfaceRequest,
]);
testInterface.ConformanceTestInterfaceStub.prototype.validator]);
}
function testIntegrationMessageValidation() {
testMessageValidation("integration_", [
testInterface.validateIntegrationTestInterface1Request,
testInterface.validateIntegrationTestInterface2Response
]);
testInterface.IntegrationTestInterface1Stub.prototype.validator,
testInterface.IntegrationTestInterface2Proxy.prototype.validator]);
}
expect(checkTestMessageParser()).toBeNull();
testConformanceMessageValidation();
testIntegrationMessageValidation();
expect(checkTestMessageParser()).toBeNull();
this.result = "PASS";
});
......@@ -157,6 +157,13 @@ params.{{parameter.name}}{% if not loop.last %}, {% endif -%}
{%- endif %}
}
{{interface.name}}Stub.prototype.validator = validate{{interface.name}}Request;
{%- if interface|has_callbacks %}
{{interface.name}}Proxy.prototype.validator = validate{{interface.name}}Response;
{%- else %}
{{interface.name}}Proxy.prototype.validator = null;
{%- endif -%}
{#--- Enums #}
{% from "enum_definition.tmpl" import enum_def -%}
{% for enum in interface.enums %}
......@@ -168,4 +175,4 @@ params.{{parameter.name}}{% if not loop.last %}, {% endif -%}
{% for constant in interface.constants %}
{{interface.name}}Proxy.{{constant.name}} = {{constant.value|expression_to_text}};
{{interface.name}}Stub.{{constant.name}} = {{interface.name}}Proxy.{{constant.name}};
{% endfor %}
{%- endfor %}
......@@ -28,15 +28,15 @@ define("{{module.path}}", [
{#--- Struct definitions #}
{% for struct in structs %}
{%- include "struct_definition.tmpl" %}
{%- endfor %}
{%- endfor -%}
{#--- Interface definitions #}
{%- for interface in interfaces %}
{%- for interface in interfaces -%}
{%- include "interface_definition.tmpl" %}
{%- endfor %}
var exports = {};
{% for constant in module.constants %}
{%- for constant in module.constants %}
exports.{{constant.name}} = {{constant.name}};
{%- endfor %}
{%- for enum in enums %}
......@@ -48,8 +48,6 @@ define("{{module.path}}", [
{%- for interface in interfaces %}
exports.{{interface.name}}Proxy = {{interface.name}}Proxy;
exports.{{interface.name}}Stub = {{interface.name}}Stub;
exports.validate{{interface.name}}Request = validate{{interface.name}}Request;
exports.validate{{interface.name}}Response = validate{{interface.name}}Response;
{%- endfor %}
return exports;
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment