blob: bafb51b680538571c4301525f16ee73763a8f6f6 [file] [log] [blame]
[email protected]b5508682014-04-22 17:10:011// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]1c39a6b2012-04-27 16:09:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b5508682014-04-22 17:10:015#include "components/usb_service/usb_service.h"
[email protected]1c39a6b2012-04-27 16:09:576
[email protected]ad2263f2014-05-05 13:57:507#include <map>
[email protected]d165a6442013-08-08 19:38:118#include <set>
[email protected]22ac24e2012-10-03 17:56:059
[email protected]e1954872014-04-13 17:43:2910#include "base/lazy_instance.h"
[email protected]ad2263f2014-05-05 13:57:5011#include "base/message_loop/message_loop.h"
[email protected]1c39a6b2012-04-27 16:09:5712#include "base/stl_util.h"
[email protected]b5508682014-04-22 17:10:0113#include "components/usb_service/usb_context.h"
[email protected]42c39c92014-05-07 14:21:3114#include "components/usb_service/usb_device_impl.h"
[email protected]99dcfca2013-08-05 01:28:5915#include "content/public/browser/browser_thread.h"
[email protected]62c76b52013-01-08 19:04:3116#include "third_party/libusb/src/libusb/libusb.h"
[email protected]1c39a6b2012-04-27 16:09:5717
[email protected]b5508682014-04-22 17:10:0118namespace usb_service {
19
[email protected]99dcfca2013-08-05 01:28:5920namespace {
21
[email protected]e1954872014-04-13 17:43:2922base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance =
23 LAZY_INSTANCE_INITIALIZER;
[email protected]88a05cf42012-05-16 21:36:2324
[email protected]99dcfca2013-08-05 01:28:5925} // namespace
26
[email protected]ad2263f2014-05-05 13:57:5027typedef struct libusb_device* PlatformUsbDevice;
28typedef struct libusb_context* PlatformUsbContext;
[email protected]5764dcce2013-09-05 20:43:3929
[email protected]ad2263f2014-05-05 13:57:5030class UsbServiceImpl
31 : public UsbService,
32 private base::MessageLoop::DestructionObserver {
33 public:
34 explicit UsbServiceImpl(PlatformUsbContext context);
35 virtual ~UsbServiceImpl();
[email protected]7ad4583b2014-01-16 00:10:2636
[email protected]ad2263f2014-05-05 13:57:5037 private:
38 // usb_service::UsbService implementation
39 virtual scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) OVERRIDE;
40 virtual void GetDevices(
41 std::vector<scoped_refptr<UsbDevice> >* devices) OVERRIDE;
42
43 // base::MessageLoop::DestructionObserver implementation.
44 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
45
46 // Enumerate USB devices from OS and Update devices_ map.
47 void RefreshDevices();
48
49 scoped_refptr<UsbContext> context_;
50
51 // TODO(ikarienator): Figure out a better solution.
52 uint32 next_unique_id_;
53
54 // The map from PlatformUsbDevices to UsbDevices.
[email protected]42c39c92014-05-07 14:21:3155 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDeviceImpl> > DeviceMap;
[email protected]ad2263f2014-05-05 13:57:5056 DeviceMap devices_;
57
58 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl);
59};
60
61scoped_refptr<UsbDevice> UsbServiceImpl::GetDeviceById(uint32 unique_id) {
[email protected]e1954872014-04-13 17:43:2962 DCHECK(CalledOnValidThread());
63 RefreshDevices();
64 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
65 if (it->second->unique_id() == unique_id)
66 return it->second;
67 }
68 return NULL;
[email protected]96816d22013-07-26 11:33:1769}
70
[email protected]ad2263f2014-05-05 13:57:5071void UsbServiceImpl::GetDevices(
72 std::vector<scoped_refptr<UsbDevice> >* devices) {
[email protected]e1954872014-04-13 17:43:2973 DCHECK(CalledOnValidThread());
[email protected]d165a6442013-08-08 19:38:1174 STLClearObject(devices);
75 RefreshDevices();
[email protected]62269732013-07-02 19:51:3176
[email protected]320a7b72013-09-05 01:51:2777 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
[email protected]d165a6442013-08-08 19:38:1178 devices->push_back(it->second);
[email protected]62269732013-07-02 19:51:3179 }
80}
81
[email protected]ad2263f2014-05-05 13:57:5082void UsbServiceImpl::WillDestroyCurrentMessageLoop() {
[email protected]e1954872014-04-13 17:43:2983 DCHECK(CalledOnValidThread());
84 g_usb_service_instance.Get().reset(NULL);
85}
[email protected]22ac24e2012-10-03 17:56:0586
[email protected]ad2263f2014-05-05 13:57:5087UsbServiceImpl::UsbServiceImpl(PlatformUsbContext context)
[email protected]e1954872014-04-13 17:43:2988 : context_(new UsbContext(context)), next_unique_id_(0) {
89 base::MessageLoop::current()->AddDestructionObserver(this);
90}
91
[email protected]ad2263f2014-05-05 13:57:5092UsbServiceImpl::~UsbServiceImpl() {
[email protected]e1954872014-04-13 17:43:2993 base::MessageLoop::current()->RemoveDestructionObserver(this);
[email protected]320a7b72013-09-05 01:51:2794 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
[email protected]e1954872014-04-13 17:43:2995 it->second->OnDisconnect();
[email protected]22ac24e2012-10-03 17:56:0596 }
[email protected]22ac24e2012-10-03 17:56:0597}
98
[email protected]ad2263f2014-05-05 13:57:5099void UsbServiceImpl::RefreshDevices() {
[email protected]e1954872014-04-13 17:43:29100 DCHECK(CalledOnValidThread());
[email protected]d165a6442013-08-08 19:38:11101
102 libusb_device** platform_devices = NULL;
103 const ssize_t device_count =
104 libusb_get_device_list(context_->context(), &platform_devices);
105
106 std::set<UsbDevice*> connected_devices;
[email protected]e1954872014-04-13 17:43:29107 std::vector<PlatformUsbDevice> disconnected_devices;
[email protected]d165a6442013-08-08 19:38:11108
109 // Populates new devices.
110 for (ssize_t i = 0; i < device_count; ++i) {
111 if (!ContainsKey(devices_, platform_devices[i])) {
112 libusb_device_descriptor descriptor;
113 // This test is needed. A valid vendor/produce pair is required.
114 if (0 != libusb_get_device_descriptor(platform_devices[i], &descriptor))
115 continue;
[email protected]42c39c92014-05-07 14:21:31116 UsbDeviceImpl* new_device = new UsbDeviceImpl(context_,
117 platform_devices[i],
118 descriptor.idVendor,
119 descriptor.idProduct,
120 ++next_unique_id_);
[email protected]d165a6442013-08-08 19:38:11121 devices_[platform_devices[i]] = new_device;
122 connected_devices.insert(new_device);
123 } else {
124 connected_devices.insert(devices_[platform_devices[i]].get());
125 }
[email protected]1c39a6b2012-04-27 16:09:57126 }
[email protected]22ac24e2012-10-03 17:56:05127
[email protected]d165a6442013-08-08 19:38:11128 // Find disconnected devices.
129 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
130 if (!ContainsKey(connected_devices, it->second)) {
131 disconnected_devices.push_back(it->first);
132 }
[email protected]22ac24e2012-10-03 17:56:05133 }
134
[email protected]d165a6442013-08-08 19:38:11135 // Remove disconnected devices from devices_.
136 for (size_t i = 0; i < disconnected_devices.size(); ++i) {
137 // UsbDevice will be destroyed after this. The corresponding
138 // PlatformUsbDevice will be unref'ed during this process.
139 devices_.erase(disconnected_devices[i]);
140 }
141
142 libusb_free_device_list(platform_devices, true);
[email protected]22ac24e2012-10-03 17:56:05143}
[email protected]b5508682014-04-22 17:10:01144
[email protected]ad2263f2014-05-05 13:57:50145// static
146UsbService* UsbService::GetInstance() {
147 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
148 UsbService* instance = g_usb_service_instance.Get().get();
149 if (!instance) {
150 PlatformUsbContext context = NULL;
151 if (libusb_init(&context) != LIBUSB_SUCCESS)
152 return NULL;
153 if (!context)
154 return NULL;
155
156 instance = new UsbServiceImpl(context);
157 g_usb_service_instance.Get().reset(instance);
158 }
159 return instance;
160}
161
162// static
163void UsbService::SetInstanceForTest(UsbService* instance) {
164 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
165 g_usb_service_instance.Get().reset(instance);
166}
167
[email protected]b5508682014-04-22 17:10:01168} // namespace usb_service