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/bindings", [ | 5 define("mojo/public/js/bindings", [ |
6 "mojo/public/js/core", | 6 "mojo/public/js/core", |
7 "mojo/public/js/lib/control_message_proxy", | |
8 "mojo/public/js/interface_types", | 7 "mojo/public/js/interface_types", |
8 "mojo/public/js/lib/interface_endpoint_client", | |
9 "mojo/public/js/router", | 9 "mojo/public/js/router", |
10 ], function(core, controlMessageProxy, types, router) { | 10 ], function(core, types, interfaceEndpointClient, router) { |
11 | |
12 var InterfaceEndpointClient = interfaceEndpointClient.InterfaceEndpointClient; | |
11 | 13 |
12 // --------------------------------------------------------------------------- | 14 // --------------------------------------------------------------------------- |
13 | 15 |
14 function makeRequest(interfacePtr) { | 16 function makeRequest(interfacePtr) { |
15 var pipe = core.createMessagePipe(); | 17 var pipe = core.createMessagePipe(); |
16 interfacePtr.ptr.bind(new types.InterfacePtrInfo(pipe.handle0, 0)); | 18 interfacePtr.ptr.bind(new types.InterfacePtrInfo(pipe.handle0, 0)); |
17 return new types.InterfaceRequest(pipe.handle1); | 19 return new types.InterfaceRequest(pipe.handle1); |
18 } | 20 } |
19 | 21 |
20 // --------------------------------------------------------------------------- | 22 // --------------------------------------------------------------------------- |
21 | 23 |
22 // Operations used to setup/configure an interface pointer. Exposed as the | 24 // Operations used to setup/configure an interface pointer. Exposed as the |
23 // |ptr| field of generated interface pointer classes. | 25 // |ptr| field of generated interface pointer classes. |
24 // |ptrInfoOrHandle| could be omitted and passed into bind() later. | 26 // |ptrInfoOrHandle| could be omitted and passed into bind() later. |
25 function InterfacePtrController(interfaceType, ptrInfoOrHandle) { | 27 function InterfacePtrController(interfaceType, ptrInfoOrHandle) { |
26 this.version = 0; | 28 this.version = 0; |
27 | 29 |
28 this.interfaceType_ = interfaceType; | 30 this.interfaceType_ = interfaceType; |
29 this.router_ = null; | 31 this.router_ = null; |
30 this.proxy_ = null; | 32 this.proxy_ = null; |
31 | 33 |
32 // |router_| is lazily initialized. |handle_| is valid between bind() and | 34 // |router_| is lazily initialized. |handle_| is valid between bind() and |
33 // the initialization of |router_|. | 35 // the initialization of |router_|. |
34 this.handle_ = null; | 36 this.handle_ = null; |
35 this.controlMessageProxy_ = null; | 37 this.controlMessageProxy_ = null; |
yzshen1
2017/03/24 21:20:12
Please update this list to include all members and
wangjimmy
2017/03/27 16:51:27
Done.
| |
36 | 38 |
37 if (ptrInfoOrHandle) | 39 if (ptrInfoOrHandle) |
38 this.bind(ptrInfoOrHandle); | 40 this.bind(ptrInfoOrHandle); |
39 } | 41 } |
40 | 42 |
41 InterfacePtrController.prototype.bind = function(ptrInfoOrHandle) { | 43 InterfacePtrController.prototype.bind = function(ptrInfoOrHandle) { |
42 this.reset(); | 44 this.reset(); |
43 | 45 |
44 if (ptrInfoOrHandle instanceof types.InterfacePtrInfo) { | 46 if (ptrInfoOrHandle instanceof types.InterfacePtrInfo) { |
45 this.version = ptrInfoOrHandle.version; | 47 this.version = ptrInfoOrHandle.version; |
(...skipping 14 matching lines...) Expand all Loading... | |
60 if (this.router_) { | 62 if (this.router_) { |
61 this.router_.close(); | 63 this.router_.close(); |
62 this.router_ = null; | 64 this.router_ = null; |
63 | 65 |
64 this.proxy_ = null; | 66 this.proxy_ = null; |
65 } | 67 } |
66 if (this.handle_) { | 68 if (this.handle_) { |
67 core.close(this.handle_); | 69 core.close(this.handle_); |
68 this.handle_ = null; | 70 this.handle_ = null; |
69 } | 71 } |
72 if (this.interfaceEndpointClient_) { | |
yzshen1
2017/03/24 21:20:12
It seems nice to move this block above line 62. Al
wangjimmy
2017/03/27 16:51:27
Done.
| |
73 this.interfaceEndpointClient_.passHandle(); | |
yzshen1
2017/03/24 21:20:12
Here we need to close the handle. You could use cl
wangjimmy
2017/03/27 16:51:27
Done.
| |
74 this.interfaceEndpointClient_ = null; | |
75 } | |
70 }; | 76 }; |
71 | 77 |
72 InterfacePtrController.prototype.setConnectionErrorHandler | 78 InterfacePtrController.prototype.resetWithReason = function(reason) { |
73 = function(callback) { | 79 this.configureProxyIfNecessary_(); |
80 this.interfaceEndpointClient_.closeWithReason(reason); | |
81 this.interfaceEndpointClient_ = null; | |
82 this.reset(); | |
83 }; | |
84 | |
85 InterfacePtrController.prototype.setConnectionErrorHandler = function( | |
86 callback) { | |
74 if (!this.isBound()) | 87 if (!this.isBound()) |
75 throw new Error("Cannot set connection error handler if not bound."); | 88 throw new Error("Cannot set connection error handler if not bound."); |
76 | 89 |
77 this.configureProxyIfNecessary_(); | 90 this.configureProxyIfNecessary_(); |
78 this.router_.setErrorHandler(callback); | 91 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); |
79 }; | 92 }; |
80 | 93 |
81 InterfacePtrController.prototype.passInterface = function() { | 94 InterfacePtrController.prototype.passInterface = function() { |
82 var result; | 95 var result; |
83 if (this.router_) { | 96 if (this.router_) { |
84 // TODO(yzshen): Fix Router interface to support extracting handle. | 97 // TODO(yzshen): Fix Router interface to support extracting handle. |
85 result = new types.InterfacePtrInfo( | 98 result = new types.InterfacePtrInfo( |
86 this.router_.connector_.handle_, this.version); | 99 this.router_.connector_.handle_, this.version); |
87 this.router_.connector_.handle_ = null; | 100 this.router_.connector_.handle_ = null; |
88 } else { | 101 } else { |
89 // This also handles the case when this object is not bound. | 102 // This also handles the case when this object is not bound. |
90 result = new types.InterfacePtrInfo(this.handle_, this.version); | 103 result = new types.InterfacePtrInfo(this.handle_, this.version); |
91 this.handle_ = null; | 104 this.handle_ = null; |
92 } | 105 } |
93 | 106 |
94 this.reset(); | 107 this.reset(); |
95 return result; | 108 return result; |
96 }; | 109 }; |
97 | 110 |
98 InterfacePtrController.prototype.getProxy = function() { | 111 InterfacePtrController.prototype.getProxy = function() { |
99 this.configureProxyIfNecessary_(); | 112 this.configureProxyIfNecessary_(); |
100 return this.proxy_; | 113 return this.proxy_; |
101 }; | 114 }; |
102 | 115 |
103 InterfacePtrController.prototype.enableTestingMode = function() { | 116 InterfacePtrController.prototype.waitForNextMessageForTesting = function() { |
104 this.configureProxyIfNecessary_(); | 117 this.configureProxyIfNecessary_(); |
105 return this.router_.enableTestingMode(); | 118 this.router_.waitForNextMessageForTesting(); |
106 }; | 119 }; |
107 | 120 |
108 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { | 121 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { |
109 if (!this.handle_) | 122 if (!this.handle_) |
110 return; | 123 return; |
111 | 124 |
112 this.router_ = new router.Router(this.handle_); | 125 this.router_ = new router.Router(this.handle_); |
113 this.handle_ = null; | 126 this.handle_ = null; |
114 this.router_ .setPayloadValidators([this.interfaceType_.validateResponse]); | |
115 | 127 |
116 this.controlMessageProxy_ = new | 128 this.interfaceEndpointClient_ = new InterfaceEndpointClient( |
117 controlMessageProxy.ControlMessageProxy(this.router_); | 129 this.router_.createLocalEndpointHandle(types.kMasterInterfaceId), |
130 this.router_); | |
118 | 131 |
119 this.proxy_ = new this.interfaceType_.proxyClass(this.router_); | 132 this.interfaceEndpointClient_ .setPayloadValidators([ |
133 this.interfaceType_.validateResponse]); | |
134 this.proxy_ = new this.interfaceType_.proxyClass( | |
135 this.interfaceEndpointClient_); | |
120 }; | 136 }; |
121 | 137 |
122 InterfacePtrController.prototype.queryVersion = function() { | 138 InterfacePtrController.prototype.queryVersion = function() { |
123 function onQueryVersion(version) { | 139 function onQueryVersion(version) { |
124 this.version = version; | 140 this.version = version; |
125 return version; | 141 return version; |
126 } | 142 } |
127 | 143 |
128 this.configureProxyIfNecessary_(); | 144 this.configureProxyIfNecessary_(); |
129 return this.controlMessageProxy_.queryVersion().then( | 145 return this.interfaceEndpointClient_.queryVersion().then( |
130 onQueryVersion.bind(this)); | 146 onQueryVersion.bind(this)); |
131 }; | 147 }; |
132 | 148 |
133 InterfacePtrController.prototype.requireVersion = function(version) { | 149 InterfacePtrController.prototype.requireVersion = function(version) { |
134 this.configureProxyIfNecessary_(); | 150 this.configureProxyIfNecessary_(); |
135 | 151 |
136 if (this.version >= version) { | 152 if (this.version >= version) { |
137 return; | 153 return; |
138 } | 154 } |
139 this.version = version; | 155 this.version = version; |
140 this.controlMessageProxy_.requireVersion(version); | 156 this.interfaceEndpointClient_.requireVersion(version); |
141 }; | 157 }; |
142 | 158 |
143 // --------------------------------------------------------------------------- | 159 // --------------------------------------------------------------------------- |
144 | 160 |
145 // |request| could be omitted and passed into bind() later. | 161 // |request| could be omitted and passed into bind() later. |
146 // | 162 // |
147 // Example: | 163 // Example: |
148 // | 164 // |
149 // // FooImpl implements mojom.Foo. | 165 // // FooImpl implements mojom.Foo. |
150 // function FooImpl() { ... } | 166 // function FooImpl() { ... } |
151 // FooImpl.prototype.fooMethod1 = function() { ... } | 167 // FooImpl.prototype.fooMethod1 = function() { ... } |
152 // FooImpl.prototype.fooMethod2 = function() { ... } | 168 // FooImpl.prototype.fooMethod2 = function() { ... } |
153 // | 169 // |
154 // var fooPtr = new mojom.FooPtr(); | 170 // var fooPtr = new mojom.FooPtr(); |
155 // var request = makeRequest(fooPtr); | 171 // var request = makeRequest(fooPtr); |
156 // var binding = new Binding(mojom.Foo, new FooImpl(), request); | 172 // var binding = new Binding(mojom.Foo, new FooImpl(), request); |
157 // fooPtr.fooMethod1(); | 173 // fooPtr.fooMethod1(); |
158 function Binding(interfaceType, impl, requestOrHandle) { | 174 function Binding(interfaceType, impl, requestOrHandle) { |
159 this.interfaceType_ = interfaceType; | 175 this.interfaceType_ = interfaceType; |
160 this.impl_ = impl; | 176 this.impl_ = impl; |
161 this.router_ = null; | 177 this.router_ = null; |
162 this.stub_ = null; | 178 this.stub_ = null; |
yzshen1
2017/03/24 21:20:12
Please update this list.
wangjimmy
2017/03/27 16:51:27
Done.
| |
163 | 179 |
164 if (requestOrHandle) | 180 if (requestOrHandle) |
165 this.bind(requestOrHandle); | 181 this.bind(requestOrHandle); |
166 } | 182 } |
167 | 183 |
168 Binding.prototype.isBound = function() { | 184 Binding.prototype.isBound = function() { |
169 return this.router_ !== null; | 185 return this.router_ !== null; |
170 }; | 186 }; |
171 | 187 |
172 Binding.prototype.createInterfacePtrAndBind = function() { | 188 Binding.prototype.createInterfacePtrAndBind = function() { |
173 var ptr = new this.interfaceType_.ptrClass(); | 189 var ptr = new this.interfaceType_.ptrClass(); |
174 // TODO(yzshen): Set the version of the interface pointer. | 190 // TODO(yzshen): Set the version of the interface pointer. |
175 this.bind(makeRequest(ptr)); | 191 this.bind(makeRequest(ptr)); |
176 return ptr; | 192 return ptr; |
177 } | 193 }; |
178 | 194 |
179 Binding.prototype.bind = function(requestOrHandle) { | 195 Binding.prototype.bind = function(requestOrHandle) { |
180 this.close(); | 196 this.close(); |
181 | 197 |
182 var handle = requestOrHandle instanceof types.InterfaceRequest ? | 198 var handle = requestOrHandle instanceof types.InterfaceRequest ? |
183 requestOrHandle.handle : requestOrHandle; | 199 requestOrHandle.handle : requestOrHandle; |
184 if (!core.isHandle(handle)) | 200 if (!core.isHandle(handle)) |
185 return; | 201 return; |
186 | 202 |
203 this.router_ = new router.Router(handle); | |
204 | |
187 this.stub_ = new this.interfaceType_.stubClass(this.impl_); | 205 this.stub_ = new this.interfaceType_.stubClass(this.impl_); |
188 this.router_ = new router.Router(handle, this.interfaceType_.kVersion); | 206 this.interfaceEndpointClient_ = new InterfaceEndpointClient( |
189 this.router_.setIncomingReceiver(this.stub_); | 207 this.router_.createLocalEndpointHandle(types.kMasterInterfaceId), |
190 this.router_ .setPayloadValidators([this.interfaceType_.validateRequest]); | 208 this.router_, this.interfaceType_.kVersion); |
209 this.interfaceEndpointClient_.setIncomingReceiver(this.stub_); | |
210 this.interfaceEndpointClient_ .setPayloadValidators([ | |
211 this.interfaceType_.validateRequest]); | |
191 }; | 212 }; |
192 | 213 |
193 Binding.prototype.close = function() { | 214 Binding.prototype.close = function() { |
194 if (!this.isBound()) | 215 if (!this.isBound()) |
195 return; | 216 return; |
196 | 217 |
218 if (this.interfaceEndpointClient_) { | |
219 this.interfaceEndpointClient_.passHandle(); | |
yzshen1
2017/03/24 21:20:12
The handle needs to be closed. Please see my comme
wangjimmy
2017/03/27 16:51:27
Done.
| |
220 this.interfaceEndpointClient_ = null; | |
221 } | |
222 | |
197 this.router_.close(); | 223 this.router_.close(); |
198 this.router_ = null; | 224 this.router_ = null; |
199 this.stub_ = null; | 225 this.stub_ = null; |
200 }; | 226 }; |
201 | 227 |
228 Binding.prototype.closeWithReason = function(reason) { | |
229 if (this.interfaceEndpointClient_) { | |
230 this.interfaceEndpointClient_.closeWithReason(reason); | |
231 this.interfaceEndpointClient_ = null; | |
232 } | |
233 this.close(); | |
234 }; | |
235 | |
202 Binding.prototype.setConnectionErrorHandler | 236 Binding.prototype.setConnectionErrorHandler |
203 = function(callback) { | 237 = function(callback) { |
204 if (!this.isBound()) | 238 if (!this.isBound()) { |
205 throw new Error("Cannot set connection error handler if not bound."); | 239 throw new Error("Cannot set connection error handler if not bound."); |
206 this.router_.setErrorHandler(callback); | 240 } |
241 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); | |
207 }; | 242 }; |
208 | 243 |
209 Binding.prototype.unbind = function() { | 244 Binding.prototype.unbind = function() { |
210 if (!this.isBound()) | 245 if (!this.isBound()) |
211 return new types.InterfaceRequest(null); | 246 return new types.InterfaceRequest(null); |
212 | 247 |
213 var result = new types.InterfaceRequest(this.router_.connector_.handle_); | 248 var result = new types.InterfaceRequest(this.router_.connector_.handle_); |
214 this.router_.connector_.handle_ = null; | 249 this.router_.connector_.handle_ = null; |
215 this.close(); | 250 this.close(); |
216 return result; | 251 return result; |
217 }; | 252 }; |
218 | 253 |
219 Binding.prototype.enableTestingMode = function() { | 254 Binding.prototype.waitForNextMessageForTesting = function() { |
220 return this.router_.enableTestingMode(); | 255 this.router_.waitForNextMessageForTesting(); |
221 }; | 256 }; |
222 | 257 |
223 // --------------------------------------------------------------------------- | 258 // --------------------------------------------------------------------------- |
224 | 259 |
225 function BindingSetEntry(bindingSet, interfaceType, impl, requestOrHandle, | 260 function BindingSetEntry(bindingSet, interfaceType, impl, requestOrHandle, |
226 bindingId) { | 261 bindingId) { |
227 this.bindingSet_ = bindingSet; | 262 this.bindingSet_ = bindingSet; |
228 this.bindingId_ = bindingId; | 263 this.bindingId_ = bindingId; |
229 this.binding_ = new Binding(interfaceType, impl, requestOrHandle); | 264 this.binding_ = new Binding(interfaceType, impl, requestOrHandle); |
230 | 265 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 var exports = {}; | 311 var exports = {}; |
277 exports.InterfacePtrInfo = types.InterfacePtrInfo; | 312 exports.InterfacePtrInfo = types.InterfacePtrInfo; |
278 exports.InterfaceRequest = types.InterfaceRequest; | 313 exports.InterfaceRequest = types.InterfaceRequest; |
279 exports.makeRequest = makeRequest; | 314 exports.makeRequest = makeRequest; |
280 exports.InterfacePtrController = InterfacePtrController; | 315 exports.InterfacePtrController = InterfacePtrController; |
281 exports.Binding = Binding; | 316 exports.Binding = Binding; |
282 exports.BindingSet = BindingSet; | 317 exports.BindingSet = BindingSet; |
283 | 318 |
284 return exports; | 319 return exports; |
285 }); | 320 }); |
OLD | NEW |