OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_impl.h" | 5 #include "device/usb/usb_device_impl.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/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/threading/thread_restrictions.h" |
18 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
19 #include "build/build_config.h" | 20 #include "build/build_config.h" |
20 #include "components/device_event_log/device_event_log.h" | 21 #include "components/device_event_log/device_event_log.h" |
21 #include "device/usb/usb_context.h" | 22 #include "device/usb/usb_context.h" |
22 #include "device/usb/usb_descriptors.h" | 23 #include "device/usb/usb_descriptors.h" |
23 #include "device/usb/usb_device_handle_impl.h" | 24 #include "device/usb/usb_device_handle_impl.h" |
24 #include "device/usb/usb_error.h" | 25 #include "device/usb/usb_error.h" |
| 26 #include "device/usb/usb_service.h" |
25 #include "third_party/libusb/src/libusb/libusb.h" | 27 #include "third_party/libusb/src/libusb/libusb.h" |
26 | 28 |
27 namespace device { | 29 namespace device { |
28 | 30 |
29 UsbDeviceImpl::UsbDeviceImpl( | 31 UsbDeviceImpl::UsbDeviceImpl(scoped_refptr<UsbContext> context, |
30 scoped_refptr<UsbContext> context, | 32 PlatformUsbDevice platform_device, |
31 PlatformUsbDevice platform_device, | 33 const libusb_device_descriptor& descriptor) |
32 const libusb_device_descriptor& descriptor, | |
33 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | |
34 : UsbDevice(descriptor.bcdUSB, | 34 : UsbDevice(descriptor.bcdUSB, |
35 descriptor.bDeviceClass, | 35 descriptor.bDeviceClass, |
36 descriptor.bDeviceSubClass, | 36 descriptor.bDeviceSubClass, |
37 descriptor.bDeviceProtocol, | 37 descriptor.bDeviceProtocol, |
38 descriptor.idVendor, | 38 descriptor.idVendor, |
39 descriptor.idProduct, | 39 descriptor.idProduct, |
40 descriptor.bcdDevice, | 40 descriptor.bcdDevice, |
41 base::string16(), | 41 base::string16(), |
42 base::string16(), | 42 base::string16(), |
43 base::string16()), | 43 base::string16()), |
44 platform_device_(platform_device), | 44 platform_device_(platform_device), |
45 context_(context), | 45 context_(context) { |
46 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
47 blocking_task_runner_(blocking_task_runner) { | |
48 CHECK(platform_device) << "platform_device cannot be NULL"; | 46 CHECK(platform_device) << "platform_device cannot be NULL"; |
49 libusb_ref_device(platform_device); | 47 libusb_ref_device(platform_device); |
50 ReadAllConfigurations(); | 48 ReadAllConfigurations(); |
51 RefreshActiveConfiguration(); | 49 RefreshActiveConfiguration(); |
52 } | 50 } |
53 | 51 |
54 UsbDeviceImpl::~UsbDeviceImpl() { | 52 UsbDeviceImpl::~UsbDeviceImpl() { |
55 // The destructor must be safe to call from any thread. | 53 // The destructor must be safe to call from any thread. |
56 libusb_unref_device(platform_device_); | 54 libusb_unref_device(platform_device_); |
57 } | 55 } |
58 | 56 |
59 void UsbDeviceImpl::Open(const OpenCallback& callback) { | 57 void UsbDeviceImpl::Open(const OpenCallback& callback) { |
60 DCHECK(thread_checker_.CalledOnValidThread()); | 58 DCHECK(thread_checker_.CalledOnValidThread()); |
61 | 59 |
62 blocking_task_runner_->PostTask( | 60 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = |
| 61 UsbService::CreateBlockingTaskRunner(); |
| 62 blocking_task_runner->PostTask( |
63 FROM_HERE, | 63 FROM_HERE, |
64 base::Bind(&UsbDeviceImpl::OpenOnBlockingThread, this, callback)); | 64 base::Bind(&UsbDeviceImpl::OpenOnBlockingThread, this, callback, |
| 65 base::ThreadTaskRunnerHandle::Get(), blocking_task_runner)); |
65 } | 66 } |
66 | 67 |
67 void UsbDeviceImpl::ReadAllConfigurations() { | 68 void UsbDeviceImpl::ReadAllConfigurations() { |
68 libusb_device_descriptor device_descriptor; | 69 libusb_device_descriptor device_descriptor; |
69 int rv = libusb_get_device_descriptor(platform_device_, &device_descriptor); | 70 int rv = libusb_get_device_descriptor(platform_device_, &device_descriptor); |
70 if (rv == LIBUSB_SUCCESS) { | 71 if (rv == LIBUSB_SUCCESS) { |
71 for (uint8_t i = 0; i < device_descriptor.bNumConfigurations; ++i) { | 72 for (uint8_t i = 0; i < device_descriptor.bNumConfigurations; ++i) { |
72 unsigned char* buffer; | 73 unsigned char* buffer; |
73 rv = libusb_get_raw_config_descriptor(platform_device_, i, &buffer); | 74 rv = libusb_get_raw_config_descriptor(platform_device_, i, &buffer); |
74 if (rv < 0) { | 75 if (rv < 0) { |
(...skipping 17 matching lines...) Expand all Loading... |
92 int rv = libusb_get_active_config_value(platform_device_, &config_value); | 93 int rv = libusb_get_active_config_value(platform_device_, &config_value); |
93 if (rv != LIBUSB_SUCCESS) { | 94 if (rv != LIBUSB_SUCCESS) { |
94 USB_LOG(EVENT) << "Failed to get active configuration: " | 95 USB_LOG(EVENT) << "Failed to get active configuration: " |
95 << ConvertPlatformUsbErrorToString(rv); | 96 << ConvertPlatformUsbErrorToString(rv); |
96 return; | 97 return; |
97 } | 98 } |
98 | 99 |
99 ActiveConfigurationChanged(config_value); | 100 ActiveConfigurationChanged(config_value); |
100 } | 101 } |
101 | 102 |
102 void UsbDeviceImpl::OpenOnBlockingThread(const OpenCallback& callback) { | 103 void UsbDeviceImpl::OpenOnBlockingThread( |
| 104 const OpenCallback& callback, |
| 105 scoped_refptr<base::TaskRunner> task_runner, |
| 106 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) { |
| 107 base::ThreadRestrictions::AssertIOAllowed(); |
103 PlatformUsbDeviceHandle handle; | 108 PlatformUsbDeviceHandle handle; |
104 const int rv = libusb_open(platform_device_, &handle); | 109 const int rv = libusb_open(platform_device_, &handle); |
105 if (LIBUSB_SUCCESS == rv) { | 110 if (LIBUSB_SUCCESS == rv) { |
106 task_runner_->PostTask( | 111 task_runner->PostTask( |
107 FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback)); | 112 FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback, |
| 113 blocking_task_runner)); |
108 } else { | 114 } else { |
109 USB_LOG(EVENT) << "Failed to open device: " | 115 USB_LOG(EVENT) << "Failed to open device: " |
110 << ConvertPlatformUsbErrorToString(rv); | 116 << ConvertPlatformUsbErrorToString(rv); |
111 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr)); | 117 task_runner->PostTask(FROM_HERE, base::Bind(callback, nullptr)); |
112 } | 118 } |
113 } | 119 } |
114 | 120 |
115 void UsbDeviceImpl::Opened(PlatformUsbDeviceHandle platform_handle, | 121 void UsbDeviceImpl::Opened( |
116 const OpenCallback& callback) { | 122 PlatformUsbDeviceHandle platform_handle, |
| 123 const OpenCallback& callback, |
| 124 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) { |
117 DCHECK(thread_checker_.CalledOnValidThread()); | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
118 scoped_refptr<UsbDeviceHandle> device_handle = new UsbDeviceHandleImpl( | 126 scoped_refptr<UsbDeviceHandle> device_handle = new UsbDeviceHandleImpl( |
119 context_, this, platform_handle, blocking_task_runner_); | 127 context_, this, platform_handle, blocking_task_runner); |
120 handles().push_back(device_handle.get()); | 128 handles().push_back(device_handle.get()); |
121 callback.Run(device_handle); | 129 callback.Run(device_handle); |
122 } | 130 } |
123 | 131 |
124 } // namespace device | 132 } // namespace device |
OLD | NEW |