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", [ ...@@ -11,6 +11,15 @@ define("mojo/public/js/bindings/connection", [
this.remote = new remoteFactory(this.router_); this.remote = new remoteFactory(this.router_);
this.local = new localFactory(this.remote); this.local = new localFactory(this.remote);
this.router_.setIncomingReceiver(this.local); 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() { Connection.prototype.close = function() {
......
...@@ -13,6 +13,7 @@ define("mojo/public/js/bindings/router", [ ...@@ -13,6 +13,7 @@ define("mojo/public/js/bindings/router", [
this.incomingReceiver_ = null; this.incomingReceiver_ = null;
this.nextRequestID_ = 0; this.nextRequestID_ = 0;
this.responders_ = {}; this.responders_ = {};
this.payloadValidators_ = [];
this.connector_.setIncomingReceiver({ this.connector_.setIncomingReceiver({
accept: this.handleIncomingMessage_.bind(this), accept: this.handleIncomingMessage_.bind(this),
...@@ -56,13 +57,21 @@ define("mojo/public/js/bindings/router", [ ...@@ -56,13 +57,21 @@ define("mojo/public/js/bindings/router", [
this.incomingReceiver_ = receiver; this.incomingReceiver_ = receiver;
}; };
Router.prototype.setPayloadValidators = function(payloadValidators) {
this.payloadValidators_ = payloadValidators;
};
Router.prototype.encounteredError = function() { Router.prototype.encounteredError = function() {
return this.connector_.encounteredError(); return this.connector_.encounteredError();
}; };
Router.prototype.handleIncomingMessage_ = function(message) { Router.prototype.handleIncomingMessage_ = function(message) {
var v = new validator.Validator(message); var noError = validator.validationError.NONE;
if (v.validateMessageHeader() !== 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(); this.close();
if (message.expectsResponse()) { if (message.expectsResponse()) {
......
...@@ -227,19 +227,17 @@ define([ ...@@ -227,19 +227,17 @@ define([
function testConformanceMessageValidation() { function testConformanceMessageValidation() {
testMessageValidation("conformance_", [ testMessageValidation("conformance_", [
testInterface.validateConformanceTestInterfaceRequest, testInterface.ConformanceTestInterfaceStub.prototype.validator]);
]);
} }
function testIntegrationMessageValidation() { function testIntegrationMessageValidation() {
testMessageValidation("integration_", [ testMessageValidation("integration_", [
testInterface.validateIntegrationTestInterface1Request, testInterface.IntegrationTestInterface1Stub.prototype.validator,
testInterface.validateIntegrationTestInterface2Response testInterface.IntegrationTestInterface2Proxy.prototype.validator]);
]);
} }
expect(checkTestMessageParser()).toBeNull();
testConformanceMessageValidation(); testConformanceMessageValidation();
testIntegrationMessageValidation(); testIntegrationMessageValidation();
expect(checkTestMessageParser()).toBeNull();
this.result = "PASS"; this.result = "PASS";
}); });
...@@ -157,6 +157,13 @@ params.{{parameter.name}}{% if not loop.last %}, {% endif -%} ...@@ -157,6 +157,13 @@ params.{{parameter.name}}{% if not loop.last %}, {% endif -%}
{%- 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 #} {#--- Enums #}
{% from "enum_definition.tmpl" import enum_def -%} {% from "enum_definition.tmpl" import enum_def -%}
{% for enum in interface.enums %} {% for enum in interface.enums %}
...@@ -168,4 +175,4 @@ params.{{parameter.name}}{% if not loop.last %}, {% endif -%} ...@@ -168,4 +175,4 @@ params.{{parameter.name}}{% if not loop.last %}, {% endif -%}
{% for constant in interface.constants %} {% for constant in interface.constants %}
{{interface.name}}Proxy.{{constant.name}} = {{constant.value|expression_to_text}}; {{interface.name}}Proxy.{{constant.name}} = {{constant.value|expression_to_text}};
{{interface.name}}Stub.{{constant.name}} = {{interface.name}}Proxy.{{constant.name}}; {{interface.name}}Stub.{{constant.name}} = {{interface.name}}Proxy.{{constant.name}};
{% endfor %} {%- endfor %}
...@@ -28,15 +28,15 @@ define("{{module.path}}", [ ...@@ -28,15 +28,15 @@ define("{{module.path}}", [
{#--- Struct definitions #} {#--- Struct definitions #}
{% for struct in structs %} {% for struct in structs %}
{%- include "struct_definition.tmpl" %} {%- include "struct_definition.tmpl" %}
{%- endfor %} {%- endfor -%}
{#--- Interface definitions #} {#--- Interface definitions #}
{%- for interface in interfaces %} {%- for interface in interfaces -%}
{%- include "interface_definition.tmpl" %} {%- include "interface_definition.tmpl" %}
{%- endfor %} {%- endfor %}
var exports = {}; var exports = {};
{% for constant in module.constants %} {%- for constant in module.constants %}
exports.{{constant.name}} = {{constant.name}}; exports.{{constant.name}} = {{constant.name}};
{%- endfor %} {%- endfor %}
{%- for enum in enums %} {%- for enum in enums %}
...@@ -48,8 +48,6 @@ define("{{module.path}}", [ ...@@ -48,8 +48,6 @@ define("{{module.path}}", [
{%- for interface in interfaces %} {%- for interface in interfaces %}
exports.{{interface.name}}Proxy = {{interface.name}}Proxy; exports.{{interface.name}}Proxy = {{interface.name}}Proxy;
exports.{{interface.name}}Stub = {{interface.name}}Stub; 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 %} {%- endfor %}
return exports; 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