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
3 // found in the LICENSE file.
5 define('data_receiver', [
6 'device/serial/data_stream.mojom',
7 'device/serial/data_stream_serialization.mojom',
9 'mojo/public/js/router',
10 ], function(dataStream
, serialization
, core
, router
) {
12 * @module data_receiver
16 * A pending receive operation.
18 * @alias module:data_receiver~PendingReceive
21 function PendingReceive() {
23 * The promise that will be resolved or rejected when this receive completes
24 * or fails, respectively.
25 * @type {!Promise<ArrayBuffer>}
28 this.promise_
= new Promise(function(resolve
, reject
) {
30 * The callback to call with the data received on success.
34 this.dataCallback_
= resolve
;
36 * The callback to call with the error on failure.
40 this.errorCallback_
= reject
;
45 * Returns the promise that will be resolved when this operation completes or
46 * rejected if an error occurs.
47 * @return {Promise<ArrayBuffer>} A promise to the data received.
49 PendingReceive
.prototype.getPromise = function() {
54 * Dispatches received data to the promise returned by
55 * [getPromise]{@link module:data_receiver.PendingReceive#getPromise}.
56 * @param {!ArrayBuffer} data The data to dispatch.
58 PendingReceive
.prototype.dispatchData = function(data
) {
59 this.dataCallback_(data
);
63 * Dispatches an error if the offset of the error has been reached.
64 * @param {!PendingReceiveError} error The error to dispatch.
65 * @param {number} bytesReceived The number of bytes that have been received.
67 PendingReceive
.prototype.dispatchError = function(error
) {
68 if (error
.queuePosition
> 0)
72 e
.error
= error
.error
;
73 this.errorCallback_(e
);
78 * Unconditionally dispatches an error.
79 * @param {number} error The error to dispatch.
81 PendingReceive
.prototype.dispatchFatalError = function(error
) {
84 this.errorCallback_(e
);
88 * A DataReceiver that receives data from a DataSource.
89 * @param {!MojoHandle} source The handle to the DataSource.
90 * @param {!MojoHandle} client The handle to the DataSourceClient.
91 * @param {number} bufferSize How large a buffer to use.
92 * @param {number} fatalErrorValue The receive error value to report in the
93 * event of a fatal error.
95 * @alias module:data_receiver.DataReceiver
97 function DataReceiver(source
, client
, bufferSize
, fatalErrorValue
) {
98 this.init_(source
, client
, fatalErrorValue
, 0, null, [], false);
99 this.source_
.init(bufferSize
);
102 DataReceiver
.prototype =
103 $Object
.create(dataStream
.DataSourceClient
.stubClass
.prototype);
106 * Closes this DataReceiver.
108 DataReceiver
.prototype.close = function() {
111 this.shutDown_
= true;
112 this.router_
.close();
113 this.clientRouter_
.close();
115 this.receive_
.dispatchFatalError(this.fatalErrorValue_
);
116 this.receive_
= null;
121 * Initialize this DataReceiver.
122 * @param {!MojoHandle} source A handle to the DataSource.
123 * @param {!MojoHandle} client A handle to the DataSourceClient.
124 * @param {number} fatalErrorValue The error to dispatch in the event of a
126 * @param {number} bytesReceived The number of bytes already received.
127 * @param {PendingReceiveError} pendingError The pending error if there is
129 * @param {!Array<!ArrayBuffer>} pendingData Data received from the
130 * DataSource not yet requested by the client.
131 * @param {boolean} paused Whether the DataSource is paused.
134 DataReceiver
.prototype.init_ = function(source
, client
, fatalErrorValue
,
135 bytesReceived
, pendingError
,
136 pendingData
, paused
) {
138 * The [Router]{@link module:mojo/public/js/router.Router} for the
139 * connection to the DataSource.
142 this.router_
= new router
.Router(source
);
144 * The [Router]{@link module:mojo/public/js/router.Router} for the
145 * connection to the DataSource.
148 this.clientRouter_
= new router
.Router(client
);
150 * The connection to the DataSource.
153 this.source_
= new dataStream
.DataSource
.proxyClass(this.router_
);
154 this.client_
= new dataStream
.DataSourceClient
.stubClass(this);
155 this.clientRouter_
.setIncomingReceiver(this.client_
);
157 * The current receive operation.
158 * @type {module:data_receiver~PendingReceive}
161 this.receive_
= null;
163 * The error to be dispatched in the event of a fatal error.
167 this.fatalErrorValue_
= fatalErrorValue
;
169 * The pending error if there is one.
170 * @type {PendingReceiveError}
173 this.pendingError_
= pendingError
;
175 * Whether the DataSource is paused.
179 this.paused_
= paused
;
181 * A queue of data that has been received from the DataSource, but not
182 * consumed by the client.
183 * @type {module:data_receiver~PendingData[]}
186 this.pendingDataBuffers_
= pendingData
;
188 * Whether this DataReceiver has shut down.
192 this.shutDown_
= false;
196 * Serializes this DataReceiver.
197 * This will cancel a receive if one is in progress.
198 * @return {!Promise<SerializedDataReceiver>} A promise that will resolve to
199 * the serialization of this DataReceiver. If this DataReceiver has shut
200 * down, the promise will resolve to null.
202 DataReceiver
.prototype.serialize = function() {
204 return Promise
.resolve(null);
207 this.receive_
.dispatchFatalError(this.fatalErrorValue_
);
208 this.receive_
= null;
210 var serialized
= new serialization
.SerializedDataReceiver();
211 serialized
.source
= this.router_
.connector_
.handle_
;
212 serialized
.client
= this.clientRouter_
.connector_
.handle_
;
213 serialized
.fatal_error_value
= this.fatalErrorValue_
;
214 serialized
.paused
= this.paused_
;
215 serialized
.pending_error
= this.pendingError_
;
216 serialized
.pending_data
= [];
217 $Array
.forEach(this.pendingDataBuffers_
, function(buffer
) {
218 serialized
.pending_data
.push(new Uint8Array(buffer
));
220 this.router_
.connector_
.handle_
= null;
221 this.router_
.close();
222 this.clientRouter_
.connector_
.handle_
= null;
223 this.clientRouter_
.close();
224 this.shutDown_
= true;
225 return Promise
.resolve(serialized
);
229 * Deserializes a SerializedDataReceiver.
230 * @param {SerializedDataReceiver} serialized The serialized DataReceiver.
231 * @return {!DataReceiver} The deserialized DataReceiver.
233 DataReceiver
.deserialize = function(serialized
) {
234 var receiver
= $Object
.create(DataReceiver
.prototype);
235 receiver
.deserialize_(serialized
);
240 * Deserializes a SerializedDataReceiver into this DataReceiver.
241 * @param {SerializedDataReceiver} serialized The serialized DataReceiver.
244 DataReceiver
.prototype.deserialize_ = function(serialized
) {
246 this.shutDown_
= true;
249 var pendingData
= [];
250 $Array
.forEach(serialized
.pending_data
, function(data
) {
251 var buffer
= new Uint8Array(data
.length
);
253 pendingData
.push(buffer
.buffer
);
255 this.init_(serialized
.source
, serialized
.client
,
256 serialized
.fatal_error_value
, serialized
.bytes_received
,
257 serialized
.pending_error
, pendingData
, serialized
.paused
);
261 * Receive data from the DataSource.
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
264 * error containing the error code.
265 * @throws Will throw if this has encountered a fatal error or another receive
268 DataReceiver
.prototype.receive = function() {
270 throw new Error('DataReceiver has been closed');
272 throw new Error('Receive already in progress.');
273 var receive
= new PendingReceive();
274 var promise
= receive
.getPromise();
275 if (this.pendingError_
&&
276 receive
.dispatchError(this.pendingError_
)) {
277 this.pendingError_
= null;
282 this.source_
.resume();
283 this.paused_
= false;
285 this.receive_
= receive
;
286 this.dispatchData_();
290 DataReceiver
.prototype.dispatchData_ = function() {
291 if (!this.receive_
) {
295 if (this.pendingDataBuffers_
.length
) {
296 this.receive_
.dispatchData(this.pendingDataBuffers_
[0]);
297 this.source_
.reportBytesReceived(this.pendingDataBuffers_
[0].byteLength
);
298 this.receive_
= null;
299 this.pendingDataBuffers_
.shift();
300 if (this.pendingError_
)
301 this.pendingError_
.queuePosition
--;
306 * Invoked by the DataSource when an error is encountered.
307 * @param {number} offset The location at which the error occurred.
308 * @param {number} error The error that occurred.
311 DataReceiver
.prototype.onError = function(error
) {
315 var pendingError
= new serialization
.PendingReceiveError();
316 pendingError
.error
= error
;
317 pendingError
.queuePosition
= this.pendingDataBuffers_
.length
;
318 if (this.receive_
&& this.receive_
.dispatchError(pendingError
)) {
319 this.receive_
= null;
323 this.pendingError_
= pendingError
;
326 DataReceiver
.prototype.onData = function(data
) {
327 var buffer
= new ArrayBuffer(data
.length
);
328 var uintView
= new Uint8Array(buffer
);
330 this.pendingDataBuffers_
.push(buffer
);
332 this.dispatchData_();
335 return {DataReceiver
: DataReceiver
};