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; |
32 this.interfaceEndpointClient_ = null; | |
30 this.proxy_ = null; | 33 this.proxy_ = null; |
31 | 34 |
32 // |router_| is lazily initialized. |handle_| is valid between bind() and | 35 // |router_| and |interfaceEndpointClient_| are lazily initialized. |
33 // the initialization of |router_|. | 36 // |handle_| is valid between bind() and |
37 // the initialization of |router_| and |interfaceEndpointClient_|. | |
34 this.handle_ = null; | 38 this.handle_ = null; |
35 this.controlMessageProxy_ = null; | 39 this.controlMessageProxy_ = null; |
yzshen1
2017/03/28 00:45:59
I think this one is not used anymore?
wangjimmy
2017/03/29 17:01:18
Done.
| |
36 | 40 |
37 if (ptrInfoOrHandle) | 41 if (ptrInfoOrHandle) |
38 this.bind(ptrInfoOrHandle); | 42 this.bind(ptrInfoOrHandle); |
39 } | 43 } |
40 | 44 |
41 InterfacePtrController.prototype.bind = function(ptrInfoOrHandle) { | 45 InterfacePtrController.prototype.bind = function(ptrInfoOrHandle) { |
42 this.reset(); | 46 this.reset(); |
43 | 47 |
44 if (ptrInfoOrHandle instanceof types.InterfacePtrInfo) { | 48 if (ptrInfoOrHandle instanceof types.InterfacePtrInfo) { |
45 this.version = ptrInfoOrHandle.version; | 49 this.version = ptrInfoOrHandle.version; |
46 this.handle_ = ptrInfoOrHandle.handle; | 50 this.handle_ = ptrInfoOrHandle.handle; |
47 } else { | 51 } else { |
48 this.handle_ = ptrInfoOrHandle; | 52 this.handle_ = ptrInfoOrHandle; |
49 } | 53 } |
50 }; | 54 }; |
51 | 55 |
52 InterfacePtrController.prototype.isBound = function() { | 56 InterfacePtrController.prototype.isBound = function() { |
53 return this.router_ !== null || this.handle_ !== null; | 57 return this.router_ !== null || this.handle_ !== null; |
54 }; | 58 }; |
55 | 59 |
56 // Although users could just discard the object, reset() closes the pipe | 60 // Although users could just discard the object, reset() closes the pipe |
57 // immediately. | 61 // immediately. |
58 InterfacePtrController.prototype.reset = function() { | 62 InterfacePtrController.prototype.reset = function() { |
59 this.version = 0; | 63 this.version = 0; |
64 if (this.interfaceEndpointClient_) { | |
65 this.interfaceEndpointClient_.closeWithReason(); | |
yzshen1
2017/03/28 00:45:59
How about we rename the method to close(), which c
wangjimmy
2017/03/29 17:01:18
Done. I agreed. Renamed to close
| |
66 this.interfaceEndpointClient_ = null; | |
67 } | |
60 if (this.router_) { | 68 if (this.router_) { |
61 this.router_.close(); | 69 this.router_.close(); |
62 this.router_ = null; | 70 this.router_ = null; |
63 | 71 |
64 this.proxy_ = null; | 72 this.proxy_ = null; |
65 } | 73 } |
66 if (this.handle_) { | 74 if (this.handle_) { |
67 core.close(this.handle_); | 75 core.close(this.handle_); |
68 this.handle_ = null; | 76 this.handle_ = null; |
69 } | 77 } |
70 }; | 78 }; |
71 | 79 |
72 InterfacePtrController.prototype.setConnectionErrorHandler | 80 InterfacePtrController.prototype.resetWithReason = function(reason) { |
73 = function(callback) { | 81 this.configureProxyIfNecessary_(); |
82 this.interfaceEndpointClient_.closeWithReason(reason); | |
83 this.interfaceEndpointClient_ = null; | |
84 this.reset(); | |
85 }; | |
86 | |
87 InterfacePtrController.prototype.setConnectionErrorHandler = function( | |
88 callback) { | |
74 if (!this.isBound()) | 89 if (!this.isBound()) |
75 throw new Error("Cannot set connection error handler if not bound."); | 90 throw new Error("Cannot set connection error handler if not bound."); |
76 | 91 |
77 this.configureProxyIfNecessary_(); | 92 this.configureProxyIfNecessary_(); |
78 this.router_.setErrorHandler(callback); | 93 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); |
79 }; | 94 }; |
80 | 95 |
81 InterfacePtrController.prototype.passInterface = function() { | 96 InterfacePtrController.prototype.passInterface = function() { |
82 var result; | 97 var result; |
83 if (this.router_) { | 98 if (this.router_) { |
84 // TODO(yzshen): Fix Router interface to support extracting handle. | 99 // TODO(yzshen): Fix Router interface to support extracting handle. |
85 result = new types.InterfacePtrInfo( | 100 result = new types.InterfacePtrInfo( |
86 this.router_.connector_.handle_, this.version); | 101 this.router_.connector_.handle_, this.version); |
87 this.router_.connector_.handle_ = null; | 102 this.router_.connector_.handle_ = null; |
88 } else { | 103 } else { |
89 // This also handles the case when this object is not bound. | 104 // This also handles the case when this object is not bound. |
90 result = new types.InterfacePtrInfo(this.handle_, this.version); | 105 result = new types.InterfacePtrInfo(this.handle_, this.version); |
91 this.handle_ = null; | 106 this.handle_ = null; |
92 } | 107 } |
93 | 108 |
94 this.reset(); | 109 this.reset(); |
95 return result; | 110 return result; |
96 }; | 111 }; |
97 | 112 |
98 InterfacePtrController.prototype.getProxy = function() { | 113 InterfacePtrController.prototype.getProxy = function() { |
99 this.configureProxyIfNecessary_(); | 114 this.configureProxyIfNecessary_(); |
100 return this.proxy_; | 115 return this.proxy_; |
101 }; | 116 }; |
102 | 117 |
103 InterfacePtrController.prototype.enableTestingMode = function() { | 118 InterfacePtrController.prototype.waitForNextMessageForTesting = function() { |
104 this.configureProxyIfNecessary_(); | 119 this.configureProxyIfNecessary_(); |
105 return this.router_.enableTestingMode(); | 120 this.router_.waitForNextMessageForTesting(); |
106 }; | 121 }; |
107 | 122 |
108 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { | 123 InterfacePtrController.prototype.configureProxyIfNecessary_ = function() { |
109 if (!this.handle_) | 124 if (!this.handle_) |
110 return; | 125 return; |
111 | 126 |
112 this.router_ = new router.Router(this.handle_); | 127 this.router_ = new router.Router(this.handle_); |
113 this.handle_ = null; | 128 this.handle_ = null; |
114 this.router_ .setPayloadValidators([this.interfaceType_.validateResponse]); | |
115 | 129 |
116 this.controlMessageProxy_ = new | 130 this.interfaceEndpointClient_ = new InterfaceEndpointClient( |
117 controlMessageProxy.ControlMessageProxy(this.router_); | 131 this.router_.createLocalEndpointHandle(types.kMasterInterfaceId), |
132 this.router_); | |
118 | 133 |
119 this.proxy_ = new this.interfaceType_.proxyClass(this.router_); | 134 this.interfaceEndpointClient_ .setPayloadValidators([ |
135 this.interfaceType_.validateResponse]); | |
136 this.proxy_ = new this.interfaceType_.proxyClass( | |
137 this.interfaceEndpointClient_); | |
120 }; | 138 }; |
121 | 139 |
122 InterfacePtrController.prototype.queryVersion = function() { | 140 InterfacePtrController.prototype.queryVersion = function() { |
123 function onQueryVersion(version) { | 141 function onQueryVersion(version) { |
124 this.version = version; | 142 this.version = version; |
125 return version; | 143 return version; |
126 } | 144 } |
127 | 145 |
128 this.configureProxyIfNecessary_(); | 146 this.configureProxyIfNecessary_(); |
129 return this.controlMessageProxy_.queryVersion().then( | 147 return this.interfaceEndpointClient_.queryVersion().then( |
130 onQueryVersion.bind(this)); | 148 onQueryVersion.bind(this)); |
131 }; | 149 }; |
132 | 150 |
133 InterfacePtrController.prototype.requireVersion = function(version) { | 151 InterfacePtrController.prototype.requireVersion = function(version) { |
134 this.configureProxyIfNecessary_(); | 152 this.configureProxyIfNecessary_(); |
135 | 153 |
136 if (this.version >= version) { | 154 if (this.version >= version) { |
137 return; | 155 return; |
138 } | 156 } |
139 this.version = version; | 157 this.version = version; |
140 this.controlMessageProxy_.requireVersion(version); | 158 this.interfaceEndpointClient_.requireVersion(version); |
141 }; | 159 }; |
142 | 160 |
143 // --------------------------------------------------------------------------- | 161 // --------------------------------------------------------------------------- |
144 | 162 |
145 // |request| could be omitted and passed into bind() later. | 163 // |request| could be omitted and passed into bind() later. |
146 // | 164 // |
147 // Example: | 165 // Example: |
148 // | 166 // |
149 // // FooImpl implements mojom.Foo. | 167 // // FooImpl implements mojom.Foo. |
150 // function FooImpl() { ... } | 168 // function FooImpl() { ... } |
151 // FooImpl.prototype.fooMethod1 = function() { ... } | 169 // FooImpl.prototype.fooMethod1 = function() { ... } |
152 // FooImpl.prototype.fooMethod2 = function() { ... } | 170 // FooImpl.prototype.fooMethod2 = function() { ... } |
153 // | 171 // |
154 // var fooPtr = new mojom.FooPtr(); | 172 // var fooPtr = new mojom.FooPtr(); |
155 // var request = makeRequest(fooPtr); | 173 // var request = makeRequest(fooPtr); |
156 // var binding = new Binding(mojom.Foo, new FooImpl(), request); | 174 // var binding = new Binding(mojom.Foo, new FooImpl(), request); |
157 // fooPtr.fooMethod1(); | 175 // fooPtr.fooMethod1(); |
158 function Binding(interfaceType, impl, requestOrHandle) { | 176 function Binding(interfaceType, impl, requestOrHandle) { |
159 this.interfaceType_ = interfaceType; | 177 this.interfaceType_ = interfaceType; |
160 this.impl_ = impl; | 178 this.impl_ = impl; |
161 this.router_ = null; | 179 this.router_ = null; |
180 this.interfaceEndpointClient_ = null; | |
162 this.stub_ = null; | 181 this.stub_ = null; |
163 | 182 |
164 if (requestOrHandle) | 183 if (requestOrHandle) |
165 this.bind(requestOrHandle); | 184 this.bind(requestOrHandle); |
166 } | 185 } |
167 | 186 |
168 Binding.prototype.isBound = function() { | 187 Binding.prototype.isBound = function() { |
169 return this.router_ !== null; | 188 return this.router_ !== null; |
170 }; | 189 }; |
171 | 190 |
172 Binding.prototype.createInterfacePtrAndBind = function() { | 191 Binding.prototype.createInterfacePtrAndBind = function() { |
173 var ptr = new this.interfaceType_.ptrClass(); | 192 var ptr = new this.interfaceType_.ptrClass(); |
174 // TODO(yzshen): Set the version of the interface pointer. | 193 // TODO(yzshen): Set the version of the interface pointer. |
175 this.bind(makeRequest(ptr)); | 194 this.bind(makeRequest(ptr)); |
176 return ptr; | 195 return ptr; |
177 } | 196 }; |
178 | 197 |
179 Binding.prototype.bind = function(requestOrHandle) { | 198 Binding.prototype.bind = function(requestOrHandle) { |
180 this.close(); | 199 this.close(); |
181 | 200 |
182 var handle = requestOrHandle instanceof types.InterfaceRequest ? | 201 var handle = requestOrHandle instanceof types.InterfaceRequest ? |
183 requestOrHandle.handle : requestOrHandle; | 202 requestOrHandle.handle : requestOrHandle; |
184 if (!core.isHandle(handle)) | 203 if (!core.isHandle(handle)) |
185 return; | 204 return; |
186 | 205 |
206 this.router_ = new router.Router(handle); | |
207 | |
187 this.stub_ = new this.interfaceType_.stubClass(this.impl_); | 208 this.stub_ = new this.interfaceType_.stubClass(this.impl_); |
188 this.router_ = new router.Router(handle, this.interfaceType_.kVersion); | 209 this.interfaceEndpointClient_ = new InterfaceEndpointClient( |
189 this.router_.setIncomingReceiver(this.stub_); | 210 this.router_.createLocalEndpointHandle(types.kMasterInterfaceId), |
190 this.router_ .setPayloadValidators([this.interfaceType_.validateRequest]); | 211 this.router_, this.interfaceType_.kVersion); |
212 this.interfaceEndpointClient_.setIncomingReceiver(this.stub_); | |
213 this.interfaceEndpointClient_ .setPayloadValidators([ | |
214 this.interfaceType_.validateRequest]); | |
191 }; | 215 }; |
192 | 216 |
193 Binding.prototype.close = function() { | 217 Binding.prototype.close = function() { |
194 if (!this.isBound()) | 218 if (!this.isBound()) |
195 return; | 219 return; |
196 | 220 |
221 if (this.interfaceEndpointClient_) { | |
222 this.interfaceEndpointClient_.closeWithReason(); | |
223 this.interfaceEndpointClient_ = null; | |
224 } | |
225 | |
197 this.router_.close(); | 226 this.router_.close(); |
198 this.router_ = null; | 227 this.router_ = null; |
199 this.stub_ = null; | 228 this.stub_ = null; |
200 }; | 229 }; |
201 | 230 |
231 Binding.prototype.closeWithReason = function(reason) { | |
232 if (this.interfaceEndpointClient_) { | |
233 this.interfaceEndpointClient_.closeWithReason(reason); | |
234 this.interfaceEndpointClient_ = null; | |
235 } | |
236 this.close(); | |
237 }; | |
238 | |
202 Binding.prototype.setConnectionErrorHandler | 239 Binding.prototype.setConnectionErrorHandler |
203 = function(callback) { | 240 = function(callback) { |
204 if (!this.isBound()) | 241 if (!this.isBound()) { |
205 throw new Error("Cannot set connection error handler if not bound."); | 242 throw new Error("Cannot set connection error handler if not bound."); |
206 this.router_.setErrorHandler(callback); | 243 } |
244 this.interfaceEndpointClient_.setConnectionErrorHandler(callback); | |
207 }; | 245 }; |
208 | 246 |
209 Binding.prototype.unbind = function() { | 247 Binding.prototype.unbind = function() { |
210 if (!this.isBound()) | 248 if (!this.isBound()) |
211 return new types.InterfaceRequest(null); | 249 return new types.InterfaceRequest(null); |
212 | 250 |
213 var result = new types.InterfaceRequest(this.router_.connector_.handle_); | 251 var result = new types.InterfaceRequest(this.router_.connector_.handle_); |
214 this.router_.connector_.handle_ = null; | 252 this.router_.connector_.handle_ = null; |
215 this.close(); | 253 this.close(); |
216 return result; | 254 return result; |
217 }; | 255 }; |
218 | 256 |
219 Binding.prototype.enableTestingMode = function() { | 257 Binding.prototype.waitForNextMessageForTesting = function() { |
220 return this.router_.enableTestingMode(); | 258 this.router_.waitForNextMessageForTesting(); |
221 }; | 259 }; |
222 | 260 |
223 // --------------------------------------------------------------------------- | 261 // --------------------------------------------------------------------------- |
224 | 262 |
225 function BindingSetEntry(bindingSet, interfaceType, impl, requestOrHandle, | 263 function BindingSetEntry(bindingSet, interfaceType, impl, requestOrHandle, |
226 bindingId) { | 264 bindingId) { |
227 this.bindingSet_ = bindingSet; | 265 this.bindingSet_ = bindingSet; |
228 this.bindingId_ = bindingId; | 266 this.bindingId_ = bindingId; |
229 this.binding_ = new Binding(interfaceType, impl, requestOrHandle); | 267 this.binding_ = new Binding(interfaceType, impl, requestOrHandle); |
230 | 268 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 var exports = {}; | 314 var exports = {}; |
277 exports.InterfacePtrInfo = types.InterfacePtrInfo; | 315 exports.InterfacePtrInfo = types.InterfacePtrInfo; |
278 exports.InterfaceRequest = types.InterfaceRequest; | 316 exports.InterfaceRequest = types.InterfaceRequest; |
279 exports.makeRequest = makeRequest; | 317 exports.makeRequest = makeRequest; |
280 exports.InterfacePtrController = InterfacePtrController; | 318 exports.InterfacePtrController = InterfacePtrController; |
281 exports.Binding = Binding; | 319 exports.Binding = Binding; |
282 exports.BindingSet = BindingSet; | 320 exports.BindingSet = BindingSet; |
283 | 321 |
284 return exports; | 322 return exports; |
285 }); | 323 }); |
OLD | NEW |