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
3 // found in the LICENSE file.
5 #include "content/renderer/usb/web_usb_device_impl.h"
8 #include "base/callback.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/child/scoped_web_callbacks.h"
11 #include "content/renderer/usb/type_converters.h"
12 #include "device/devices_app/public/cpp/constants.h"
13 #include "mojo/application/public/cpp/connect.h"
14 #include "mojo/application/public/interfaces/shell.mojom.h"
15 #include "third_party/WebKit/public/platform/WebVector.h"
16 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h"
17 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBTransferInfo.h"
23 const char kClaimInterfaceFailed
[] = "Unable to claim interface.";
24 const char kClearHaltFailed
[] = "Unable to clear endpoint.";
25 const char kDeviceNoAccess
[] = "Access denied.";
26 const char kDeviceNotFound
[] = "Device not found.";
27 const char kDeviceNotOpened
[] = "Device not opened.";
28 const char kDeviceUnavailable
[] = "Device unavailable.";
29 const char kDeviceResetFailed
[] = "Unable to reset the device.";
30 const char kReleaseInterfaceFailed
[] = "Unable to release interface.";
31 const char kSetConfigurationFailed
[] = "Unable to set device configuration.";
32 const char kSetInterfaceFailed
[] = "Unable to set device interface.";
33 const char kTransferFailed
[] = "Transfer failed.";
35 // Generic default rejection handler for any WebUSB callbacks type. Assumes
36 // |CallbacksType| is a blink::WebCallbacks<T, const blink::WebUSBError&>
38 template <typename CallbacksType
>
39 void RejectWithError(const blink::WebUSBError
& error
,
40 scoped_ptr
<CallbacksType
> callbacks
) {
41 callbacks
->onError(error
);
44 template <typename CallbacksType
>
45 void RejectWithDeviceError(const std::string
& message
,
46 scoped_ptr
<CallbacksType
> callbacks
) {
47 RejectWithError(blink::WebUSBError(blink::WebUSBError::Error::Device
,
48 base::UTF8ToUTF16(message
)),
52 template <typename CallbacksType
>
53 void RejectWithTransferError(scoped_ptr
<CallbacksType
> callbacks
) {
54 RejectWithError(blink::WebUSBError(blink::WebUSBError::Error::Transfer
,
55 base::UTF8ToUTF16(kTransferFailed
)),
59 // Create a new ScopedWebCallbacks for WebUSB device callbacks, defaulting to
60 // a "device unavailable" rejection.
61 template <typename CallbacksType
>
62 ScopedWebCallbacks
<CallbacksType
> MakeScopedUSBCallbacks(
63 CallbacksType
* callbacks
) {
64 return make_scoped_web_callbacks(
66 base::Bind(&RejectWithError
<CallbacksType
>,
67 blink::WebUSBError(blink::WebUSBError::Error::Device
,
68 base::UTF8ToUTF16(kDeviceUnavailable
))));
72 ScopedWebCallbacks
<blink::WebUSBDeviceOpenCallbacks
> callbacks
,
73 device::usb::OpenDeviceError error
) {
74 auto scoped_callbacks
= callbacks
.PassCallbacks();
76 case device::usb::OPEN_DEVICE_ERROR_OK
:
77 scoped_callbacks
->onSuccess();
79 case device::usb::OPEN_DEVICE_ERROR_NOT_FOUND
:
80 scoped_callbacks
->onError(blink::WebUSBError(
81 blink::WebUSBError::Error::Device
,
82 base::UTF8ToUTF16(kDeviceNotFound
)));
84 case device::usb::OPEN_DEVICE_ERROR_ACCESS_DENIED
:
85 scoped_callbacks
->onError(blink::WebUSBError(
86 blink::WebUSBError::Error::Device
,
87 base::UTF8ToUTF16(kDeviceNoAccess
)));
95 ScopedWebCallbacks
<blink::WebUSBDeviceCloseCallbacks
> callbacks
) {
96 callbacks
.PassCallbacks()->onSuccess();
99 void HandlePassFailDeviceOperation(
100 ScopedWebCallbacks
<blink::WebCallbacks
<void, const blink::WebUSBError
&>>
102 const std::string
& failure_message
,
104 auto scoped_callbacks
= callbacks
.PassCallbacks();
106 scoped_callbacks
->onSuccess();
108 RejectWithDeviceError(failure_message
, scoped_callbacks
.Pass());
112 ScopedWebCallbacks
<blink::WebUSBDeviceControlTransferCallbacks
> callbacks
,
113 device::usb::TransferStatus status
,
114 mojo::Array
<uint8_t> data
) {
115 auto scoped_callbacks
= callbacks
.PassCallbacks();
116 if (status
!= device::usb::TRANSFER_STATUS_COMPLETED
) {
117 RejectWithTransferError(scoped_callbacks
.Pass());
120 scoped_ptr
<blink::WebUSBTransferInfo
> info(new blink::WebUSBTransferInfo());
121 info
->status
= blink::WebUSBTransferInfo::Status::Ok
;
122 info
->data
.assign(data
);
123 scoped_callbacks
->onSuccess(adoptWebPtr(info
.release()));
127 ScopedWebCallbacks
<blink::WebUSBDeviceControlTransferCallbacks
> callbacks
,
128 size_t bytes_written
,
129 device::usb::TransferStatus status
) {
130 auto scoped_callbacks
= callbacks
.PassCallbacks();
131 scoped_ptr
<blink::WebUSBTransferInfo
> info(new blink::WebUSBTransferInfo());
133 case device::usb::TRANSFER_STATUS_COMPLETED
:
134 info
->status
= blink::WebUSBTransferInfo::Status::Ok
;
136 case device::usb::TRANSFER_STATUS_STALLED
:
137 info
->status
= blink::WebUSBTransferInfo::Status::Stall
;
139 case device::usb::TRANSFER_STATUS_BABBLE
:
140 info
->status
= blink::WebUSBTransferInfo::Status::Babble
;
143 RejectWithTransferError(scoped_callbacks
.Pass());
147 // TODO(rockot): Device::ControlTransferOut should expose the number of bytes
148 // actually transferred so we can send it from here.
149 info
->bytesWritten
= bytes_written
;
150 scoped_callbacks
->onSuccess(adoptWebPtr(info
.release()));
155 WebUSBDeviceImpl::WebUSBDeviceImpl(device::usb::DeviceManagerPtr device_manager
,
156 const blink::WebUSBDeviceInfo
& device_info
)
157 : device_manager_(device_manager
.Pass()),
158 device_info_(device_info
),
159 weak_factory_(this) {}
161 WebUSBDeviceImpl::~WebUSBDeviceImpl() {}
163 const blink::WebUSBDeviceInfo
& WebUSBDeviceImpl::info() const {
167 void WebUSBDeviceImpl::open(blink::WebUSBDeviceOpenCallbacks
* callbacks
) {
168 auto scoped_callbacks
= MakeScopedUSBCallbacks(callbacks
);
169 device_manager_
->OpenDevice(
170 device_info_
.guid
.utf8(),
171 mojo::GetProxy(&device_
),
172 base::Bind(&OnOpenDevice
, base::Passed(&scoped_callbacks
)));
175 void WebUSBDeviceImpl::close(blink::WebUSBDeviceCloseCallbacks
* callbacks
) {
176 auto scoped_callbacks
= MakeScopedUSBCallbacks(callbacks
);
178 RejectWithDeviceError(kDeviceNotOpened
, scoped_callbacks
.PassCallbacks());
181 base::Bind(&OnDeviceClosed
, base::Passed(&scoped_callbacks
)));
185 void WebUSBDeviceImpl::setConfiguration(
186 uint8_t configuration_value
,
187 blink::WebUSBDeviceSetConfigurationCallbacks
* callbacks
) {
188 auto scoped_callbacks
= MakeScopedUSBCallbacks(callbacks
);
190 RejectWithDeviceError(kDeviceNotOpened
, scoped_callbacks
.PassCallbacks());
192 device_
->SetConfiguration(
194 base::Bind(&HandlePassFailDeviceOperation
,
195 base::Passed(&scoped_callbacks
), kSetConfigurationFailed
));
199 void WebUSBDeviceImpl::claimInterface(
200 uint8_t interface_number
,
201 blink::WebUSBDeviceClaimInterfaceCallbacks
* callbacks
) {
202 auto scoped_callbacks
= MakeScopedUSBCallbacks(callbacks
);
204 RejectWithDeviceError(kDeviceNotOpened
, scoped_callbacks
.PassCallbacks());
206 device_
->ClaimInterface(
208 base::Bind(&HandlePassFailDeviceOperation
,
209 base::Passed(&scoped_callbacks
), kClaimInterfaceFailed
));
213 void WebUSBDeviceImpl::releaseInterface(
214 uint8_t interface_number
,
215 blink::WebUSBDeviceReleaseInterfaceCallbacks
* callbacks
) {
216 auto scoped_callbacks
= MakeScopedUSBCallbacks(callbacks
);
218 RejectWithDeviceError(kDeviceNotOpened
, scoped_callbacks
.PassCallbacks());
220 device_
->ReleaseInterface(
222 base::Bind(&HandlePassFailDeviceOperation
,
223 base::Passed(&scoped_callbacks
), kReleaseInterfaceFailed
));
227 void WebUSBDeviceImpl::setInterface(
228 uint8_t interface_number
,
229 uint8_t alternate_setting
,
230 blink::WebUSBDeviceSetInterfaceAlternateSettingCallbacks
* callbacks
) {
231 auto scoped_callbacks
= MakeScopedUSBCallbacks(callbacks
);
233 RejectWithDeviceError(kDeviceNotOpened
, scoped_callbacks
.PassCallbacks());
235 device_
->SetInterfaceAlternateSetting(
236 interface_number
, alternate_setting
,
237 base::Bind(&HandlePassFailDeviceOperation
,
238 base::Passed(&scoped_callbacks
), kSetInterfaceFailed
));
242 void WebUSBDeviceImpl::clearHalt(
243 uint8_t endpoint_number
,
244 blink::WebUSBDeviceClearHaltCallbacks
* callbacks
) {
245 auto scoped_callbacks
= MakeScopedUSBCallbacks(callbacks
);
247 RejectWithDeviceError(kDeviceNotOpened
, scoped_callbacks
.PassCallbacks());
251 base::Bind(&HandlePassFailDeviceOperation
,
252 base::Passed(&scoped_callbacks
), kClearHaltFailed
));
256 void WebUSBDeviceImpl::controlTransfer(
257 const blink::WebUSBDevice::ControlTransferParameters
& parameters
,
260 unsigned int timeout
,
261 blink::WebUSBDeviceControlTransferCallbacks
* callbacks
) {
262 auto scoped_callbacks
= MakeScopedUSBCallbacks(callbacks
);
264 RejectWithDeviceError(kDeviceNotOpened
, scoped_callbacks
.PassCallbacks());
266 device::usb::ControlTransferParamsPtr params
=
267 device::usb::ControlTransferParams::From(parameters
);
268 switch (parameters
.direction
) {
269 case WebUSBDevice::TransferDirection::In
:
270 device_
->ControlTransferIn(params
.Pass(), data_size
, timeout
,
271 base::Bind(&OnTransferIn
, base::Passed(&scoped_callbacks
)));
273 case WebUSBDevice::TransferDirection::Out
: {
274 std::vector
<uint8_t> bytes
;
276 bytes
.assign(data
, data
+ data_size
);
277 mojo::Array
<uint8_t> mojo_bytes
;
278 mojo_bytes
.Swap(&bytes
);
279 device_
->ControlTransferOut(params
.Pass(), mojo_bytes
.Pass(), timeout
,
280 base::Bind(&OnTransferOut
, base::Passed(&scoped_callbacks
),
290 void WebUSBDeviceImpl::transfer(
291 blink::WebUSBDevice::TransferDirection direction
,
292 uint8_t endpoint_number
,
295 unsigned int timeout
,
296 blink::WebUSBDeviceBulkTransferCallbacks
* callbacks
) {
297 auto scoped_callbacks
= MakeScopedUSBCallbacks(callbacks
);
299 RejectWithDeviceError(kDeviceNotOpened
, scoped_callbacks
.PassCallbacks());
302 case WebUSBDevice::TransferDirection::In
:
303 device_
->GenericTransferIn(
304 endpoint_number
, data_size
, timeout
,
305 base::Bind(&OnTransferIn
, base::Passed(&scoped_callbacks
)));
307 case WebUSBDevice::TransferDirection::Out
: {
308 std::vector
<uint8_t> bytes
;
310 bytes
.assign(data
, data
+ data_size
);
311 mojo::Array
<uint8_t> mojo_bytes
;
312 mojo_bytes
.Swap(&bytes
);
313 device_
->GenericTransferOut(
314 endpoint_number
, mojo_bytes
.Pass(), timeout
,
315 base::Bind(&OnTransferOut
, base::Passed(&scoped_callbacks
),
325 void WebUSBDeviceImpl::reset(blink::WebUSBDeviceResetCallbacks
* callbacks
) {
326 auto scoped_callbacks
= MakeScopedUSBCallbacks(callbacks
);
328 RejectWithDeviceError(kDeviceNotOpened
, scoped_callbacks
.PassCallbacks());
331 base::Bind(&HandlePassFailDeviceOperation
,
332 base::Passed(&scoped_callbacks
), kDeviceResetFailed
));
336 } // namespace content