Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(329)

Side by Side Diff: extensions/renderer/resources/data_sender.js

Issue 571333002: Add serialization support to the JS DataSender and DataReceiver. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@stash-service
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 'mojo/public/js/bindings/core', 8 'mojo/public/js/bindings/core',
9 'mojo/public/js/bindings/router', 9 'mojo/public/js/bindings/router',
10 ], function(asyncWaiter, dataStreamMojom, core, routerModule) { 10 ], function(asyncWaiter, dataStreamMojom, core, routerModule) {
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 this.router_.close(); 250 this.router_.close();
251 core.close(this.sendPipe_); 251 core.close(this.sendPipe_);
252 while (this.pendingSends_.length) { 252 while (this.pendingSends_.length) {
253 this.pendingSends_.pop().reportBytesSentAndError( 253 this.pendingSends_.pop().reportBytesSentAndError(
254 0, this.fatalErrorValue_); 254 0, this.fatalErrorValue_);
255 } 255 }
256 while (this.sendsAwaitingAck_.length) { 256 while (this.sendsAwaitingAck_.length) {
257 this.sendsAwaitingAck_.pop().reportBytesSentAndError( 257 this.sendsAwaitingAck_.pop().reportBytesSentAndError(
258 0, this.fatalErrorValue_); 258 0, this.fatalErrorValue_);
259 } 259 }
260 if (this.pendingCancel_) { 260 this.callCancelCallback_();
261 this.pendingCancel_(); 261 };
262 this.pendingCancel_ = null; 262
263 DataSender.prototype.serialize = function() {
264 var readyToSerialize = Promise.resolve();
265 if (this.pendingSends_.length) {
266 if (this.pendingCancel_)
267 readyToSerialize = this.cancelPromise_;
268 else
269 readyToSerialize = this.cancel(this.fatalErrorValue_);
263 } 270 }
271 return readyToSerialize.then(function() {
272 this.waiter_.stop();
273 var serialized = {
274 sink: this.router_.connector_.handle_,
275 data_pipe: this.sendPipe_,
276 fatal_error_value: this.fatalErrorValue_,
277 shut_down: this.shutDown_,
278 };
279 this.router_.connector_.handle_ = null;
280 this.router_.close();
281 this.shutDown_ = true;
282 return serialized;
283 }.bind(this));
284 };
285
286 DataSender.deserialize = function(serialized) {
287 var sender = $Object.create(DataSender.prototype);
288 sender.deserialize_(serialized);
289 return sender;
290 };
291
292 DataSender.prototype.deserialize_ = function(serialized) {
293 this.sendPipe_ = serialized.data_pipe;
294 this.fatalErrorValue_ = serialized.fatal_error_value;
295 this.shutDown_ = serialized.shut_down;
296 this.router_ = new routerModule.Router(serialized.sink);
297 this.sink_ = new dataStreamMojom.DataSinkProxy(this.router_);
298 this.router_.setIncomingReceiver(this);
299 this.waiter_ = new asyncWaiter.AsyncWaiter(this.sendPipe_,
300 core.HANDLE_SIGNAL_WRITABLE,
301 this.onHandleReady_.bind(this));
302 this.pendingSends_ = [];
303 this.sendsAwaitingAck_ = [];
264 }; 304 };
265 305
266 /** 306 /**
267 * Sends data to the DataSink. 307 * Sends data to the DataSink.
268 * @return {Promise.<number>} A promise to the number of bytes sent. If an 308 * @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 309 * error occurs, the promise will reject with an Error object with a
270 * property error containing the error code. 310 * property error containing the error code.
271 * @throws Will throw if this has encountered a fatal error or a cancel is in 311 * @throws Will throw if this has encountered a fatal error or a cancel is in
272 * progress. 312 * progress.
273 */ 313 */
(...skipping 20 matching lines...) Expand all
294 */ 334 */
295 DataSender.prototype.cancel = function(error) { 335 DataSender.prototype.cancel = function(error) {
296 if (this.shutDown_) 336 if (this.shutDown_)
297 throw new Error('DataSender has been closed'); 337 throw new Error('DataSender has been closed');
298 if (this.pendingCancel_) 338 if (this.pendingCancel_)
299 throw new Error('Cancel already in progress'); 339 throw new Error('Cancel already in progress');
300 if (this.pendingSends_.length + this.sendsAwaitingAck_.length == 0) 340 if (this.pendingSends_.length + this.sendsAwaitingAck_.length == 0)
301 return Promise.resolve(); 341 return Promise.resolve();
302 342
303 this.sink_.cancel(error); 343 this.sink_.cancel(error);
304 return new Promise(function(resolve) { 344 this.cancelPromise_ = new Promise(function(resolve) {
305 this.pendingCancel_ = resolve; 345 this.pendingCancel_ = resolve;
306 }.bind(this)); 346 }.bind(this));
347 return this.cancelPromise_;
307 }; 348 };
308 349
309 /** 350 /**
310 * Invoked when |handle_| is ready to write. Writes to the data pipe if the 351 * Invoked when |handle_| is ready to write. Writes to the data pipe if the
311 * wait is successful. 352 * wait is successful.
312 * @param {number} waitResult The result of the asynchronous wait. 353 * @param {number} waitResult The result of the asynchronous wait.
313 * @private 354 * @private
314 */ 355 */
315 DataSender.prototype.onHandleReady_ = function(result) { 356 DataSender.prototype.onHandleReady_ = function(result) {
316 if (result != core.RESULT_OK) { 357 if (result != core.RESULT_OK) {
(...skipping 13 matching lines...) Expand all
330 } 371 }
331 } 372 }
332 }; 373 };
333 374
334 /** 375 /**
335 * Calls and clears the pending cancel callback if one is pending. 376 * Calls and clears the pending cancel callback if one is pending.
336 * @private 377 * @private
337 */ 378 */
338 DataSender.prototype.callCancelCallback_ = function() { 379 DataSender.prototype.callCancelCallback_ = function() {
339 if (this.pendingCancel_) { 380 if (this.pendingCancel_) {
381 this.cancelPromise_ = null;
340 this.pendingCancel_(); 382 this.pendingCancel_();
341 this.pendingCancel_ = null; 383 this.pendingCancel_ = null;
342 } 384 }
343 }; 385 };
344 386
345 /** 387 /**
346 * Invoked by the DataSink to report that data has been successfully sent. 388 * Invoked by the DataSink to report that data has been successfully sent.
347 * @param {number} numBytes The number of bytes sent. 389 * @param {number} numBytes The number of bytes sent.
348 * @private 390 * @private
349 */ 391 */
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 // Note: Only the first PendingSend in |pendingSends_| will have data to 430 // 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. 431 // flush as only the first can have written data to the data pipe.
390 bytesToFlush += result.bytesToFlush; 432 bytesToFlush += result.bytesToFlush;
391 } 433 }
392 this.callCancelCallback_(); 434 this.callCancelCallback_();
393 return Promise.resolve({bytes_to_flush: bytesToFlush}); 435 return Promise.resolve({bytes_to_flush: bytesToFlush});
394 }; 436 };
395 437
396 return {DataSender: DataSender}; 438 return {DataSender: DataSender};
397 }); 439 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698