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

Side by Side Diff: device/usb/usb_device_impl.cc

Issue 2857473002: Use the task scheduler in the USB service on macOS and Windows (Closed)
Patch Set: Created 3 years, 7 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 unified diff | Download patch
OLDNEW
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_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
19 #include "build/build_config.h" 19 #include "build/build_config.h"
20 #include "components/device_event_log/device_event_log.h" 20 #include "components/device_event_log/device_event_log.h"
21 #include "device/usb/usb_context.h" 21 #include "device/usb/usb_context.h"
22 #include "device/usb/usb_descriptors.h" 22 #include "device/usb/usb_descriptors.h"
23 #include "device/usb/usb_device_handle_impl.h" 23 #include "device/usb/usb_device_handle_impl.h"
24 #include "device/usb/usb_error.h" 24 #include "device/usb/usb_error.h"
25 #include "device/usb/usb_service.h"
25 #include "third_party/libusb/src/libusb/libusb.h" 26 #include "third_party/libusb/src/libusb/libusb.h"
26 27
27 namespace device { 28 namespace device {
28 29
29 UsbDeviceImpl::UsbDeviceImpl( 30 UsbDeviceImpl::UsbDeviceImpl(scoped_refptr<UsbContext> context,
30 scoped_refptr<UsbContext> context, 31 PlatformUsbDevice platform_device,
31 PlatformUsbDevice platform_device, 32 const libusb_device_descriptor& descriptor)
32 const libusb_device_descriptor& descriptor,
33 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner)
34 : UsbDevice(descriptor.bcdUSB, 33 : UsbDevice(descriptor.bcdUSB,
35 descriptor.bDeviceClass, 34 descriptor.bDeviceClass,
36 descriptor.bDeviceSubClass, 35 descriptor.bDeviceSubClass,
37 descriptor.bDeviceProtocol, 36 descriptor.bDeviceProtocol,
38 descriptor.idVendor, 37 descriptor.idVendor,
39 descriptor.idProduct, 38 descriptor.idProduct,
40 descriptor.bcdDevice, 39 descriptor.bcdDevice,
41 base::string16(), 40 base::string16(),
42 base::string16(), 41 base::string16(),
43 base::string16()), 42 base::string16()),
44 platform_device_(platform_device), 43 platform_device_(platform_device),
45 context_(context), 44 context_(context) {
46 task_runner_(base::ThreadTaskRunnerHandle::Get()),
47 blocking_task_runner_(blocking_task_runner) {
48 CHECK(platform_device) << "platform_device cannot be NULL"; 45 CHECK(platform_device) << "platform_device cannot be NULL";
49 libusb_ref_device(platform_device); 46 libusb_ref_device(platform_device);
50 ReadAllConfigurations(); 47 ReadAllConfigurations();
51 RefreshActiveConfiguration(); 48 RefreshActiveConfiguration();
52 } 49 }
53 50
54 UsbDeviceImpl::~UsbDeviceImpl() { 51 UsbDeviceImpl::~UsbDeviceImpl() {
55 // The destructor must be safe to call from any thread. 52 // The destructor must be safe to call from any thread.
56 libusb_unref_device(platform_device_); 53 libusb_unref_device(platform_device_);
57 } 54 }
58 55
59 void UsbDeviceImpl::Open(const OpenCallback& callback) { 56 void UsbDeviceImpl::Open(const OpenCallback& callback) {
60 DCHECK(thread_checker_.CalledOnValidThread()); 57 DCHECK(thread_checker_.CalledOnValidThread());
61 58
62 blocking_task_runner_->PostTask( 59 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner =
60 UsbService::CreateBlockingTaskRunner();
61 blocking_task_runner->PostTask(
63 FROM_HERE, 62 FROM_HERE,
64 base::Bind(&UsbDeviceImpl::OpenOnBlockingThread, this, callback)); 63 base::Bind(&UsbDeviceImpl::OpenOnBlockingThread, this, callback,
64 base::ThreadTaskRunnerHandle::Get(), blocking_task_runner));
65 } 65 }
66 66
67 void UsbDeviceImpl::ReadAllConfigurations() { 67 void UsbDeviceImpl::ReadAllConfigurations() {
68 libusb_device_descriptor device_descriptor; 68 libusb_device_descriptor device_descriptor;
69 int rv = libusb_get_device_descriptor(platform_device_, &device_descriptor); 69 int rv = libusb_get_device_descriptor(platform_device_, &device_descriptor);
70 if (rv == LIBUSB_SUCCESS) { 70 if (rv == LIBUSB_SUCCESS) {
71 for (uint8_t i = 0; i < device_descriptor.bNumConfigurations; ++i) { 71 for (uint8_t i = 0; i < device_descriptor.bNumConfigurations; ++i) {
72 unsigned char* buffer; 72 unsigned char* buffer;
73 rv = libusb_get_raw_config_descriptor(platform_device_, i, &buffer); 73 rv = libusb_get_raw_config_descriptor(platform_device_, i, &buffer);
74 if (rv < 0) { 74 if (rv < 0) {
(...skipping 17 matching lines...) Expand all
92 int rv = libusb_get_active_config_value(platform_device_, &config_value); 92 int rv = libusb_get_active_config_value(platform_device_, &config_value);
93 if (rv != LIBUSB_SUCCESS) { 93 if (rv != LIBUSB_SUCCESS) {
94 USB_LOG(EVENT) << "Failed to get active configuration: " 94 USB_LOG(EVENT) << "Failed to get active configuration: "
95 << ConvertPlatformUsbErrorToString(rv); 95 << ConvertPlatformUsbErrorToString(rv);
96 return; 96 return;
97 } 97 }
98 98
99 ActiveConfigurationChanged(config_value); 99 ActiveConfigurationChanged(config_value);
100 } 100 }
101 101
102 void UsbDeviceImpl::OpenOnBlockingThread(const OpenCallback& callback) { 102 void UsbDeviceImpl::OpenOnBlockingThread(
103 const OpenCallback& callback,
104 scoped_refptr<base::TaskRunner> task_runner,
105 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) {
103 PlatformUsbDeviceHandle handle; 106 PlatformUsbDeviceHandle handle;
robliao 2017/05/02 14:32:59 Optional: You can do a base::ThreadRestrictions::A
Reilly Grant (use Gerrit) 2017/05/02 23:41:44 Done.
104 const int rv = libusb_open(platform_device_, &handle); 107 const int rv = libusb_open(platform_device_, &handle);
105 if (LIBUSB_SUCCESS == rv) { 108 if (LIBUSB_SUCCESS == rv) {
106 task_runner_->PostTask( 109 task_runner->PostTask(
107 FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback)); 110 FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback,
111 blocking_task_runner));
108 } else { 112 } else {
109 USB_LOG(EVENT) << "Failed to open device: " 113 USB_LOG(EVENT) << "Failed to open device: "
110 << ConvertPlatformUsbErrorToString(rv); 114 << ConvertPlatformUsbErrorToString(rv);
111 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr)); 115 task_runner->PostTask(FROM_HERE, base::Bind(callback, nullptr));
112 } 116 }
113 } 117 }
114 118
115 void UsbDeviceImpl::Opened(PlatformUsbDeviceHandle platform_handle, 119 void UsbDeviceImpl::Opened(
116 const OpenCallback& callback) { 120 PlatformUsbDeviceHandle platform_handle,
121 const OpenCallback& callback,
122 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) {
117 DCHECK(thread_checker_.CalledOnValidThread()); 123 DCHECK(thread_checker_.CalledOnValidThread());
118 scoped_refptr<UsbDeviceHandle> device_handle = new UsbDeviceHandleImpl( 124 scoped_refptr<UsbDeviceHandle> device_handle = new UsbDeviceHandleImpl(
119 context_, this, platform_handle, blocking_task_runner_); 125 context_, this, platform_handle, blocking_task_runner);
120 handles().push_back(device_handle.get()); 126 handles().push_back(device_handle.get());
121 callback.Run(device_handle); 127 callback.Run(device_handle);
122 } 128 }
123 129
124 } // namespace device 130 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698