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('data_sender', [ | 5 define('data_sender', [ |
6 'device/serial/data_stream.mojom', | 6 'device/serial/data_stream.mojom', |
7 'device/serial/data_stream_serialization.mojom', | 7 'device/serial/data_stream_serialization.mojom', |
8 'mojo/public/js/core', | 8 'mojo/public/js/core', |
9 'mojo/public/js/router', | 9 'mojo/public/js/router', |
10 ], function(dataStreamMojom, serialization, core, routerModule) { | 10 ], function(dataStreamMojom, serialization, core, routerModule) { |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 sink.onData(new Uint8Array(this.data_, 0, numBytesToSend)); | 158 sink.onData(new Uint8Array(this.data_, 0, numBytesToSend)); |
159 this.data_ = this.data_.slice(numBytesToSend); | 159 this.data_ = this.data_.slice(numBytesToSend); |
160 return { | 160 return { |
161 completed: this.data_.byteLength == 0, | 161 completed: this.data_.byteLength == 0, |
162 remainingBufferCapacity: availableBufferCapacity - numBytesToSend, | 162 remainingBufferCapacity: availableBufferCapacity - numBytesToSend, |
163 }; | 163 }; |
164 }; | 164 }; |
165 | 165 |
166 /** | 166 /** |
167 * A DataSender that sends data to a DataSink. | 167 * A DataSender that sends data to a DataSink. |
168 * @param {!MojoHandle} sink The handle to the DataSink. | 168 * @param {!MojoHandle} handle The handle to the DataSink. |
169 * @param {!MojoHandle} client The handle to the DataSinkClient. | |
170 * @param {number} bufferSize How large a buffer to use for data. | 169 * @param {number} bufferSize How large a buffer to use for data. |
171 * @param {number} fatalErrorValue The send error value to report in the | 170 * @param {number} fatalErrorValue The send error value to report in the |
172 * event of a fatal error. | 171 * event of a fatal error. |
173 * @constructor | 172 * @constructor |
174 * @alias module:data_sender.DataSender | 173 * @alias module:data_sender.DataSender |
175 */ | 174 */ |
176 function DataSender(sink, client, bufferSize, fatalErrorValue) { | 175 function DataSender(handle, bufferSize, fatalErrorValue) { |
177 this.init_(sink, client, fatalErrorValue, bufferSize); | 176 this.init_(handle, fatalErrorValue, bufferSize); |
178 this.sink_.init(bufferSize); | 177 this.sink_.init(bufferSize); |
179 } | 178 } |
180 | 179 |
181 DataSender.prototype = | 180 DataSender.prototype = |
182 $Object.create(dataStreamMojom.DataSinkClient.stubClass.prototype); | 181 $Object.create(dataStreamMojom.DataSinkClient.stubClass.prototype); |
183 | 182 |
184 /** | 183 /** |
185 * Closes this DataSender. | 184 * Closes this DataSender. |
186 */ | 185 */ |
187 DataSender.prototype.close = function() { | 186 DataSender.prototype.close = function() { |
188 if (this.shutDown_) | 187 if (this.shutDown_) |
189 return; | 188 return; |
190 this.shutDown_ = true; | 189 this.shutDown_ = true; |
191 this.router_.close(); | 190 this.router_.close(); |
192 this.clientRouter_.close(); | |
193 while (this.pendingSends_.length) { | 191 while (this.pendingSends_.length) { |
194 this.pendingSends_.pop().reportBytesSentAndError( | 192 this.pendingSends_.pop().reportBytesSentAndError( |
195 0, this.fatalErrorValue_); | 193 0, this.fatalErrorValue_); |
196 } | 194 } |
197 while (this.sendsAwaitingAck_.length) { | 195 while (this.sendsAwaitingAck_.length) { |
198 this.sendsAwaitingAck_.pop().reportBytesSentAndError( | 196 this.sendsAwaitingAck_.pop().reportBytesSentAndError( |
199 0, this.fatalErrorValue_); | 197 0, this.fatalErrorValue_); |
200 } | 198 } |
201 this.callCancelCallback_(); | 199 this.callCancelCallback_(); |
202 }; | 200 }; |
203 | 201 |
204 /** | 202 /** |
205 * Initialize this DataSender. | 203 * Initialize this DataSender. |
206 * @param {!MojoHandle} sink A handle to the DataSink. | 204 * @param {!MojoHandle} sink A handle to the DataSink |
207 * @param {!MojoHandle} sinkClient A handle to the DataSinkClient. | |
208 * @param {number} fatalErrorValue The error to dispatch in the event of a | 205 * @param {number} fatalErrorValue The error to dispatch in the event of a |
209 * fatal error. | 206 * fatal error. |
210 * @param {number} bufferSize The size of the send buffer. | 207 * @param {number} bufferSize The size of the send buffer. |
211 * @private | 208 * @private |
212 */ | 209 */ |
213 DataSender.prototype.init_ = function(sink, sinkClient, fatalErrorValue, | 210 DataSender.prototype.init_ = function(sink, fatalErrorValue, bufferSize) { |
214 bufferSize) { | |
215 /** | 211 /** |
216 * The error to be dispatched in the event of a fatal error. | 212 * The error to be dispatched in the event of a fatal error. |
217 * @const {number} | 213 * @const {number} |
218 * @private | 214 * @private |
219 */ | 215 */ |
220 this.fatalErrorValue_ = fatalErrorValue; | 216 this.fatalErrorValue_ = fatalErrorValue; |
221 /** | 217 /** |
222 * Whether this DataSender has shut down. | 218 * Whether this DataSender has shut down. |
223 * @type {boolean} | 219 * @type {boolean} |
224 * @private | 220 * @private |
225 */ | 221 */ |
226 this.shutDown_ = false; | 222 this.shutDown_ = false; |
227 /** | 223 /** |
228 * The [Router]{@link module:mojo/public/js/router.Router} for the | 224 * The [Router]{@link module:mojo/public/js/router.Router} for the |
229 * connection to the DataSink. | 225 * connection to the DataSink. |
230 * @private | 226 * @private |
231 */ | 227 */ |
232 this.router_ = new routerModule.Router(sink); | 228 this.router_ = new routerModule.Router(sink); |
233 /** | 229 /** |
234 * The [Router]{@link module:mojo/public/js/router.Router} for the | |
235 * connection to the DataSinkClient. | |
236 * @private | |
237 */ | |
238 this.clientRouter_ = new routerModule.Router(sinkClient); | |
239 /** | |
240 * The connection to the DataSink. | 230 * The connection to the DataSink. |
241 * @private | 231 * @private |
242 */ | 232 */ |
243 this.sink_ = new dataStreamMojom.DataSink.proxyClass(this.router_); | 233 this.sink_ = new dataStreamMojom.DataSink.proxyClass(this.router_); |
244 this.client_ = new dataStreamMojom.DataSinkClient.stubClass(this); | 234 this.router_.setIncomingReceiver(this); |
245 this.clientRouter_.setIncomingReceiver(this.client_); | |
246 /** | 235 /** |
247 * A queue of sends that have not fully sent their data to the DataSink. | 236 * A queue of sends that have not fully sent their data to the DataSink. |
248 * @type {!module:data_sender~PendingSend[]} | 237 * @type {!module:data_sender~PendingSend[]} |
249 * @private | 238 * @private |
250 */ | 239 */ |
251 this.pendingSends_ = []; | 240 this.pendingSends_ = []; |
252 /** | 241 /** |
253 * A queue of sends that have sent their data to the DataSink, but have not | 242 * A queue of sends that have sent their data to the DataSink, but have not |
254 * been received by the DataSink. | 243 * been received by the DataSink. |
255 * @type {!module:data_sender~PendingSend[]} | 244 * @type {!module:data_sender~PendingSend[]} |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 var readyToSerialize = Promise.resolve(); | 283 var readyToSerialize = Promise.resolve(); |
295 if (this.pendingSends_.length || this.sendsAwaitingAck_.length) { | 284 if (this.pendingSends_.length || this.sendsAwaitingAck_.length) { |
296 if (this.pendingCancel_) | 285 if (this.pendingCancel_) |
297 readyToSerialize = this.cancelPromise_; | 286 readyToSerialize = this.cancelPromise_; |
298 else | 287 else |
299 readyToSerialize = this.cancel(this.fatalErrorValue_); | 288 readyToSerialize = this.cancel(this.fatalErrorValue_); |
300 } | 289 } |
301 return readyToSerialize.then(function() { | 290 return readyToSerialize.then(function() { |
302 var serialized = new serialization.SerializedDataSender(); | 291 var serialized = new serialization.SerializedDataSender(); |
303 serialized.sink = this.router_.connector_.handle_; | 292 serialized.sink = this.router_.connector_.handle_; |
304 serialized.client = this.clientRouter_.connector_.handle_; | |
305 serialized.fatal_error_value = this.fatalErrorValue_; | 293 serialized.fatal_error_value = this.fatalErrorValue_; |
306 serialized.buffer_size = this.availableBufferCapacity_; | 294 serialized.buffer_size = this.availableBufferCapacity_; |
307 this.router_.connector_.handle_ = null; | 295 this.router_.connector_.handle_ = null; |
308 this.router_.close(); | 296 this.router_.close(); |
309 this.clientRouter_.connector_.handle_ = null; | |
310 this.clientRouter_.close(); | |
311 this.shutDown_ = true; | 297 this.shutDown_ = true; |
312 return serialized; | 298 return serialized; |
313 }.bind(this)); | 299 }.bind(this)); |
314 }; | 300 }; |
315 | 301 |
316 /** | 302 /** |
317 * Deserializes a SerializedDataSender. | 303 * Deserializes a SerializedDataSender. |
318 * @param {SerializedDataSender} serialized The serialized DataSender. | 304 * @param {SerializedDataSender} serialized The serialized DataSender. |
319 * @return {!DataSender} The deserialized DataSender. | 305 * @return {!DataSender} The deserialized DataSender. |
320 */ | 306 */ |
321 DataSender.deserialize = function(serialized) { | 307 DataSender.deserialize = function(serialized) { |
322 var sender = $Object.create(DataSender.prototype); | 308 var sender = $Object.create(DataSender.prototype); |
323 sender.deserialize_(serialized); | 309 sender.deserialize_(serialized); |
324 return sender; | 310 return sender; |
325 }; | 311 }; |
326 | 312 |
327 /** | 313 /** |
328 * Deserializes a SerializedDataSender into this DataSender. | 314 * Deserializes a SerializedDataSender into this DataSender. |
329 * @param {SerializedDataSender} serialized The serialized DataSender. | 315 * @param {SerializedDataSender} serialized The serialized DataSender. |
330 * @private | 316 * @private |
331 */ | 317 */ |
332 DataSender.prototype.deserialize_ = function(serialized) { | 318 DataSender.prototype.deserialize_ = function(serialized) { |
333 if (!serialized) { | 319 if (!serialized) { |
334 this.shutDown_ = true; | 320 this.shutDown_ = true; |
335 return; | 321 return; |
336 } | 322 } |
337 this.init_(serialized.sink, serialized.client, serialized.fatal_error_value, | 323 this.init_( |
338 serialized.buffer_size); | 324 serialized.sink, serialized.fatal_error_value, serialized.buffer_size); |
339 }; | 325 }; |
340 | 326 |
341 /** | 327 /** |
342 * Sends data to the DataSink. | 328 * Sends data to the DataSink. |
343 * @return {!Promise.<number>} A promise to the number of bytes sent. If an | 329 * @return {!Promise.<number>} A promise to the number of bytes sent. If an |
344 * error occurs, the promise will reject with an Error object with a | 330 * error occurs, the promise will reject with an Error object with a |
345 * property error containing the error code. | 331 * property error containing the error code. |
346 * @throws Will throw if this has encountered a fatal error or a cancel is in | 332 * @throws Will throw if this has encountered a fatal error or a cancel is in |
347 * progress. | 333 * progress. |
348 */ | 334 */ |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 // Note: Only the first PendingSend in |pendingSends_| will have data to | 439 // Note: Only the first PendingSend in |pendingSends_| will have data to |
454 // flush as only the first can have sent data to the DataSink. | 440 // flush as only the first can have sent data to the DataSink. |
455 this.availableBufferCapacity_ += result.bytesToFlush; | 441 this.availableBufferCapacity_ += result.bytesToFlush; |
456 } | 442 } |
457 this.callCancelCallback_(); | 443 this.callCancelCallback_(); |
458 return Promise.resolve(); | 444 return Promise.resolve(); |
459 }; | 445 }; |
460 | 446 |
461 return {DataSender: DataSender}; | 447 return {DataSender: DataSender}; |
462 }); | 448 }); |
OLD | NEW |