Index: extensions/renderer/resources/data_receiver.js |
diff --git a/extensions/renderer/resources/data_receiver.js b/extensions/renderer/resources/data_receiver.js |
index 9224e967bc92293f8c60e8cb1374c7ceeba5dd0f..b48f319e46ede3cf0ebac4a1107abf88ae6a6a42 100644 |
--- a/extensions/renderer/resources/data_receiver.js |
+++ b/extensions/renderer/resources/data_receiver.js |
@@ -5,22 +5,15 @@ |
define('data_receiver', [ |
'async_waiter', |
'device/serial/data_stream.mojom', |
+ 'device/serial/data_stream_serialization.mojom', |
'mojo/public/js/bindings/core', |
'mojo/public/js/bindings/router', |
-], function(asyncWaiter, dataStream, core, router) { |
+], function(asyncWaiter, dataStream, serialization, core, router) { |
/** |
* @module data_receiver |
*/ |
/** |
- * @typedef module:data_receiver~PendingError |
- * @type {Object} |
- * @property {number} error - the error |
- * @property {number} offset - the location of the error |
- * @private |
- */ |
- |
- /** |
* A pending receive operation. |
* @constructor |
* @alias module:data_receiver~PendingReceive |
@@ -69,7 +62,8 @@ define('data_receiver', [ |
/** |
* Dispatches an error if the offset of the error has been reached. |
- * @param {module:data_receiver~PendingError} error The error to dispatch. |
+ * @param {module:device/serial/data_stream_serialization~PendingReceiveError} |
+ * error The error to dispatch. |
* @param {number} bytesReceived The number of bytes that have been received. |
*/ |
PendingReceive.prototype.dispatchError = function(error, bytesReceived) { |
@@ -102,30 +96,69 @@ define('data_receiver', [ |
* @alias module:data_receiver.DataReceiver |
*/ |
function DataReceiver(handle, bufferSize, fatalErrorValue) { |
+ var dataPipeOptions = { |
+ flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, |
+ elementNumBytes: 1, |
+ capacityNumBytes: bufferSize, |
+ }; |
+ var receivePipe = core.createDataPipe(dataPipeOptions); |
+ this.init_( |
+ handle, receivePipe.consumerHandle, fatalErrorValue, 0, null, false); |
+ this.source_.init(receivePipe.producerHandle); |
+ } |
+ |
+ DataReceiver.prototype = |
+ $Object.create(dataStream.DataSourceClientStub.prototype); |
+ |
+ /** |
+ * Closes this DataReceiver. |
+ */ |
+ DataReceiver.prototype.close = function() { |
+ if (this.shutDown_) |
+ return; |
+ this.shutDown_ = true; |
+ this.router_.close(); |
+ this.waiter_.stop(); |
+ core.close(this.receivePipe_); |
+ if (this.receive_) { |
+ this.receive_.dispatchFatalError(this.fatalErrorValue_); |
+ this.receive_ = null; |
+ } |
+ }; |
+ |
+ /** |
+ * Initialize this DataReceiver. |
+ * @param {MojoHandle} source A handle to the DataSource |
+ * @param {MojoHandle} dataPipe A handle to use for receiving data from the |
+ * DataSource. |
+ * @param {number} fatalErrorValue The error to dispatch in the event of a |
+ * fatal error. |
+ * @param {number} bytesReceived The number of bytes already received. |
+ * @param |
+ * {module:device/serial/data_stream_serialization~PendingReceiveError?} |
+ * pendingError The pending error if there is one. |
+ * @param {boolean} paused Whether the DataSource is paused. |
+ * @private |
+ */ |
+ DataReceiver.prototype.init_ = function( |
+ source, dataPipe, fatalErrorValue, bytesReceived, pendingError, paused) { |
raymes
2014/09/23 03:20:57
Is this valid JS style? In C++ we would put 1 arg
Sam McNally
2014/09/23 03:47:24
Not sure - clang format is happy with it, but I th
Ken Rockot(use gerrit already)
2014/09/24 16:08:02
Either one is fine for Google JS style. See the fi
|
/** |
* The [Router]{@link module:mojo/public/js/bindings/router.Router} for the |
* connection to the DataSource. |
* @private |
*/ |
- this.router_ = new router.Router(handle); |
+ this.router_ = new router.Router(source); |
/** |
* The connection to the DataSource. |
* @private |
*/ |
this.source_ = new dataStream.DataSourceProxy(this.router_); |
this.router_.setIncomingReceiver(this); |
- var dataPipeOptions = { |
- flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, |
- elementNumBytes: 1, |
- capacityNumBytes: bufferSize, |
- }; |
- var receivePipe = core.createDataPipe(dataPipeOptions); |
- this.source_.init(receivePipe.producerHandle); |
/** |
* The handle to the data pipe to use for receiving data. |
* @private |
*/ |
- this.receivePipe_ = receivePipe.consumerHandle; |
+ this.receivePipe_ = dataPipe; |
/** |
* The current receive operation. |
* @type {module:data_receiver~PendingReceive} |
@@ -152,44 +185,83 @@ define('data_receiver', [ |
* @type {number} |
* @private |
*/ |
- this.bytesReceived_ = 0; |
+ this.bytesReceived_ = bytesReceived; |
/** |
* The pending error if there is one. |
- * @type module:data_receiver~PendingError |
+ * @type module:device/serial/data_stream_serialization~PendingReceiveError |
* @private |
*/ |
- this.pendingError_ = null; |
+ this.pendingError_ = pendingError; |
/** |
* Whether the DataSource is paused. |
* @type {boolean} |
* @private |
*/ |
- this.paused_ = false; |
+ this.paused_ = paused; |
/** |
* Whether this DataReceiver has shut down. |
* @type {boolean} |
* @private |
*/ |
this.shutDown_ = false; |
- } |
- |
- DataReceiver.prototype = |
- $Object.create(dataStream.DataSourceClientStub.prototype); |
+ }; |
/** |
- * Closes this DataReceiver. |
+ * Serializes this DataReceiver. |
+ * This will cancel a receive if one is in progress. |
+ * @return {Promise.<?SerializedDataReceiver>} A promise that will resolve to |
+ * the serialization of this DataReceiver. If this DataReceiver has shut |
+ * down, the promise will resolve to null. |
*/ |
- DataReceiver.prototype.close = function() { |
+ DataReceiver.prototype.serialize = function() { |
if (this.shutDown_) |
- return; |
- this.shutDown_ = true; |
- this.router_.close(); |
+ return Promise.resolve(null); |
+ |
this.waiter_.stop(); |
- core.close(this.receivePipe_); |
if (this.receive_) { |
this.receive_.dispatchFatalError(this.fatalErrorValue_); |
this.receive_ = null; |
} |
+ var serialized = new serialization.SerializedDataReceiver(); |
+ serialized.source = this.router_.connector_.handle_; |
+ serialized.data_pipe = this.receivePipe_; |
+ serialized.fatal_error_value = this.fatalErrorValue_; |
+ serialized.bytes_received = this.bytesReceived_; |
+ serialized.paused = this.paused_; |
+ serialized.pending_error = this.pendingError_; |
+ this.router_.connector_.handle_ = null; |
+ this.router_.close(); |
+ this.shutDown_ = true; |
+ return Promise.resolve(serialized); |
+ }; |
+ |
+ /** |
+ * Deserializes a SerializedDataReceiver. |
+ * @param {?SerializedDataReceiver} serialized The serialized DataReceiver. |
+ * @return {DataReceiver} The deserialized DataReceiver. |
+ */ |
+ DataReceiver.deserialize = function(serialized) { |
+ var receiver = $Object.create(DataReceiver.prototype); |
+ receiver.deserialize_(serialized); |
+ return receiver; |
+ }; |
+ |
+ /** |
+ * Deserializes a SerializedDataReceiver into this DataReceiver. |
+ * @param {?SerializedDataReceiver} serialized The serialized DataReceiver. |
+ * @private |
+ */ |
+ DataReceiver.prototype.deserialize_ = function(serialized) { |
+ if (!serialized) { |
+ this.shutDown_ = true; |
+ return; |
+ } |
+ this.init_(serialized.source, |
+ serialized.data_pipe, |
+ serialized.fatal_error_value, |
+ serialized.bytes_received, |
+ serialized.pending_error, |
+ serialized.paused); |
}; |
/** |
@@ -202,7 +274,7 @@ define('data_receiver', [ |
*/ |
DataReceiver.prototype.receive = function() { |
if (this.shutDown_) |
- throw new Error('System error'); |
+ throw new Error('DataReceiver has been closed'); |
if (this.receive_) |
throw new Error('Receive already in progress.'); |
var receive = new PendingReceive(); |
@@ -256,13 +328,9 @@ define('data_receiver', [ |
if (this.shutDown_) |
return; |
- /** |
- * @type module:data_receiver~PendingError |
- */ |
- var pendingError = { |
- error: error, |
- offset: offset, |
- }; |
+ var pendingError = new serialization.PendingReceiveError(); |
+ pendingError.error = error; |
+ pendingError.offset = offset; |
if (this.receive_ && |
this.receive_.dispatchError(pendingError, this.bytesReceived_)) { |
this.receive_ = null; |