| 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/connector", | |
| 7 "mojo/public/js/core", | |
| 8 "mojo/public/js/router", | |
| 9 ], function(connector, core, router) { | |
| 10 | |
| 11 // TODO(yzshen): This module should only be used by the JS bindings internally | |
| 12 // and it will be removed soon. | |
| 13 | |
| 14 var Router = router.Router; | |
| 15 var TestConnector = connector.TestConnector; | |
| 16 var TestRouter = router.TestRouter; | |
| 17 | |
| 18 var kProxyProperties = Symbol("proxyProperties"); | |
| 19 var kStubProperties = Symbol("stubProperties"); | |
| 20 | |
| 21 // Public proxy class properties that are managed at runtime by the JS | |
| 22 // bindings. See ProxyBindings below. | |
| 23 function ProxyProperties(receiver) { | |
| 24 this.receiver = receiver; | |
| 25 } | |
| 26 | |
| 27 // TODO(hansmuller): remove then after 'Client=' has been removed from Mojom. | |
| 28 ProxyProperties.prototype.getLocalDelegate = function() { | |
| 29 return this.local && StubBindings(this.local).delegate; | |
| 30 } | |
| 31 | |
| 32 // TODO(hansmuller): remove then after 'Client=' has been removed from Mojom. | |
| 33 ProxyProperties.prototype.setLocalDelegate = function(impl) { | |
| 34 if (this.local) | |
| 35 StubBindings(this.local).delegate = impl; | |
| 36 else | |
| 37 throw new Error("no stub object"); | |
| 38 } | |
| 39 | |
| 40 ProxyProperties.prototype.close = function() { | |
| 41 this.connection.close(); | |
| 42 } | |
| 43 | |
| 44 // Public stub class properties that are managed at runtime by the JS | |
| 45 // bindings. See StubBindings below. | |
| 46 function StubProperties(delegate) { | |
| 47 this.delegate = delegate; | |
| 48 } | |
| 49 | |
| 50 StubProperties.prototype.close = function() { | |
| 51 this.connection.close(); | |
| 52 } | |
| 53 | |
| 54 // The base class for generated proxy classes. | |
| 55 function ProxyBase(receiver) { | |
| 56 this[kProxyProperties] = new ProxyProperties(receiver); | |
| 57 | |
| 58 // TODO(hansmuller): Temporary, for Chrome backwards compatibility. | |
| 59 if (receiver instanceof Router) | |
| 60 this.receiver_ = receiver; | |
| 61 } | |
| 62 | |
| 63 // The base class for generated stub classes. | |
| 64 function StubBase(delegate) { | |
| 65 this[kStubProperties] = new StubProperties(delegate); | |
| 66 } | |
| 67 | |
| 68 // TODO(hansmuller): remove everything except the connection property doc | |
| 69 // after 'Client=' has been removed from Mojom. | |
| 70 | |
| 71 // Provides access to properties added to a proxy object without risking | |
| 72 // Mojo interface name collisions. Unless otherwise specified, the initial | |
| 73 // value of all properties is undefined. | |
| 74 // | |
| 75 // ProxyBindings(proxy).connection - The Connection object that links the | |
| 76 // proxy for a remote Mojo service to an optional local stub for a local | |
| 77 // service. The value of ProxyBindings(proxy).connection.remote == proxy. | |
| 78 // | |
| 79 // ProxyBindings(proxy).local - The "local" stub object whose delegate | |
| 80 // implements the proxy's Mojo client interface. | |
| 81 // | |
| 82 // ProxyBindings(proxy).setLocalDelegate(impl) - Sets the implementation | |
| 83 // delegate of the proxy's client stub object. This is just shorthand | |
| 84 // for |StubBindings(ProxyBindings(proxy).local).delegate = impl|. | |
| 85 // | |
| 86 // ProxyBindings(proxy).getLocalDelegate() - Returns the implementation | |
| 87 // delegate of the proxy's client stub object. This is just shorthand | |
| 88 // for |StubBindings(ProxyBindings(proxy).local).delegate|. | |
| 89 | |
| 90 function ProxyBindings(proxy) { | |
| 91 return (proxy instanceof ProxyBase) ? proxy[kProxyProperties] : proxy; | |
| 92 } | |
| 93 | |
| 94 // TODO(hansmuller): remove the remote doc after 'Client=' has been | |
| 95 // removed from Mojom. | |
| 96 | |
| 97 // Provides access to properties added to a stub object without risking | |
| 98 // Mojo interface name collisions. Unless otherwise specified, the initial | |
| 99 // value of all properties is undefined. | |
| 100 // | |
| 101 // StubBindings(stub).delegate - The optional implementation delegate for | |
| 102 // the Mojo interface stub. | |
| 103 // | |
| 104 // StubBindings(stub).connection - The Connection object that links an | |
| 105 // optional proxy for a remote service to this stub. The value of | |
| 106 // StubBindings(stub).connection.local == stub. | |
| 107 // | |
| 108 // StubBindings(stub).remote - A proxy for the the stub's Mojo client | |
| 109 // service. | |
| 110 | |
| 111 function StubBindings(stub) { | |
| 112 return stub instanceof StubBase ? stub[kStubProperties] : stub; | |
| 113 } | |
| 114 | |
| 115 // TODO(hansmuller): the proxy receiver_ property should be receiver$ | |
| 116 | |
| 117 function BaseConnection(localStub, remoteProxy, router) { | |
| 118 this.router_ = router; | |
| 119 this.local = localStub; | |
| 120 this.remote = remoteProxy; | |
| 121 | |
| 122 this.router_.setIncomingReceiver(localStub); | |
| 123 this.router_.setErrorHandler(function() { | |
| 124 if (StubBindings(this.local) && | |
| 125 StubBindings(this.local).connectionErrorHandler) | |
| 126 StubBindings(this.local).connectionErrorHandler(); | |
| 127 }.bind(this)); | |
| 128 if (this.remote) | |
| 129 this.remote.receiver_ = router; | |
| 130 | |
| 131 // Validate incoming messages: remote responses and local requests. | |
| 132 var validateRequest = localStub && localStub.validator; | |
| 133 var validateResponse = remoteProxy && remoteProxy.validator; | |
| 134 var payloadValidators = []; | |
| 135 if (validateRequest) | |
| 136 payloadValidators.push(validateRequest); | |
| 137 if (validateResponse) | |
| 138 payloadValidators.push(validateResponse); | |
| 139 this.router_.setPayloadValidators(payloadValidators); | |
| 140 } | |
| 141 | |
| 142 BaseConnection.prototype.close = function() { | |
| 143 this.router_.close(); | |
| 144 this.router_ = null; | |
| 145 this.local = null; | |
| 146 this.remote = null; | |
| 147 }; | |
| 148 | |
| 149 BaseConnection.prototype.encounteredError = function() { | |
| 150 return this.router_.encounteredError(); | |
| 151 }; | |
| 152 | |
| 153 function Connection( | |
| 154 handle, localFactory, remoteFactory, routerFactory, connectorFactory) { | |
| 155 var routerClass = routerFactory || Router; | |
| 156 var router = new routerClass(handle, connectorFactory); | |
| 157 var remoteProxy = remoteFactory && new remoteFactory(router); | |
| 158 var localStub = localFactory && new localFactory(remoteProxy); | |
| 159 BaseConnection.call(this, localStub, remoteProxy, router); | |
| 160 } | |
| 161 | |
| 162 Connection.prototype = Object.create(BaseConnection.prototype); | |
| 163 | |
| 164 // The TestConnection subclass is only intended to be used in unit tests. | |
| 165 function TestConnection(handle, localFactory, remoteFactory) { | |
| 166 Connection.call(this, | |
| 167 handle, | |
| 168 localFactory, | |
| 169 remoteFactory, | |
| 170 TestRouter, | |
| 171 TestConnector); | |
| 172 } | |
| 173 | |
| 174 TestConnection.prototype = Object.create(Connection.prototype); | |
| 175 | |
| 176 // Return a handle for a message pipe that's connected to a proxy | |
| 177 // for remoteInterface. Used by generated code for outgoing interface& | |
| 178 // (request) parameters: the caller is given the generated proxy via | |
| 179 // |proxyCallback(proxy)| and the generated code sends the handle | |
| 180 // returned by this function. | |
| 181 function bindProxy(proxyCallback, remoteInterface) { | |
| 182 var messagePipe = core.createMessagePipe(); | |
| 183 if (messagePipe.result != core.RESULT_OK) | |
| 184 throw new Error("createMessagePipe failed " + messagePipe.result); | |
| 185 | |
| 186 var proxy = new remoteInterface.proxyClass; | |
| 187 var router = new Router(messagePipe.handle0); | |
| 188 var connection = new BaseConnection(undefined, proxy, router); | |
| 189 ProxyBindings(proxy).connection = connection; | |
| 190 if (proxyCallback) | |
| 191 proxyCallback(proxy); | |
| 192 | |
| 193 return messagePipe.handle1; | |
| 194 } | |
| 195 | |
| 196 // Return a handle and proxy for a message pipe that's connected to a proxy | |
| 197 // for remoteInterface. Used by generated code for outgoing interface& | |
| 198 // (request) parameters | |
| 199 function getProxy(remoteInterface) { | |
| 200 var messagePipe = core.createMessagePipe(); | |
| 201 if (messagePipe.result != core.RESULT_OK) | |
| 202 throw new Error("createMessagePipe failed " + messagePipe.result); | |
| 203 | |
| 204 var proxy = new remoteInterface.proxyClass; | |
| 205 var router = new Router(messagePipe.handle0); | |
| 206 var connection = new BaseConnection(undefined, proxy, router); | |
| 207 ProxyBindings(proxy).connection = connection; | |
| 208 | |
| 209 return { | |
| 210 requestHandle: messagePipe.handle1, | |
| 211 proxy: proxy | |
| 212 }; | |
| 213 } | |
| 214 | |
| 215 // Return a handle for a message pipe that's connected to a stub for | |
| 216 // localInterface. Used by generated code for outgoing interface | |
| 217 // parameters: the caller is given the generated stub via | |
| 218 // |stubCallback(stub)| and the generated code sends the handle | |
| 219 // returned by this function. The caller is responsible for managing | |
| 220 // the lifetime of the stub and for setting it's implementation | |
| 221 // delegate with: StubBindings(stub).delegate = myImpl; | |
| 222 function bindImpl(stubCallback, localInterface) { | |
| 223 var messagePipe = core.createMessagePipe(); | |
| 224 if (messagePipe.result != core.RESULT_OK) | |
| 225 throw new Error("createMessagePipe failed " + messagePipe.result); | |
| 226 | |
| 227 var stub = new localInterface.stubClass; | |
| 228 var router = new Router(messagePipe.handle0); | |
| 229 var connection = new BaseConnection(stub, undefined, router); | |
| 230 StubBindings(stub).connection = connection; | |
| 231 if (stubCallback) | |
| 232 stubCallback(stub); | |
| 233 | |
| 234 return messagePipe.handle1; | |
| 235 } | |
| 236 | |
| 237 // Return a remoteInterface proxy for handle. Used by generated code | |
| 238 // for converting incoming interface parameters to proxies. | |
| 239 function bindHandleToProxy(handle, remoteInterface) { | |
| 240 if (!core.isHandle(handle)) | |
| 241 throw new Error("Not a handle " + handle); | |
| 242 | |
| 243 var proxy = new remoteInterface.proxyClass; | |
| 244 var router = new Router(handle); | |
| 245 var connection = new BaseConnection(undefined, proxy, router); | |
| 246 ProxyBindings(proxy).connection = connection; | |
| 247 return proxy; | |
| 248 } | |
| 249 | |
| 250 // Return a localInterface stub for handle. Used by generated code | |
| 251 // for converting incoming interface& request parameters to localInterface | |
| 252 // stubs. The caller can specify the stub's implementation of localInterface | |
| 253 // like this: StubBindings(stub).delegate = myStubImpl. | |
| 254 function bindHandleToStub(handle, localInterface) { | |
| 255 if (!core.isHandle(handle)) | |
| 256 throw new Error("Not a handle " + handle); | |
| 257 | |
| 258 var stub = new localInterface.stubClass; | |
| 259 var router = new Router(handle); | |
| 260 var connection = new BaseConnection(stub, undefined, router); | |
| 261 StubBindings(stub).connection = connection; | |
| 262 return stub; | |
| 263 } | |
| 264 | |
| 265 /** | |
| 266 * Creates a messape pipe and links one end of the pipe to the given object. | |
| 267 * @param {!Object} obj The object to create a handle for. Must be a subclass | |
| 268 * of an auto-generated stub class. | |
| 269 * @return {!MojoHandle} The other (not yet connected) end of the message | |
| 270 * pipe. | |
| 271 */ | |
| 272 function bindStubDerivedImpl(obj) { | |
| 273 var pipe = core.createMessagePipe(); | |
| 274 var router = new Router(pipe.handle0); | |
| 275 var connection = new BaseConnection(obj, undefined, router); | |
| 276 obj.connection = connection; | |
| 277 return pipe.handle1; | |
| 278 } | |
| 279 | |
| 280 var exports = {}; | |
| 281 exports.Connection = Connection; | |
| 282 exports.EmptyProxy = ProxyBase; | |
| 283 exports.EmptyStub = StubBase; | |
| 284 exports.ProxyBase = ProxyBase; | |
| 285 exports.ProxyBindings = ProxyBindings; | |
| 286 exports.StubBase = StubBase; | |
| 287 exports.StubBindings = StubBindings; | |
| 288 exports.TestConnection = TestConnection; | |
| 289 | |
| 290 exports.bindProxy = bindProxy; | |
| 291 exports.getProxy = getProxy; | |
| 292 exports.bindImpl = bindImpl; | |
| 293 exports.bindHandleToProxy = bindHandleToProxy; | |
| 294 exports.bindHandleToStub = bindHandleToStub; | |
| 295 exports.bindStubDerivedImpl = bindStubDerivedImpl; | |
| 296 return exports; | |
| 297 }); | |
| OLD | NEW |