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
3 // found in the LICENSE file.
5 #include "device/usb/usb_service.h"
10 #include "base/lazy_instance.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/stl_util.h"
14 #include "device/usb/usb_context.h"
15 #include "device/usb/usb_device_impl.h"
16 #include "device/usb/usb_error.h"
17 #include "third_party/libusb/src/libusb/libusb.h"
23 base::LazyInstance
<scoped_ptr
<UsbService
> >::Leaky g_usb_service_instance
=
24 LAZY_INSTANCE_INITIALIZER
;
28 typedef struct libusb_device
* PlatformUsbDevice
;
29 typedef struct libusb_context
* PlatformUsbContext
;
31 class UsbServiceImpl
: public UsbService
,
32 private base::MessageLoop::DestructionObserver
{
34 explicit UsbServiceImpl(
35 PlatformUsbContext context
,
36 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
);
37 ~UsbServiceImpl() override
;
40 // device::UsbService implementation
41 scoped_refptr
<UsbDevice
> GetDeviceById(uint32 unique_id
) override
;
42 void GetDevices(std::vector
<scoped_refptr
<UsbDevice
>>* devices
) override
;
44 // base::MessageLoop::DestructionObserver implementation.
45 void WillDestroyCurrentMessageLoop() override
;
47 // Enumerate USB devices from OS and update devices_ map.
48 void RefreshDevices();
50 scoped_refptr
<UsbContext
> context_
;
51 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
52 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
54 // TODO(reillyg): Figure out a better solution.
55 uint32 next_unique_id_
;
57 // The map from unique IDs to UsbDevices.
58 typedef std::map
<uint32
, scoped_refptr
<UsbDeviceImpl
> > DeviceMap
;
61 // The map from PlatformUsbDevices to UsbDevices.
62 typedef std::map
<PlatformUsbDevice
, scoped_refptr
<UsbDeviceImpl
> >
64 PlatformDeviceMap platform_devices_
;
66 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl
);
69 scoped_refptr
<UsbDevice
> UsbServiceImpl::GetDeviceById(uint32 unique_id
) {
70 DCHECK(CalledOnValidThread());
72 DeviceMap::iterator it
= devices_
.find(unique_id
);
73 if (it
!= devices_
.end()) {
79 void UsbServiceImpl::GetDevices(
80 std::vector
<scoped_refptr
<UsbDevice
> >* devices
) {
81 DCHECK(CalledOnValidThread());
82 STLClearObject(devices
);
85 for (DeviceMap::iterator it
= devices_
.begin(); it
!= devices_
.end(); ++it
) {
86 devices
->push_back(it
->second
);
90 void UsbServiceImpl::WillDestroyCurrentMessageLoop() {
91 DCHECK(CalledOnValidThread());
92 g_usb_service_instance
.Get().reset(NULL
);
95 UsbServiceImpl::UsbServiceImpl(
96 PlatformUsbContext context
,
97 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
)
98 : context_(new UsbContext(context
)),
99 ui_task_runner_(ui_task_runner
),
101 base::MessageLoop::current()->AddDestructionObserver(this);
104 UsbServiceImpl::~UsbServiceImpl() {
105 base::MessageLoop::current()->RemoveDestructionObserver(this);
106 for (DeviceMap::iterator it
= devices_
.begin(); it
!= devices_
.end(); ++it
) {
107 it
->second
->OnDisconnect();
111 void UsbServiceImpl::RefreshDevices() {
112 DCHECK(CalledOnValidThread());
114 libusb_device
** platform_devices
= NULL
;
115 const ssize_t device_count
=
116 libusb_get_device_list(context_
->context(), &platform_devices
);
117 if (device_count
< 0) {
118 VLOG(1) << "Failed to get device list: "
119 << ConvertPlatformUsbErrorToString(device_count
);
122 std::set
<UsbDevice
*> connected_devices
;
123 std::vector
<PlatformUsbDevice
> disconnected_devices
;
125 // Populates new devices.
126 for (ssize_t i
= 0; i
< device_count
; ++i
) {
127 if (!ContainsKey(platform_devices_
, platform_devices
[i
])) {
128 libusb_device_descriptor descriptor
;
130 libusb_get_device_descriptor(platform_devices
[i
], &descriptor
);
131 // This test is needed. A valid vendor/produce pair is required.
132 if (rv
!= LIBUSB_SUCCESS
) {
133 VLOG(1) << "Failed to get device descriptor: "
134 << ConvertPlatformUsbErrorToString(rv
);
140 unique_id
= ++next_unique_id_
;
141 } while (devices_
.find(unique_id
) != devices_
.end());
143 scoped_refptr
<UsbDeviceImpl
> new_device(
144 new UsbDeviceImpl(context_
,
148 descriptor
.idProduct
,
150 platform_devices_
[platform_devices
[i
]] = new_device
;
151 devices_
[unique_id
] = new_device
;
152 connected_devices
.insert(new_device
.get());
154 connected_devices
.insert(platform_devices_
[platform_devices
[i
]].get());
158 // Find disconnected devices.
159 for (PlatformDeviceMap::iterator it
= platform_devices_
.begin();
160 it
!= platform_devices_
.end();
162 if (!ContainsKey(connected_devices
, it
->second
.get())) {
163 disconnected_devices
.push_back(it
->first
);
164 devices_
.erase(it
->second
->unique_id());
165 it
->second
->OnDisconnect();
169 // Remove disconnected devices from platform_devices_.
170 for (size_t i
= 0; i
< disconnected_devices
.size(); ++i
) {
171 // UsbDevice will be destroyed after this. The corresponding
172 // PlatformUsbDevice will be unref'ed during this process.
173 platform_devices_
.erase(disconnected_devices
[i
]);
176 libusb_free_device_list(platform_devices
, true);
180 UsbService
* UsbService::GetInstance(
181 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
) {
182 UsbService
* instance
= g_usb_service_instance
.Get().get();
184 PlatformUsbContext context
= NULL
;
186 const int rv
= libusb_init(&context
);
187 if (rv
!= LIBUSB_SUCCESS
) {
188 VLOG(1) << "Failed to initialize libusb: "
189 << ConvertPlatformUsbErrorToString(rv
);
195 instance
= new UsbServiceImpl(context
, ui_task_runner
);
196 g_usb_service_instance
.Get().reset(instance
);
202 void UsbService::SetInstanceForTest(UsbService
* instance
) {
203 g_usb_service_instance
.Get().reset(instance
);
206 } // namespace device