| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/usb/usb_service.h" | 5 #include "chrome/browser/usb/usb_service.h" |
| 6 | 6 |
| 7 #include <set> |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 13 #include "chrome/browser/usb/usb_device.h" | 14 #include "chrome/browser/usb/usb_device_handle.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 14 #include "third_party/libusb/src/libusb/libusb.h" | 16 #include "third_party/libusb/src/libusb/libusb.h" |
| 15 | 17 |
| 16 #if defined(OS_CHROMEOS) | 18 #if defined(OS_CHROMEOS) |
| 17 #include "base/chromeos/chromeos_version.h" | 19 #include "base/chromeos/chromeos_version.h" |
| 18 #include "chromeos/dbus/dbus_thread_manager.h" | 20 #include "chromeos/dbus/dbus_thread_manager.h" |
| 19 #include "chromeos/dbus/permission_broker_client.h" | 21 #include "chromeos/dbus/permission_broker_client.h" |
| 20 #endif // defined(OS_CHROMEOS) | 22 #endif // defined(OS_CHROMEOS) |
| 21 | 23 |
| 22 using std::vector; | 24 using std::vector; |
| 25 using std::set; |
| 26 using content::BrowserThread; |
| 23 | 27 |
| 24 // The UsbEventHandler works around a design flaw in the libusb interface. There | 28 // The UsbEventDispatcher dispatches USB events on separate thread. There is |
| 25 // is currently no way to signal to libusb that any caller into one of the event | 29 // currently no way to signal to libusb that any caller into one of the event |
| 26 // handler calls should return without handling any events. | 30 // handler calls should return without handling any events. |
| 27 class UsbEventHandler : public base::PlatformThread::Delegate { | 31 class UsbEventDispatcher : public base::PlatformThread::Delegate { |
| 28 public: | 32 public: |
| 29 explicit UsbEventHandler(PlatformUsbContext context) | 33 explicit UsbEventDispatcher(PlatformUsbContext context) |
| 30 : running_(true), context_(context) { | 34 : running_(true), context_(context), thread_handle_(0) { |
| 31 base::PlatformThread::CreateNonJoinable(0, this); | 35 base::PlatformThread::Create(0, this, &thread_handle_); |
| 32 } | 36 } |
| 33 | 37 |
| 34 virtual ~UsbEventHandler() {} | 38 virtual ~UsbEventDispatcher() {} |
| 35 | 39 |
| 36 virtual void ThreadMain() OVERRIDE { | 40 virtual void ThreadMain() OVERRIDE { |
| 37 base::PlatformThread::SetName("UsbEventHandler"); | 41 base::PlatformThread::SetName("UsbEventDispatcher"); |
| 38 | 42 struct timeval tv; |
| 39 DLOG(INFO) << "UsbEventHandler started."; | 43 tv.tv_sec = 1; |
| 44 tv.tv_usec = 0; |
| 45 DVLOG(1) << "UsbEventDispatcher started."; |
| 40 while (running_) { | 46 while (running_) { |
| 41 libusb_handle_events(context_); | 47 libusb_handle_events_timeout(context_, &tv); |
| 42 } | 48 } |
| 43 DLOG(INFO) << "UsbEventHandler shutting down."; | 49 DVLOG(1) << "UsbEventDispatcher shutting down."; |
| 44 libusb_exit(context_); | |
| 45 | 50 |
| 46 delete this; | 51 delete this; |
| 47 } | 52 } |
| 48 | 53 |
| 49 void Stop() { | 54 void Stop() { |
| 50 running_ = false; | 55 running_ = false; |
| 56 base::PlatformThread::Join(thread_handle_); |
| 51 } | 57 } |
| 52 | 58 |
| 53 private: | 59 private: |
| 54 bool running_; | 60 bool running_; |
| 55 PlatformUsbContext context_; | 61 PlatformUsbContext context_; |
| 56 | 62 base::PlatformThreadHandle thread_handle_; |
| 57 DISALLOW_EVIL_CONSTRUCTORS(UsbEventHandler); | 63 DISALLOW_COPY_AND_ASSIGN(UsbEventDispatcher); |
| 58 }; | 64 }; |
| 59 | 65 |
| 60 UsbService::UsbService() { | 66 // UsbDevice class uniquely represents a USB devices recognized by libusb and |
| 67 // maintains all its opened handles. It is assigned with an unique id by |
| 68 // UsbService. Once the device is disconnected it will invalidate all the |
| 69 // UsbDeviceHandle objects attached to it. The class is only visible to |
| 70 // UsbService and other classes need to access the device using its unique id. |
| 71 class UsbDevice : public base::RefCounted<UsbDevice> { |
| 72 public: |
| 73 explicit UsbDevice(PlatformUsbDevice device, |
| 74 int unique_id); |
| 75 PlatformUsbDevice device() const { return device_; } |
| 76 int unique_id() const { return unique_id_; } |
| 77 |
| 78 scoped_refptr<UsbDeviceHandle> OpenDevice(UsbService* service); |
| 79 void CloseDeviceHandle(UsbDeviceHandle* device); |
| 80 |
| 81 private: |
| 82 virtual ~UsbDevice(); |
| 83 friend class base::RefCounted<UsbDevice>; |
| 84 std::vector<scoped_refptr<UsbDeviceHandle> > handles_; |
| 85 PlatformUsbDevice device_; |
| 86 const int unique_id_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(UsbDevice); |
| 89 }; |
| 90 |
| 91 UsbDevice::UsbDevice( |
| 92 PlatformUsbDevice device, |
| 93 int unique_id) : device_(device), unique_id_(unique_id) { |
| 94 libusb_ref_device(device_); |
| 95 } |
| 96 |
| 97 UsbDevice::~UsbDevice() { |
| 98 libusb_unref_device(device_); |
| 99 |
| 100 // Device is lost. |
| 101 // Invalidates all the opened handle. |
| 102 for (vector<scoped_refptr<UsbDeviceHandle> >::iterator it = handles_.begin(); |
| 103 it != handles_.end(); |
| 104 ++it) { |
| 105 it->get()->InternalClose(); |
| 106 } |
| 107 STLClearObject(&handles_); |
| 108 } |
| 109 |
| 110 scoped_refptr<UsbDeviceHandle> |
| 111 UsbDevice::OpenDevice(UsbService* service) { |
| 112 PlatformUsbDeviceHandle handle; |
| 113 if (0 == libusb_open(device_, &handle)) { |
| 114 scoped_refptr<UsbDeviceHandle> wrapper = |
| 115 make_scoped_refptr(new UsbDeviceHandle(service, unique_id_, handle)); |
| 116 handles_.push_back(wrapper); |
| 117 return wrapper; |
| 118 } |
| 119 return scoped_refptr<UsbDeviceHandle>(); |
| 120 } |
| 121 |
| 122 void UsbDevice::CloseDeviceHandle(UsbDeviceHandle* device) { |
| 123 device->InternalClose(); |
| 124 for (vector<scoped_refptr<UsbDeviceHandle> >::iterator it = handles_.begin(); |
| 125 it != handles_.end(); |
| 126 ++it) { |
| 127 if (it->get() == device) { |
| 128 handles_.erase(it); |
| 129 return; |
| 130 } |
| 131 } |
| 132 } |
| 133 |
| 134 UsbService::UsbService() |
| 135 : next_unique_id_(1) { |
| 61 libusb_init(&context_); | 136 libusb_init(&context_); |
| 62 event_handler_ = new UsbEventHandler(context_); | 137 event_dispatcher_ = new UsbEventDispatcher(context_); |
| 63 } | 138 } |
| 64 | 139 |
| 65 UsbService::~UsbService() {} | 140 UsbService::~UsbService() {} |
| 66 | 141 |
| 67 void UsbService::Cleanup() { | 142 void UsbService::Shutdown() { |
| 68 event_handler_->Stop(); | 143 event_dispatcher_->Stop(); |
| 69 event_handler_ = NULL; | 144 devices_.clear(); |
| 145 libusb_exit(context_); |
| 70 } | 146 } |
| 71 | 147 |
| 72 void UsbService::FindDevices(const uint16 vendor_id, | 148 void UsbService::FindDevices(const uint16 vendor_id, |
| 73 const uint16 product_id, | 149 const uint16 product_id, |
| 74 int interface_id, | 150 const int interface_id, |
| 75 vector<scoped_refptr<UsbDevice> >* devices, | 151 vector<int>* devices, |
| 76 const base::Callback<void()>& callback) { | 152 const base::Callback<void()>& callback) { |
| 77 DCHECK(event_handler_) << "FindDevices called after event handler stopped."; | 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 154 DCHECK(event_dispatcher_) |
| 155 << "FindDevices called after event handler stopped."; |
| 78 #if defined(OS_CHROMEOS) | 156 #if defined(OS_CHROMEOS) |
| 79 // ChromeOS builds on non-ChromeOS machines (dev) should not attempt to | 157 // ChromeOS builds on non-ChromeOS machines (dev) should not attempt to |
| 80 // use permission broker. | 158 // use permission broker. |
| 81 if (base::chromeos::IsRunningOnChromeOS()) { | 159 if (base::chromeos::IsRunningOnChromeOS()) { |
| 82 chromeos::PermissionBrokerClient* client = | 160 chromeos::PermissionBrokerClient* client = |
| 83 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); | 161 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
| 84 DCHECK(client) << "Could not get permission broker client."; | 162 DCHECK(client) << "Could not get permission broker client."; |
| 85 if (!client) { | 163 if (!client) { |
| 86 callback.Run(); | 164 callback.Run(); |
| 87 return; | 165 return; |
| 88 } | 166 } |
| 89 | 167 |
| 90 client->RequestUsbAccess(vendor_id, | 168 client->RequestUsbAccess(vendor_id, |
| 91 product_id, | 169 product_id, |
| 92 interface_id, | 170 interface_id, |
| 93 base::Bind(&UsbService::FindDevicesImpl, | 171 base::Bind(&UsbService::FindDevicesImpl, |
| 94 base::Unretained(this), | 172 base::Unretained(this), |
| 95 vendor_id, | 173 vendor_id, |
| 96 product_id, | 174 product_id, |
| 175 interface_id, |
| 97 devices, | 176 devices, |
| 98 callback)); | 177 callback)); |
| 99 } else { | 178 } else { |
| 100 FindDevicesImpl(vendor_id, product_id, devices, callback, true); | 179 FindDevicesImpl(vendor_id, product_id, devices, callback, true); |
| 101 } | 180 } |
| 102 #else | 181 #else |
| 103 FindDevicesImpl(vendor_id, product_id, devices, callback, true); | 182 FindDevicesImpl(vendor_id, product_id, devices, callback, true); |
| 104 #endif // defined(OS_CHROMEOS) | 183 #endif // defined(OS_CHROMEOS) |
| 105 } | 184 } |
| 106 | 185 |
| 107 void UsbService::FindDevicesImpl(const uint16 vendor_id, | 186 void UsbService::FindDevicesImpl(const uint16 vendor_id, |
| 108 const uint16 product_id, | 187 const uint16 product_id, |
| 109 vector<scoped_refptr<UsbDevice> >* devices, | 188 vector<int>* devices, |
| 110 const base::Callback<void()>& callback, | 189 const base::Callback<void()>& callback, |
| 111 bool success) { | 190 bool success) { |
| 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 112 base::ScopedClosureRunner run_callback(callback); | 192 base::ScopedClosureRunner run_callback(callback); |
| 113 | 193 |
| 114 devices->clear(); | 194 devices->clear(); |
| 115 | 195 |
| 116 // If the permission broker was unable to obtain permission for the specified | 196 // If the permission broker was unable to obtain permission for the specified |
| 117 // devices then there is no point in attempting to enumerate the devices. On | 197 // devices then there is no point in attempting to enumerate the devices. On |
| 118 // platforms without a permission broker, we assume permission is granted. | 198 // platforms without a permission broker, we assume permission is granted. |
| 119 if (!success) | 199 if (!success) |
| 120 return; | 200 return; |
| 121 | 201 |
| 122 DeviceVector enumerated_devices; | 202 EnumerateDevices(); |
| 123 EnumerateDevices(&enumerated_devices); | |
| 124 if (enumerated_devices.empty()) | |
| 125 return; | |
| 126 | 203 |
| 127 for (unsigned int i = 0; i < enumerated_devices.size(); ++i) { | 204 for (DeviceMap::iterator it = devices_.begin(); |
| 128 PlatformUsbDevice device = enumerated_devices[i].device(); | 205 it != devices_.end(); ++it) { |
| 129 if (DeviceMatches(device, vendor_id, product_id)) { | 206 if (DeviceMatches(it->first, vendor_id, product_id)) { |
| 130 UsbDevice* const wrapper = LookupOrCreateDevice(device); | 207 devices->push_back(it->second->unique_id()); |
| 131 if (wrapper) | |
| 132 devices->push_back(wrapper); | |
| 133 } | 208 } |
| 134 } | 209 } |
| 135 } | 210 } |
| 136 | 211 |
| 137 void UsbService::CloseDevice(scoped_refptr<UsbDevice> device) { | 212 void UsbService::OpenDevice( |
| 138 DCHECK(event_handler_) << "CloseDevice called after event handler stopped."; | 213 int device, |
| 139 | 214 const base::Callback<void(scoped_refptr<UsbDeviceHandle>)>& callback) { |
| 140 PlatformUsbDevice platform_device = libusb_get_device(device->handle()); | 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 141 if (!ContainsKey(devices_, platform_device)) { | 216 EnumerateDevices(); |
| 142 LOG(WARNING) << "CloseDevice called for device we're not tracking!"; | 217 for (DeviceMap::iterator it = devices_.begin(); |
| 143 return; | 218 it != devices_.end(); ++it) { |
| 219 if (it->second->unique_id() == device) { |
| 220 callback.Run(it->second->OpenDevice(this)); |
| 221 return; |
| 222 } |
| 144 } | 223 } |
| 145 | 224 callback.Run(NULL); |
| 146 devices_.erase(platform_device); | |
| 147 libusb_close(device->handle()); | |
| 148 } | 225 } |
| 149 | 226 |
| 150 UsbService::RefCountedPlatformUsbDevice::RefCountedPlatformUsbDevice( | 227 void UsbService::CloseDeviceHandle(scoped_refptr<UsbDeviceHandle> device, |
| 151 PlatformUsbDevice device) : device_(device) { | 228 const base::Callback<void()>& callback) { |
| 152 libusb_ref_device(device_); | 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 230 int id = device->device(); |
| 231 |
| 232 for (DeviceMap::iterator it = devices_.begin(); |
| 233 it != devices_.end(); ++it) { |
| 234 if (it->second->unique_id() == id) { |
| 235 it->second->CloseDeviceHandle(device); |
| 236 break; |
| 237 } |
| 238 } |
| 239 callback.Run(); |
| 153 } | 240 } |
| 154 | 241 |
| 155 UsbService::RefCountedPlatformUsbDevice::RefCountedPlatformUsbDevice( | 242 void UsbService::ScheduleEnumerateDevice() { |
| 156 const RefCountedPlatformUsbDevice& other) : device_(other.device_) { | 243 // TODO(ikarienator): Throttle it. |
| 157 libusb_ref_device(device_); | 244 BrowserThread::PostTask( |
| 245 BrowserThread::FILE, |
| 246 FROM_HERE, |
| 247 base::Bind(&UsbService::EnumerateDevices, base::Unretained(this))); |
| 158 } | 248 } |
| 159 | 249 |
| 160 UsbService::RefCountedPlatformUsbDevice::~RefCountedPlatformUsbDevice() { | 250 void UsbService::EnumerateDevices() { |
| 161 libusb_unref_device(device_); | 251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 162 } | |
| 163 | |
| 164 PlatformUsbDevice UsbService::RefCountedPlatformUsbDevice::device() { | |
| 165 return device_; | |
| 166 } | |
| 167 | |
| 168 void UsbService::EnumerateDevices(DeviceVector* output) { | |
| 169 STLClearObject(output); | |
| 170 | |
| 171 libusb_device** devices = NULL; | 252 libusb_device** devices = NULL; |
| 172 const ssize_t device_count = libusb_get_device_list(context_, &devices); | 253 const ssize_t device_count = libusb_get_device_list(context_, &devices); |
| 173 if (device_count < 0) | 254 if (device_count < 0) |
| 174 return; | 255 return; |
| 175 | 256 |
| 257 set<int> connected_devices; |
| 258 vector<PlatformUsbDevice> disconnected_devices; |
| 259 |
| 260 // Populates new devices. |
| 176 for (int i = 0; i < device_count; ++i) { | 261 for (int i = 0; i < device_count; ++i) { |
| 177 libusb_device* device = devices[i]; | 262 connected_devices.insert(LookupOrCreateDevice(devices[i])); |
| 178 libusb_ref_device(device); | 263 } |
| 179 output->push_back(RefCountedPlatformUsbDevice(device)); | 264 libusb_free_device_list(devices, true); |
| 265 |
| 266 // Find disconnected devices. |
| 267 for (DeviceMap::iterator it = devices_.begin(); |
| 268 it != devices_.end(); ++it) { |
| 269 if (!ContainsKey(connected_devices, it->second->unique_id())) { |
| 270 disconnected_devices.push_back(it->first); |
| 271 } |
| 180 } | 272 } |
| 181 | 273 |
| 182 libusb_free_device_list(devices, true); | 274 // Remove disconnected devices from devices_. |
| 275 for (size_t i = 0; i < disconnected_devices.size(); ++i) { |
| 276 // This should delete those devices and invalidate their handles. |
| 277 // It might take long. |
| 278 devices_.erase(disconnected_devices[i]); |
| 279 } |
| 183 } | 280 } |
| 184 | 281 |
| 185 bool UsbService::DeviceMatches(PlatformUsbDevice device, | 282 bool UsbService::DeviceMatches(PlatformUsbDevice device, |
| 186 const uint16 vendor_id, | 283 const uint16 vendor_id, |
| 187 const uint16 product_id) { | 284 const uint16 product_id) { |
| 188 libusb_device_descriptor descriptor; | 285 libusb_device_descriptor descriptor; |
| 189 if (libusb_get_device_descriptor(device, &descriptor)) | 286 if (libusb_get_device_descriptor(device, &descriptor)) |
| 190 return false; | 287 return false; |
| 191 return descriptor.idVendor == vendor_id && descriptor.idProduct == product_id; | 288 return descriptor.idVendor == vendor_id && descriptor.idProduct == product_id; |
| 192 } | 289 } |
| 193 | 290 |
| 194 UsbDevice* UsbService::LookupOrCreateDevice(PlatformUsbDevice device) { | 291 int UsbService::LookupOrCreateDevice(PlatformUsbDevice device) { |
| 195 if (!ContainsKey(devices_, device)) { | 292 if (!ContainsKey(devices_, device)) { |
| 196 libusb_device_handle* handle = NULL; | 293 devices_[device] = |
| 197 if (libusb_open(device, &handle)) { | 294 make_scoped_refptr(new UsbDevice(device, next_unique_id_)); |
| 198 LOG(WARNING) << "Could not open device."; | 295 ++next_unique_id_; |
| 199 return NULL; | |
| 200 } | |
| 201 | |
| 202 UsbDevice* wrapper = new UsbDevice(this, handle); | |
| 203 devices_[device] = wrapper; | |
| 204 } | 296 } |
| 205 return devices_[device].get(); | 297 return devices_[device]->unique_id(); |
| 206 } | 298 } |
| OLD | NEW |