[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 1 | // Copyright (c) 2012 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. |
| 4 | |
| 5 | #include "chrome/browser/usb/usb_service.h" |
| 6 | |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 7 | #include <set> |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 11 | #include "base/bind.h" |
| 12 | #include "base/bind_helpers.h" |
[email protected] | 88a05cf4 | 2012-05-16 21:36:23 | [diff] [blame] | 13 | #include "base/logging.h" |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 15 | #include "base/stl_util.h" |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 16 | #include "chrome/browser/chrome_notification_types.h" |
| 17 | #include "chrome/browser/usb/usb_context.h" |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 18 | #include "chrome/browser/usb/usb_device.h" |
[email protected] | 57a6bfe | 2013-07-23 19:26:48 | [diff] [blame] | 19 | #include "chrome/browser/usb/usb_device_handle.h" |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 20 | #include "content/public/browser/browser_thread.h" |
| 21 | #include "content/public/browser/notification_observer.h" |
| 22 | #include "content/public/browser/notification_registrar.h" |
| 23 | #include "content/public/browser/notification_service.h" |
[email protected] | 62c76b5 | 2013-01-08 19:04:31 | [diff] [blame] | 24 | #include "third_party/libusb/src/libusb/libusb.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 25 | |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 26 | namespace content { |
| 27 | |
| 28 | class NotificationDetails; |
| 29 | class NotificationSource; |
| 30 | |
| 31 | } // namespace content |
| 32 | |
| 33 | using content::BrowserThread; |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 34 | using std::vector; |
| 35 | |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | class ExitObserver : public content::NotificationObserver { |
[email protected] | 88a05cf4 | 2012-05-16 21:36:23 | [diff] [blame] | 39 | public: |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 40 | explicit ExitObserver(UsbService* service) : service_(service) { |
[email protected] | f2ecf785b | 2013-09-05 11:17:47 | [diff] [blame] | 41 | BrowserThread::PostTask( |
| 42 | BrowserThread::UI, FROM_HERE, |
| 43 | base::Bind(&content::NotificationRegistrar::Add, |
| 44 | base::Unretained(®istrar_), this, |
| 45 | chrome::NOTIFICATION_APP_TERMINATING, |
| 46 | content::NotificationService::AllSources())); |
[email protected] | 88a05cf4 | 2012-05-16 21:36:23 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | private: |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 50 | // content::NotificationObserver |
| 51 | virtual void Observe(int type, |
| 52 | const content::NotificationSource& source, |
| 53 | const content::NotificationDetails& details) OVERRIDE { |
| 54 | if (type == chrome::NOTIFICATION_APP_TERMINATING) { |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 55 | BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, service_); |
[email protected] | f2ecf785b | 2013-09-05 11:17:47 | [diff] [blame] | 56 | delete this; |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | UsbService* service_; |
| 60 | content::NotificationRegistrar registrar_; |
[email protected] | 88a05cf4 | 2012-05-16 21:36:23 | [diff] [blame] | 61 | }; |
| 62 | |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 63 | } // namespace |
| 64 | |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 65 | using content::BrowserThread; |
| 66 | |
[email protected] | 5764dcce | 2013-09-05 20:43:39 | [diff] [blame^] | 67 | UsbService::UsbService(PlatformUsbContext context) |
| 68 | : context_(new UsbContext(context)), |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 69 | next_unique_id_(0) { |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 70 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | f2ecf785b | 2013-09-05 11:17:47 | [diff] [blame] | 71 | // Will be deleted upon NOTIFICATION_APP_TERMINATING. |
| 72 | new ExitObserver(this); |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | 96816d2 | 2013-07-26 11:33:17 | [diff] [blame] | 75 | UsbService::~UsbService() { |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 76 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[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 | it->second->OnDisconnect(); |
| 79 | } |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | 5764dcce | 2013-09-05 20:43:39 | [diff] [blame^] | 82 | struct InitUsbContextTraits : public LeakySingletonTraits<UsbService> { |
| 83 | // LeakySingletonTraits<UsbService> |
| 84 | static UsbService* New() { |
| 85 | PlatformUsbContext context = NULL; |
| 86 | if (libusb_init(&context) != LIBUSB_SUCCESS) |
| 87 | return NULL; |
| 88 | if (!context) |
| 89 | return NULL; |
| 90 | return new UsbService(context); |
| 91 | } |
| 92 | }; |
| 93 | |
[email protected] | 96816d2 | 2013-07-26 11:33:17 | [diff] [blame] | 94 | UsbService* UsbService::GetInstance() { |
[email protected] | 99dcfca | 2013-08-05 01:28:59 | [diff] [blame] | 95 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 96 | // UsbService deletes itself upon APP_TERMINATING. |
[email protected] | 5764dcce | 2013-09-05 20:43:39 | [diff] [blame^] | 97 | return Singleton<UsbService, InitUsbContextTraits>::get(); |
[email protected] | 96816d2 | 2013-07-26 11:33:17 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 100 | void UsbService::GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) { |
| 101 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 102 | STLClearObject(devices); |
| 103 | RefreshDevices(); |
[email protected] | 6226973 | 2013-07-02 19:51:31 | [diff] [blame] | 104 | |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 105 | for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 106 | devices->push_back(it->second); |
[email protected] | 6226973 | 2013-07-02 19:51:31 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 110 | scoped_refptr<UsbDevice> UsbService::GetDeviceById(uint32 unique_id) { |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 111 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 112 | RefreshDevices(); |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 113 | |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 114 | for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
| 115 | if (it->second->unique_id() == unique_id) return it->second; |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 116 | } |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 117 | return NULL; |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 120 | void UsbService::RefreshDevices() { |
| 121 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 122 | |
| 123 | libusb_device** platform_devices = NULL; |
| 124 | const ssize_t device_count = |
| 125 | libusb_get_device_list(context_->context(), &platform_devices); |
| 126 | |
| 127 | std::set<UsbDevice*> connected_devices; |
| 128 | vector<PlatformUsbDevice> disconnected_devices; |
| 129 | |
| 130 | // Populates new devices. |
| 131 | for (ssize_t i = 0; i < device_count; ++i) { |
| 132 | if (!ContainsKey(devices_, platform_devices[i])) { |
| 133 | libusb_device_descriptor descriptor; |
| 134 | // This test is needed. A valid vendor/produce pair is required. |
| 135 | if (0 != libusb_get_device_descriptor(platform_devices[i], &descriptor)) |
| 136 | continue; |
| 137 | UsbDevice* new_device = new UsbDevice(context_, |
[email protected] | 320a7b7 | 2013-09-05 01:51:27 | [diff] [blame] | 138 | platform_devices[i], |
| 139 | descriptor.idVendor, |
| 140 | descriptor.idProduct, |
| 141 | ++next_unique_id_); |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 142 | devices_[platform_devices[i]] = new_device; |
| 143 | connected_devices.insert(new_device); |
| 144 | } else { |
| 145 | connected_devices.insert(devices_[platform_devices[i]].get()); |
| 146 | } |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 147 | } |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 148 | |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 149 | // Find disconnected devices. |
| 150 | for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
| 151 | if (!ContainsKey(connected_devices, it->second)) { |
| 152 | disconnected_devices.push_back(it->first); |
| 153 | } |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 156 | // Remove disconnected devices from devices_. |
| 157 | for (size_t i = 0; i < disconnected_devices.size(); ++i) { |
| 158 | // UsbDevice will be destroyed after this. The corresponding |
| 159 | // PlatformUsbDevice will be unref'ed during this process. |
| 160 | devices_.erase(disconnected_devices[i]); |
| 161 | } |
| 162 | |
| 163 | libusb_free_device_list(platform_devices, true); |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 164 | } |