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/task_scheduler/post_task.h" | |
17 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
18 #include "components/device_event_log/device_event_log.h" | 19 #include "components/device_event_log/device_event_log.h" |
19 #include "device/usb/usb_descriptors.h" | 20 #include "device/usb/usb_descriptors.h" |
20 #include "device/usb/usb_device_handle_usbfs.h" | 21 #include "device/usb/usb_device_handle_usbfs.h" |
21 #include "device/usb/usb_error.h" | 22 #include "device/usb/usb_error.h" |
22 | 23 |
23 #if defined(OS_CHROMEOS) | 24 #if defined(OS_CHROMEOS) |
24 #include "chromeos/dbus/dbus_thread_manager.h" | 25 #include "chromeos/dbus/dbus_thread_manager.h" |
25 #include "chromeos/dbus/permission_broker_client.h" | 26 #include "chromeos/dbus/permission_broker_client.h" |
26 #endif // defined(OS_CHROMEOS) | 27 #endif // defined(OS_CHROMEOS) |
27 | 28 |
28 namespace device { | 29 namespace device { |
29 | 30 |
30 UsbDeviceLinux::UsbDeviceLinux( | 31 namespace { |
31 const std::string& device_path, | 32 |
32 const UsbDeviceDescriptor& descriptor, | 33 scoped_refptr<base::SequencedTaskRunner> CreateBlockingTaskRunner() { |
robliao
2017/05/01 16:36:19
It might be useful to have this in a shared header
Reilly Grant (use Gerrit)
2017/05/01 20:57:43
Done.
| |
33 const std::string& manufacturer_string, | 34 return base::CreateSequencedTaskRunnerWithTraits( |
34 const std::string& product_string, | 35 base::TaskTraits() |
35 const std::string& serial_number, | 36 .MayBlock() |
36 uint8_t active_configuration, | 37 .WithPriority(base::TaskPriority::USER_VISIBLE) |
37 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | 38 .WithShutdownBehavior( |
39 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)); | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 UsbDeviceLinux::UsbDeviceLinux(const std::string& device_path, | |
45 const UsbDeviceDescriptor& descriptor, | |
46 const std::string& manufacturer_string, | |
47 const std::string& product_string, | |
48 const std::string& serial_number, | |
49 uint8_t active_configuration) | |
38 : UsbDevice(descriptor, | 50 : UsbDevice(descriptor, |
39 base::UTF8ToUTF16(manufacturer_string), | 51 base::UTF8ToUTF16(manufacturer_string), |
40 base::UTF8ToUTF16(product_string), | 52 base::UTF8ToUTF16(product_string), |
41 base::UTF8ToUTF16(serial_number)), | 53 base::UTF8ToUTF16(serial_number)), |
42 device_path_(device_path), | 54 device_path_(device_path) { |
43 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
44 blocking_task_runner_(blocking_task_runner) { | |
45 ActiveConfigurationChanged(active_configuration); | 55 ActiveConfigurationChanged(active_configuration); |
46 } | 56 } |
47 | 57 |
48 UsbDeviceLinux::~UsbDeviceLinux() {} | 58 UsbDeviceLinux::~UsbDeviceLinux() {} |
49 | 59 |
50 #if defined(OS_CHROMEOS) | 60 #if defined(OS_CHROMEOS) |
51 | 61 |
52 void UsbDeviceLinux::CheckUsbAccess(const ResultCallback& callback) { | 62 void UsbDeviceLinux::CheckUsbAccess(const ResultCallback& callback) { |
53 DCHECK(thread_checker_.CalledOnValidThread()); | 63 DCHECK(sequence_checker_.CalledOnValidSequence()); |
54 chromeos::PermissionBrokerClient* client = | 64 chromeos::PermissionBrokerClient* client = |
55 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); | 65 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
56 DCHECK(client) << "Could not get permission broker client."; | 66 DCHECK(client) << "Could not get permission broker client."; |
57 client->CheckPathAccess(device_path_, callback); | 67 client->CheckPathAccess(device_path_, callback); |
58 } | 68 } |
59 | 69 |
60 #endif // defined(OS_CHROMEOS) | 70 #endif // defined(OS_CHROMEOS) |
61 | 71 |
62 void UsbDeviceLinux::Open(const OpenCallback& callback) { | 72 void UsbDeviceLinux::Open(const OpenCallback& callback) { |
63 DCHECK(thread_checker_.CalledOnValidThread()); | 73 DCHECK(sequence_checker_.CalledOnValidSequence()); |
64 | 74 |
65 #if defined(OS_CHROMEOS) | 75 #if defined(OS_CHROMEOS) |
66 chromeos::PermissionBrokerClient* client = | 76 chromeos::PermissionBrokerClient* client = |
67 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); | 77 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
68 DCHECK(client) << "Could not get permission broker client."; | 78 DCHECK(client) << "Could not get permission broker client."; |
69 client->OpenPath( | 79 client->OpenPath( |
70 device_path_, | 80 device_path_, |
71 base::Bind(&UsbDeviceLinux::OnOpenRequestComplete, this, callback), | 81 base::Bind(&UsbDeviceLinux::OnOpenRequestComplete, this, callback), |
72 base::Bind(&UsbDeviceLinux::OnOpenRequestError, this, callback)); | 82 base::Bind(&UsbDeviceLinux::OnOpenRequestError, this, callback)); |
73 #else | 83 #else |
74 blocking_task_runner_->PostTask( | 84 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = |
85 CreateBlockingTaskRunner(); | |
86 blocking_task_runner->PostTask( | |
75 FROM_HERE, | 87 FROM_HERE, |
76 base::Bind(&UsbDeviceLinux::OpenOnBlockingThread, this, callback)); | 88 base::Bind(&UsbDeviceLinux::OpenOnBlockingThread, this, callback, |
89 base::ThreadTaskRunnerHandle::Get(), blocking_task_runner)); | |
77 #endif // defined(OS_CHROMEOS) | 90 #endif // defined(OS_CHROMEOS) |
78 } | 91 } |
79 | 92 |
80 #if defined(OS_CHROMEOS) | 93 #if defined(OS_CHROMEOS) |
81 | 94 |
82 void UsbDeviceLinux::OnOpenRequestComplete(const OpenCallback& callback, | 95 void UsbDeviceLinux::OnOpenRequestComplete(const OpenCallback& callback, |
83 base::ScopedFD fd) { | 96 base::ScopedFD fd) { |
84 if (!fd.is_valid()) { | 97 if (!fd.is_valid()) { |
85 USB_LOG(EVENT) << "Did not get valid device handle from permission broker."; | 98 USB_LOG(EVENT) << "Did not get valid device handle from permission broker."; |
86 callback.Run(nullptr); | 99 callback.Run(nullptr); |
87 return; | 100 return; |
88 } | 101 } |
89 Opened(std::move(fd), callback); | 102 Opened(std::move(fd), callback, CreateBlockingTaskRunner()); |
90 } | 103 } |
91 | 104 |
92 void UsbDeviceLinux::OnOpenRequestError(const OpenCallback& callback, | 105 void UsbDeviceLinux::OnOpenRequestError(const OpenCallback& callback, |
93 const std::string& error_name, | 106 const std::string& error_name, |
94 const std::string& error_message) { | 107 const std::string& error_message) { |
95 USB_LOG(EVENT) << "Permission broker failed to open the device: " | 108 USB_LOG(EVENT) << "Permission broker failed to open the device: " |
96 << error_name << ": " << error_message; | 109 << error_name << ": " << error_message; |
97 callback.Run(nullptr); | 110 callback.Run(nullptr); |
98 } | 111 } |
99 | 112 |
100 #else | 113 #else |
101 | 114 |
102 void UsbDeviceLinux::OpenOnBlockingThread(const OpenCallback& callback) { | 115 void UsbDeviceLinux::OpenOnBlockingThread( |
116 const OpenCallback& callback, | |
117 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
118 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) { | |
103 base::ScopedFD fd(HANDLE_EINTR(open(device_path_.c_str(), O_RDWR))); | 119 base::ScopedFD fd(HANDLE_EINTR(open(device_path_.c_str(), O_RDWR))); |
104 if (fd.is_valid()) { | 120 if (fd.is_valid()) { |
105 task_runner_->PostTask(FROM_HERE, base::Bind(&UsbDeviceLinux::Opened, this, | 121 task_runner->PostTask( |
106 base::Passed(&fd), callback)); | 122 FROM_HERE, base::Bind(&UsbDeviceLinux::Opened, this, base::Passed(&fd), |
123 callback, blocking_task_runner)); | |
107 } else { | 124 } else { |
108 USB_PLOG(EVENT) << "Failed to open " << device_path_; | 125 USB_PLOG(EVENT) << "Failed to open " << device_path_; |
109 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr)); | 126 task_runner->PostTask(FROM_HERE, base::Bind(callback, nullptr)); |
110 } | 127 } |
111 } | 128 } |
112 | 129 |
113 #endif // defined(OS_CHROMEOS) | 130 #endif // defined(OS_CHROMEOS) |
114 | 131 |
115 void UsbDeviceLinux::Opened(base::ScopedFD fd, const OpenCallback& callback) { | 132 void UsbDeviceLinux::Opened( |
116 DCHECK(thread_checker_.CalledOnValidThread()); | 133 base::ScopedFD fd, |
134 const OpenCallback& callback, | |
135 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) { | |
136 DCHECK(sequence_checker_.CalledOnValidSequence()); | |
117 scoped_refptr<UsbDeviceHandle> device_handle = | 137 scoped_refptr<UsbDeviceHandle> device_handle = |
118 new UsbDeviceHandleUsbfs(this, std::move(fd), blocking_task_runner_); | 138 new UsbDeviceHandleUsbfs(this, std::move(fd), blocking_task_runner); |
119 handles().push_back(device_handle.get()); | 139 handles().push_back(device_handle.get()); |
120 callback.Run(device_handle); | 140 callback.Run(device_handle); |
121 } | 141 } |
122 | 142 |
123 } // namespace device | 143 } // namespace device |
OLD | NEW |