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 'device/serial/data_stream.mojom', | 6 'device/serial/data_stream.mojom', |
7 'device/serial/data_stream_serialization.mojom', | 7 'device/serial/data_stream_serialization.mojom', |
8 'mojo/public/js/core', | 8 'mojo/public/js/core', |
9 'mojo/public/js/router', | 9 'mojo/public/js/router', |
10 ], function(dataStream, serialization, core, router) { | 10 ], function(dataStream, serialization, core, router) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 * @param {number} error The error to dispatch. | 79 * @param {number} error The error to dispatch. |
80 */ | 80 */ |
81 PendingReceive.prototype.dispatchFatalError = function(error) { | 81 PendingReceive.prototype.dispatchFatalError = function(error) { |
82 var e = new Error(); | 82 var e = new Error(); |
83 e.error = error; | 83 e.error = error; |
84 this.errorCallback_(e); | 84 this.errorCallback_(e); |
85 }; | 85 }; |
86 | 86 |
87 /** | 87 /** |
88 * A DataReceiver that receives data from a DataSource. | 88 * A DataReceiver that receives data from a DataSource. |
89 * @param {!MojoHandle} handle The handle to the DataSource. | 89 * @param {!MojoHandle} source The handle to the DataSource. |
| 90 * @param {!MojoHandle} client The handle to the DataSourceClient. |
90 * @param {number} bufferSize How large a buffer to use. | 91 * @param {number} bufferSize How large a buffer to use. |
91 * @param {number} fatalErrorValue The receive error value to report in the | 92 * @param {number} fatalErrorValue The receive error value to report in the |
92 * event of a fatal error. | 93 * event of a fatal error. |
93 * @constructor | 94 * @constructor |
94 * @alias module:data_receiver.DataReceiver | 95 * @alias module:data_receiver.DataReceiver |
95 */ | 96 */ |
96 function DataReceiver(handle, bufferSize, fatalErrorValue) { | 97 function DataReceiver(source, client, bufferSize, fatalErrorValue) { |
97 this.init_(handle, fatalErrorValue, 0, null, [], false); | 98 this.init_(source, client, fatalErrorValue, 0, null, [], false); |
98 this.source_.init(bufferSize); | 99 this.source_.init(bufferSize); |
99 } | 100 } |
100 | 101 |
101 DataReceiver.prototype = | 102 DataReceiver.prototype = |
102 $Object.create(dataStream.DataSourceClient.stubClass.prototype); | 103 $Object.create(dataStream.DataSourceClient.stubClass.prototype); |
103 | 104 |
104 /** | 105 /** |
105 * Closes this DataReceiver. | 106 * Closes this DataReceiver. |
106 */ | 107 */ |
107 DataReceiver.prototype.close = function() { | 108 DataReceiver.prototype.close = function() { |
108 if (this.shutDown_) | 109 if (this.shutDown_) |
109 return; | 110 return; |
110 this.shutDown_ = true; | 111 this.shutDown_ = true; |
111 this.router_.close(); | 112 this.router_.close(); |
| 113 this.clientRouter_.close(); |
112 if (this.receive_) { | 114 if (this.receive_) { |
113 this.receive_.dispatchFatalError(this.fatalErrorValue_); | 115 this.receive_.dispatchFatalError(this.fatalErrorValue_); |
114 this.receive_ = null; | 116 this.receive_ = null; |
115 } | 117 } |
116 }; | 118 }; |
117 | 119 |
118 /** | 120 /** |
119 * Initialize this DataReceiver. | 121 * Initialize this DataReceiver. |
120 * @param {!MojoHandle} source A handle to the DataSource | 122 * @param {!MojoHandle} source A handle to the DataSource. |
| 123 * @param {!MojoHandle} client A handle to the DataSourceClient. |
121 * @param {number} fatalErrorValue The error to dispatch in the event of a | 124 * @param {number} fatalErrorValue The error to dispatch in the event of a |
122 * fatal error. | 125 * fatal error. |
123 * @param {number} bytesReceived The number of bytes already received. | 126 * @param {number} bytesReceived The number of bytes already received. |
124 * @param {PendingReceiveError} pendingError The pending error if there is | 127 * @param {PendingReceiveError} pendingError The pending error if there is |
125 * one. | 128 * one. |
126 * @param {!Array.<!ArrayBuffer>} pendingData Data received from the | 129 * @param {!Array.<!ArrayBuffer>} pendingData Data received from the |
127 * DataSource not yet requested by the client. | 130 * DataSource not yet requested by the client. |
128 * @param {boolean} paused Whether the DataSource is paused. | 131 * @param {boolean} paused Whether the DataSource is paused. |
129 * @private | 132 * @private |
130 */ | 133 */ |
131 DataReceiver.prototype.init_ = function(source, | 134 DataReceiver.prototype.init_ = function(source, client, fatalErrorValue, |
132 fatalErrorValue, | 135 bytesReceived, pendingError, |
133 bytesReceived, | 136 pendingData, paused) { |
134 pendingError, | |
135 pendingData, | |
136 paused) { | |
137 /** | 137 /** |
138 * The [Router]{@link module:mojo/public/js/router.Router} for the | 138 * The [Router]{@link module:mojo/public/js/router.Router} for the |
139 * connection to the DataSource. | 139 * connection to the DataSource. |
140 * @private | 140 * @private |
141 */ | 141 */ |
142 this.router_ = new router.Router(source); | 142 this.router_ = new router.Router(source); |
143 /** | 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 /** |
144 * The connection to the DataSource. | 150 * The connection to the DataSource. |
145 * @private | 151 * @private |
146 */ | 152 */ |
147 this.source_ = new dataStream.DataSource.proxyClass(this.router_); | 153 this.source_ = new dataStream.DataSource.proxyClass(this.router_); |
148 this.router_.setIncomingReceiver(this); | 154 this.client_ = new dataStream.DataSourceClient.stubClass(this); |
| 155 this.clientRouter_.setIncomingReceiver(this.client_); |
149 /** | 156 /** |
150 * The current receive operation. | 157 * The current receive operation. |
151 * @type {module:data_receiver~PendingReceive} | 158 * @type {module:data_receiver~PendingReceive} |
152 * @private | 159 * @private |
153 */ | 160 */ |
154 this.receive_ = null; | 161 this.receive_ = null; |
155 /** | 162 /** |
156 * The error to be dispatched in the event of a fatal error. | 163 * The error to be dispatched in the event of a fatal error. |
157 * @const {number} | 164 * @const {number} |
158 * @private | 165 * @private |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 DataReceiver.prototype.serialize = function() { | 202 DataReceiver.prototype.serialize = function() { |
196 if (this.shutDown_) | 203 if (this.shutDown_) |
197 return Promise.resolve(null); | 204 return Promise.resolve(null); |
198 | 205 |
199 if (this.receive_) { | 206 if (this.receive_) { |
200 this.receive_.dispatchFatalError(this.fatalErrorValue_); | 207 this.receive_.dispatchFatalError(this.fatalErrorValue_); |
201 this.receive_ = null; | 208 this.receive_ = null; |
202 } | 209 } |
203 var serialized = new serialization.SerializedDataReceiver(); | 210 var serialized = new serialization.SerializedDataReceiver(); |
204 serialized.source = this.router_.connector_.handle_; | 211 serialized.source = this.router_.connector_.handle_; |
| 212 serialized.client = this.clientRouter_.connector_.handle_; |
205 serialized.fatal_error_value = this.fatalErrorValue_; | 213 serialized.fatal_error_value = this.fatalErrorValue_; |
206 serialized.paused = this.paused_; | 214 serialized.paused = this.paused_; |
207 serialized.pending_error = this.pendingError_; | 215 serialized.pending_error = this.pendingError_; |
208 serialized.pending_data = []; | 216 serialized.pending_data = []; |
209 $Array.forEach(this.pendingDataBuffers_, function(buffer) { | 217 $Array.forEach(this.pendingDataBuffers_, function(buffer) { |
210 serialized.pending_data.push(new Uint8Array(buffer)); | 218 serialized.pending_data.push(new Uint8Array(buffer)); |
211 }); | 219 }); |
212 this.router_.connector_.handle_ = null; | 220 this.router_.connector_.handle_ = null; |
213 this.router_.close(); | 221 this.router_.close(); |
| 222 this.clientRouter_.connector_.handle_ = null; |
| 223 this.clientRouter_.close(); |
214 this.shutDown_ = true; | 224 this.shutDown_ = true; |
215 return Promise.resolve(serialized); | 225 return Promise.resolve(serialized); |
216 }; | 226 }; |
217 | 227 |
218 /** | 228 /** |
219 * Deserializes a SerializedDataReceiver. | 229 * Deserializes a SerializedDataReceiver. |
220 * @param {SerializedDataReceiver} serialized The serialized DataReceiver. | 230 * @param {SerializedDataReceiver} serialized The serialized DataReceiver. |
221 * @return {!DataReceiver} The deserialized DataReceiver. | 231 * @return {!DataReceiver} The deserialized DataReceiver. |
222 */ | 232 */ |
223 DataReceiver.deserialize = function(serialized) { | 233 DataReceiver.deserialize = function(serialized) { |
(...skipping 11 matching lines...) Expand all Loading... |
235 if (!serialized) { | 245 if (!serialized) { |
236 this.shutDown_ = true; | 246 this.shutDown_ = true; |
237 return; | 247 return; |
238 } | 248 } |
239 var pendingData = []; | 249 var pendingData = []; |
240 $Array.forEach(serialized.pending_data, function(data) { | 250 $Array.forEach(serialized.pending_data, function(data) { |
241 var buffer = new Uint8Array(data.length); | 251 var buffer = new Uint8Array(data.length); |
242 buffer.set(data); | 252 buffer.set(data); |
243 pendingData.push(buffer.buffer); | 253 pendingData.push(buffer.buffer); |
244 }); | 254 }); |
245 this.init_(serialized.source, | 255 this.init_(serialized.source, serialized.client, |
246 serialized.fatal_error_value, | 256 serialized.fatal_error_value, serialized.bytes_received, |
247 serialized.bytes_received, | 257 serialized.pending_error, pendingData, serialized.paused); |
248 serialized.pending_error, | |
249 pendingData, | |
250 serialized.paused); | |
251 }; | 258 }; |
252 | 259 |
253 /** | 260 /** |
254 * Receive data from the DataSource. | 261 * Receive data from the DataSource. |
255 * @return {Promise.<ArrayBuffer>} A promise to the received data. If an error | 262 * @return {Promise.<ArrayBuffer>} A promise to the received data. If an error |
256 * occurs, the promise will reject with an Error object with a property | 263 * occurs, the promise will reject with an Error object with a property |
257 * error containing the error code. | 264 * error containing the error code. |
258 * @throws Will throw if this has encountered a fatal error or another receive | 265 * @throws Will throw if this has encountered a fatal error or another receive |
259 * is in progress. | 266 * is in progress. |
260 */ | 267 */ |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 var buffer = new ArrayBuffer(data.length); | 327 var buffer = new ArrayBuffer(data.length); |
321 var uintView = new Uint8Array(buffer); | 328 var uintView = new Uint8Array(buffer); |
322 uintView.set(data); | 329 uintView.set(data); |
323 this.pendingDataBuffers_.push(buffer); | 330 this.pendingDataBuffers_.push(buffer); |
324 if (this.receive_) | 331 if (this.receive_) |
325 this.dispatchData_(); | 332 this.dispatchData_(); |
326 }; | 333 }; |
327 | 334 |
328 return {DataReceiver: DataReceiver}; | 335 return {DataReceiver: DataReceiver}; |
329 }); | 336 }); |
OLD | NEW |