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_receiver', [ | 5 define('data_receiver', [ |
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(dataStream, serialization, core, router) { | 10 ], function(dataStream, serialization, core, router) { |
11 /** | 11 /** |
12 * @module data_receiver | 12 * @module data_receiver |
13 */ | 13 */ |
14 | 14 |
15 /** | 15 /** |
16 * A pending receive operation. | 16 * A pending receive operation. |
17 * @constructor | 17 * @constructor |
18 * @alias module:data_receiver~PendingReceive | 18 * @alias module:data_receiver~PendingReceive |
19 * @private | 19 * @private |
20 */ | 20 */ |
21 function PendingReceive() { | 21 function PendingReceive() { |
22 /** | 22 /** |
23 * The promise that will be resolved or rejected when this receive completes | 23 * The promise that will be resolved or rejected when this receive completes |
24 * or fails, respectively. | 24 * or fails, respectively. |
25 * @type {!Promise.<ArrayBuffer>} | 25 * @type {!Promise<ArrayBuffer>} |
26 * @private | 26 * @private |
27 */ | 27 */ |
28 this.promise_ = new Promise(function(resolve, reject) { | 28 this.promise_ = new Promise(function(resolve, reject) { |
29 /** | 29 /** |
30 * The callback to call with the data received on success. | 30 * The callback to call with the data received on success. |
31 * @type {Function} | 31 * @type {Function} |
32 * @private | 32 * @private |
33 */ | 33 */ |
34 this.dataCallback_ = resolve; | 34 this.dataCallback_ = resolve; |
35 /** | 35 /** |
36 * The callback to call with the error on failure. | 36 * The callback to call with the error on failure. |
37 * @type {Function} | 37 * @type {Function} |
38 * @private | 38 * @private |
39 */ | 39 */ |
40 this.errorCallback_ = reject; | 40 this.errorCallback_ = reject; |
41 }.bind(this)); | 41 }.bind(this)); |
42 } | 42 } |
43 | 43 |
44 /** | 44 /** |
45 * Returns the promise that will be resolved when this operation completes or | 45 * Returns the promise that will be resolved when this operation completes or |
46 * rejected if an error occurs. | 46 * rejected if an error occurs. |
47 * @return {Promise.<ArrayBuffer>} A promise to the data received. | 47 * @return {Promise<ArrayBuffer>} A promise to the data received. |
48 */ | 48 */ |
49 PendingReceive.prototype.getPromise = function() { | 49 PendingReceive.prototype.getPromise = function() { |
50 return this.promise_; | 50 return this.promise_; |
51 }; | 51 }; |
52 | 52 |
53 /** | 53 /** |
54 * Dispatches received data to the promise returned by | 54 * Dispatches received data to the promise returned by |
55 * [getPromise]{@link module:data_receiver.PendingReceive#getPromise}. | 55 * [getPromise]{@link module:data_receiver.PendingReceive#getPromise}. |
56 * @param {!ArrayBuffer} data The data to dispatch. | 56 * @param {!ArrayBuffer} data The data to dispatch. |
57 */ | 57 */ |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 119 |
120 /** | 120 /** |
121 * Initialize this DataReceiver. | 121 * Initialize this DataReceiver. |
122 * @param {!MojoHandle} source A handle to the DataSource. | 122 * @param {!MojoHandle} source A handle to the DataSource. |
123 * @param {!MojoHandle} client A handle to the DataSourceClient. | 123 * @param {!MojoHandle} client A handle to the DataSourceClient. |
124 * @param {number} fatalErrorValue The error to dispatch in the event of a | 124 * @param {number} fatalErrorValue The error to dispatch in the event of a |
125 * fatal error. | 125 * fatal error. |
126 * @param {number} bytesReceived The number of bytes already received. | 126 * @param {number} bytesReceived The number of bytes already received. |
127 * @param {PendingReceiveError} pendingError The pending error if there is | 127 * @param {PendingReceiveError} pendingError The pending error if there is |
128 * one. | 128 * one. |
129 * @param {!Array.<!ArrayBuffer>} pendingData Data received from the | 129 * @param {!Array<!ArrayBuffer>} pendingData Data received from the |
130 * DataSource not yet requested by the client. | 130 * DataSource not yet requested by the client. |
131 * @param {boolean} paused Whether the DataSource is paused. | 131 * @param {boolean} paused Whether the DataSource is paused. |
132 * @private | 132 * @private |
133 */ | 133 */ |
134 DataReceiver.prototype.init_ = function(source, client, fatalErrorValue, | 134 DataReceiver.prototype.init_ = function(source, client, fatalErrorValue, |
135 bytesReceived, pendingError, | 135 bytesReceived, pendingError, |
136 pendingData, paused) { | 136 pendingData, paused) { |
137 /** | 137 /** |
138 * The [Router]{@link module:mojo/public/js/router.Router} for the | 138 * The [Router]{@link module:mojo/public/js/router.Router} for the |
139 * connection to the DataSource. | 139 * connection to the DataSource. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 * Whether this DataReceiver has shut down. | 188 * Whether this DataReceiver has shut down. |
189 * @type {boolean} | 189 * @type {boolean} |
190 * @private | 190 * @private |
191 */ | 191 */ |
192 this.shutDown_ = false; | 192 this.shutDown_ = false; |
193 }; | 193 }; |
194 | 194 |
195 /** | 195 /** |
196 * Serializes this DataReceiver. | 196 * Serializes this DataReceiver. |
197 * This will cancel a receive if one is in progress. | 197 * This will cancel a receive if one is in progress. |
198 * @return {!Promise.<SerializedDataReceiver>} A promise that will resolve to | 198 * @return {!Promise<SerializedDataReceiver>} A promise that will resolve to |
199 * the serialization of this DataReceiver. If this DataReceiver has shut | 199 * the serialization of this DataReceiver. If this DataReceiver has shut |
200 * down, the promise will resolve to null. | 200 * down, the promise will resolve to null. |
201 */ | 201 */ |
202 DataReceiver.prototype.serialize = function() { | 202 DataReceiver.prototype.serialize = function() { |
203 if (this.shutDown_) | 203 if (this.shutDown_) |
204 return Promise.resolve(null); | 204 return Promise.resolve(null); |
205 | 205 |
206 if (this.receive_) { | 206 if (this.receive_) { |
207 this.receive_.dispatchFatalError(this.fatalErrorValue_); | 207 this.receive_.dispatchFatalError(this.fatalErrorValue_); |
208 this.receive_ = null; | 208 this.receive_ = null; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 buffer.set(data); | 252 buffer.set(data); |
253 pendingData.push(buffer.buffer); | 253 pendingData.push(buffer.buffer); |
254 }); | 254 }); |
255 this.init_(serialized.source, serialized.client, | 255 this.init_(serialized.source, serialized.client, |
256 serialized.fatal_error_value, serialized.bytes_received, | 256 serialized.fatal_error_value, serialized.bytes_received, |
257 serialized.pending_error, pendingData, serialized.paused); | 257 serialized.pending_error, pendingData, serialized.paused); |
258 }; | 258 }; |
259 | 259 |
260 /** | 260 /** |
261 * Receive data from the DataSource. | 261 * Receive data from the DataSource. |
262 * @return {Promise.<ArrayBuffer>} A promise to the received data. If an error | 262 * @return {Promise<ArrayBuffer>} A promise to the received data. If an error |
263 * occurs, the promise will reject with an Error object with a property | 263 * occurs, the promise will reject with an Error object with a property |
264 * error containing the error code. | 264 * error containing the error code. |
265 * @throws Will throw if this has encountered a fatal error or another receive | 265 * @throws Will throw if this has encountered a fatal error or another receive |
266 * is in progress. | 266 * is in progress. |
267 */ | 267 */ |
268 DataReceiver.prototype.receive = function() { | 268 DataReceiver.prototype.receive = function() { |
269 if (this.shutDown_) | 269 if (this.shutDown_) |
270 throw new Error('DataReceiver has been closed'); | 270 throw new Error('DataReceiver has been closed'); |
271 if (this.receive_) | 271 if (this.receive_) |
272 throw new Error('Receive already in progress.'); | 272 throw new Error('Receive already in progress.'); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 var buffer = new ArrayBuffer(data.length); | 327 var buffer = new ArrayBuffer(data.length); |
328 var uintView = new Uint8Array(buffer); | 328 var uintView = new Uint8Array(buffer); |
329 uintView.set(data); | 329 uintView.set(data); |
330 this.pendingDataBuffers_.push(buffer); | 330 this.pendingDataBuffers_.push(buffer); |
331 if (this.receive_) | 331 if (this.receive_) |
332 this.dispatchData_(); | 332 this.dispatchData_(); |
333 }; | 333 }; |
334 | 334 |
335 return {DataReceiver: DataReceiver}; | 335 return {DataReceiver: DataReceiver}; |
336 }); | 336 }); |
OLD | NEW |