[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 5 | #include "components/usb_service/usb_service.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 6 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 7 | #include <map> |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 8 | #include <set> |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 9 | |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 10 | #include "base/lazy_instance.h" |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 12 | #include "base/stl_util.h" |
[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 13 | #include "components/usb_service/usb_context.h" |
[email protected] | 42c39c9 | 2014-05-07 14:21:31 | [diff] [blame^] | 14 | #include "components/usb_service/usb_device_impl.h" |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 15 | #include "content/public/browser/browser_thread.h" |
[email protected] | 62c76b5 | 2013-01-08 19:04:31 | [diff] [blame] | 16 | #include "third_party/libusb/src/libusb/libusb.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 17 | |
[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 18 | namespace usb_service { |
| 19 | |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 20 | namespace { |
| 21 | |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 22 | base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance = |
| 23 | LAZY_INSTANCE_INITIALIZER; |
[email protected] | 88a05cf4 | 2012-05-16 21:36:23 | [diff] [blame] | 24 | |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 25 | } // namespace |
| 26 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 27 | typedef struct libusb_device* PlatformUsbDevice; |
| 28 | typedef struct libusb_context* PlatformUsbContext; |
[email protected] | 5764dcce | 2013-09-05 20:43:39 | [diff] [blame] | 29 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 30 | class UsbServiceImpl |
| 31 | : public UsbService, |
| 32 | private base::MessageLoop::DestructionObserver { |
| 33 | public: |
| 34 | explicit UsbServiceImpl(PlatformUsbContext context); |
| 35 | virtual ~UsbServiceImpl(); |
[email protected] | 7ad4583b | 2014-01-16 00:10:26 | [diff] [blame] | 36 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 37 | 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] | 42c39c9 | 2014-05-07 14:21:31 | [diff] [blame^] | 55 | typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDeviceImpl> > DeviceMap; |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 56 | DeviceMap devices_; |
| 57 | |
| 58 | DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl); |
| 59 | }; |
| 60 | |
| 61 | scoped_refptr<UsbDevice> UsbServiceImpl::GetDeviceById(uint32 unique_id) { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 62 | 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] | 96816d2 | 2013-07-26 11:33:17 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 71 | void UsbServiceImpl::GetDevices( |
| 72 | std::vector<scoped_refptr<UsbDevice> >* devices) { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 73 | DCHECK(CalledOnValidThread()); |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 74 | STLClearObject(devices); |
| 75 | RefreshDevices(); |
[email protected] | 6226973 | 2013-07-02 19:51:31 | [diff] [blame] | 76 | |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 77 | for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 78 | devices->push_back(it->second); |
[email protected] | 6226973 | 2013-07-02 19:51:31 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 82 | void UsbServiceImpl::WillDestroyCurrentMessageLoop() { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 83 | DCHECK(CalledOnValidThread()); |
| 84 | g_usb_service_instance.Get().reset(NULL); |
| 85 | } |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 86 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 87 | UsbServiceImpl::UsbServiceImpl(PlatformUsbContext context) |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 88 | : context_(new UsbContext(context)), next_unique_id_(0) { |
| 89 | base::MessageLoop::current()->AddDestructionObserver(this); |
| 90 | } |
| 91 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 92 | UsbServiceImpl::~UsbServiceImpl() { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 93 | base::MessageLoop::current()->RemoveDestructionObserver(this); |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 94 | for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 95 | it->second->OnDisconnect(); |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 96 | } |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 99 | void UsbServiceImpl::RefreshDevices() { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 100 | DCHECK(CalledOnValidThread()); |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 101 | |
| 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] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 107 | std::vector<PlatformUsbDevice> disconnected_devices; |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 108 | |
| 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] | 42c39c9 | 2014-05-07 14:21:31 | [diff] [blame^] | 116 | UsbDeviceImpl* new_device = new UsbDeviceImpl(context_, |
| 117 | platform_devices[i], |
| 118 | descriptor.idVendor, |
| 119 | descriptor.idProduct, |
| 120 | ++next_unique_id_); |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 121 | 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] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 126 | } |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 127 | |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 128 | // 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] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 135 | // 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] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 143 | } |
[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 144 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 145 | // static |
| 146 | UsbService* 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 |
| 163 | void UsbService::SetInstanceForTest(UsbService* instance) { |
| 164 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 165 | g_usb_service_instance.Get().reset(instance); |
| 166 | } |
| 167 | |
[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 168 | } // namespace usb_service |