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 'async_waiter', | 6 'async_waiter', |
7 'device/serial/data_stream.mojom', | 7 'device/serial/data_stream.mojom', |
| 8 'device/serial/data_stream_serialization.mojom', |
8 'mojo/public/js/bindings/core', | 9 'mojo/public/js/bindings/core', |
9 'mojo/public/js/bindings/router', | 10 'mojo/public/js/bindings/router', |
10 ], function(asyncWaiter, dataStreamMojom, core, routerModule) { | 11 ], function(asyncWaiter, dataStreamMojom, serialization, core, routerModule) { |
11 /** | 12 /** |
12 * @module data_sender | 13 * @module data_sender |
13 */ | 14 */ |
14 | 15 |
15 /** | 16 /** |
16 * A pending send operation. | 17 * A pending send operation. |
17 * @param {ArrayBuffer} data The data to be sent. | 18 * @param {!ArrayBuffer} data The data to be sent. |
18 * @constructor | 19 * @constructor |
19 * @alias module:data_sender~PendingSend | 20 * @alias module:data_sender~PendingSend |
20 * @private | 21 * @private |
21 */ | 22 */ |
22 function PendingSend(data) { | 23 function PendingSend(data) { |
23 /** | 24 /** |
24 * The remaining data to be sent. | 25 * The remaining data to be sent. |
25 * @type {ArrayBuffer} | 26 * @type {!ArrayBuffer} |
26 * @private | 27 * @private |
27 */ | 28 */ |
28 this.data_ = data; | 29 this.data_ = data; |
29 /** | 30 /** |
30 * The total length of data to be sent. | 31 * The total length of data to be sent. |
31 * @type {number} | 32 * @type {number} |
32 * @private | 33 * @private |
33 */ | 34 */ |
34 this.length_ = data.byteLength; | 35 this.length_ = data.byteLength; |
35 /** | 36 /** |
36 * The number of bytes that have been received by the DataSink. | 37 * The number of bytes that have been received by the DataSink. |
37 * @type {number} | 38 * @type {number} |
38 * @private | 39 * @private |
39 */ | 40 */ |
40 this.bytesReceivedBySink_ = 0; | 41 this.bytesReceivedBySink_ = 0; |
41 /** | 42 /** |
42 * The promise that will be resolved or rejected when this send completes | 43 * The promise that will be resolved or rejected when this send completes |
43 * or fails, respectively. | 44 * or fails, respectively. |
44 * @type {Promise.<number>} | 45 * @type {!Promise.<number>} |
45 * @private | 46 * @private |
46 */ | 47 */ |
47 this.promise_ = new Promise(function(resolve, reject) { | 48 this.promise_ = new Promise(function(resolve, reject) { |
48 /** | 49 /** |
49 * The callback to call on success. | 50 * The callback to call on success. |
50 * @type {Function} | 51 * @type {Function} |
51 * @private | 52 * @private |
52 */ | 53 */ |
53 this.successCallback_ = resolve; | 54 this.successCallback_ = resolve; |
54 /** | 55 /** |
55 * The callback to call with the error on failure. | 56 * The callback to call with the error on failure. |
56 * @type {Function} | 57 * @type {Function} |
57 * @private | 58 * @private |
58 */ | 59 */ |
59 this.errorCallback_ = reject; | 60 this.errorCallback_ = reject; |
60 }.bind(this)); | 61 }.bind(this)); |
61 } | 62 } |
62 | 63 |
63 /** | 64 /** |
64 * Returns the promise that will be resolved when this operation completes or | 65 * Returns the promise that will be resolved when this operation completes or |
65 * rejected if an error occurs. | 66 * rejected if an error occurs. |
66 * @return {Promise.<number>} A promise to the number of bytes sent. | 67 * @return {!Promise.<number>} A promise to the number of bytes sent. |
67 */ | 68 */ |
68 PendingSend.prototype.getPromise = function() { | 69 PendingSend.prototype.getPromise = function() { |
69 return this.promise_; | 70 return this.promise_; |
70 }; | 71 }; |
71 | 72 |
72 /** | 73 /** |
73 * @typedef module:data_sender~PendingSend.ReportBytesResult | 74 * @typedef module:data_sender~PendingSend.ReportBytesResult |
74 * @property {number} bytesUnreported The number of bytes reported that were | 75 * @property {number} bytesUnreported The number of bytes reported that were |
75 * not part of the send. | 76 * not part of the send. |
76 * @property {boolean?} done Whether this send has completed. | 77 * @property {boolean} done Whether this send has completed. |
77 * @property {number?} bytesToFlush The number of bytes to flush in the event | 78 * @property {?number} bytesToFlush The number of bytes to flush in the event |
78 * of an error. | 79 * of an error. |
79 */ | 80 */ |
80 | 81 |
81 /** | 82 /** |
82 * Invoked when the DataSink reports that bytes have been sent. Resolves the | 83 * Invoked when the DataSink reports that bytes have been sent. Resolves the |
83 * promise returned by | 84 * promise returned by |
84 * [getPromise()]{@link module:data_sender~PendingSend#getPromise} once all | 85 * [getPromise()]{@link module:data_sender~PendingSend#getPromise} once all |
85 * bytes have been reported as sent. | 86 * bytes have been reported as sent. |
86 * @param {number} numBytes The number of bytes sent. | 87 * @param {number} numBytes The number of bytes sent. |
87 * @return {module:data_sender~PendingSend.ReportBytesResult} | 88 * @return {!module:data_sender~PendingSend.ReportBytesResult} |
88 */ | 89 */ |
89 PendingSend.prototype.reportBytesSent = function(numBytes) { | 90 PendingSend.prototype.reportBytesSent = function(numBytes) { |
90 var result = this.reportBytesSentInternal_(numBytes); | 91 var result = this.reportBytesSentInternal_(numBytes); |
91 if (this.bytesReceivedBySink_ == this.length_) { | 92 if (this.bytesReceivedBySink_ == this.length_) { |
92 result.done = true; | 93 result.done = true; |
93 this.successCallback_(this.bytesReceivedBySink_); | 94 this.successCallback_(this.bytesReceivedBySink_); |
94 } | 95 } |
95 return result; | 96 return result; |
96 }; | 97 }; |
97 | 98 |
98 /** | 99 /** |
99 * Invoked when the DataSink reports an error. Rejects the promise returned by | 100 * Invoked when the DataSink reports an error. Rejects the promise returned by |
100 * [getPromise()]{@link module:data_sender~PendingSend#getPromise} unless the | 101 * [getPromise()]{@link module:data_sender~PendingSend#getPromise} unless the |
101 * error occurred after this send, that is, unless numBytes is greater than | 102 * error occurred after this send, that is, unless numBytes is greater than |
102 * the nubmer of outstanding bytes. | 103 * the nubmer of outstanding bytes. |
103 * @param {number} numBytes The number of bytes sent. | 104 * @param {number} numBytes The number of bytes sent. |
104 * @param {number} error The error reported by the DataSink. | 105 * @param {number} error The error reported by the DataSink. |
105 * @return {module:data_sender~PendingSend.ReportBytesResult} | 106 * @return {!module:data_sender~PendingSend.ReportBytesResult} |
106 */ | 107 */ |
107 PendingSend.prototype.reportBytesSentAndError = function(numBytes, error) { | 108 PendingSend.prototype.reportBytesSentAndError = function(numBytes, error) { |
108 var result = this.reportBytesSentInternal_(numBytes); | 109 var result = this.reportBytesSentInternal_(numBytes); |
109 // If there are remaining bytes to report, the error occurred after this | 110 // If there are remaining bytes to report, the error occurred after this |
110 // PendingSend so we should report success. | 111 // PendingSend so we should report success. |
111 if (result.bytesUnreported > 0) { | 112 if (result.bytesUnreported > 0) { |
112 this.successCallback_(this.bytesReceivedBySink_); | 113 this.successCallback_(this.bytesReceivedBySink_); |
113 result.bytesToFlush = 0; | 114 result.bytesToFlush = 0; |
114 return result; | 115 return result; |
115 } | 116 } |
116 | 117 |
117 var e = new Error(); | 118 var e = new Error(); |
118 e.error = error; | 119 e.error = error; |
119 e.bytesSent = this.bytesReceivedBySink_; | 120 e.bytesSent = this.bytesReceivedBySink_; |
120 this.errorCallback_(e); | 121 this.errorCallback_(e); |
121 this.done = true; | 122 this.done = true; |
122 result.bytesToFlush = | 123 result.bytesToFlush = |
123 this.length_ - this.data_.byteLength - this.bytesReceivedBySink_; | 124 this.length_ - this.data_.byteLength - this.bytesReceivedBySink_; |
124 return result; | 125 return result; |
125 }; | 126 }; |
126 | 127 |
127 /** | 128 /** |
128 * Updates the internal state in response to a report from the DataSink. | 129 * Updates the internal state in response to a report from the DataSink. |
129 * @param {number} numBytes The number of bytes sent. | 130 * @param {number} numBytes The number of bytes sent. |
130 * @return {module:data_sender~PendingSend.ReportBytesResult} | 131 * @return {!module:data_sender~PendingSend.ReportBytesResult} |
131 * @private | 132 * @private |
132 */ | 133 */ |
133 PendingSend.prototype.reportBytesSentInternal_ = function(numBytes) { | 134 PendingSend.prototype.reportBytesSentInternal_ = function(numBytes) { |
134 this.bytesReceivedBySink_ += numBytes; | 135 this.bytesReceivedBySink_ += numBytes; |
135 var result = {bytesUnreported: 0}; | 136 var result = {bytesUnreported: 0}; |
136 if (this.bytesReceivedBySink_ > this.length_) { | 137 if (this.bytesReceivedBySink_ > this.length_) { |
137 result.bytesUnreported = this.bytesReceivedBySink_ - this.length_; | 138 result.bytesUnreported = this.bytesReceivedBySink_ - this.length_; |
138 this.bytesReceivedBySink_ = this.length_; | 139 this.bytesReceivedBySink_ = this.length_; |
139 } | 140 } |
140 result.done = false; | 141 result.done = false; |
141 return result; | 142 return result; |
142 }; | 143 }; |
143 | 144 |
144 /** | 145 /** |
145 * Writes pending data into the data pipe. | 146 * Writes pending data into the data pipe. |
146 * @param {MojoHandle} handle The handle to the data pipe. | 147 * @param {!MojoHandle} handle The handle to the data pipe. |
147 * @return {number} The Mojo result corresponding to the outcome: | 148 * @return {number} The Mojo result corresponding to the outcome: |
148 * <ul> | 149 * <ul> |
149 * <li>RESULT_OK if the write completes successfully; | 150 * <li>RESULT_OK if the write completes successfully; |
150 * <li>RESULT_SHOULD_WAIT if some, but not all data was written; or | 151 * <li>RESULT_SHOULD_WAIT if some, but not all data was written; or |
151 * <li>the data pipe error if the write failed. | 152 * <li>the data pipe error if the write failed. |
152 * </ul> | 153 * </ul> |
153 */ | 154 */ |
154 PendingSend.prototype.sendData = function(handle) { | 155 PendingSend.prototype.sendData = function(handle) { |
155 var result = core.writeData( | 156 var result = core.writeData( |
156 handle, new Int8Array(this.data_), core.WRITE_DATA_FLAG_NONE); | 157 handle, new Int8Array(this.data_), core.WRITE_DATA_FLAG_NONE); |
157 if (result.result != core.RESULT_OK) | 158 if (result.result != core.RESULT_OK) |
158 return result.result; | 159 return result.result; |
159 this.data_ = this.data_.slice(result.numBytes); | 160 this.data_ = this.data_.slice(result.numBytes); |
160 return this.data_.byteLength ? core.RESULT_SHOULD_WAIT : core.RESULT_OK; | 161 return this.data_.byteLength ? core.RESULT_SHOULD_WAIT : core.RESULT_OK; |
161 }; | 162 }; |
162 | 163 |
163 /** | 164 /** |
164 * A DataSender that sends data to a DataSink. | 165 * A DataSender that sends data to a DataSink. |
165 * @param {MojoHandle} handle The handle to the DataSink. | 166 * @param {!MojoHandle} handle The handle to the DataSink. |
166 * @param {number} bufferSize How large a buffer the data pipe should use. | 167 * @param {number} bufferSize How large a buffer the data pipe should use. |
167 * @param {number} fatalErrorValue The send error value to report in the | 168 * @param {number} fatalErrorValue The send error value to report in the |
168 * event of a fatal error. | 169 * event of a fatal error. |
169 * @constructor | 170 * @constructor |
170 * @alias module:data_sender.DataSender | 171 * @alias module:data_sender.DataSender |
171 */ | 172 */ |
172 function DataSender(handle, bufferSize, fatalErrorValue) { | 173 function DataSender(handle, bufferSize, fatalErrorValue) { |
| 174 var dataPipeOptions = { |
| 175 flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, |
| 176 elementNumBytes: 1, |
| 177 capacityNumBytes: bufferSize, |
| 178 }; |
| 179 var sendPipe = core.createDataPipe(dataPipeOptions); |
| 180 this.init_(handle, sendPipe.producerHandle, fatalErrorValue); |
| 181 this.sink_.init(sendPipe.consumerHandle); |
| 182 } |
| 183 |
| 184 DataSender.prototype = |
| 185 $Object.create(dataStreamMojom.DataSinkClientStub.prototype); |
| 186 |
| 187 /** |
| 188 * Closes this DataSender. |
| 189 */ |
| 190 DataSender.prototype.close = function() { |
| 191 if (this.shutDown_) |
| 192 return; |
| 193 this.shutDown_ = true; |
| 194 this.waiter_.stop(); |
| 195 this.router_.close(); |
| 196 core.close(this.sendPipe_); |
| 197 while (this.pendingSends_.length) { |
| 198 this.pendingSends_.pop().reportBytesSentAndError( |
| 199 0, this.fatalErrorValue_); |
| 200 } |
| 201 while (this.sendsAwaitingAck_.length) { |
| 202 this.sendsAwaitingAck_.pop().reportBytesSentAndError( |
| 203 0, this.fatalErrorValue_); |
| 204 } |
| 205 this.callCancelCallback_(); |
| 206 }; |
| 207 |
| 208 /** |
| 209 * Initialize this DataSender. |
| 210 * @param {!MojoHandle} sink A handle to the DataSink |
| 211 * @param {!MojoHandle} dataPipe A handle to use for sending data to the |
| 212 * DataSink. |
| 213 * @param {number} fatalErrorValue The error to dispatch in the event of a |
| 214 * fatal error. |
| 215 * @private |
| 216 */ |
| 217 DataSender.prototype.init_ = function(sink, dataPipe, fatalErrorValue) { |
| 218 /** |
| 219 * The handle to the data pipe to use for sending data. |
| 220 * @private |
| 221 */ |
| 222 this.sendPipe_ = dataPipe; |
| 223 /** |
| 224 * The error to be dispatched in the event of a fatal error. |
| 225 * @const {number} |
| 226 * @private |
| 227 */ |
| 228 this.fatalErrorValue_ = fatalErrorValue; |
| 229 /** |
| 230 * Whether this DataSender has shut down. |
| 231 * @type {boolean} |
| 232 * @private |
| 233 */ |
| 234 this.shutDown_ = false; |
173 /** | 235 /** |
174 * The [Router]{@link module:mojo/public/js/bindings/router.Router} for the | 236 * The [Router]{@link module:mojo/public/js/bindings/router.Router} for the |
175 * connection to the DataSink. | 237 * connection to the DataSink. |
176 * @private | 238 * @private |
177 */ | 239 */ |
178 this.router_ = new routerModule.Router(handle); | 240 this.router_ = new routerModule.Router(sink); |
179 /** | 241 /** |
180 * The connection to the DataSink. | 242 * The connection to the DataSink. |
181 * @private | 243 * @private |
182 */ | 244 */ |
183 this.sink_ = new dataStreamMojom.DataSinkProxy(this.router_); | 245 this.sink_ = new dataStreamMojom.DataSinkProxy(this.router_); |
184 this.router_.setIncomingReceiver(this); | 246 this.router_.setIncomingReceiver(this); |
185 var dataPipeOptions = { | |
186 flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, | |
187 elementNumBytes: 1, | |
188 capacityNumBytes: bufferSize, | |
189 }; | |
190 var sendPipe = core.createDataPipe(dataPipeOptions); | |
191 this.sink_.init(sendPipe.consumerHandle); | |
192 /** | |
193 * The handle to the data pipe to use for sending data. | |
194 * @private | |
195 */ | |
196 this.sendPipe_ = sendPipe.producerHandle; | |
197 /** | |
198 * The error to be dispatched in the event of a fatal error. | |
199 * @type {number} | |
200 * @private | |
201 */ | |
202 this.fatalErrorValue_ = fatalErrorValue; | |
203 /** | 247 /** |
204 * The async waiter used to wait for | 248 * The async waiter used to wait for |
205 * {@link module:data_sender.DataSender#sendPipe_} to be writable. | 249 * {@link module:data_sender.DataSender#sendPipe_} to be writable. |
206 * @type module:async_waiter.AsyncWaiter | 250 * @type {!module:async_waiter.AsyncWaiter} |
207 * @private | 251 * @private |
208 */ | 252 */ |
209 this.waiter_ = new asyncWaiter.AsyncWaiter( | 253 this.waiter_ = new asyncWaiter.AsyncWaiter( |
210 this.sendPipe_, core.HANDLE_SIGNAL_WRITABLE, | 254 this.sendPipe_, core.HANDLE_SIGNAL_WRITABLE, |
211 this.onHandleReady_.bind(this)); | 255 this.onHandleReady_.bind(this)); |
212 /** | 256 /** |
213 * A queue of sends that have not fully written their data to the data pipe. | 257 * A queue of sends that have not fully written their data to the data pipe. |
214 * @type module:data_sender~PendingSend[] | 258 * @type {!module:data_sender~PendingSend[]} |
215 * @private | 259 * @private |
216 */ | 260 */ |
217 this.pendingSends_ = []; | 261 this.pendingSends_ = []; |
218 /** | 262 /** |
219 * A queue of sends that have written their data to the data pipe, but have | 263 * A queue of sends that have written their data to the data pipe, but have |
220 * not been received by the DataSink. | 264 * not been received by the DataSink. |
221 * @type module:data_sender~PendingSend[] | 265 * @type {!module:data_sender~PendingSend[]} |
222 * @private | 266 * @private |
223 */ | 267 */ |
224 this.sendsAwaitingAck_ = []; | 268 this.sendsAwaitingAck_ = []; |
| 269 |
225 /** | 270 /** |
226 * The callback that will resolve a pending cancel if one is in progress. | 271 * The callback that will resolve a pending cancel if one is in progress. |
227 * @type Function | 272 * @type {?Function} |
228 * @private | 273 * @private |
229 */ | 274 */ |
230 this.pendingCancel_ = null; | 275 this.pendingCancel_ = null; |
| 276 |
231 /** | 277 /** |
232 * Whether this DataReceiver has shut down. | 278 * The promise that will be resolved when a pending cancel completes if one |
233 * @type {boolean} | 279 * is in progress. |
| 280 * @type {Promise} |
234 * @private | 281 * @private |
235 */ | 282 */ |
236 this.shutDown_ = false; | 283 this.cancelPromise_ = null; |
237 } | 284 }; |
238 | |
239 DataSender.prototype = | |
240 $Object.create(dataStreamMojom.DataSinkClientStub.prototype); | |
241 | 285 |
242 /** | 286 /** |
243 * Closes this DataSender. | 287 * Serializes this DataSender. |
| 288 * This will cancel any sends in progress before the returned promise |
| 289 * resolves. |
| 290 * @return {!Promise.<SerializedDataSender>} A promise that will resolve to |
| 291 * the serialization of this DataSender. If this DataSender has shut down, |
| 292 * the promise will resolve to null. |
244 */ | 293 */ |
245 DataSender.prototype.close = function() { | 294 DataSender.prototype.serialize = function() { |
246 if (this.shutDown_) | 295 if (this.shutDown_) |
| 296 return Promise.resolve(null); |
| 297 |
| 298 var readyToSerialize = Promise.resolve(); |
| 299 if (this.pendingSends_.length) { |
| 300 if (this.pendingCancel_) |
| 301 readyToSerialize = this.cancelPromise_; |
| 302 else |
| 303 readyToSerialize = this.cancel(this.fatalErrorValue_); |
| 304 } |
| 305 return readyToSerialize.then(function() { |
| 306 this.waiter_.stop(); |
| 307 var serialized = new serialization.SerializedDataSender(); |
| 308 serialized.sink = this.router_.connector_.handle_, |
| 309 serialized.data_pipe = this.sendPipe_, |
| 310 serialized.fatal_error_value = this.fatalErrorValue_, |
| 311 this.router_.connector_.handle_ = null; |
| 312 this.router_.close(); |
| 313 this.shutDown_ = true; |
| 314 return serialized; |
| 315 }.bind(this)); |
| 316 }; |
| 317 |
| 318 /** |
| 319 * Deserializes a SerializedDataSender. |
| 320 * @param {SerializedDataSender} serialized The serialized DataSender. |
| 321 * @return {!DataSender} The deserialized DataSender. |
| 322 */ |
| 323 DataSender.deserialize = function(serialized) { |
| 324 var sender = $Object.create(DataSender.prototype); |
| 325 sender.deserialize_(serialized); |
| 326 return sender; |
| 327 }; |
| 328 |
| 329 /** |
| 330 * Deserializes a SerializedDataSender into this DataSender. |
| 331 * @param {SerializedDataSender} serialized The serialized DataSender. |
| 332 * @private |
| 333 */ |
| 334 DataSender.prototype.deserialize_ = function(serialized) { |
| 335 if (!serialized) { |
| 336 this.shutDown_ = true; |
247 return; | 337 return; |
248 this.shutDown_ = true; | |
249 this.waiter_.stop(); | |
250 this.router_.close(); | |
251 core.close(this.sendPipe_); | |
252 while (this.pendingSends_.length) { | |
253 this.pendingSends_.pop().reportBytesSentAndError( | |
254 0, this.fatalErrorValue_); | |
255 } | 338 } |
256 while (this.sendsAwaitingAck_.length) { | 339 this.init_( |
257 this.sendsAwaitingAck_.pop().reportBytesSentAndError( | 340 serialized.sink, serialized.data_pipe, serialized.fatal_error_value); |
258 0, this.fatalErrorValue_); | |
259 } | |
260 if (this.pendingCancel_) { | |
261 this.pendingCancel_(); | |
262 this.pendingCancel_ = null; | |
263 } | |
264 }; | 341 }; |
265 | 342 |
266 /** | 343 /** |
267 * Sends data to the DataSink. | 344 * Sends data to the DataSink. |
268 * @return {Promise.<number>} A promise to the number of bytes sent. If an | 345 * @return {!Promise.<number>} A promise to the number of bytes sent. If an |
269 * error occurs, the promise will reject with an Error object with a | 346 * error occurs, the promise will reject with an Error object with a |
270 * property error containing the error code. | 347 * property error containing the error code. |
271 * @throws Will throw if this has encountered a fatal error or a cancel is in | 348 * @throws Will throw if this has encountered a fatal error or a cancel is in |
272 * progress. | 349 * progress. |
273 */ | 350 */ |
274 DataSender.prototype.send = function(data) { | 351 DataSender.prototype.send = function(data) { |
275 if (this.shutDown_) | 352 if (this.shutDown_) |
276 throw new Error('DataSender has been closed'); | 353 throw new Error('DataSender has been closed'); |
277 if (this.pendingCancel_) | 354 if (this.pendingCancel_) |
278 throw new Error('Cancel in progress'); | 355 throw new Error('Cancel in progress'); |
279 var send = new PendingSend(data); | 356 var send = new PendingSend(data); |
280 this.pendingSends_.push(send); | 357 this.pendingSends_.push(send); |
281 if (!this.waiter_.isWaiting()) | 358 if (!this.waiter_.isWaiting()) |
282 this.waiter_.start(); | 359 this.waiter_.start(); |
283 return send.getPromise(); | 360 return send.getPromise(); |
284 }; | 361 }; |
285 | 362 |
286 /** | 363 /** |
287 * Requests the cancellation of any in-progress sends. Calls to | 364 * Requests the cancellation of any in-progress sends. Calls to |
288 * [send()]{@link module:data_sender.DataSender#send} will fail until the | 365 * [send()]{@link module:data_sender.DataSender#send} will fail until the |
289 * cancel has completed. | 366 * cancel has completed. |
290 * @param {number} error The error to report for cancelled sends. | 367 * @param {number} error The error to report for cancelled sends. |
291 * @return {Promise} A promise that will resolve when the cancel completes. | 368 * @return {!Promise} A promise that will resolve when the cancel completes. |
292 * @throws Will throw if this has encountered a fatal error or another cancel | 369 * @throws Will throw if this has encountered a fatal error or another cancel |
293 * is in progress. | 370 * is in progress. |
294 */ | 371 */ |
295 DataSender.prototype.cancel = function(error) { | 372 DataSender.prototype.cancel = function(error) { |
296 if (this.shutDown_) | 373 if (this.shutDown_) |
297 throw new Error('DataSender has been closed'); | 374 throw new Error('DataSender has been closed'); |
298 if (this.pendingCancel_) | 375 if (this.pendingCancel_) |
299 throw new Error('Cancel already in progress'); | 376 throw new Error('Cancel already in progress'); |
300 if (this.pendingSends_.length + this.sendsAwaitingAck_.length == 0) | 377 if (this.pendingSends_.length + this.sendsAwaitingAck_.length == 0) |
301 return Promise.resolve(); | 378 return Promise.resolve(); |
302 | 379 |
303 this.sink_.cancel(error); | 380 this.sink_.cancel(error); |
304 return new Promise(function(resolve) { | 381 this.cancelPromise_ = new Promise(function(resolve) { |
305 this.pendingCancel_ = resolve; | 382 this.pendingCancel_ = resolve; |
306 }.bind(this)); | 383 }.bind(this)); |
| 384 return this.cancelPromise_; |
307 }; | 385 }; |
308 | 386 |
309 /** | 387 /** |
310 * Invoked when |handle_| is ready to write. Writes to the data pipe if the | 388 * Invoked when |
311 * wait is successful. | 389 * |[sendPipe_]{@link module:data_sender.DataSender#sendPipe_}| is ready to |
| 390 * write. Writes to the data pipe if the wait is successful. |
312 * @param {number} waitResult The result of the asynchronous wait. | 391 * @param {number} waitResult The result of the asynchronous wait. |
313 * @private | 392 * @private |
314 */ | 393 */ |
315 DataSender.prototype.onHandleReady_ = function(result) { | 394 DataSender.prototype.onHandleReady_ = function(result) { |
316 if (result != core.RESULT_OK) { | 395 if (result != core.RESULT_OK) { |
317 this.close(); | 396 this.close(); |
318 return; | 397 return; |
319 } | 398 } |
320 while (this.pendingSends_.length) { | 399 while (this.pendingSends_.length) { |
321 var result = this.pendingSends_[0].sendData(this.sendPipe_); | 400 var result = this.pendingSends_[0].sendData(this.sendPipe_); |
322 if (result == core.RESULT_OK) { | 401 if (result == core.RESULT_OK) { |
323 this.sendsAwaitingAck_.push(this.pendingSends_.shift()); | 402 this.sendsAwaitingAck_.push(this.pendingSends_.shift()); |
324 } else if (result == core.RESULT_SHOULD_WAIT) { | 403 } else if (result == core.RESULT_SHOULD_WAIT) { |
325 this.waiter_.start(); | 404 this.waiter_.start(); |
326 return; | 405 return; |
327 } else { | 406 } else { |
328 this.close(); | 407 this.close(); |
329 return; | 408 return; |
330 } | 409 } |
331 } | 410 } |
332 }; | 411 }; |
333 | 412 |
334 /** | 413 /** |
335 * Calls and clears the pending cancel callback if one is pending. | 414 * Calls and clears the pending cancel callback if one is pending. |
336 * @private | 415 * @private |
337 */ | 416 */ |
338 DataSender.prototype.callCancelCallback_ = function() { | 417 DataSender.prototype.callCancelCallback_ = function() { |
339 if (this.pendingCancel_) { | 418 if (this.pendingCancel_) { |
| 419 this.cancelPromise_ = null; |
340 this.pendingCancel_(); | 420 this.pendingCancel_(); |
341 this.pendingCancel_ = null; | 421 this.pendingCancel_ = null; |
342 } | 422 } |
343 }; | 423 }; |
344 | 424 |
345 /** | 425 /** |
346 * Invoked by the DataSink to report that data has been successfully sent. | 426 * Invoked by the DataSink to report that data has been successfully sent. |
347 * @param {number} numBytes The number of bytes sent. | 427 * @param {number} numBytes The number of bytes sent. |
348 * @private | 428 * @private |
349 */ | 429 */ |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 // Note: Only the first PendingSend in |pendingSends_| will have data to | 468 // Note: Only the first PendingSend in |pendingSends_| will have data to |
389 // flush as only the first can have written data to the data pipe. | 469 // flush as only the first can have written data to the data pipe. |
390 bytesToFlush += result.bytesToFlush; | 470 bytesToFlush += result.bytesToFlush; |
391 } | 471 } |
392 this.callCancelCallback_(); | 472 this.callCancelCallback_(); |
393 return Promise.resolve({bytes_to_flush: bytesToFlush}); | 473 return Promise.resolve({bytes_to_flush: bytesToFlush}); |
394 }; | 474 }; |
395 | 475 |
396 return {DataSender: DataSender}; | 476 return {DataSender: DataSender}; |
397 }); | 477 }); |
OLD | NEW |