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

Side by Side Diff: content/renderer/usb/web_usb_device_impl.cc

Issue 1635903004: Add support for isochronous transfers in WebUSB. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@usb_isoc
Patch Set: Fix tense. Created 4 years, 10 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 #include "content/renderer/usb/web_usb_device_impl.h" 5 #include "content/renderer/usb/web_usb_device_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "content/child/mojo/type_converters.h"
12 #include "content/child/scoped_web_callbacks.h" 13 #include "content/child/scoped_web_callbacks.h"
13 #include "content/renderer/usb/type_converters.h" 14 #include "content/renderer/usb/type_converters.h"
14 #include "device/devices_app/public/cpp/constants.h" 15 #include "device/devices_app/public/cpp/constants.h"
15 #include "mojo/shell/public/cpp/connect.h" 16 #include "mojo/shell/public/cpp/connect.h"
16 #include "mojo/shell/public/interfaces/shell.mojom.h" 17 #include "mojo/shell/public/interfaces/shell.mojom.h"
17 #include "third_party/WebKit/public/platform/WebVector.h" 18 #include "third_party/WebKit/public/platform/WebVector.h"
18 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h" 19 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h"
19 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBTransferInfo.h " 20 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBTransferInfo.h "
20 21
21 namespace content { 22 namespace content {
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 if (success) { 107 if (success) {
107 scoped_callbacks->onSuccess(); 108 scoped_callbacks->onSuccess();
108 } else { 109 } else {
109 RejectWithError(blink::WebUSBError(blink::WebUSBError::Error::Network, 110 RejectWithError(blink::WebUSBError(blink::WebUSBError::Error::Network,
110 base::ASCIIToUTF16(failure_message)), 111 base::ASCIIToUTF16(failure_message)),
111 std::move(scoped_callbacks)); 112 std::move(scoped_callbacks));
112 } 113 }
113 } 114 }
114 115
115 void OnTransferIn( 116 void OnTransferIn(
116 ScopedWebCallbacks<blink::WebUSBDeviceControlTransferCallbacks> callbacks, 117 ScopedWebCallbacks<blink::WebUSBDeviceTransferCallbacks> callbacks,
117 device::usb::TransferStatus status, 118 device::usb::TransferStatus status,
118 mojo::Array<uint8_t> data) { 119 mojo::Array<uint8_t> data) {
119 auto scoped_callbacks = callbacks.PassCallbacks(); 120 auto scoped_callbacks = callbacks.PassCallbacks();
120 if (status != device::usb::TransferStatus::COMPLETED) { 121 blink::WebUSBTransferInfo::Status web_status;
121 RejectWithTransferError(std::move(scoped_callbacks)); 122 switch (status) {
122 return; 123 case device::usb::TransferStatus::COMPLETED:
124 web_status = blink::WebUSBTransferInfo::Status::Ok;
125 break;
126 case device::usb::TransferStatus::STALLED:
127 web_status = blink::WebUSBTransferInfo::Status::Stall;
128 break;
129 case device::usb::TransferStatus::BABBLE:
130 web_status = blink::WebUSBTransferInfo::Status::Babble;
131 break;
132 default:
133 RejectWithTransferError(std::move(scoped_callbacks));
134 return;
123 } 135 }
124 scoped_ptr<blink::WebUSBTransferInfo> info(new blink::WebUSBTransferInfo()); 136 scoped_ptr<blink::WebUSBTransferInfo> info(new blink::WebUSBTransferInfo());
125 info->status = blink::WebUSBTransferInfo::Status::Ok; 137 info->status.assign(
138 std::vector<blink::WebUSBTransferInfo::Status>(1, web_status));
126 info->data.assign(data); 139 info->data.assign(data);
127 scoped_callbacks->onSuccess(adoptWebPtr(info.release())); 140 scoped_callbacks->onSuccess(adoptWebPtr(info.release()));
128 } 141 }
129 142
130 void OnTransferOut( 143 void OnTransferOut(
131 ScopedWebCallbacks<blink::WebUSBDeviceControlTransferCallbacks> callbacks, 144 ScopedWebCallbacks<blink::WebUSBDeviceTransferCallbacks> callbacks,
132 size_t bytes_written, 145 size_t bytes_written,
133 device::usb::TransferStatus status) { 146 device::usb::TransferStatus status) {
134 auto scoped_callbacks = callbacks.PassCallbacks(); 147 auto scoped_callbacks = callbacks.PassCallbacks();
135 scoped_ptr<blink::WebUSBTransferInfo> info(new blink::WebUSBTransferInfo()); 148 blink::WebUSBTransferInfo::Status web_status;
136 switch (status) { 149 switch (status) {
137 case device::usb::TransferStatus::COMPLETED: 150 case device::usb::TransferStatus::COMPLETED:
138 info->status = blink::WebUSBTransferInfo::Status::Ok; 151 web_status = blink::WebUSBTransferInfo::Status::Ok;
139 break; 152 break;
140 case device::usb::TransferStatus::STALLED: 153 case device::usb::TransferStatus::STALLED:
141 info->status = blink::WebUSBTransferInfo::Status::Stall; 154 web_status = blink::WebUSBTransferInfo::Status::Stall;
142 break;
143 case device::usb::TransferStatus::BABBLE:
144 info->status = blink::WebUSBTransferInfo::Status::Babble;
145 break; 155 break;
146 default: 156 default:
147 RejectWithTransferError(std::move(scoped_callbacks)); 157 RejectWithTransferError(std::move(scoped_callbacks));
148 return; 158 return;
149 } 159 }
150
151 // TODO(rockot): Device::ControlTransferOut should expose the number of bytes 160 // TODO(rockot): Device::ControlTransferOut should expose the number of bytes
152 // actually transferred so we can send it from here. 161 // actually transferred so we can send it from here.
153 info->bytesWritten = bytes_written; 162 scoped_ptr<blink::WebUSBTransferInfo> info(new blink::WebUSBTransferInfo());
163 info->status.assign(
164 std::vector<blink::WebUSBTransferInfo::Status>(1, web_status));
165 info->bytesTransferred.assign(std::vector<uint32_t>(1, bytes_written));
154 scoped_callbacks->onSuccess(adoptWebPtr(info.release())); 166 scoped_callbacks->onSuccess(adoptWebPtr(info.release()));
155 } 167 }
156 168
169 void OnIsochronousTransferIn(
170 ScopedWebCallbacks<blink::WebUSBDeviceTransferCallbacks> callbacks,
171 mojo::Array<uint8_t> data,
172 mojo::Array<device::usb::IsochronousPacketPtr> packets) {
173 auto scoped_callbacks = callbacks.PassCallbacks();
174 scoped_ptr<blink::WebUSBTransferInfo> info(new blink::WebUSBTransferInfo());
175 info->data.assign(data);
176 for (size_t i = 0; i < packets.size(); ++i) {
177 switch (packets[i]->status) {
178 case device::usb::TransferStatus::COMPLETED:
179 info->status[i] = blink::WebUSBTransferInfo::Status::Ok;
180 break;
181 case device::usb::TransferStatus::STALLED:
182 info->status[i] = blink::WebUSBTransferInfo::Status::Stall;
183 break;
184 case device::usb::TransferStatus::BABBLE:
185 info->status[i] = blink::WebUSBTransferInfo::Status::Babble;
186 break;
187 default:
188 RejectWithTransferError(std::move(scoped_callbacks));
189 return;
190 }
191 info->packetLength[i] = packets[i]->length;
192 info->bytesTransferred[i] = packets[i]->transferred_length;
193 }
194 scoped_callbacks->onSuccess(adoptWebPtr(info.release()));
195 }
196
197 void OnIsochronousTransferOut(
198 ScopedWebCallbacks<blink::WebUSBDeviceTransferCallbacks> callbacks,
199 mojo::Array<device::usb::IsochronousPacketPtr> packets) {
200 auto scoped_callbacks = callbacks.PassCallbacks();
201 scoped_ptr<blink::WebUSBTransferInfo> info(new blink::WebUSBTransferInfo());
202 for (size_t i = 0; i < packets.size(); ++i) {
203 switch (packets[i]->status) {
204 case device::usb::TransferStatus::COMPLETED:
205 info->status[i] = blink::WebUSBTransferInfo::Status::Ok;
206 break;
207 case device::usb::TransferStatus::STALLED:
208 info->status[i] = blink::WebUSBTransferInfo::Status::Stall;
209 break;
210 default:
211 RejectWithTransferError(std::move(scoped_callbacks));
212 return;
213 }
214 info->bytesTransferred[i] = packets[i]->transferred_length;
215 }
216 scoped_callbacks->onSuccess(adoptWebPtr(info.release()));
217 }
218
157 } // namespace 219 } // namespace
158 220
159 WebUSBDeviceImpl::WebUSBDeviceImpl(device::usb::DevicePtr device, 221 WebUSBDeviceImpl::WebUSBDeviceImpl(device::usb::DevicePtr device,
160 const blink::WebUSBDeviceInfo& device_info) 222 const blink::WebUSBDeviceInfo& device_info)
161 : device_(std::move(device)), 223 : device_(std::move(device)),
162 device_info_(device_info), 224 device_info_(device_info),
163 weak_factory_(this) {} 225 weak_factory_(this) {}
164 226
165 WebUSBDeviceImpl::~WebUSBDeviceImpl() {} 227 WebUSBDeviceImpl::~WebUSBDeviceImpl() {}
166 228
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 endpoint_number, 296 endpoint_number,
235 base::Bind(&HandlePassFailDeviceOperation, 297 base::Bind(&HandlePassFailDeviceOperation,
236 base::Passed(&scoped_callbacks), kClearHaltFailed)); 298 base::Passed(&scoped_callbacks), kClearHaltFailed));
237 } 299 }
238 300
239 void WebUSBDeviceImpl::controlTransfer( 301 void WebUSBDeviceImpl::controlTransfer(
240 const blink::WebUSBDevice::ControlTransferParameters& parameters, 302 const blink::WebUSBDevice::ControlTransferParameters& parameters,
241 uint8_t* data, 303 uint8_t* data,
242 size_t data_size, 304 size_t data_size,
243 unsigned int timeout, 305 unsigned int timeout,
244 blink::WebUSBDeviceControlTransferCallbacks* callbacks) { 306 blink::WebUSBDeviceTransferCallbacks* callbacks) {
245 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); 307 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks);
246 device::usb::ControlTransferParamsPtr params = 308 device::usb::ControlTransferParamsPtr params =
247 device::usb::ControlTransferParams::From(parameters); 309 device::usb::ControlTransferParams::From(parameters);
248 switch (parameters.direction) { 310 switch (parameters.direction) {
249 case WebUSBDevice::TransferDirection::In: 311 case WebUSBDevice::TransferDirection::In:
250 device_->ControlTransferIn( 312 device_->ControlTransferIn(
251 std::move(params), data_size, timeout, 313 std::move(params), data_size, timeout,
252 base::Bind(&OnTransferIn, base::Passed(&scoped_callbacks))); 314 base::Bind(&OnTransferIn, base::Passed(&scoped_callbacks)));
253 break; 315 break;
254 case WebUSBDevice::TransferDirection::Out: { 316 case WebUSBDevice::TransferDirection::Out: {
(...skipping 12 matching lines...) Expand all
267 NOTREACHED(); 329 NOTREACHED();
268 } 330 }
269 } 331 }
270 332
271 void WebUSBDeviceImpl::transfer( 333 void WebUSBDeviceImpl::transfer(
272 blink::WebUSBDevice::TransferDirection direction, 334 blink::WebUSBDevice::TransferDirection direction,
273 uint8_t endpoint_number, 335 uint8_t endpoint_number,
274 uint8_t* data, 336 uint8_t* data,
275 size_t data_size, 337 size_t data_size,
276 unsigned int timeout, 338 unsigned int timeout,
277 blink::WebUSBDeviceBulkTransferCallbacks* callbacks) { 339 blink::WebUSBDeviceTransferCallbacks* callbacks) {
278 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); 340 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks);
279 switch (direction) { 341 switch (direction) {
280 case WebUSBDevice::TransferDirection::In: 342 case WebUSBDevice::TransferDirection::In:
281 device_->GenericTransferIn( 343 device_->GenericTransferIn(
282 endpoint_number, data_size, timeout, 344 endpoint_number, data_size, timeout,
283 base::Bind(&OnTransferIn, base::Passed(&scoped_callbacks))); 345 base::Bind(&OnTransferIn, base::Passed(&scoped_callbacks)));
284 break; 346 break;
285 case WebUSBDevice::TransferDirection::Out: { 347 case WebUSBDevice::TransferDirection::Out: {
286 std::vector<uint8_t> bytes; 348 std::vector<uint8_t> bytes;
287 if (data) 349 if (data)
288 bytes.assign(data, data + data_size); 350 bytes.assign(data, data + data_size);
289 mojo::Array<uint8_t> mojo_bytes; 351 mojo::Array<uint8_t> mojo_bytes;
290 mojo_bytes.Swap(&bytes); 352 mojo_bytes.Swap(&bytes);
291 device_->GenericTransferOut( 353 device_->GenericTransferOut(
292 endpoint_number, std::move(mojo_bytes), timeout, 354 endpoint_number, std::move(mojo_bytes), timeout,
293 base::Bind(&OnTransferOut, base::Passed(&scoped_callbacks), 355 base::Bind(&OnTransferOut, base::Passed(&scoped_callbacks),
294 data_size)); 356 data_size));
295 break; 357 break;
296 } 358 }
297 default: 359 default:
298 NOTREACHED(); 360 NOTREACHED();
299 } 361 }
300 } 362 }
301 363
364 void WebUSBDeviceImpl::isochronousTransfer(
365 blink::WebUSBDevice::TransferDirection direction,
366 uint8_t endpoint_number,
367 uint8_t* data,
368 size_t data_size,
369 blink::WebVector<uint32_t> packet_lengths,
370 unsigned int timeout,
371 blink::WebUSBDeviceTransferCallbacks* callbacks) {
372 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks);
373 switch (direction) {
374 case WebUSBDevice::TransferDirection::In:
375 device_->IsochronousTransferIn(
376 endpoint_number, mojo::Array<uint32_t>::From(packet_lengths), timeout,
377 base::Bind(&OnIsochronousTransferIn,
378 base::Passed(&scoped_callbacks)));
379 break;
380 case WebUSBDevice::TransferDirection::Out: {
381 std::vector<uint8_t> bytes;
382 if (data)
383 bytes.assign(data, data + data_size);
384 mojo::Array<uint8_t> mojo_bytes;
385 mojo_bytes.Swap(&bytes);
386 device_->IsochronousTransferOut(
387 endpoint_number, std::move(mojo_bytes),
388 mojo::Array<uint32_t>::From(packet_lengths), timeout,
389 base::Bind(&OnIsochronousTransferOut,
390 base::Passed(&scoped_callbacks)));
391 break;
392 }
393 default:
394 NOTREACHED();
395 }
396 }
397
302 void WebUSBDeviceImpl::reset(blink::WebUSBDeviceResetCallbacks* callbacks) { 398 void WebUSBDeviceImpl::reset(blink::WebUSBDeviceResetCallbacks* callbacks) {
303 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); 399 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks);
304 device_->Reset(base::Bind(&HandlePassFailDeviceOperation, 400 device_->Reset(base::Bind(&HandlePassFailDeviceOperation,
305 base::Passed(&scoped_callbacks), 401 base::Passed(&scoped_callbacks),
306 kDeviceResetFailed)); 402 kDeviceResetFailed));
307 } 403 }
308 404
309 } // namespace content 405 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698