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 '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, dataStream, core, router) { | 11 ], function(asyncWaiter, dataStream, serialization, core, router) { |
11 /** | 12 /** |
12 * @module data_receiver | 13 * @module data_receiver |
13 */ | 14 */ |
14 | 15 |
15 /** | 16 /** |
16 * @typedef module:data_receiver~PendingError | 17 * @typedef module:data_receiver~PendingError |
17 * @type {Object} | 18 * @type {Object} |
18 * @property {number} error - the error | 19 * @property {number} error - the error |
19 * @property {number} offset - the location of the error | 20 * @property {number} offset - the location of the error |
20 * @private | 21 * @private |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 this.router_.close(); | 187 this.router_.close(); |
187 this.waiter_.stop(); | 188 this.waiter_.stop(); |
188 core.close(this.receivePipe_); | 189 core.close(this.receivePipe_); |
189 if (this.receive_) { | 190 if (this.receive_) { |
190 this.receive_.dispatchFatalError(this.fatalErrorValue_); | 191 this.receive_.dispatchFatalError(this.fatalErrorValue_); |
191 this.receive_ = null; | 192 this.receive_ = null; |
192 } | 193 } |
193 }; | 194 }; |
194 | 195 |
195 /** | 196 /** |
197 * Serializes this DataReceiver. | |
198 * @return {Promise.<?SerializedDataReceiver>} The serialization of this | |
raymes
2014/09/18 03:16:06
Well really it returns a promise that resolves to
Sam McNally
2014/09/19 04:58:46
Done.
| |
199 * DataReceiver. If this DataReceiver has shut down, this will return | |
200 * null. | |
201 */ | |
202 DataReceiver.prototype.serialize = function() { | |
203 if (this.shutDown_) | |
204 return Promise.resolve(null); | |
raymes
2014/09/18 03:16:06
How come you decided to return null in this case r
Sam McNally
2014/09/19 04:58:46
None of the other state is useful after shutdown.
| |
205 | |
206 this.waiter_.stop(); | |
207 if (this.receive_) { | |
208 this.receive_.dispatchFatalError(this.fatalErrorValue_); | |
209 this.receive_ = null; | |
210 } | |
211 var serialized = new serialization.SerializedDataReceiver(); | |
212 serialized.source = this.router_.connector_.handle_; | |
213 serialized.data_pipe = this.receivePipe_; | |
214 serialized.fatal_error_value = this.fatalErrorValue_; | |
215 serialized.bytes_received = this.bytesReceived_; | |
216 serialized.paused = this.paused_; | |
217 this.router_.connector_.handle_ = null; | |
218 this.router_.close(); | |
219 this.shutDown_ = true; | |
220 return Promise.resolve(serialized); | |
221 }; | |
222 | |
223 /** | |
224 * Deserializes a SerializedDataReceiver. | |
225 * @param {?SerializedDataReceiver} serialized The serialized DataReceiver. | |
226 * @return {DataReceiver} The deserialized DataReceiver. | |
227 */ | |
228 DataReceiver.deserialize = function(serialized) { | |
229 var receiver = $Object.create(DataReceiver.prototype); | |
230 receiver.deserialize_(serialized); | |
231 return receiver; | |
232 }; | |
233 | |
234 /** | |
235 * Deserializes a SerializedDataReceiver into this DataReceiver. | |
236 * @param {?SerializedDataReceiver} serialized The serialized DataReceiver. | |
237 * @private | |
238 */ | |
239 DataReceiver.prototype.deserialize_ = function(serialized) { | |
240 if (!serialized) { | |
241 this.shutDown_ = true; | |
242 return; | |
243 } | |
244 this.receivePipe_ = serialized.data_pipe; | |
245 this.fatalErrorValue_ = serialized.fatal_error_value; | |
246 this.bytesReceived_ = serialized.bytes_received; | |
247 this.paused_ = serialized.paused; | |
248 this.shutDown_ = false; | |
249 this.router_ = new router.Router(serialized.source); | |
250 this.router_.setIncomingReceiver(this); | |
251 this.source_ = new dataStream.DataSourceProxy(this.router_); | |
252 this.waiter_ = new asyncWaiter.AsyncWaiter(this.receivePipe_, | |
253 core.HANDLE_SIGNAL_READABLE, | |
254 this.onHandleReady_.bind(this)); | |
255 }; | |
256 | |
257 /** | |
196 * Receive data from the DataSource. | 258 * Receive data from the DataSource. |
197 * @return {Promise.<ArrayBuffer>} A promise to the received data. If an error | 259 * @return {Promise.<ArrayBuffer>} A promise to the received data. If an error |
198 * occurs, the promise will reject with an Error object with a property | 260 * occurs, the promise will reject with an Error object with a property |
199 * error containing the error code. | 261 * error containing the error code. |
200 * @throws Will throw if this has encountered a fatal error or another receive | 262 * @throws Will throw if this has encountered a fatal error or another receive |
201 * is in progress. | 263 * is in progress. |
202 */ | 264 */ |
203 DataReceiver.prototype.receive = function() { | 265 DataReceiver.prototype.receive = function() { |
204 if (this.shutDown_) | 266 if (this.shutDown_) |
205 throw new Error('System error'); | 267 throw new Error('DataReceiver has been closed'); |
206 if (this.receive_) | 268 if (this.receive_) |
207 throw new Error('Receive already in progress.'); | 269 throw new Error('Receive already in progress.'); |
208 var receive = new PendingReceive(); | 270 var receive = new PendingReceive(); |
209 var promise = receive.getPromise(); | 271 var promise = receive.getPromise(); |
210 if (this.pendingError_ && | 272 if (this.pendingError_ && |
211 receive.dispatchError(this.pendingError_, this.bytesReceived_)) { | 273 receive.dispatchError(this.pendingError_, this.bytesReceived_)) { |
212 this.pendingError_ = null; | 274 this.pendingError_ = null; |
213 this.paused_ = true; | 275 this.paused_ = true; |
214 return promise; | 276 return promise; |
215 } | 277 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 this.receive_ = null; | 330 this.receive_ = null; |
269 this.waiter_.stop(); | 331 this.waiter_.stop(); |
270 this.paused_ = true; | 332 this.paused_ = true; |
271 return; | 333 return; |
272 } | 334 } |
273 this.pendingError_ = pendingError; | 335 this.pendingError_ = pendingError; |
274 }; | 336 }; |
275 | 337 |
276 return {DataReceiver: DataReceiver}; | 338 return {DataReceiver: DataReceiver}; |
277 }); | 339 }); |
OLD | NEW |