| 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_service.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 #endif // defined(OS_CHROMEOS) | 26 #endif // defined(OS_CHROMEOS) |
| 27 | 27 |
| 28 namespace device { | 28 namespace device { |
| 29 | 29 |
| 30 UsbDeviceLinux::UsbDeviceLinux( | 30 UsbDeviceLinux::UsbDeviceLinux(const std::string& device_path, |
| 31 const std::string& device_path, | 31 const UsbDeviceDescriptor& descriptor, |
| 32 const UsbDeviceDescriptor& descriptor, | 32 const std::string& manufacturer_string, |
| 33 const std::string& manufacturer_string, | 33 const std::string& product_string, |
| 34 const std::string& product_string, | 34 const std::string& serial_number, |
| 35 const std::string& serial_number, | 35 uint8_t active_configuration) |
| 36 uint8_t active_configuration, | |
| 37 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | |
| 38 : UsbDevice(descriptor, | 36 : UsbDevice(descriptor, |
| 39 base::UTF8ToUTF16(manufacturer_string), | 37 base::UTF8ToUTF16(manufacturer_string), |
| 40 base::UTF8ToUTF16(product_string), | 38 base::UTF8ToUTF16(product_string), |
| 41 base::UTF8ToUTF16(serial_number)), | 39 base::UTF8ToUTF16(serial_number)), |
| 42 device_path_(device_path), | 40 device_path_(device_path) { |
| 43 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 44 blocking_task_runner_(blocking_task_runner) { | |
| 45 ActiveConfigurationChanged(active_configuration); | 41 ActiveConfigurationChanged(active_configuration); |
| 46 } | 42 } |
| 47 | 43 |
| 48 UsbDeviceLinux::~UsbDeviceLinux() {} | 44 UsbDeviceLinux::~UsbDeviceLinux() {} |
| 49 | 45 |
| 50 #if defined(OS_CHROMEOS) | 46 #if defined(OS_CHROMEOS) |
| 51 | 47 |
| 52 void UsbDeviceLinux::CheckUsbAccess(const ResultCallback& callback) { | 48 void UsbDeviceLinux::CheckUsbAccess(const ResultCallback& callback) { |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); | 49 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 54 chromeos::PermissionBrokerClient* client = | 50 chromeos::PermissionBrokerClient* client = |
| 55 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); | 51 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
| 56 DCHECK(client) << "Could not get permission broker client."; | 52 DCHECK(client) << "Could not get permission broker client."; |
| 57 client->CheckPathAccess(device_path_, callback); | 53 client->CheckPathAccess(device_path_, callback); |
| 58 } | 54 } |
| 59 | 55 |
| 60 #endif // defined(OS_CHROMEOS) | 56 #endif // defined(OS_CHROMEOS) |
| 61 | 57 |
| 62 void UsbDeviceLinux::Open(const OpenCallback& callback) { | 58 void UsbDeviceLinux::Open(const OpenCallback& callback) { |
| 63 DCHECK(thread_checker_.CalledOnValidThread()); | 59 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 64 | 60 |
| 65 #if defined(OS_CHROMEOS) | 61 #if defined(OS_CHROMEOS) |
| 66 chromeos::PermissionBrokerClient* client = | 62 chromeos::PermissionBrokerClient* client = |
| 67 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); | 63 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
| 68 DCHECK(client) << "Could not get permission broker client."; | 64 DCHECK(client) << "Could not get permission broker client."; |
| 69 client->OpenPath( | 65 client->OpenPath( |
| 70 device_path_, | 66 device_path_, |
| 71 base::Bind(&UsbDeviceLinux::OnOpenRequestComplete, this, callback), | 67 base::Bind(&UsbDeviceLinux::OnOpenRequestComplete, this, callback), |
| 72 base::Bind(&UsbDeviceLinux::OnOpenRequestError, this, callback)); | 68 base::Bind(&UsbDeviceLinux::OnOpenRequestError, this, callback)); |
| 73 #else | 69 #else |
| 74 blocking_task_runner_->PostTask( | 70 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = |
| 71 UsbService::CreateBlockingTaskRunner(); |
| 72 blocking_task_runner->PostTask( |
| 75 FROM_HERE, | 73 FROM_HERE, |
| 76 base::Bind(&UsbDeviceLinux::OpenOnBlockingThread, this, callback)); | 74 base::Bind(&UsbDeviceLinux::OpenOnBlockingThread, this, callback, |
| 75 base::ThreadTaskRunnerHandle::Get(), blocking_task_runner)); |
| 77 #endif // defined(OS_CHROMEOS) | 76 #endif // defined(OS_CHROMEOS) |
| 78 } | 77 } |
| 79 | 78 |
| 80 #if defined(OS_CHROMEOS) | 79 #if defined(OS_CHROMEOS) |
| 81 | 80 |
| 82 void UsbDeviceLinux::OnOpenRequestComplete(const OpenCallback& callback, | 81 void UsbDeviceLinux::OnOpenRequestComplete(const OpenCallback& callback, |
| 83 base::ScopedFD fd) { | 82 base::ScopedFD fd) { |
| 84 if (!fd.is_valid()) { | 83 if (!fd.is_valid()) { |
| 85 USB_LOG(EVENT) << "Did not get valid device handle from permission broker."; | 84 USB_LOG(EVENT) << "Did not get valid device handle from permission broker."; |
| 86 callback.Run(nullptr); | 85 callback.Run(nullptr); |
| 87 return; | 86 return; |
| 88 } | 87 } |
| 89 Opened(std::move(fd), callback); | 88 Opened(std::move(fd), callback, UsbService::CreateBlockingTaskRunner()); |
| 90 } | 89 } |
| 91 | 90 |
| 92 void UsbDeviceLinux::OnOpenRequestError(const OpenCallback& callback, | 91 void UsbDeviceLinux::OnOpenRequestError(const OpenCallback& callback, |
| 93 const std::string& error_name, | 92 const std::string& error_name, |
| 94 const std::string& error_message) { | 93 const std::string& error_message) { |
| 95 USB_LOG(EVENT) << "Permission broker failed to open the device: " | 94 USB_LOG(EVENT) << "Permission broker failed to open the device: " |
| 96 << error_name << ": " << error_message; | 95 << error_name << ": " << error_message; |
| 97 callback.Run(nullptr); | 96 callback.Run(nullptr); |
| 98 } | 97 } |
| 99 | 98 |
| 100 #else | 99 #else |
| 101 | 100 |
| 102 void UsbDeviceLinux::OpenOnBlockingThread(const OpenCallback& callback) { | 101 void UsbDeviceLinux::OpenOnBlockingThread( |
| 102 const OpenCallback& callback, |
| 103 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 104 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) { |
| 103 base::ScopedFD fd(HANDLE_EINTR(open(device_path_.c_str(), O_RDWR))); | 105 base::ScopedFD fd(HANDLE_EINTR(open(device_path_.c_str(), O_RDWR))); |
| 104 if (fd.is_valid()) { | 106 if (fd.is_valid()) { |
| 105 task_runner_->PostTask(FROM_HERE, base::Bind(&UsbDeviceLinux::Opened, this, | 107 task_runner->PostTask( |
| 106 base::Passed(&fd), callback)); | 108 FROM_HERE, base::Bind(&UsbDeviceLinux::Opened, this, base::Passed(&fd), |
| 109 callback, blocking_task_runner)); |
| 107 } else { | 110 } else { |
| 108 USB_PLOG(EVENT) << "Failed to open " << device_path_; | 111 USB_PLOG(EVENT) << "Failed to open " << device_path_; |
| 109 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr)); | 112 task_runner->PostTask(FROM_HERE, base::Bind(callback, nullptr)); |
| 110 } | 113 } |
| 111 } | 114 } |
| 112 | 115 |
| 113 #endif // defined(OS_CHROMEOS) | 116 #endif // defined(OS_CHROMEOS) |
| 114 | 117 |
| 115 void UsbDeviceLinux::Opened(base::ScopedFD fd, const OpenCallback& callback) { | 118 void UsbDeviceLinux::Opened( |
| 116 DCHECK(thread_checker_.CalledOnValidThread()); | 119 base::ScopedFD fd, |
| 120 const OpenCallback& callback, |
| 121 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) { |
| 122 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 117 scoped_refptr<UsbDeviceHandle> device_handle = | 123 scoped_refptr<UsbDeviceHandle> device_handle = |
| 118 new UsbDeviceHandleUsbfs(this, std::move(fd), blocking_task_runner_); | 124 new UsbDeviceHandleUsbfs(this, std::move(fd), blocking_task_runner); |
| 119 handles().push_back(device_handle.get()); | 125 handles().push_back(device_handle.get()); |
| 120 callback.Run(device_handle); | 126 callback.Run(device_handle); |
| 121 } | 127 } |
| 122 | 128 |
| 123 } // namespace device | 129 } // namespace device |
| OLD | NEW |