| 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 // Return a handle for a message pipe that's connected to a proxy | |
| 77 // for remoteInterface. Used by generated code for outgoing interface& | |
| 78 // (request) parameters: the caller is given the generated proxy via | |
| 79 // |proxyCallback(proxy)| and the generated code sends the handle | |
| 80 // returned by this function. | |
| 81 function bindProxy(proxyCallback, remoteInterface) { | |
| 82 var messagePipe = core.createMessagePipe(); | |
| 83 if (messagePipe.result != core.RESULT_OK) | |
| 84 throw new Error("createMessagePipe failed " + messagePipe.result); | |
| 85 | |
| 86 var proxy = new remoteInterface.proxyClass; | |
| 87 var router = new Router(messagePipe.handle0); | |
| 88 var connection = new BaseConnection(undefined, proxy, router); | |
| 89 ProxyBindings(proxy).connection = connection; | |
| 90 if (proxyCallback) | |
| 91 proxyCallback(proxy); | |
| 92 | |
| 93 return messagePipe.handle1; | |
| 94 } | |
| 95 | |
| 96 // Return a handle for a message pipe that's connected to a stub for | |
| 97 // localInterface. Used by generated code for outgoing interface | |
| 98 // parameters: the caller is given the generated stub via | |
| 99 // |stubCallback(stub)| and the generated code sends the handle | |
| 100 // returned by this function. The caller is responsible for managing | |
| 101 // the lifetime of the stub and for setting it's implementation | |
| 102 // delegate with: StubBindings(stub).delegate = myImpl; | |
| 103 function bindImpl(stubCallback, localInterface) { | |
| 104 var messagePipe = core.createMessagePipe(); | |
| 105 if (messagePipe.result != core.RESULT_OK) | |
| 106 throw new Error("createMessagePipe failed " + messagePipe.result); | |
| 107 | |
| 108 var stub = new localInterface.stubClass; | |
| 109 var router = new Router(messagePipe.handle0); | |
| 110 var connection = new BaseConnection(stub, undefined, router); | |
| 111 StubBindings(stub).connection = connection; | |
| 112 if (stubCallback) | |
| 113 stubCallback(stub); | |
| 114 | |
| 115 return messagePipe.handle1; | |
| 116 } | |
| 117 | |
| 118 // Return a remoteInterface proxy for handle. Used by generated code | |
| 119 // for converting incoming interface parameters to proxies. | |
| 120 function bindHandleToProxy(handle, remoteInterface) { | |
| 121 if (!core.isHandle(handle)) | |
| 122 throw new Error("Not a handle " + handle); | |
| 123 | |
| 124 var proxy = new remoteInterface.proxyClass; | |
| 125 var router = new Router(handle); | |
| 126 var connection = new BaseConnection(undefined, proxy, router); | |
| 127 ProxyBindings(proxy).connection = connection; | |
| 128 return proxy; | |
| 129 } | |
| 130 | |
| 131 // Return a localInterface stub for handle. Used by generated code | |
| 132 // for converting incoming interface& request parameters to localInterface | |
| 133 // stubs. The caller can specify the stub's implementation of localInterface | |
| 134 // like this: StubBindings(stub).delegate = myStubImpl. | |
| 135 function bindHandleToStub(handle, localInterface) { | |
| 136 if (!core.isHandle(handle)) | |
| 137 throw new Error("Not a handle " + handle); | |
| 138 | |
| 139 var stub = new localInterface.stubClass; | |
| 140 var router = new Router(handle); | |
| 141 var connection = new BaseConnection(stub, undefined, router); | |
| 142 StubBindings(stub).connection = connection; | |
| 143 return stub; | |
| 144 } | |
| 145 | |
| 146 var exports = {}; | |
| 147 exports.Connection = Connection; | |
| 148 exports.TestConnection = TestConnection; | |
| 149 | |
| 150 exports.bindProxy = bindProxy; | |
| 151 exports.bindImpl = bindImpl; | |
| 152 exports.bindHandleToProxy = bindHandleToProxy; | |
| 153 exports.bindHandleToStub = bindHandleToStub; | |
| 154 return exports; | |
| 155 }); | |
| OLD | NEW |