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

Unified Diff: device/usb/usb_device_impl.cc

Issue 1877503003: Replace libusb in the Linux/Chrome OS USB I/O path. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased and addressed feedback. Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « device/usb/usb_device_impl.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/usb/usb_device_impl.cc
diff --git a/device/usb/usb_device_impl.cc b/device/usb/usb_device_impl.cc
index 4a134bf4d1311e367567d2ba9faaa47acc6ab273..520b9efeb164ad22b9da49f5dec4f9899151b2f1 100644
--- a/device/usb/usb_device_impl.cc
+++ b/device/usb/usb_device_impl.cc
@@ -4,12 +4,14 @@
#include "device/usb/usb_device_impl.h"
+#include <fcntl.h>
#include <stddef.h>
#include <algorithm>
#include "base/bind.h"
#include "base/location.h"
+#include "base/posix/eintr_wrapper.h"
#include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
@@ -18,10 +20,15 @@
#include "components/device_event_log/device_event_log.h"
#include "device/usb/usb_context.h"
#include "device/usb/usb_descriptors.h"
-#include "device/usb/usb_device_handle_impl.h"
#include "device/usb/usb_error.h"
#include "third_party/libusb/src/libusb/libusb.h"
+#if defined(OS_ANDROID) || defined(OS_CHROMEOS) || defined(OS_LINUX)
+#include "device/usb/usb_device_handle_usbfs.h"
+#else
+#include "device/usb/usb_device_handle_impl.h"
+#endif
+
#if defined(OS_CHROMEOS)
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/permission_broker_client.h"
@@ -201,9 +208,9 @@ void UsbDeviceImpl::Open(const OpenCallback& callback) {
#endif // defined(OS_CHROMEOS)
}
-void UsbDeviceImpl::HandleClosed(scoped_refptr<UsbDeviceHandle> handle) {
+void UsbDeviceImpl::HandleClosed(UsbDeviceHandle* handle) {
DCHECK(thread_checker_.CalledOnValidThread());
- handles_.remove(handle.get());
+ handles_.remove(handle);
}
const UsbConfigDescriptor* UsbDeviceImpl::GetActiveConfiguration() const {
@@ -250,6 +257,15 @@ void UsbDeviceImpl::ReadAllConfigurations() {
}
}
+void UsbDeviceImpl::ActiveConfigurationChanged(int configuration_value) {
+ for (const auto& config : configurations_) {
+ if (config.configuration_value == configuration_value) {
+ active_configuration_ = &config;
+ return;
+ }
+ }
+}
+
void UsbDeviceImpl::RefreshActiveConfiguration() {
active_configuration_ = nullptr;
libusb_config_descriptor* platform_config;
@@ -261,13 +277,7 @@ void UsbDeviceImpl::RefreshActiveConfiguration() {
return;
}
- for (const auto& config : configurations_) {
- if (config.configuration_value == platform_config->bConfigurationValue) {
- active_configuration_ = &config;
- break;
- }
- }
-
+ ActiveConfigurationChanged(platform_config->bConfigurationValue);
libusb_free_config_descriptor(platform_config);
}
@@ -291,21 +301,32 @@ void UsbDeviceImpl::OnOpenRequestError(const OpenCallback& callback,
void UsbDeviceImpl::OpenOnBlockingThreadWithFd(dbus::FileDescriptor fd,
const OpenCallback& callback) {
fd.CheckValidity();
- DCHECK(fd.is_valid());
+ if (fd.is_valid()) {
+ base::ScopedFD scoped_fd(fd.TakeValue());
+ task_runner_->PostTask(FROM_HERE,
+ base::Bind(&UsbDeviceImpl::Opened, this,
+ base::Passed(&scoped_fd), callback));
+ } else {
+ USB_LOG(EVENT) << "Did not get valid device handle from permission broker.";
+ task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
+ }
+}
- PlatformUsbDeviceHandle handle;
- const int rv = libusb_open_fd(platform_device_, fd.TakeValue(), &handle);
- if (LIBUSB_SUCCESS == rv) {
- task_runner_->PostTask(
- FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback));
+#else
+#if defined(OS_LINUX)
+
+void UsbDeviceImpl::OpenOnBlockingThread(const OpenCallback& callback) {
+ base::ScopedFD fd(HANDLE_EINTR(open(device_path_.c_str(), O_RDWR)));
+ if (fd.is_valid()) {
+ task_runner_->PostTask(FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this,
+ base::Passed(&fd), callback));
} else {
- USB_LOG(EVENT) << "Failed to open device: "
- << ConvertPlatformUsbErrorToString(rv);
+ USB_PLOG(EVENT) << "Failed to open device";
task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
}
}
-#endif // defined(OS_CHROMEOS)
+#else
void UsbDeviceImpl::OpenOnBlockingThread(const OpenCallback& callback) {
PlatformUsbDeviceHandle handle;
@@ -316,17 +337,33 @@ void UsbDeviceImpl::OpenOnBlockingThread(const OpenCallback& callback) {
} else {
USB_LOG(EVENT) << "Failed to open device: "
<< ConvertPlatformUsbErrorToString(rv);
- task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
}
}
+#endif // defined(OS_LINUX)
+#endif // defined(OS_CHROMEOS)
+
+#if defined(OS_LINUX)
+
+void UsbDeviceImpl::Opened(base::ScopedFD fd, const OpenCallback& callback) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ scoped_refptr<UsbDeviceHandle> device_handle =
+ new UsbDeviceHandleUsbfs(this, std::move(fd), blocking_task_runner_);
+ handles_.push_back(device_handle.get());
+ callback.Run(device_handle);
+}
+
+#else
+
void UsbDeviceImpl::Opened(PlatformUsbDeviceHandle platform_handle,
const OpenCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
- scoped_refptr<UsbDeviceHandleImpl> device_handle = new UsbDeviceHandleImpl(
+ scoped_refptr<UsbDeviceHandle> device_handle = new UsbDeviceHandleImpl(
context_, this, platform_handle, blocking_task_runner_);
handles_.push_back(device_handle.get());
callback.Run(device_handle);
}
+#endif // defined(OS_LINUX)
+
} // namespace device
« no previous file with comments | « device/usb/usb_device_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698