| 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 <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/mojo/type_converters.h" |
| 13 #include "content/child/scoped_web_callbacks.h" | 13 #include "content/child/scoped_web_callbacks.h" |
| 14 #include "content/renderer/usb/type_converters.h" | 14 #include "content/renderer/usb/type_converters.h" |
| 15 #include "mojo/shell/public/cpp/connect.h" | 15 #include "mojo/shell/public/cpp/connect.h" |
| 16 #include "mojo/shell/public/interfaces/connector.mojom.h" | 16 #include "mojo/shell/public/interfaces/connector.mojom.h" |
| 17 #include "third_party/WebKit/public/platform/WebVector.h" | 17 #include "third_party/WebKit/public/platform/WebVector.h" |
| 18 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h" | 18 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h" |
| 19 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBTransferInfo.h
" | 19 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBTransferInfo.h
" |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const char kClaimInterfaceFailed[] = "Unable to claim interface."; | 25 const char kClaimInterfaceFailed[] = "Unable to claim interface."; |
| 26 const char kClearHaltFailed[] = "Unable to clear endpoint."; | 26 const char kClearHaltFailed[] = "Unable to clear endpoint."; |
| 27 const char kDeviceAlreadyOpen[] = "Device has already been opened."; | 27 const char kDeviceAlreadyOpen[] = "Device has already been opened."; |
| 28 const char kDeviceNoAccess[] = "Access denied."; | 28 const char kDeviceNoAccess[] = "Access denied."; |
| 29 const char kDeviceNotConfigured[] = "Device not configured."; | |
| 30 const char kDeviceUnavailable[] = "Device unavailable."; | 29 const char kDeviceUnavailable[] = "Device unavailable."; |
| 31 const char kDeviceResetFailed[] = "Unable to reset the device."; | 30 const char kDeviceResetFailed[] = "Unable to reset the device."; |
| 32 const char kReleaseInterfaceFailed[] = "Unable to release interface."; | 31 const char kReleaseInterfaceFailed[] = "Unable to release interface."; |
| 33 const char kSetConfigurationFailed[] = "Unable to set device configuration."; | 32 const char kSetConfigurationFailed[] = "Unable to set device configuration."; |
| 34 const char kSetInterfaceFailed[] = "Unable to set device interface."; | 33 const char kSetInterfaceFailed[] = "Unable to set device interface."; |
| 35 const char kTransferFailed[] = "Transfer failed."; | 34 const char kTransferFailed[] = "Transfer failed."; |
| 36 | 35 |
| 37 // Generic default rejection handler for any WebUSB callbacks type. Assumes | 36 // Generic default rejection handler for any WebUSB callbacks type. Assumes |
| 38 // |CallbacksType| is a blink::WebCallbacks<T, const blink::WebUSBError&> | 37 // |CallbacksType| is a blink::WebCallbacks<T, const blink::WebUSBError&> |
| 39 // for any type |T|. | 38 // for any type |T|. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 default: | 82 default: |
| 84 NOTREACHED(); | 83 NOTREACHED(); |
| 85 } | 84 } |
| 86 } | 85 } |
| 87 | 86 |
| 88 void OnDeviceClosed( | 87 void OnDeviceClosed( |
| 89 ScopedWebCallbacks<blink::WebUSBDeviceCloseCallbacks> callbacks) { | 88 ScopedWebCallbacks<blink::WebUSBDeviceCloseCallbacks> callbacks) { |
| 90 callbacks.PassCallbacks()->onSuccess(); | 89 callbacks.PassCallbacks()->onSuccess(); |
| 91 } | 90 } |
| 92 | 91 |
| 93 void OnGetConfiguration( | |
| 94 ScopedWebCallbacks<blink::WebUSBDeviceGetConfigurationCallbacks> callbacks, | |
| 95 uint8_t configuration_value) { | |
| 96 auto scoped_callbacks = callbacks.PassCallbacks(); | |
| 97 if (configuration_value == 0) { | |
| 98 RejectWithError(blink::WebUSBError(blink::WebUSBError::Error::NotFound, | |
| 99 kDeviceNotConfigured), | |
| 100 std::move(scoped_callbacks)); | |
| 101 } else { | |
| 102 scoped_callbacks->onSuccess(configuration_value); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void HandlePassFailDeviceOperation( | 92 void HandlePassFailDeviceOperation( |
| 107 ScopedWebCallbacks<blink::WebCallbacks<void, const blink::WebUSBError&>> | 93 ScopedWebCallbacks<blink::WebCallbacks<void, const blink::WebUSBError&>> |
| 108 callbacks, | 94 callbacks, |
| 109 const std::string& failure_message, | 95 const std::string& failure_message, |
| 110 bool success) { | 96 bool success) { |
| 111 auto scoped_callbacks = callbacks.PassCallbacks(); | 97 auto scoped_callbacks = callbacks.PassCallbacks(); |
| 112 if (success) { | 98 if (success) { |
| 113 scoped_callbacks->onSuccess(); | 99 scoped_callbacks->onSuccess(); |
| 114 } else { | 100 } else { |
| 115 RejectWithError(blink::WebUSBError(blink::WebUSBError::Error::Network, | 101 RejectWithError(blink::WebUSBError(blink::WebUSBError::Error::Network, |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 device_->Open(base::Bind(&OnOpenDevice, base::Passed(&scoped_callbacks))); | 237 device_->Open(base::Bind(&OnOpenDevice, base::Passed(&scoped_callbacks))); |
| 252 } | 238 } |
| 253 | 239 |
| 254 void WebUSBDeviceImpl::close(blink::WebUSBDeviceCloseCallbacks* callbacks) { | 240 void WebUSBDeviceImpl::close(blink::WebUSBDeviceCloseCallbacks* callbacks) { |
| 255 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 241 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 256 if (device_) | 242 if (device_) |
| 257 device_->Close( | 243 device_->Close( |
| 258 base::Bind(&OnDeviceClosed, base::Passed(&scoped_callbacks))); | 244 base::Bind(&OnDeviceClosed, base::Passed(&scoped_callbacks))); |
| 259 } | 245 } |
| 260 | 246 |
| 261 void WebUSBDeviceImpl::getConfiguration( | |
| 262 blink::WebUSBDeviceGetConfigurationCallbacks* callbacks) { | |
| 263 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | |
| 264 if (device_) | |
| 265 device_->GetConfiguration( | |
| 266 base::Bind(&OnGetConfiguration, base::Passed(&scoped_callbacks))); | |
| 267 } | |
| 268 | |
| 269 void WebUSBDeviceImpl::setConfiguration( | 247 void WebUSBDeviceImpl::setConfiguration( |
| 270 uint8_t configuration_value, | 248 uint8_t configuration_value, |
| 271 blink::WebUSBDeviceSetConfigurationCallbacks* callbacks) { | 249 blink::WebUSBDeviceSetConfigurationCallbacks* callbacks) { |
| 272 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 250 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 273 if (device_) | 251 if (device_) |
| 274 device_->SetConfiguration( | 252 device_->SetConfiguration( |
| 275 configuration_value, | 253 configuration_value, |
| 276 base::Bind(&HandlePassFailDeviceOperation, | 254 base::Bind(&HandlePassFailDeviceOperation, |
| 277 base::Passed(&scoped_callbacks), kSetConfigurationFailed)); | 255 base::Passed(&scoped_callbacks), kSetConfigurationFailed)); |
| 278 } | 256 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 | 408 |
| 431 void WebUSBDeviceImpl::reset(blink::WebUSBDeviceResetCallbacks* callbacks) { | 409 void WebUSBDeviceImpl::reset(blink::WebUSBDeviceResetCallbacks* callbacks) { |
| 432 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 410 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 433 if (device_) | 411 if (device_) |
| 434 device_->Reset(base::Bind(&HandlePassFailDeviceOperation, | 412 device_->Reset(base::Bind(&HandlePassFailDeviceOperation, |
| 435 base::Passed(&scoped_callbacks), | 413 base::Passed(&scoped_callbacks), |
| 436 kDeviceResetFailed)); | 414 kDeviceResetFailed)); |
| 437 } | 415 } |
| 438 | 416 |
| 439 } // namespace content | 417 } // namespace content |
| OLD | NEW |