| 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 <cstring> |
| 8 #include <set> |
| 7 #include <vector> | 9 #include <vector> |
| 8 | 10 |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 11 #include "base/logging.h" | 13 #include "base/logging.h" |
| 12 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 13 #include "chrome/browser/usb/usb_device.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "chrome/browser/usb/usb_device_handle.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 14 #include "third_party/libusb/src/libusb/libusb.h" | 18 #include "third_party/libusb/src/libusb/libusb.h" |
| 15 | 19 |
| 16 #if defined(OS_CHROMEOS) | 20 #if defined(OS_CHROMEOS) |
| 17 #include "base/chromeos/chromeos_version.h" | 21 #include "base/chromeos/chromeos_version.h" |
| 18 #include "chromeos/dbus/dbus_thread_manager.h" | 22 #include "chromeos/dbus/dbus_thread_manager.h" |
| 19 #include "chromeos/dbus/permission_broker_client.h" | 23 #include "chromeos/dbus/permission_broker_client.h" |
| 20 #endif // defined(OS_CHROMEOS) | 24 #endif // defined(OS_CHROMEOS) |
| 21 | 25 |
| 22 using std::vector; | 26 using std::vector; |
| 27 using std::set; |
| 28 using content::BrowserThread; |
| 29 using base::PlatformThreadHandle; |
| 30 using base::RefCountedThreadSafe; |
| 23 | 31 |
| 24 // The UsbEventHandler works around a design flaw in the libusb interface. There | 32 // The UsbEventHandler 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 | 33 // currently no way to signal to libusb that any caller into one of the event |
| 26 // handler calls should return without handling any events. | 34 // handler calls should return without handling any events. |
| 35 // |
| 36 // This class manages the polling thread and assures the thread exits safely. |
| 37 // This class is only visible to UsbContext. UsbContext manages its life cycle. |
| 27 class UsbEventHandler : public base::PlatformThread::Delegate { | 38 class UsbEventHandler : public base::PlatformThread::Delegate { |
| 28 public: | 39 private: |
| 29 explicit UsbEventHandler(PlatformUsbContext context) | 40 friend class UsbContext; |
| 30 : running_(true), context_(context) { | 41 friend struct base::DefaultDeleter<UsbEventHandler>; |
| 31 base::PlatformThread::CreateNonJoinable(0, this); | |
| 32 } | |
| 33 | 42 |
| 34 virtual ~UsbEventHandler() {} | 43 explicit UsbEventHandler(PlatformUsbContext context); |
| 44 virtual ~UsbEventHandler(); |
| 35 | 45 |
| 36 virtual void ThreadMain() OVERRIDE { | 46 virtual void ThreadMain() OVERRIDE { |
| 37 base::PlatformThread::SetName("UsbEventHandler"); | 47 base::PlatformThread::SetName("UsbEventDispatcher"); |
| 38 | 48 VLOG(1) << "UsbEventDispatcher started."; |
| 39 DLOG(INFO) << "UsbEventHandler started."; | 49 while (true) { |
| 40 while (running_) { | 50 { |
| 51 base::AutoLock running_guard(running_lock_); |
| 52 if (!running_) break; |
| 53 } |
| 41 libusb_handle_events(context_); | 54 libusb_handle_events(context_); |
| 42 } | 55 } |
| 43 DLOG(INFO) << "UsbEventHandler shutting down."; | 56 VLOG(1) << "UsbEventDispatcher shutting down."; |
| 44 libusb_exit(context_); | |
| 45 | |
| 46 delete this; | |
| 47 } | 57 } |
| 48 | 58 |
| 49 void Stop() { | 59 void Stop() { |
| 50 running_ = false; | 60 { |
| 61 base::AutoLock running_guard(running_lock_); |
| 62 running_ = false; |
| 63 } |
| 64 // Send an event to interrupt the the polling. |
| 65 libusb_send_event(context_); |
| 66 // Wait for the thread to exit. |
| 67 base::PlatformThread::Join(thread_handle); |
| 51 } | 68 } |
| 52 | 69 |
| 53 private: | 70 volatile bool running_; |
| 54 bool running_; | 71 base::Lock running_lock_; |
| 55 PlatformUsbContext context_; | 72 const PlatformUsbContext context_; |
| 56 | 73 PlatformThreadHandle thread_handle; |
| 57 DISALLOW_EVIL_CONSTRUCTORS(UsbEventHandler); | 74 DISALLOW_COPY_AND_ASSIGN(UsbEventHandler); |
| 58 }; | 75 }; |
| 59 | 76 |
| 60 UsbService::UsbService() { | 77 UsbEventHandler::UsbEventHandler(PlatformUsbContext context) |
| 61 libusb_init(&context_); | 78 : running_(true), context_(context), thread_handle(0) { |
| 62 event_handler_ = new UsbEventHandler(context_); | 79 base::PlatformThread::Create(0, this, &thread_handle); |
| 63 } | 80 } |
| 64 | 81 |
| 65 UsbService::~UsbService() {} | 82 UsbEventHandler::~UsbEventHandler() {} |
| 66 | 83 |
| 67 void UsbService::Cleanup() { | 84 // Ref-counted wrapper for PlatformUsbContext. |
| 68 event_handler_->Stop(); | 85 // It also manages the life-cycle of UsbEventHandler |
| 69 event_handler_ = NULL; | 86 class UsbContext : public RefCountedThreadSafe<UsbContext> { |
| 87 public: |
| 88 UsbContext(); |
| 89 PlatformUsbContext context() const { return context_; } |
| 90 |
| 91 private: |
| 92 friend class RefCountedThreadSafe<UsbContext>; |
| 93 |
| 94 virtual ~UsbContext(); |
| 95 PlatformUsbContext context_; |
| 96 scoped_ptr<UsbEventHandler> event_handler_; |
| 97 }; |
| 98 |
| 99 UsbContext::UsbContext() |
| 100 : context_(NULL) { |
| 101 libusb_init(&context_); |
| 102 event_handler_.reset(new UsbEventHandler(context_)); |
| 70 } | 103 } |
| 71 | 104 |
| 72 void UsbService::FindDevices(const uint16 vendor_id, | 105 UsbContext::~UsbContext() { |
| 73 const uint16 product_id, | 106 event_handler_->Stop(); |
| 74 int interface_id, | 107 event_handler_.reset(NULL); |
| 75 vector<scoped_refptr<UsbDevice> >* devices, | 108 // The following statement will inform the event handler to stop waiting. |
| 109 libusb_exit(context_); |
| 110 } |
| 111 |
| 112 // UsbDevice class uniquely represents a USB devices recognized by libusb and |
| 113 // maintains all its opened handles. It is assigned with an unique id by |
| 114 // UsbService. Once the device is disconnected it will invalidate all the |
| 115 // UsbDeviceHandle objects attached to it. The class is only visible to |
| 116 // UsbService and other classes need to access the device using its unique id. |
| 117 class UsbDevice : public base::NonThreadSafe { |
| 118 public: |
| 119 explicit UsbDevice(UsbContext* context, PlatformUsbDevice device, |
| 120 const int unique_id, const uint16 vendor_id, |
| 121 const uint16 product_id); |
| 122 virtual ~UsbDevice(); |
| 123 PlatformUsbDevice device() const { return device_; } |
| 124 int unique_id() const { return unique_id_; } |
| 125 int vendor_id() const { return vendor_id_; } |
| 126 int product_id() const { return product_id_; } |
| 127 |
| 128 scoped_refptr<UsbDeviceHandle> OpenDevice(UsbService* service); |
| 129 void CloseDeviceHandle(UsbDeviceHandle* device); |
| 130 |
| 131 private: |
| 132 // Retain the context so it will not be release before the destruction |
| 133 // of the UsbDevice object. |
| 134 scoped_refptr<UsbContext> context_; |
| 135 vector<scoped_refptr<UsbDeviceHandle> > handles_; |
| 136 const PlatformUsbDevice device_; |
| 137 const uint16 unique_id_; |
| 138 const uint16 vendor_id_; |
| 139 const int product_id_; |
| 140 DISALLOW_COPY_AND_ASSIGN(UsbDevice); |
| 141 }; |
| 142 |
| 143 UsbDevice::UsbDevice(UsbContext* context, PlatformUsbDevice device, |
| 144 const int unique_id, const uint16 vendor_id, |
| 145 const uint16 product_id) |
| 146 : context_(context), |
| 147 device_(device), |
| 148 unique_id_(unique_id), |
| 149 vendor_id_(vendor_id), |
| 150 product_id_(product_id) { |
| 151 DCHECK(CalledOnValidThread()); |
| 152 libusb_ref_device(device_); |
| 153 } |
| 154 |
| 155 UsbDevice::~UsbDevice() { |
| 156 DCHECK(CalledOnValidThread()); |
| 157 libusb_unref_device(device_); |
| 158 |
| 159 // Device is lost. |
| 160 // Invalidates all the opened handle. |
| 161 for (vector<scoped_refptr<UsbDeviceHandle> >::iterator it = handles_.begin(); |
| 162 it != handles_.end(); ++it) { |
| 163 it->get()->InternalClose(); |
| 164 } |
| 165 STLClearObject(&handles_); |
| 166 } |
| 167 |
| 168 scoped_refptr<UsbDeviceHandle> UsbDevice::OpenDevice(UsbService* service) { |
| 169 DCHECK(CalledOnValidThread()); |
| 170 PlatformUsbDeviceHandle handle; |
| 171 if (0 == libusb_open(device_, &handle)) { |
| 172 scoped_refptr<UsbDeviceHandle> wrapper = |
| 173 make_scoped_refptr(new UsbDeviceHandle(service, unique_id_, vendor_id_, |
| 174 product_id_, handle)); |
| 175 handles_.push_back(wrapper); |
| 176 return wrapper; |
| 177 } |
| 178 return scoped_refptr<UsbDeviceHandle>(); |
| 179 } |
| 180 |
| 181 void UsbDevice::CloseDeviceHandle(UsbDeviceHandle* device) { |
| 182 DCHECK(CalledOnValidThread()); |
| 183 device->InternalClose(); |
| 184 for (vector<scoped_refptr<UsbDeviceHandle> >::iterator it = handles_.begin(); |
| 185 it != handles_.end(); ++it) { |
| 186 if (it->get() == device) { |
| 187 handles_.erase(it); |
| 188 return; |
| 189 } |
| 190 } |
| 191 } |
| 192 |
| 193 UsbService::UsbService() |
| 194 : context_(new UsbContext()), |
| 195 next_unique_id_(1), |
| 196 device_enumeration_scheduled_(false) { |
| 197 // This class will be consequently called on FILE thread. |
| 198 DetachFromThread(); |
| 199 } |
| 200 |
| 201 UsbService::~UsbService() { |
| 202 // The destructor will be called on UI thread. |
| 203 DetachFromThread(); |
| 204 } |
| 205 |
| 206 void UsbService::Shutdown() { |
| 207 context_ = NULL; |
| 208 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
| 209 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, it->second); |
| 210 } |
| 211 devices_.clear(); |
| 212 } |
| 213 |
| 214 void UsbService::FindDevices(const uint16 vendor_id, const uint16 product_id, |
| 215 const int interface_id, vector<int>* devices, |
| 76 const base::Callback<void()>& callback) { | 216 const base::Callback<void()>& callback) { |
| 77 DCHECK(event_handler_) << "FindDevices called after event handler stopped."; | 217 DCHECK(CalledOnValidThread()); |
| 78 #if defined(OS_CHROMEOS) | 218 #if defined(OS_CHROMEOS) |
| 79 // ChromeOS builds on non-ChromeOS machines (dev) should not attempt to | 219 // ChromeOS builds on non-ChromeOS machines (dev) should not attempt to |
| 80 // use permission broker. | 220 // use permission broker. |
| 81 if (base::chromeos::IsRunningOnChromeOS()) { | 221 if (base::chromeos::IsRunningOnChromeOS()) { |
| 82 chromeos::PermissionBrokerClient* client = | 222 chromeos::PermissionBrokerClient* client = |
| 83 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); | 223 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
| 84 DCHECK(client) << "Could not get permission broker client."; | 224 DCHECK(client) << "Could not get permission broker client."; |
| 85 if (!client) { | 225 if (!client) { |
| 86 callback.Run(); | 226 callback.Run(); |
| 87 return; | 227 return; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 98 callback)); | 238 callback)); |
| 99 } else { | 239 } else { |
| 100 FindDevicesImpl(vendor_id, product_id, devices, callback, true); | 240 FindDevicesImpl(vendor_id, product_id, devices, callback, true); |
| 101 } | 241 } |
| 102 #else | 242 #else |
| 103 FindDevicesImpl(vendor_id, product_id, devices, callback, true); | 243 FindDevicesImpl(vendor_id, product_id, devices, callback, true); |
| 104 #endif // defined(OS_CHROMEOS) | 244 #endif // defined(OS_CHROMEOS) |
| 105 } | 245 } |
| 106 | 246 |
| 107 void UsbService::FindDevicesImpl(const uint16 vendor_id, | 247 void UsbService::FindDevicesImpl(const uint16 vendor_id, |
| 108 const uint16 product_id, | 248 const uint16 product_id, vector<int>* devices, |
| 109 vector<scoped_refptr<UsbDevice> >* devices, | |
| 110 const base::Callback<void()>& callback, | 249 const base::Callback<void()>& callback, |
| 111 bool success) { | 250 bool success) { |
| 251 DCHECK(CalledOnValidThread()); |
| 112 base::ScopedClosureRunner run_callback(callback); | 252 base::ScopedClosureRunner run_callback(callback); |
| 113 | 253 |
| 114 devices->clear(); | 254 devices->clear(); |
| 115 | 255 |
| 116 // If the permission broker was unable to obtain permission for the specified | 256 // 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 | 257 // devices then there is no point in attempting to enumerate the devices. On |
| 118 // platforms without a permission broker, we assume permission is granted. | 258 // platforms without a permission broker, we assume permission is granted. |
| 119 if (!success) | 259 if (!success) return; |
| 120 return; | |
| 121 | 260 |
| 122 DeviceVector enumerated_devices; | 261 EnumerateDevices(); |
| 123 EnumerateDevices(&enumerated_devices); | |
| 124 if (enumerated_devices.empty()) | |
| 125 return; | |
| 126 | 262 |
| 127 for (unsigned int i = 0; i < enumerated_devices.size(); ++i) { | 263 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
| 128 PlatformUsbDevice device = enumerated_devices[i].device(); | 264 if (DeviceMatches(it->second, vendor_id, product_id)) |
| 129 if (DeviceMatches(device, vendor_id, product_id)) { | 265 devices->push_back(it->second->unique_id()); |
| 130 UsbDevice* const wrapper = LookupOrCreateDevice(device); | 266 } |
| 131 if (wrapper) | 267 } |
| 132 devices->push_back(wrapper); | 268 |
| 269 scoped_refptr<UsbDeviceHandle> UsbService::OpenDevice(int device) { |
| 270 DCHECK(CalledOnValidThread()); |
| 271 EnumerateDevices(); |
| 272 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
| 273 if (it->second->unique_id() == device) |
| 274 return it->second->OpenDevice(this); |
| 275 } |
| 276 return NULL; |
| 277 } |
| 278 |
| 279 void UsbService::CloseDeviceHandle(scoped_refptr<UsbDeviceHandle> device) { |
| 280 DCHECK(CalledOnValidThread()); |
| 281 int id = device->device(); |
| 282 |
| 283 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
| 284 if (it->second->unique_id() == id) { |
| 285 it->second->CloseDeviceHandle(device); |
| 286 break; |
| 133 } | 287 } |
| 134 } | 288 } |
| 135 } | 289 } |
| 136 | 290 |
| 137 void UsbService::CloseDevice(scoped_refptr<UsbDevice> device) { | 291 void UsbService::ScheduleEnumerateDevice() { |
| 138 DCHECK(event_handler_) << "CloseDevice called after event handler stopped."; | 292 DCHECK(CalledOnValidThread()); |
| 139 | 293 if (device_enumeration_scheduled_) |
| 140 PlatformUsbDevice platform_device = libusb_get_device(device->handle()); | |
| 141 if (!ContainsKey(devices_, platform_device)) { | |
| 142 LOG(WARNING) << "CloseDevice called for device we're not tracking!"; | |
| 143 return; | 294 return; |
| 144 } | 295 device_enumeration_scheduled_ = true; |
| 145 | 296 BrowserThread::PostTask( |
| 146 devices_.erase(platform_device); | 297 BrowserThread::FILE, FROM_HERE, |
| 147 libusb_close(device->handle()); | 298 base::Bind(&UsbService::EnumerateDevices, base::Unretained(this))); |
| 148 } | 299 } |
| 149 | 300 |
| 150 UsbService::RefCountedPlatformUsbDevice::RefCountedPlatformUsbDevice( | 301 void UsbService::EnumerateDevices() { |
| 151 PlatformUsbDevice device) : device_(device) { | 302 DCHECK(CalledOnValidThread()); |
| 152 libusb_ref_device(device_); | 303 device_enumeration_scheduled_ = false; |
| 153 } | |
| 154 | |
| 155 UsbService::RefCountedPlatformUsbDevice::RefCountedPlatformUsbDevice( | |
| 156 const RefCountedPlatformUsbDevice& other) : device_(other.device_) { | |
| 157 libusb_ref_device(device_); | |
| 158 } | |
| 159 | |
| 160 UsbService::RefCountedPlatformUsbDevice::~RefCountedPlatformUsbDevice() { | |
| 161 libusb_unref_device(device_); | |
| 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; | 304 libusb_device** devices = NULL; |
| 172 const ssize_t device_count = libusb_get_device_list(context_, &devices); | 305 const ssize_t device_count = |
| 306 libusb_get_device_list(context_->context(), &devices); |
| 173 if (device_count < 0) | 307 if (device_count < 0) |
| 174 return; | 308 return; |
| 175 | 309 |
| 176 for (int i = 0; i < device_count; ++i) { | 310 set<int> connected_devices; |
| 177 libusb_device* device = devices[i]; | 311 vector<PlatformUsbDevice> disconnected_devices; |
| 178 libusb_ref_device(device); | 312 |
| 179 output->push_back(RefCountedPlatformUsbDevice(device)); | 313 // Populates new devices. |
| 314 for (ssize_t i = 0; i < device_count; ++i) { |
| 315 if (!ContainsKey(devices_, devices[i])) { |
| 316 libusb_device_descriptor descriptor; |
| 317 if (0 != libusb_get_device_descriptor(devices[i], &descriptor)) |
| 318 continue; |
| 319 devices_[devices[i]] = new UsbDevice(context_.get(), devices[i], |
| 320 next_unique_id_, descriptor.idVendor, |
| 321 descriptor.idProduct); |
| 322 ++next_unique_id_; |
| 323 } |
| 324 connected_devices.insert(devices_[devices[i]]->unique_id()); |
| 180 } | 325 } |
| 181 | 326 |
| 327 // Find disconnected devices. |
| 328 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
| 329 if (!ContainsKey(connected_devices, it->second->unique_id())) { |
| 330 disconnected_devices.push_back(it->first); |
| 331 } |
| 332 } |
| 333 |
| 334 // Remove disconnected devices from devices_. |
| 335 for (size_t i = 0; i < disconnected_devices.size(); ++i) { |
| 336 // This should delete those devices and invalidate their handles. |
| 337 // It might take long. |
| 338 delete devices_[disconnected_devices[i]]; |
| 339 devices_.erase(disconnected_devices[i]); |
| 340 } |
| 182 libusb_free_device_list(devices, true); | 341 libusb_free_device_list(devices, true); |
| 183 } | 342 } |
| 184 | 343 |
| 185 bool UsbService::DeviceMatches(PlatformUsbDevice device, | 344 bool UsbService::DeviceMatches(const UsbDevice* device, const uint16 vendor_id, |
| 186 const uint16 vendor_id, | |
| 187 const uint16 product_id) { | 345 const uint16 product_id) { |
| 188 libusb_device_descriptor descriptor; | 346 return device->vendor_id() == vendor_id && device->product_id() == product_id; |
| 189 if (libusb_get_device_descriptor(device, &descriptor)) | |
| 190 return false; | |
| 191 return descriptor.idVendor == vendor_id && descriptor.idProduct == product_id; | |
| 192 } | 347 } |
| 193 | |
| 194 UsbDevice* UsbService::LookupOrCreateDevice(PlatformUsbDevice device) { | |
| 195 if (!ContainsKey(devices_, device)) { | |
| 196 libusb_device_handle* handle = NULL; | |
| 197 if (libusb_open(device, &handle)) { | |
| 198 LOG(WARNING) << "Could not open device."; | |
| 199 return NULL; | |
| 200 } | |
| 201 | |
| 202 UsbDevice* wrapper = new UsbDevice(this, handle); | |
| 203 devices_[device] = wrapper; | |
| 204 } | |
| 205 return devices_[device].get(); | |
| 206 } | |
| OLD | NEW |