| 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 17 matching lines...) Expand all Loading... |
| 28 this.data_ = data; | 28 this.data_ = data; |
| 29 /** | 29 /** |
| 30 * The total length of data to be sent. | 30 * The total length of data to be sent. |
| 31 * @type {number} | 31 * @type {number} |
| 32 * @private | 32 * @private |
| 33 */ | 33 */ |
| 34 this.length_ = data.byteLength; | 34 this.length_ = data.byteLength; |
| 35 /** | 35 /** |
| 36 * The promise that will be resolved or rejected when this send completes | 36 * The promise that will be resolved or rejected when this send completes |
| 37 * or fails, respectively. | 37 * or fails, respectively. |
| 38 * @type {!Promise.<number>} | 38 * @type {!Promise<number>} |
| 39 * @private | 39 * @private |
| 40 */ | 40 */ |
| 41 this.promise_ = new Promise(function(resolve, reject) { | 41 this.promise_ = new Promise(function(resolve, reject) { |
| 42 /** | 42 /** |
| 43 * The callback to call on success. | 43 * The callback to call on success. |
| 44 * @type {Function} | 44 * @type {Function} |
| 45 * @private | 45 * @private |
| 46 */ | 46 */ |
| 47 this.successCallback_ = resolve; | 47 this.successCallback_ = resolve; |
| 48 /** | 48 /** |
| 49 * The callback to call with the error on failure. | 49 * The callback to call with the error on failure. |
| 50 * @type {Function} | 50 * @type {Function} |
| 51 * @private | 51 * @private |
| 52 */ | 52 */ |
| 53 this.errorCallback_ = reject; | 53 this.errorCallback_ = reject; |
| 54 }.bind(this)); | 54 }.bind(this)); |
| 55 } | 55 } |
| 56 | 56 |
| 57 /** | 57 /** |
| 58 * Returns the promise that will be resolved when this operation completes or | 58 * Returns the promise that will be resolved when this operation completes or |
| 59 * rejected if an error occurs. | 59 * rejected if an error occurs. |
| 60 * @return {!Promise.<number>} A promise to the number of bytes sent. | 60 * @return {!Promise<number>} A promise to the number of bytes sent. |
| 61 */ | 61 */ |
| 62 PendingSend.prototype.getPromise = function() { | 62 PendingSend.prototype.getPromise = function() { |
| 63 return this.promise_; | 63 return this.promise_; |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 /** | 66 /** |
| 67 * Invoked when the DataSink reports that bytes have been sent. Resolves the | 67 * Invoked when the DataSink reports that bytes have been sent. Resolves the |
| 68 * promise returned by | 68 * promise returned by |
| 69 * [getPromise()]{@link module:data_sender~PendingSend#getPromise} once all | 69 * [getPromise()]{@link module:data_sender~PendingSend#getPromise} once all |
| 70 * bytes have been reported as sent. | 70 * bytes have been reported as sent. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 * @type {Promise} | 181 * @type {Promise} |
| 182 * @private | 182 * @private |
| 183 */ | 183 */ |
| 184 this.cancelPromise_ = null; | 184 this.cancelPromise_ = null; |
| 185 }; | 185 }; |
| 186 | 186 |
| 187 /** | 187 /** |
| 188 * Serializes this DataSender. | 188 * Serializes this DataSender. |
| 189 * This will cancel any sends in progress before the returned promise | 189 * This will cancel any sends in progress before the returned promise |
| 190 * resolves. | 190 * resolves. |
| 191 * @return {!Promise.<SerializedDataSender>} A promise that will resolve to | 191 * @return {!Promise<SerializedDataSender>} A promise that will resolve to |
| 192 * the serialization of this DataSender. If this DataSender has shut down, | 192 * the serialization of this DataSender. If this DataSender has shut down, |
| 193 * the promise will resolve to null. | 193 * the promise will resolve to null. |
| 194 */ | 194 */ |
| 195 DataSender.prototype.serialize = function() { | 195 DataSender.prototype.serialize = function() { |
| 196 if (this.shutDown_) | 196 if (this.shutDown_) |
| 197 return Promise.resolve(null); | 197 return Promise.resolve(null); |
| 198 | 198 |
| 199 var readyToSerialize = Promise.resolve(); | 199 var readyToSerialize = Promise.resolve(); |
| 200 if (this.sendsAwaitingAck_.length) { | 200 if (this.sendsAwaitingAck_.length) { |
| 201 if (this.pendingCancel_) | 201 if (this.pendingCancel_) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 if (!serialized) { | 234 if (!serialized) { |
| 235 this.shutDown_ = true; | 235 this.shutDown_ = true; |
| 236 return; | 236 return; |
| 237 } | 237 } |
| 238 this.init_(serialized.sink, serialized.fatal_error_value, | 238 this.init_(serialized.sink, serialized.fatal_error_value, |
| 239 serialized.buffer_size); | 239 serialized.buffer_size); |
| 240 }; | 240 }; |
| 241 | 241 |
| 242 /** | 242 /** |
| 243 * Sends data to the DataSink. | 243 * Sends data to the DataSink. |
| 244 * @return {!Promise.<number>} A promise to the number of bytes sent. If an | 244 * @return {!Promise<number>} A promise to the number of bytes sent. If an |
| 245 * error occurs, the promise will reject with an Error object with a | 245 * error occurs, the promise will reject with an Error object with a |
| 246 * property error containing the error code. | 246 * property error containing the error code. |
| 247 * @throws Will throw if this has encountered a fatal error or a cancel is in | 247 * @throws Will throw if this has encountered a fatal error or a cancel is in |
| 248 * progress. | 248 * progress. |
| 249 */ | 249 */ |
| 250 DataSender.prototype.send = function(data) { | 250 DataSender.prototype.send = function(data) { |
| 251 if (this.shutDown_) | 251 if (this.shutDown_) |
| 252 throw new Error('DataSender has been closed'); | 252 throw new Error('DataSender has been closed'); |
| 253 if (this.pendingCancel_) | 253 if (this.pendingCancel_) |
| 254 throw new Error('Cancel in progress'); | 254 throw new Error('Cancel in progress'); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 this.sendsAwaitingAck_[0].reportBytesSentAndError(numBytes, error); | 326 this.sendsAwaitingAck_[0].reportBytesSentAndError(numBytes, error); |
| 327 this.sendsAwaitingAck_.shift(); | 327 this.sendsAwaitingAck_.shift(); |
| 328 if (this.sendsAwaitingAck_.length) | 328 if (this.sendsAwaitingAck_.length) |
| 329 return; | 329 return; |
| 330 this.callCancelCallback_(); | 330 this.callCancelCallback_(); |
| 331 this.sink_.clearError(); | 331 this.sink_.clearError(); |
| 332 }; | 332 }; |
| 333 | 333 |
| 334 return {DataSender: DataSender}; | 334 return {DataSender: DataSender}; |
| 335 }); | 335 }); |
| OLD | NEW |