Roll src/third_party/WebKit a452221:9ff6d11 (svn 202117:202119)
[chromium-blink-merge.git] / content / renderer / usb / web_usb_device_impl.cc
blobef2d61131785360a46283eee86e4617771fcd1e8
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"
7 #include "base/bind.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"
19 namespace content {
21 namespace {
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&>
37 // for any type |T|.
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)),
49 callbacks.Pass());
52 template <typename CallbacksType>
53 void RejectWithTransferError(scoped_ptr<CallbacksType> callbacks) {
54 RejectWithError(blink::WebUSBError(blink::WebUSBError::Error::Transfer,
55 base::UTF8ToUTF16(kTransferFailed)),
56 callbacks.Pass());
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(
65 callbacks,
66 base::Bind(&RejectWithError<CallbacksType>,
67 blink::WebUSBError(blink::WebUSBError::Error::Device,
68 base::UTF8ToUTF16(kDeviceUnavailable))));
71 void OnOpenDevice(
72 ScopedWebCallbacks<blink::WebUSBDeviceOpenCallbacks> callbacks,
73 device::usb::OpenDeviceError error) {
74 auto scoped_callbacks = callbacks.PassCallbacks();
75 switch(error) {
76 case device::usb::OPEN_DEVICE_ERROR_OK:
77 scoped_callbacks->onSuccess();
78 break;
79 case device::usb::OPEN_DEVICE_ERROR_NOT_FOUND:
80 scoped_callbacks->onError(blink::WebUSBError(
81 blink::WebUSBError::Error::Device,
82 base::UTF8ToUTF16(kDeviceNotFound)));
83 break;
84 case device::usb::OPEN_DEVICE_ERROR_ACCESS_DENIED:
85 scoped_callbacks->onError(blink::WebUSBError(
86 blink::WebUSBError::Error::Device,
87 base::UTF8ToUTF16(kDeviceNoAccess)));
88 break;
89 default:
90 NOTREACHED();
94 void OnDeviceClosed(
95 ScopedWebCallbacks<blink::WebUSBDeviceCloseCallbacks> callbacks) {
96 callbacks.PassCallbacks()->onSuccess();
99 void HandlePassFailDeviceOperation(
100 ScopedWebCallbacks<blink::WebCallbacks<void, const blink::WebUSBError&>>
101 callbacks,
102 const std::string& failure_message,
103 bool success) {
104 auto scoped_callbacks = callbacks.PassCallbacks();
105 if (success)
106 scoped_callbacks->onSuccess();
107 else
108 RejectWithDeviceError(failure_message, scoped_callbacks.Pass());
111 void OnTransferIn(
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());
118 return;
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()));
126 void OnTransferOut(
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());
132 switch (status) {
133 case device::usb::TRANSFER_STATUS_COMPLETED:
134 info->status = blink::WebUSBTransferInfo::Status::Ok;
135 break;
136 case device::usb::TRANSFER_STATUS_STALLED:
137 info->status = blink::WebUSBTransferInfo::Status::Stall;
138 break;
139 case device::usb::TRANSFER_STATUS_BABBLE:
140 info->status = blink::WebUSBTransferInfo::Status::Babble;
141 break;
142 default:
143 RejectWithTransferError(scoped_callbacks.Pass());
144 return;
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()));
153 } // namespace
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 {
164 return device_info_;
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);
177 if (!device_) {
178 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks());
179 } else {
180 device_->Close(
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);
189 if (!device_) {
190 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks());
191 } else {
192 device_->SetConfiguration(
193 configuration_value,
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);
203 if (!device_) {
204 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks());
205 } else {
206 device_->ClaimInterface(
207 interface_number,
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);
217 if (!device_) {
218 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks());
219 } else {
220 device_->ReleaseInterface(
221 interface_number,
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);
232 if (!device_) {
233 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks());
234 } else {
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);
246 if (!device_) {
247 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks());
248 } else {
249 device_->ClearHalt(
250 endpoint_number,
251 base::Bind(&HandlePassFailDeviceOperation,
252 base::Passed(&scoped_callbacks), kClearHaltFailed));
256 void WebUSBDeviceImpl::controlTransfer(
257 const blink::WebUSBDevice::ControlTransferParameters& parameters,
258 uint8_t* data,
259 size_t data_size,
260 unsigned int timeout,
261 blink::WebUSBDeviceControlTransferCallbacks* callbacks) {
262 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks);
263 if (!device_) {
264 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks());
265 } else {
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)));
272 break;
273 case WebUSBDevice::TransferDirection::Out: {
274 std::vector<uint8_t> bytes;
275 if (data)
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),
281 data_size));
282 break;
284 default:
285 NOTREACHED();
290 void WebUSBDeviceImpl::transfer(
291 blink::WebUSBDevice::TransferDirection direction,
292 uint8_t endpoint_number,
293 uint8_t* data,
294 size_t data_size,
295 unsigned int timeout,
296 blink::WebUSBDeviceBulkTransferCallbacks* callbacks) {
297 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks);
298 if (!device_) {
299 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks());
300 } else {
301 switch (direction) {
302 case WebUSBDevice::TransferDirection::In:
303 device_->GenericTransferIn(
304 endpoint_number, data_size, timeout,
305 base::Bind(&OnTransferIn, base::Passed(&scoped_callbacks)));
306 break;
307 case WebUSBDevice::TransferDirection::Out: {
308 std::vector<uint8_t> bytes;
309 if (data)
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),
316 data_size));
317 break;
319 default:
320 NOTREACHED();
325 void WebUSBDeviceImpl::reset(blink::WebUSBDeviceResetCallbacks* callbacks) {
326 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks);
327 if (!device_) {
328 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks());
329 } else {
330 device_->Reset(
331 base::Bind(&HandlePassFailDeviceOperation,
332 base::Passed(&scoped_callbacks), kDeviceResetFailed));
336 } // namespace content