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/bindings", |
6 "mojo/public/js/connector", | 7 "mojo/public/js/connector", |
7 "mojo/public/js/core", | 8 "mojo/public/js/core", |
8 "mojo/public/js/router", | 9 "mojo/public/js/router", |
9 ], function(connector, core, router) { | 10 ], function(bindings, connector, core, router) { |
10 | 11 |
11 var Router = router.Router; | 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; |
12 var TestConnector = connector.TestConnector; | 17 var TestConnector = connector.TestConnector; |
13 var TestRouter = router.TestRouter; | 18 var TestRouter = router.TestRouter; |
14 | 19 |
15 // TODO(hansmuller): the proxy receiver_ property should be receiver$ | 20 // TODO(hansmuller): the proxy receiver_ property should be receiver$ |
16 | 21 |
17 function BaseConnection(localStub, remoteProxy, router) { | 22 function BaseConnection(localStub, remoteProxy, router) { |
18 this.router_ = router; | 23 this.router_ = router; |
19 this.local = localStub; | 24 this.local = localStub; |
20 this.remote = remoteProxy; | 25 this.remote = remoteProxy; |
21 | 26 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 Connection.call(this, | 66 Connection.call(this, |
62 handle, | 67 handle, |
63 localFactory, | 68 localFactory, |
64 remoteFactory, | 69 remoteFactory, |
65 TestRouter, | 70 TestRouter, |
66 TestConnector); | 71 TestConnector); |
67 } | 72 } |
68 | 73 |
69 TestConnection.prototype = Object.create(Connection.prototype); | 74 TestConnection.prototype = Object.create(Connection.prototype); |
70 | 75 |
71 // Called by the generated interface Proxy constructor classes. | |
72 function initProxyInstance(proxy, proxyInterface, receiver) { | |
73 Object.defineProperty(proxy, 'local$', { | |
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( | 76 function createOpenConnection( |
98 messagePipeHandle, clientImpl, localInterface, remoteInterface) { | 77 messagePipeHandle, client, localInterface, remoteInterface) { |
99 var stubClass = localInterface && localInterface.stubClass | 78 var stubClass = (localInterface && localInterface.stubClass) || EmptyStub; |
100 var proxyClass = remoteInterface && remoteInterface.proxyClass; | 79 var proxyClass = |
101 var stub = stubClass && | 80 (remoteInterface && remoteInterface.proxyClass) || EmptyProxy; |
102 (clientImpl ? new stubClass(clientImpl) : new stubClass); | 81 var proxy = new proxyClass; |
103 var proxy = proxyClass ? new proxyClass : createEmptyProxy(); | 82 var stub = new stubClass; |
104 var router = new Router(messagePipeHandle); | 83 var router = new Router(messagePipeHandle); |
105 var connection = new BaseConnection(stub, proxy, router); | 84 var connection = new BaseConnection(stub, proxy, router); |
106 proxy.connection$ = connection; | 85 |
107 if (clientImpl) { | 86 ProxyBindings(proxy).connection = connection; |
108 clientImpl.connection$ = connection; | 87 ProxyBindings(proxy).local = connection.local; |
109 clientImpl.remote$ = proxy; | 88 StubBindings(stub).connection = connection; |
110 } | 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 |
111 return connection; | 95 return connection; |
112 } | 96 } |
113 | 97 |
114 // Return a message pipe handle. | 98 // Return a message pipe handle. |
115 function bindProxyClient(clientImpl, localInterface, remoteInterface) { | 99 function bindProxyClient(clientImpl, localInterface, remoteInterface) { |
116 var messagePipe = core.createMessagePipe(); | 100 var messagePipe = core.createMessagePipe(); |
117 if (messagePipe.result != core.RESULT_OK) | 101 if (messagePipe.result != core.RESULT_OK) |
118 throw new Error("createMessagePipe failed " + messagePipe.result); | 102 throw new Error("createMessagePipe failed " + messagePipe.result); |
119 | 103 |
120 createOpenConnection( | 104 createOpenConnection( |
121 messagePipe.handle0, clientImpl, localInterface, remoteInterface); | 105 messagePipe.handle0, clientImpl, localInterface, remoteInterface); |
122 return messagePipe.handle1; | 106 return messagePipe.handle1; |
123 } | 107 } |
124 | 108 |
125 // Return a proxy. | 109 // Return a proxy. |
126 function bindProxyHandle(proxyHandle, localInterface, remoteInterface) { | 110 function bindProxyHandle(proxyHandle, localInterface, remoteInterface) { |
127 if (!core.isHandle(proxyHandle)) | 111 if (!core.isHandle(proxyHandle)) |
128 throw new Error("Not a handle " + proxyHandle); | 112 throw new Error("Not a handle " + proxyHandle); |
129 | 113 |
130 var connection = createOpenConnection( | 114 var connection = createOpenConnection( |
131 proxyHandle, undefined, localInterface, remoteInterface); | 115 proxyHandle, undefined, localInterface, remoteInterface); |
132 return connection.remote; | 116 return connection.remote; |
133 } | 117 } |
134 | 118 |
135 var exports = {}; | 119 var exports = {}; |
136 exports.Connection = Connection; | 120 exports.Connection = Connection; |
137 exports.TestConnection = TestConnection; | 121 exports.TestConnection = TestConnection; |
138 exports.bindProxyHandle = bindProxyHandle; | 122 exports.bindProxyHandle = bindProxyHandle; |
139 exports.bindProxyClient = bindProxyClient; | 123 exports.bindProxyClient = bindProxyClient; |
140 exports.initProxyInstance = initProxyInstance; | |
141 return exports; | 124 return exports; |
142 }); | 125 }); |
OLD | NEW |