OLD | NEW |
| (Empty) |
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 = remoteFactory && new remoteFactory(this.router_); | |
16 this.local = localFactory && new localFactory(this.remote); | |
17 this.router_.setIncomingReceiver(this.local); | |
18 | |
19 // Validate incoming messages: remote responses and local requests. | |
20 var validateRequest = localFactory && localFactory.prototype.validator; | |
21 var validateResponse = remoteFactory.prototype.validator; | |
22 var payloadValidators = []; | |
23 if (validateRequest) | |
24 payloadValidators.push(validateRequest); | |
25 if (validateResponse) | |
26 payloadValidators.push(validateResponse); | |
27 this.router_.setPayloadValidators(payloadValidators); | |
28 } | |
29 | |
30 Connection.prototype.close = function() { | |
31 this.router_.close(); | |
32 this.router_ = null; | |
33 this.local = null; | |
34 this.remote = null; | |
35 }; | |
36 | |
37 Connection.prototype.encounteredError = function() { | |
38 return this.router_.encounteredError(); | |
39 }; | |
40 | |
41 // The TestConnection subclass is only intended to be used in unit tests. | |
42 | |
43 function TestConnection(handle, localFactory, remoteFactory) { | |
44 Connection.call(this, | |
45 handle, | |
46 localFactory, | |
47 remoteFactory, | |
48 router.TestRouter, | |
49 connector.TestConnector); | |
50 } | |
51 | |
52 TestConnection.prototype = Object.create(Connection.prototype); | |
53 | |
54 var exports = {}; | |
55 exports.Connection = Connection; | |
56 exports.TestConnection = TestConnection; | |
57 return exports; | |
58 }); | |
OLD | NEW |