From 08768f75fe85867797a826fc53f555d592cfda5c Mon Sep 17 00:00:00 2001 From: hansmuller Date: Wed, 27 Aug 2014 16:45:25 -0700 Subject: [PATCH] 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} --- mojo/public/js/bindings/connection.js | 9 +++++++++ mojo/public/js/bindings/router.js | 13 +++++++++++-- mojo/public/js/bindings/validation_unittests.js | 10 ++++------ .../generators/js_templates/interface_definition.tmpl | 9 ++++++++- .../tools/bindings/generators/js_templates/module.js.tmpl | 8 +++----- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/mojo/public/js/bindings/connection.js b/mojo/public/js/bindings/connection.js index ebf60adb7f8d..03bb76811ec0 100644 --- a/mojo/public/js/bindings/connection.js +++ b/mojo/public/js/bindings/connection.js @@ -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() { diff --git a/mojo/public/js/bindings/router.js b/mojo/public/js/bindings/router.js index 648ecd455816..efb5eb0e6ab2 100644 --- a/mojo/public/js/bindings/router.js +++ b/mojo/public/js/bindings/router.js @@ -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()) { diff --git a/mojo/public/js/bindings/validation_unittests.js b/mojo/public/js/bindings/validation_unittests.js index be07ac97a081..cfba1162c95f 100644 --- a/mojo/public/js/bindings/validation_unittests.js +++ b/mojo/public/js/bindings/validation_unittests.js @@ -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"; }); diff --git a/mojo/public/tools/bindings/generators/js_templates/interface_definition.tmpl b/mojo/public/tools/bindings/generators/js_templates/interface_definition.tmpl index 62f03ae7e9f7..b41929c245b4 100644 --- a/mojo/public/tools/bindings/generators/js_templates/interface_definition.tmpl +++ b/mojo/public/tools/bindings/generators/js_templates/interface_definition.tmpl @@ -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 %} diff --git a/mojo/public/tools/bindings/generators/js_templates/module.js.tmpl b/mojo/public/tools/bindings/generators/js_templates/module.js.tmpl index a37a7a6efe43..93fa537023dd 100644 --- a/mojo/public/tools/bindings/generators/js_templates/module.js.tmpl +++ b/mojo/public/tools/bindings/generators/js_templates/module.js.tmpl @@ -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; }); -- 2.11.4.GIT