1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 define("mojo/public/js/bindings/connection", [ 6 "mojo/public/js/bindings/connector", 7 "mojo/public/js/bindings/router", 8 ], function(connector, router) { 9 10 function Connection( 11 handle, localFactory, remoteFactory, routerFactory, connectorFactory) { 12 if (routerFactory === undefined) 13 routerFactory = router.Router; 14 this.router_ = new routerFactory(handle, connectorFactory); 15 this.remote = new remoteFactory(this.router_); 16 this.local = new localFactory(this.remote); 17 this.router_.setIncomingReceiver(this.local); 18 19 var validateRequest = localFactory.prototype.validator; 20 var validateResponse = remoteFactory.prototype.validator; 21 var payloadValidators = []; 22 if (validateRequest) 23 payloadValidators.push(validateRequest); 24 if (validateResponse) 25 payloadValidators.push(validateResponse); 26 this.router_.setPayloadValidators(payloadValidators); 27 } 28 29 Connection.prototype.close = function() { 30 this.router_.close(); 31 this.router_ = null; 32 this.local = null; 33 this.remote = null; 34 }; 35 36 Connection.prototype.encounteredError = function() { 37 return this.router_.encounteredError(); 38 }; 39 40 // The TestConnection subclass is only intended to be used in unit tests. 41 42 function TestConnection(handle, localFactory, remoteFactory) { 43 Connection.call(this, 44 handle, 45 localFactory, 46 remoteFactory, 47 router.TestRouter, 48 connector.TestConnector); 49 } 50 51 TestConnection.prototype = Object.create(Connection.prototype); 52 53 var exports = {}; 54 exports.Connection = Connection; 55 exports.TestConnection = TestConnection; 56 return exports; 57 }); 58