Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "device/usb/usb_device_linux.h" | 5 #include "device/usb/usb_device_linux.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
| 15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "components/device_event_log/device_event_log.h" | 18 #include "components/device_event_log/device_event_log.h" |
| 19 #include "device/usb/usb_descriptors.h" | 19 #include "device/usb/usb_descriptors.h" |
| 20 #include "device/usb/usb_device_handle_usbfs.h" | 20 #include "device/usb/usb_device_handle_usbfs.h" |
| 21 #include "device/usb/usb_error.h" | 21 #include "device/usb/usb_error.h" |
| 22 | 22 |
| 23 #if defined(OS_CHROMEOS) | 23 #if defined(OS_CHROMEOS) |
| 24 #include "chromeos/dbus/dbus_thread_manager.h" | 24 #include "chromeos/dbus/dbus_thread_manager.h" |
| 25 #include "chromeos/dbus/permission_broker_client.h" | 25 #include "chromeos/dbus/permission_broker_client.h" |
| 26 #include "dbus/file_descriptor.h" // nogncheck | |
| 27 #endif // defined(OS_CHROMEOS) | 26 #endif // defined(OS_CHROMEOS) |
| 28 | 27 |
| 29 namespace device { | 28 namespace device { |
| 30 | 29 |
| 31 UsbDeviceLinux::UsbDeviceLinux( | 30 UsbDeviceLinux::UsbDeviceLinux( |
| 32 const std::string& device_path, | 31 const std::string& device_path, |
| 33 const UsbDeviceDescriptor& descriptor, | 32 const UsbDeviceDescriptor& descriptor, |
| 34 const std::string& manufacturer_string, | 33 const std::string& manufacturer_string, |
| 35 const std::string& product_string, | 34 const std::string& product_string, |
| 36 const std::string& serial_number, | 35 const std::string& serial_number, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 #else | 80 #else |
| 82 blocking_task_runner_->PostTask( | 81 blocking_task_runner_->PostTask( |
| 83 FROM_HERE, | 82 FROM_HERE, |
| 84 base::Bind(&UsbDeviceLinux::OpenOnBlockingThread, this, callback)); | 83 base::Bind(&UsbDeviceLinux::OpenOnBlockingThread, this, callback)); |
| 85 #endif // defined(OS_CHROMEOS) | 84 #endif // defined(OS_CHROMEOS) |
| 86 } | 85 } |
| 87 | 86 |
| 88 #if defined(OS_CHROMEOS) | 87 #if defined(OS_CHROMEOS) |
| 89 | 88 |
| 90 void UsbDeviceLinux::OnOpenRequestComplete(const OpenCallback& callback, | 89 void UsbDeviceLinux::OnOpenRequestComplete(const OpenCallback& callback, |
| 91 dbus::FileDescriptor fd) { | 90 base::ScopedFD fd) { |
| 92 blocking_task_runner_->PostTask( | 91 if (fd.is_valid()) { |
| 93 FROM_HERE, base::Bind(&UsbDeviceLinux::OpenOnBlockingThreadWithFd, this, | 92 Opened(std::move(fd), callback); |
| 94 base::Passed(&fd), callback)); | 93 } else { |
| 94 USB_LOG(EVENT) << "Did not get valid device handle from permission broker."; | |
| 95 callback.Run(nullptr); | |
| 96 } | |
|
stevenjb
2016/08/30 15:45:59
nit: invert if and early exit, no else.
hashimoto
2016/08/31 07:09:24
Done.
| |
| 95 } | 97 } |
| 96 | 98 |
| 97 void UsbDeviceLinux::OnOpenRequestError(const OpenCallback& callback, | 99 void UsbDeviceLinux::OnOpenRequestError(const OpenCallback& callback, |
| 98 const std::string& error_name, | 100 const std::string& error_name, |
| 99 const std::string& error_message) { | 101 const std::string& error_message) { |
| 100 USB_LOG(EVENT) << "Permission broker failed to open the device: " | 102 USB_LOG(EVENT) << "Permission broker failed to open the device: " |
| 101 << error_name << ": " << error_message; | 103 << error_name << ": " << error_message; |
| 102 callback.Run(nullptr); | 104 callback.Run(nullptr); |
| 103 } | 105 } |
| 104 | 106 |
| 105 void UsbDeviceLinux::OpenOnBlockingThreadWithFd(dbus::FileDescriptor fd, | |
| 106 const OpenCallback& callback) { | |
| 107 fd.CheckValidity(); | |
| 108 if (fd.is_valid()) { | |
| 109 base::ScopedFD scoped_fd(fd.TakeValue()); | |
| 110 task_runner_->PostTask(FROM_HERE, | |
| 111 base::Bind(&UsbDeviceLinux::Opened, this, | |
| 112 base::Passed(&scoped_fd), callback)); | |
| 113 } else { | |
| 114 USB_LOG(EVENT) << "Did not get valid device handle from permission broker."; | |
| 115 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr)); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 #else | 107 #else |
| 120 | 108 |
| 121 void UsbDeviceLinux::OpenOnBlockingThread(const OpenCallback& callback) { | 109 void UsbDeviceLinux::OpenOnBlockingThread(const OpenCallback& callback) { |
| 122 base::ScopedFD fd(HANDLE_EINTR(open(device_path_.c_str(), O_RDWR))); | 110 base::ScopedFD fd(HANDLE_EINTR(open(device_path_.c_str(), O_RDWR))); |
| 123 if (fd.is_valid()) { | 111 if (fd.is_valid()) { |
| 124 task_runner_->PostTask(FROM_HERE, base::Bind(&UsbDeviceLinux::Opened, this, | 112 task_runner_->PostTask(FROM_HERE, base::Bind(&UsbDeviceLinux::Opened, this, |
| 125 base::Passed(&fd), callback)); | 113 base::Passed(&fd), callback)); |
| 126 } else { | 114 } else { |
| 127 USB_PLOG(EVENT) << "Failed to open " << device_path_; | 115 USB_PLOG(EVENT) << "Failed to open " << device_path_; |
| 128 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr)); | 116 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr)); |
| 129 } | 117 } |
| 130 } | 118 } |
| 131 | 119 |
| 132 #endif // defined(OS_CHROMEOS) | 120 #endif // defined(OS_CHROMEOS) |
| 133 | 121 |
| 134 void UsbDeviceLinux::Opened(base::ScopedFD fd, const OpenCallback& callback) { | 122 void UsbDeviceLinux::Opened(base::ScopedFD fd, const OpenCallback& callback) { |
| 135 DCHECK(thread_checker_.CalledOnValidThread()); | 123 DCHECK(thread_checker_.CalledOnValidThread()); |
| 136 scoped_refptr<UsbDeviceHandle> device_handle = | 124 scoped_refptr<UsbDeviceHandle> device_handle = |
| 137 new UsbDeviceHandleUsbfs(this, std::move(fd), blocking_task_runner_); | 125 new UsbDeviceHandleUsbfs(this, std::move(fd), blocking_task_runner_); |
| 138 handles().push_back(device_handle.get()); | 126 handles().push_back(device_handle.get()); |
| 139 callback.Run(device_handle); | 127 callback.Run(device_handle); |
| 140 } | 128 } |
| 141 | 129 |
| 142 } // namespace device | 130 } // namespace device |
| OLD | NEW |