| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 define("mojo/public/js/connection", [ | 5 define("mojo/public/js/connection", [ |
| 6 "mojo/public/js/connector", | 6 "mojo/public/js/connector", |
| 7 "mojo/public/js/core", | 7 "mojo/public/js/core", |
| 8 "mojo/public/js/router", | 8 "mojo/public/js/router", |
| 9 ], function(connector, core, routerModule) { | 9 ], function(connector, core, router) { |
| 10 | 10 |
| 11 // TODO(hansmuller): the proxy connection_ property should connection$ | 11 var Router = router.Router; |
| 12 var TestConnector = connector.TestConnector; |
| 13 var TestRouter = router.TestRouter; |
| 14 |
| 12 // TODO(hansmuller): the proxy receiver_ property should be receiver$ | 15 // TODO(hansmuller): the proxy receiver_ property should be receiver$ |
| 13 | 16 |
| 14 function BaseConnection(localStub, remoteProxy, router) { | 17 function BaseConnection(localStub, remoteProxy, router) { |
| 15 this.router_ = router; | 18 this.router_ = router; |
| 16 this.local = localStub; | 19 this.local = localStub; |
| 17 this.remote = remoteProxy; | 20 this.remote = remoteProxy; |
| 18 | 21 |
| 19 this.router_.setIncomingReceiver(localStub); | 22 this.router_.setIncomingReceiver(localStub); |
| 20 if (this.remote) | 23 if (this.remote) |
| 21 this.remote.receiver_ = router; | 24 this.remote.receiver_ = router; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 37 this.local = null; | 40 this.local = null; |
| 38 this.remote = null; | 41 this.remote = null; |
| 39 }; | 42 }; |
| 40 | 43 |
| 41 BaseConnection.prototype.encounteredError = function() { | 44 BaseConnection.prototype.encounteredError = function() { |
| 42 return this.router_.encounteredError(); | 45 return this.router_.encounteredError(); |
| 43 }; | 46 }; |
| 44 | 47 |
| 45 function Connection( | 48 function Connection( |
| 46 handle, localFactory, remoteFactory, routerFactory, connectorFactory) { | 49 handle, localFactory, remoteFactory, routerFactory, connectorFactory) { |
| 47 var routerClass = routerFactory || routerModule.Router; | 50 var routerClass = routerFactory || Router; |
| 48 var router = new routerClass(handle, connectorFactory); | 51 var router = new routerClass(handle, connectorFactory); |
| 49 var remoteProxy = remoteFactory && new remoteFactory(router); | 52 var remoteProxy = remoteFactory && new remoteFactory(router); |
| 50 var localStub = localFactory && new localFactory(remoteProxy); | 53 var localStub = localFactory && new localFactory(remoteProxy); |
| 51 BaseConnection.call(this, localStub, remoteProxy, router); | 54 BaseConnection.call(this, localStub, remoteProxy, router); |
| 52 } | 55 } |
| 53 | 56 |
| 54 Connection.prototype = Object.create(BaseConnection.prototype); | 57 Connection.prototype = Object.create(BaseConnection.prototype); |
| 55 | 58 |
| 56 // The TestConnection subclass is only intended to be used in unit tests. | 59 // The TestConnection subclass is only intended to be used in unit tests. |
| 57 | |
| 58 function TestConnection(handle, localFactory, remoteFactory) { | 60 function TestConnection(handle, localFactory, remoteFactory) { |
| 59 Connection.call(this, | 61 Connection.call(this, |
| 60 handle, | 62 handle, |
| 61 localFactory, | 63 localFactory, |
| 62 remoteFactory, | 64 remoteFactory, |
| 63 routerModule.TestRouter, | 65 TestRouter, |
| 64 connector.TestConnector); | 66 TestConnector); |
| 65 } | 67 } |
| 66 | 68 |
| 67 TestConnection.prototype = Object.create(Connection.prototype); | 69 TestConnection.prototype = Object.create(Connection.prototype); |
| 68 | 70 |
| 69 function createOpenConnection(stub, proxy) { | 71 // Called by the generated interface Proxy constructor classes. |
| 70 var messagePipe = core.createMessagePipe(); | 72 function initProxyInstance(proxy, proxyInterface, receiver) { |
| 71 // TODO(hansmuller): Check messagePipe.result. | 73 Object.defineProperty(proxy, 'local$', { |
| 72 var router = new routerModule.Router(messagePipe.handle0); | 74 get: function() { |
| 75 return proxy.connection$ && |
| 76 proxy.connection$.local && proxy.connection$.local.delegate$ |
| 77 }, |
| 78 set: function(value) { |
| 79 // TODO: what if the connection hasn't been created yet? |
| 80 if (proxy.connection$ && proxy.connection$.local) { |
| 81 proxy.connection$.local.delegate$ = value; |
| 82 value.remote$ = proxy; |
| 83 } |
| 84 } |
| 85 }); |
| 86 // TODO(hansmuller): Temporary, for Chrome backwards compatibility. |
| 87 if (receiver instanceof Router) |
| 88 proxy.receiver_ = receiver; |
| 89 } |
| 90 |
| 91 function createEmptyProxy() { |
| 92 var proxy = {}; |
| 93 initProxyInstance(proxy); |
| 94 return proxy; |
| 95 } |
| 96 |
| 97 function createOpenConnection( |
| 98 messagePipeHandle, clientImpl, localInterface, remoteInterface) { |
| 99 var stubClass = localInterface && localInterface.stubClass |
| 100 var proxyClass = remoteInterface && remoteInterface.proxyClass; |
| 101 var stub = stubClass && |
| 102 (clientImpl ? new stubClass(clientImpl) : new stubClass); |
| 103 var proxy = proxyClass ? new proxyClass : createEmptyProxy(); |
| 104 var router = new Router(messagePipeHandle); |
| 73 var connection = new BaseConnection(stub, proxy, router); | 105 var connection = new BaseConnection(stub, proxy, router); |
| 74 connection.messagePipeHandle = messagePipe.handle1; | 106 proxy.connection$ = connection; |
| 107 if (clientImpl) { |
| 108 clientImpl.connection$ = connection; |
| 109 clientImpl.remote$ = proxy; |
| 110 } |
| 75 return connection; | 111 return connection; |
| 76 } | 112 } |
| 77 | 113 |
| 78 function getProxyConnection(proxy, proxyInterface) { | 114 // Return a message pipe handle. |
| 79 if (!proxy.connection_) { | 115 function bindProxyClient(clientImpl, localInterface, remoteInterface) { |
| 80 var stub = proxyInterface.client && new proxyInterface.client.stubClass; | 116 var messagePipe = core.createMessagePipe(); |
| 81 proxy.connection_ = createOpenConnection(stub, proxy); | 117 if (messagePipe.result != core.RESULT_OK) |
| 82 } | 118 throw new Error("createMessagePipe failed " + messagePipe.result); |
| 83 return proxy.connection_; | 119 |
| 120 createOpenConnection( |
| 121 messagePipe.handle0, clientImpl, localInterface, remoteInterface); |
| 122 return messagePipe.handle1; |
| 84 } | 123 } |
| 85 | 124 |
| 86 function getStubConnection(stub, proxyInterface) { | 125 // Return a proxy. |
| 87 if (!stub.connection_) { | 126 function bindProxyHandle(proxyHandle, localInterface, remoteInterface) { |
| 88 var proxy = proxyInterface.client && new proxyInterface.client.proxyClass; | 127 if (!core.isHandle(proxyHandle)) |
| 89 stub.connection_ = createOpenConnection(stub, proxy); | 128 throw new Error("Not a handle " + proxyHandle); |
| 90 } | |
| 91 return stub.connection_; | |
| 92 } | |
| 93 | 129 |
| 94 function initProxyInstance(proxy, proxyInterface, receiver) { | 130 var connection = createOpenConnection( |
| 95 if (proxyInterface.client) { | 131 proxyHandle, undefined, localInterface, remoteInterface); |
| 96 Object.defineProperty(proxy, 'client$', { | 132 return connection.remote; |
| 97 get: function() { | |
| 98 var connection = getProxyConnection(proxy, proxyInterface); | |
| 99 return connection.local && connection.local.delegate$ | |
| 100 }, | |
| 101 set: function(value) { | |
| 102 var connection = getProxyConnection(proxy, proxyInterface); | |
| 103 if (connection.local) | |
| 104 connection.local.delegate$ = value; | |
| 105 } | |
| 106 }); | |
| 107 } | |
| 108 if (receiver instanceof routerModule.Router) { | |
| 109 // TODO(hansmuller): Temporary, for Chrome backwards compatibility. | |
| 110 proxy.receiver_ = receiver; | |
| 111 } else if (receiver) { | |
| 112 var router = new routerModule.Router(receiver); | |
| 113 var stub = proxyInterface.client && new proxyInterface.client.stubClass; | |
| 114 proxy.connection_ = new BaseConnection(stub, proxy, router); | |
| 115 proxy.connection_.messagePipeHandle = receiver; | |
| 116 } | |
| 117 } | 133 } |
| 118 | 134 |
| 119 var exports = {}; | 135 var exports = {}; |
| 120 exports.Connection = Connection; | 136 exports.Connection = Connection; |
| 121 exports.TestConnection = TestConnection; | 137 exports.TestConnection = TestConnection; |
| 122 exports.getProxyConnection = getProxyConnection; | 138 exports.bindProxyHandle = bindProxyHandle; |
| 123 exports.getStubConnection = getStubConnection; | 139 exports.bindProxyClient = bindProxyClient; |
| 124 exports.initProxyInstance = initProxyInstance; | 140 exports.initProxyInstance = initProxyInstance; |
| 125 return exports; | 141 return exports; |
| 126 }); | 142 }); |
| OLD | NEW |