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