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 "components/usb_service/usb_service.h"
10 #include "base/lazy_instance.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/stl_util.h"
13 #include "components/usb_service/usb_context.h"
14 #include "components/usb_service/usb_device_impl.h"
15 #include "components/usb_service/usb_error.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "third_party/libusb/src/libusb/libusb.h"
19 namespace usb_service
{
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
;
33 private base::MessageLoop::DestructionObserver
{
35 explicit UsbServiceImpl(PlatformUsbContext context
);
36 virtual ~UsbServiceImpl();
39 // usb_service::UsbService implementation
40 virtual scoped_refptr
<UsbDevice
> GetDeviceById(uint32 unique_id
) OVERRIDE
;
41 virtual void GetDevices(
42 std::vector
<scoped_refptr
<UsbDevice
> >* devices
) OVERRIDE
;
44 // base::MessageLoop::DestructionObserver implementation.
45 virtual void WillDestroyCurrentMessageLoop() OVERRIDE
;
47 // Enumerate USB devices from OS and Update devices_ map.
48 void RefreshDevices();
50 scoped_refptr
<UsbContext
> context_
;
52 // TODO(ikarienator): Figure out a better solution.
53 uint32 next_unique_id_
;
55 // The map from PlatformUsbDevices to UsbDevices.
56 typedef std::map
<PlatformUsbDevice
, scoped_refptr
<UsbDeviceImpl
> > DeviceMap
;
59 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl
);
62 scoped_refptr
<UsbDevice
> UsbServiceImpl::GetDeviceById(uint32 unique_id
) {
63 DCHECK(CalledOnValidThread());
65 for (DeviceMap::iterator it
= devices_
.begin(); it
!= devices_
.end(); ++it
) {
66 if (it
->second
->unique_id() == unique_id
)
72 void UsbServiceImpl::GetDevices(
73 std::vector
<scoped_refptr
<UsbDevice
> >* devices
) {
74 DCHECK(CalledOnValidThread());
75 STLClearObject(devices
);
78 for (DeviceMap::iterator it
= devices_
.begin(); it
!= devices_
.end(); ++it
) {
79 devices
->push_back(it
->second
);
83 void UsbServiceImpl::WillDestroyCurrentMessageLoop() {
84 DCHECK(CalledOnValidThread());
85 g_usb_service_instance
.Get().reset(NULL
);
88 UsbServiceImpl::UsbServiceImpl(PlatformUsbContext context
)
89 : context_(new UsbContext(context
)), next_unique_id_(0) {
90 base::MessageLoop::current()->AddDestructionObserver(this);
93 UsbServiceImpl::~UsbServiceImpl() {
94 base::MessageLoop::current()->RemoveDestructionObserver(this);
95 for (DeviceMap::iterator it
= devices_
.begin(); it
!= devices_
.end(); ++it
) {
96 it
->second
->OnDisconnect();
100 void UsbServiceImpl::RefreshDevices() {
101 DCHECK(CalledOnValidThread());
103 libusb_device
** platform_devices
= NULL
;
104 const ssize_t device_count
=
105 libusb_get_device_list(context_
->context(), &platform_devices
);
106 if (device_count
< 0) {
107 VLOG(1) << "Failed to get device list: "
108 << ConvertErrorToString(device_count
);
111 std::set
<UsbDevice
*> connected_devices
;
112 std::vector
<PlatformUsbDevice
> disconnected_devices
;
114 // Populates new devices.
115 for (ssize_t i
= 0; i
< device_count
; ++i
) {
116 if (!ContainsKey(devices_
, platform_devices
[i
])) {
117 libusb_device_descriptor descriptor
;
119 libusb_get_device_descriptor(platform_devices
[i
], &descriptor
);
120 // This test is needed. A valid vendor/produce pair is required.
121 if (rv
!= LIBUSB_SUCCESS
) {
122 VLOG(1) << "Failed to get device descriptor: "
123 << ConvertErrorToString(rv
);
126 UsbDeviceImpl
* new_device
= new UsbDeviceImpl(context_
,
129 descriptor
.idProduct
,
131 devices_
[platform_devices
[i
]] = new_device
;
132 connected_devices
.insert(new_device
);
134 connected_devices
.insert(devices_
[platform_devices
[i
]].get());
138 // Find disconnected devices.
139 for (DeviceMap::iterator it
= devices_
.begin(); it
!= devices_
.end(); ++it
) {
140 if (!ContainsKey(connected_devices
, it
->second
)) {
141 disconnected_devices
.push_back(it
->first
);
145 // Remove disconnected devices from devices_.
146 for (size_t i
= 0; i
< disconnected_devices
.size(); ++i
) {
147 // UsbDevice will be destroyed after this. The corresponding
148 // PlatformUsbDevice will be unref'ed during this process.
149 devices_
.erase(disconnected_devices
[i
]);
152 libusb_free_device_list(platform_devices
, true);
156 UsbService
* UsbService::GetInstance() {
157 UsbService
* instance
= g_usb_service_instance
.Get().get();
159 PlatformUsbContext context
= NULL
;
161 const int rv
= libusb_init(&context
);
162 if (rv
!= LIBUSB_SUCCESS
) {
163 VLOG(1) << "Failed to initialize libusb: " << ConvertErrorToString(rv
);
169 instance
= new UsbServiceImpl(context
);
170 g_usb_service_instance
.Get().reset(instance
);
176 void UsbService::SetInstanceForTest(UsbService
* instance
) {
177 g_usb_service_instance
.Get().reset(instance
);
180 } // namespace usb_service