OLD | NEW |
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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "content/child/scoped_web_callbacks.h" | 10 #include "content/child/scoped_web_callbacks.h" |
11 #include "content/renderer/usb/type_converters.h" | 11 #include "content/renderer/usb/type_converters.h" |
12 #include "device/devices_app/public/cpp/constants.h" | 12 #include "device/devices_app/public/cpp/constants.h" |
13 #include "mojo/application/public/cpp/connect.h" | 13 #include "mojo/application/public/cpp/connect.h" |
14 #include "mojo/application/public/interfaces/shell.mojom.h" | 14 #include "mojo/application/public/interfaces/shell.mojom.h" |
15 #include "third_party/WebKit/public/platform/WebVector.h" | 15 #include "third_party/WebKit/public/platform/WebVector.h" |
16 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h" | 16 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h" |
17 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBTransferInfo.h
" | 17 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBTransferInfo.h
" |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 const char kClaimInterfaceFailed[] = "Unable to claim interface."; | 23 const char kClaimInterfaceFailed[] = "Unable to claim interface."; |
24 const char kClearHaltFailed[] = "Unable to clear endpoint."; | 24 const char kClearHaltFailed[] = "Unable to clear endpoint."; |
25 const char kDeviceNoAccess[] = "Access denied."; | 25 const char kDeviceNoAccess[] = "Access denied."; |
26 const char kDeviceNotFound[] = "Device not found."; | |
27 const char kDeviceNotOpened[] = "Device not opened."; | 26 const char kDeviceNotOpened[] = "Device not opened."; |
28 const char kDeviceUnavailable[] = "Device unavailable."; | 27 const char kDeviceUnavailable[] = "Device unavailable."; |
29 const char kDeviceResetFailed[] = "Unable to reset the device."; | 28 const char kDeviceResetFailed[] = "Unable to reset the device."; |
30 const char kReleaseInterfaceFailed[] = "Unable to release interface."; | 29 const char kReleaseInterfaceFailed[] = "Unable to release interface."; |
31 const char kSetConfigurationFailed[] = "Unable to set device configuration."; | 30 const char kSetConfigurationFailed[] = "Unable to set device configuration."; |
32 const char kSetInterfaceFailed[] = "Unable to set device interface."; | 31 const char kSetInterfaceFailed[] = "Unable to set device interface."; |
33 const char kTransferFailed[] = "Transfer failed."; | 32 const char kTransferFailed[] = "Transfer failed."; |
34 | 33 |
35 // Generic default rejection handler for any WebUSB callbacks type. Assumes | 34 // Generic default rejection handler for any WebUSB callbacks type. Assumes |
36 // |CallbacksType| is a blink::WebCallbacks<T, const blink::WebUSBError&> | 35 // |CallbacksType| is a blink::WebCallbacks<T, const blink::WebUSBError&> |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 } | 68 } |
70 | 69 |
71 void OnOpenDevice( | 70 void OnOpenDevice( |
72 ScopedWebCallbacks<blink::WebUSBDeviceOpenCallbacks> callbacks, | 71 ScopedWebCallbacks<blink::WebUSBDeviceOpenCallbacks> callbacks, |
73 device::usb::OpenDeviceError error) { | 72 device::usb::OpenDeviceError error) { |
74 auto scoped_callbacks = callbacks.PassCallbacks(); | 73 auto scoped_callbacks = callbacks.PassCallbacks(); |
75 switch(error) { | 74 switch(error) { |
76 case device::usb::OPEN_DEVICE_ERROR_OK: | 75 case device::usb::OPEN_DEVICE_ERROR_OK: |
77 scoped_callbacks->onSuccess(); | 76 scoped_callbacks->onSuccess(); |
78 break; | 77 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: | 78 case device::usb::OPEN_DEVICE_ERROR_ACCESS_DENIED: |
85 scoped_callbacks->onError(blink::WebUSBError( | 79 scoped_callbacks->onError(blink::WebUSBError( |
86 blink::WebUSBError::Error::Device, | 80 blink::WebUSBError::Error::Device, |
87 base::UTF8ToUTF16(kDeviceNoAccess))); | 81 base::UTF8ToUTF16(kDeviceNoAccess))); |
88 break; | 82 break; |
89 default: | 83 default: |
90 NOTREACHED(); | 84 NOTREACHED(); |
91 } | 85 } |
92 } | 86 } |
93 | 87 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 } | 139 } |
146 | 140 |
147 // TODO(rockot): Device::ControlTransferOut should expose the number of bytes | 141 // TODO(rockot): Device::ControlTransferOut should expose the number of bytes |
148 // actually transferred so we can send it from here. | 142 // actually transferred so we can send it from here. |
149 info->bytesWritten = bytes_written; | 143 info->bytesWritten = bytes_written; |
150 scoped_callbacks->onSuccess(adoptWebPtr(info.release())); | 144 scoped_callbacks->onSuccess(adoptWebPtr(info.release())); |
151 } | 145 } |
152 | 146 |
153 } // namespace | 147 } // namespace |
154 | 148 |
155 WebUSBDeviceImpl::WebUSBDeviceImpl(device::usb::DeviceManagerPtr device_manager, | 149 WebUSBDeviceImpl::WebUSBDeviceImpl(device::usb::DevicePtr device, |
156 const blink::WebUSBDeviceInfo& device_info) | 150 const blink::WebUSBDeviceInfo& device_info) |
157 : device_manager_(device_manager.Pass()), | 151 : device_(device.Pass()), device_info_(device_info), weak_factory_(this) {} |
158 device_info_(device_info), | |
159 weak_factory_(this) {} | |
160 | 152 |
161 WebUSBDeviceImpl::~WebUSBDeviceImpl() {} | 153 WebUSBDeviceImpl::~WebUSBDeviceImpl() {} |
162 | 154 |
163 const blink::WebUSBDeviceInfo& WebUSBDeviceImpl::info() const { | 155 const blink::WebUSBDeviceInfo& WebUSBDeviceImpl::info() const { |
164 return device_info_; | 156 return device_info_; |
165 } | 157 } |
166 | 158 |
167 void WebUSBDeviceImpl::open(blink::WebUSBDeviceOpenCallbacks* callbacks) { | 159 void WebUSBDeviceImpl::open(blink::WebUSBDeviceOpenCallbacks* callbacks) { |
168 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 160 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
169 device_manager_->OpenDevice( | 161 device_->Open(base::Bind(&OnOpenDevice, base::Passed(&scoped_callbacks))); |
170 device_info_.guid.utf8(), | |
171 mojo::GetProxy(&device_), | |
172 base::Bind(&OnOpenDevice, base::Passed(&scoped_callbacks))); | |
173 } | 162 } |
174 | 163 |
175 void WebUSBDeviceImpl::close(blink::WebUSBDeviceCloseCallbacks* callbacks) { | 164 void WebUSBDeviceImpl::close(blink::WebUSBDeviceCloseCallbacks* callbacks) { |
176 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 165 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
177 if (!device_) { | 166 if (!device_) { |
178 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks()); | 167 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks()); |
179 } else { | 168 } else { |
180 device_->Close( | 169 device_->Close( |
181 base::Bind(&OnDeviceClosed, base::Passed(&scoped_callbacks))); | 170 base::Bind(&OnDeviceClosed, base::Passed(&scoped_callbacks))); |
182 } | 171 } |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 if (!device_) { | 316 if (!device_) { |
328 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks()); | 317 RejectWithDeviceError(kDeviceNotOpened, scoped_callbacks.PassCallbacks()); |
329 } else { | 318 } else { |
330 device_->Reset( | 319 device_->Reset( |
331 base::Bind(&HandlePassFailDeviceOperation, | 320 base::Bind(&HandlePassFailDeviceOperation, |
332 base::Passed(&scoped_callbacks), kDeviceResetFailed)); | 321 base::Passed(&scoped_callbacks), kDeviceResetFailed)); |
333 } | 322 } |
334 } | 323 } |
335 | 324 |
336 } // namespace content | 325 } // namespace content |
OLD | NEW |