Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / renderer / resources / data_receiver.js
bloba248ffbf4be0dfc311fde9eb29a6d56fab5f524f
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',
8     'mojo/public/js/core',
9     'mojo/public/js/router',
10 ], function(dataStream, serialization, core, router) {
11   /**
12    * @module data_receiver
13    */
15   /**
16    * A pending receive operation.
17    * @constructor
18    * @alias module:data_receiver~PendingReceive
19    * @private
20    */
21   function PendingReceive() {
22     /**
23      * The promise that will be resolved or rejected when this receive completes
24      * or fails, respectively.
25      * @type {!Promise<ArrayBuffer>}
26      * @private
27      */
28     this.promise_ = new Promise(function(resolve, reject) {
29       /**
30        * The callback to call with the data received on success.
31        * @type {Function}
32        * @private
33        */
34       this.dataCallback_ = resolve;
35       /**
36        * The callback to call with the error on failure.
37        * @type {Function}
38        * @private
39        */
40       this.errorCallback_ = reject;
41     }.bind(this));
42   }
44   /**
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.
48    */
49   PendingReceive.prototype.getPromise = function() {
50     return this.promise_;
51   };
53   /**
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.
57    */
58   PendingReceive.prototype.dispatchData = function(data) {
59     this.dataCallback_(data);
60   };
62   /**
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.
66    */
67   PendingReceive.prototype.dispatchError = function(error) {
68     if (error.queuePosition > 0)
69       return false;
71     var e = new Error();
72     e.error = error.error;
73     this.errorCallback_(e);
74     return true;
75   };
77   /**
78    * Unconditionally dispatches an error.
79    * @param {number} error The error to dispatch.
80    */
81   PendingReceive.prototype.dispatchFatalError = function(error) {
82     var e = new Error();
83     e.error = error;
84     this.errorCallback_(e);
85   };
87   /**
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.
94    * @constructor
95    * @alias module:data_receiver.DataReceiver
96    */
97   function DataReceiver(source, client, bufferSize, fatalErrorValue) {
98     this.init_(source, client, fatalErrorValue, 0, null, [], false);
99     this.source_.init(bufferSize);
100   }
102   DataReceiver.prototype =
103       $Object.create(dataStream.DataSourceClient.stubClass.prototype);
105   /**
106    * Closes this DataReceiver.
107    */
108   DataReceiver.prototype.close = function() {
109     if (this.shutDown_)
110       return;
111     this.shutDown_ = true;
112     this.router_.close();
113     this.clientRouter_.close();
114     if (this.receive_) {
115       this.receive_.dispatchFatalError(this.fatalErrorValue_);
116       this.receive_ = null;
117     }
118   };
120   /**
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
125    *     fatal error.
126    * @param {number} bytesReceived The number of bytes already received.
127    * @param {PendingReceiveError} pendingError The pending error if there is
128    *     one.
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.
132    * @private
133    */
134   DataReceiver.prototype.init_ = function(source, client, fatalErrorValue,
135                                           bytesReceived, pendingError,
136                                           pendingData, paused) {
137     /**
138      * The [Router]{@link module:mojo/public/js/router.Router} for the
139      * connection to the DataSource.
140      * @private
141      */
142     this.router_ = new router.Router(source);
143     /**
144      * The [Router]{@link module:mojo/public/js/router.Router} for the
145      * connection to the DataSource.
146      * @private
147      */
148     this.clientRouter_ = new router.Router(client);
149     /**
150      * The connection to the DataSource.
151      * @private
152      */
153     this.source_ = new dataStream.DataSource.proxyClass(this.router_);
154     this.client_ = new dataStream.DataSourceClient.stubClass(this);
155     this.clientRouter_.setIncomingReceiver(this.client_);
156     /**
157      * The current receive operation.
158      * @type {module:data_receiver~PendingReceive}
159      * @private
160      */
161     this.receive_ = null;
162     /**
163      * The error to be dispatched in the event of a fatal error.
164      * @const {number}
165      * @private
166      */
167     this.fatalErrorValue_ = fatalErrorValue;
168     /**
169      * The pending error if there is one.
170      * @type {PendingReceiveError}
171      * @private
172      */
173     this.pendingError_ = pendingError;
174     /**
175      * Whether the DataSource is paused.
176      * @type {boolean}
177      * @private
178      */
179     this.paused_ = paused;
180     /**
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[]}
184      * @private
185      */
186     this.pendingDataBuffers_ = pendingData;
187     /**
188      * Whether this DataReceiver has shut down.
189      * @type {boolean}
190      * @private
191      */
192     this.shutDown_ = false;
193   };
195   /**
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.
201    */
202   DataReceiver.prototype.serialize = function() {
203     if (this.shutDown_)
204       return Promise.resolve(null);
206     if (this.receive_) {
207       this.receive_.dispatchFatalError(this.fatalErrorValue_);
208       this.receive_ = null;
209     }
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));
219     });
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);
226   };
228   /**
229    * Deserializes a SerializedDataReceiver.
230    * @param {SerializedDataReceiver} serialized The serialized DataReceiver.
231    * @return {!DataReceiver} The deserialized DataReceiver.
232    */
233   DataReceiver.deserialize = function(serialized) {
234     var receiver = $Object.create(DataReceiver.prototype);
235     receiver.deserialize_(serialized);
236     return receiver;
237   };
239   /**
240    * Deserializes a SerializedDataReceiver into this DataReceiver.
241    * @param {SerializedDataReceiver} serialized The serialized DataReceiver.
242    * @private
243    */
244   DataReceiver.prototype.deserialize_ = function(serialized) {
245     if (!serialized) {
246       this.shutDown_ = true;
247       return;
248     }
249     var pendingData = [];
250     $Array.forEach(serialized.pending_data, function(data) {
251       var buffer = new Uint8Array(data.length);
252       buffer.set(data);
253       pendingData.push(buffer.buffer);
254     });
255     this.init_(serialized.source, serialized.client,
256                serialized.fatal_error_value, serialized.bytes_received,
257                serialized.pending_error, pendingData, serialized.paused);
258   };
260   /**
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
266    *     is in progress.
267    */
268   DataReceiver.prototype.receive = function() {
269     if (this.shutDown_)
270       throw new Error('DataReceiver has been closed');
271     if (this.receive_)
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;
278       this.paused_ = true;
279       return promise;
280     }
281     if (this.paused_) {
282       this.source_.resume();
283       this.paused_ = false;
284     }
285     this.receive_ = receive;
286     this.dispatchData_();
287     return promise;
288   };
290   DataReceiver.prototype.dispatchData_ = function() {
291     if (!this.receive_) {
292       this.close();
293       return;
294     }
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--;
302     }
303   };
305   /**
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.
309    * @private
310    */
311   DataReceiver.prototype.onError = function(error) {
312     if (this.shutDown_)
313       return;
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;
320       this.paused_ = true;
321       return;
322     }
323     this.pendingError_ = pendingError;
324   };
326   DataReceiver.prototype.onData = function(data) {
327     var buffer = new ArrayBuffer(data.length);
328     var uintView = new Uint8Array(buffer);
329     uintView.set(data);
330     this.pendingDataBuffers_.push(buffer);
331     if (this.receive_)
332       this.dispatchData_();
333   };
335   return {DataReceiver: DataReceiver};