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