| 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/connection", [ | |
| 6 "mojo/public/js/bindings", | |
| 7 "mojo/public/js/connector", | |
| 8 "mojo/public/js/core", | |
| 9 "mojo/public/js/router", | |
| 10 ], function(bindings, connector, core, router) { | |
| 11 | |
| 12 var Router = router.Router; | |
| 13 var EmptyProxy = bindings.EmptyProxy; | |
| 14 var EmptyStub = bindings.EmptyStub; | |
| 15 var ProxyBindings = bindings.ProxyBindings; | |
| 16 var StubBindings = bindings.StubBindings; | |
| 17 var TestConnector = connector.TestConnector; | |
| 18 var TestRouter = router.TestRouter; | |
| 19 | |
| 20 // TODO(hansmuller): the proxy receiver_ property should be receiver$ | |
| 21 | |
| 22 function BaseConnection(localStub, remoteProxy, router) { | |
| 23 this.router_ = router; | |
| 24 this.local = localStub; | |
| 25 this.remote = remoteProxy; | |
| 26 | |
| 27 this.router_.setIncomingReceiver(localStub); | |
| 28 if (this.remote) | |
| 29 this.remote.receiver_ = router; | |
| 30 | |
| 31 // Validate incoming messages: remote responses and local requests. | |
| 32 var validateRequest = localStub && localStub.validator; | |
| 33 var validateResponse = remoteProxy && remoteProxy.validator; | |
| 34 var payloadValidators = []; | |
| 35 if (validateRequest) | |
| 36 payloadValidators.push(validateRequest); | |
| 37 if (validateResponse) | |
| 38 payloadValidators.push(validateResponse); | |
| 39 this.router_.setPayloadValidators(payloadValidators); | |
| 40 } | |
| 41 | |
| 42 BaseConnection.prototype.close = function() { | |
| 43 this.router_.close(); | |
| 44 this.router_ = null; | |
| 45 this.local = null; | |
| 46 this.remote = null; | |
| 47 }; | |
| 48 | |
| 49 BaseConnection.prototype.encounteredError = function() { | |
| 50 return this.router_.encounteredError(); | |
| 51 }; | |
| 52 | |
| 53 function Connection( | |
| 54 handle, localFactory, remoteFactory, routerFactory, connectorFactory) { | |
| 55 var routerClass = routerFactory || Router; | |
| 56 var router = new routerClass(handle, connectorFactory); | |
| 57 var remoteProxy = remoteFactory && new remoteFactory(router); | |
| 58 var localStub = localFactory && new localFactory(remoteProxy); | |
| 59 BaseConnection.call(this, localStub, remoteProxy, router); | |
| 60 } | |
| 61 | |
| 62 Connection.prototype = Object.create(BaseConnection.prototype); | |
| 63 | |
| 64 // The TestConnection subclass is only intended to be used in unit tests. | |
| 65 function TestConnection(handle, localFactory, remoteFactory) { | |
| 66 Connection.call(this, | |
| 67 handle, | |
| 68 localFactory, | |
| 69 remoteFactory, | |
| 70 TestRouter, | |
| 71 TestConnector); | |
| 72 } | |
| 73 | |
| 74 TestConnection.prototype = Object.create(Connection.prototype); | |
| 75 | |
| 76 function createOpenConnection( | |
| 77 messagePipeHandle, client, localInterface, remoteInterface) { | |
| 78 var stubClass = (localInterface && localInterface.stubClass) || EmptyStub; | |
| 79 var proxyClass = | |
| 80 (remoteInterface && remoteInterface.proxyClass) || EmptyProxy; | |
| 81 var proxy = new proxyClass; | |
| 82 var stub = new stubClass; | |
| 83 var router = new Router(messagePipeHandle); | |
| 84 var connection = new BaseConnection(stub, proxy, router); | |
| 85 | |
| 86 ProxyBindings(proxy).connection = connection; | |
| 87 ProxyBindings(proxy).local = connection.local; | |
| 88 StubBindings(stub).connection = connection; | |
| 89 StubBindings(proxy).remote = connection.remote; | |
| 90 | |
| 91 var clientImpl = client instanceof Function ? client(proxy) : client; | |
| 92 if (clientImpl) | |
| 93 StubBindings(stub).delegate = clientImpl; | |
| 94 | |
| 95 return connection; | |
| 96 } | |
| 97 | |
| 98 // Return a message pipe handle. | |
| 99 function bindProxyClient(clientImpl, localInterface, remoteInterface) { | |
| 100 var messagePipe = core.createMessagePipe(); | |
| 101 if (messagePipe.result != core.RESULT_OK) | |
| 102 throw new Error("createMessagePipe failed " + messagePipe.result); | |
| 103 | |
| 104 createOpenConnection( | |
| 105 messagePipe.handle0, clientImpl, localInterface, remoteInterface); | |
| 106 return messagePipe.handle1; | |
| 107 } | |
| 108 | |
| 109 // Return a proxy. | |
| 110 function bindProxyHandle(proxyHandle, localInterface, remoteInterface) { | |
| 111 if (!core.isHandle(proxyHandle)) | |
| 112 throw new Error("Not a handle " + proxyHandle); | |
| 113 | |
| 114 var connection = createOpenConnection( | |
| 115 proxyHandle, undefined, localInterface, remoteInterface); | |
| 116 return connection.remote; | |
| 117 } | |
| 118 | |
| 119 var exports = {}; | |
| 120 exports.Connection = Connection; | |
| 121 exports.TestConnection = TestConnection; | |
| 122 exports.bindProxyHandle = bindProxyHandle; | |
| 123 exports.bindProxyClient = bindProxyClient; | |
| 124 return exports; | |
| 125 }); | |
| OLD | NEW |