OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/usb/usb_device_impl.h" | 5 #include "device/usb/usb_device_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
16 #include "device/usb/usb_context.h" | 16 #include "device/usb/usb_context.h" |
17 #include "device/usb/usb_descriptors.h" | 17 #include "device/usb/usb_descriptors.h" |
18 #include "device/usb/usb_device_handle_impl.h" | 18 #include "device/usb/usb_device_handle_impl.h" |
19 #include "device/usb/usb_error.h" | 19 #include "device/usb/usb_error.h" |
20 #include "third_party/libusb/src/libusb/libusb.h" | 20 #include "third_party/libusb/src/libusb/libusb.h" |
21 | 21 |
22 #if defined(OS_CHROMEOS) | 22 #if defined(OS_CHROMEOS) |
23 #include "base/sys_info.h" | 23 #include "base/sys_info.h" |
24 #include "chromeos/dbus/dbus_thread_manager.h" | 24 #include "chromeos/dbus/dbus_thread_manager.h" |
25 #include "chromeos/dbus/permission_broker_client.h" | 25 #include "chromeos/dbus/permission_broker_client.h" |
26 #endif // defined(OS_CHROMEOS) | 26 #endif // defined(OS_CHROMEOS) |
27 | 27 |
28 #if defined(OS_LINUX) | 28 #if defined(USE_UDEV) |
29 #include "device/udev_linux/udev.h" | 29 #include "device/udev_linux/udev.h" |
30 #endif // defined(OS_LINUX) | 30 #endif // defined(USE_UDEV) |
31 | 31 |
32 namespace device { | 32 namespace device { |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
37 void OnRequestUsbAccessReplied( | 37 void OnRequestUsbAccessReplied( |
38 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 38 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
39 const base::Callback<void(bool success)>& callback, | 39 const base::Callback<void(bool success)>& callback, |
40 bool success) { | 40 bool success) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 uint16 product_id, | 123 uint16 product_id, |
124 uint32 unique_id) | 124 uint32 unique_id) |
125 : UsbDevice(vendor_id, product_id, unique_id), | 125 : UsbDevice(vendor_id, product_id, unique_id), |
126 platform_device_(platform_device), | 126 platform_device_(platform_device), |
127 current_configuration_cached_(false), | 127 current_configuration_cached_(false), |
128 context_(context), | 128 context_(context), |
129 ui_task_runner_(ui_task_runner) { | 129 ui_task_runner_(ui_task_runner) { |
130 CHECK(platform_device) << "platform_device cannot be NULL"; | 130 CHECK(platform_device) << "platform_device cannot be NULL"; |
131 libusb_ref_device(platform_device); | 131 libusb_ref_device(platform_device); |
132 | 132 |
133 #if defined(OS_LINUX) | 133 #if defined(USE_UDEV) |
134 ScopedUdevPtr udev(udev_new()); | 134 ScopedUdevPtr udev(udev_new()); |
135 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev.get())); | 135 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev.get())); |
136 | 136 |
137 udev_enumerate_add_match_subsystem(enumerate.get(), "usb"); | 137 udev_enumerate_add_match_subsystem(enumerate.get(), "usb"); |
138 if (udev_enumerate_scan_devices(enumerate.get()) != 0) { | 138 if (udev_enumerate_scan_devices(enumerate.get()) != 0) { |
139 return; | 139 return; |
140 } | 140 } |
141 std::string bus_number = | 141 std::string bus_number = |
142 base::IntToString(libusb_get_bus_number(platform_device)); | 142 base::IntToString(libusb_get_bus_number(platform_device)); |
143 std::string device_address = | 143 std::string device_address = |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 libusb_free_config_descriptor(platform_config); | 315 libusb_free_config_descriptor(platform_config); |
316 current_configuration_cached_ = true; | 316 current_configuration_cached_ = true; |
317 } | 317 } |
318 | 318 |
319 return current_configuration_; | 319 return current_configuration_; |
320 } | 320 } |
321 | 321 |
322 bool UsbDeviceImpl::GetManufacturer(base::string16* manufacturer) { | 322 bool UsbDeviceImpl::GetManufacturer(base::string16* manufacturer) { |
323 DCHECK(thread_checker_.CalledOnValidThread()); | 323 DCHECK(thread_checker_.CalledOnValidThread()); |
324 | 324 |
325 #if defined(OS_LINUX) | 325 #if defined(USE_UDEV) |
326 if (manufacturer_.empty()) { | 326 if (manufacturer_.empty()) { |
327 return false; | 327 return false; |
328 } | 328 } |
329 *manufacturer = base::UTF8ToUTF16(manufacturer_); | 329 *manufacturer = base::UTF8ToUTF16(manufacturer_); |
330 return true; | 330 return true; |
331 #else | 331 #else |
332 // This is a non-blocking call as libusb has the descriptor in memory. | 332 // This is a non-blocking call as libusb has the descriptor in memory. |
333 libusb_device_descriptor desc; | 333 libusb_device_descriptor desc; |
334 const int rv = libusb_get_device_descriptor(platform_device_, &desc); | 334 const int rv = libusb_get_device_descriptor(platform_device_, &desc); |
335 if (rv != LIBUSB_SUCCESS) { | 335 if (rv != LIBUSB_SUCCESS) { |
(...skipping 10 matching lines...) Expand all Loading... |
346 if (device_handle.get()) { | 346 if (device_handle.get()) { |
347 return device_handle->GetStringDescriptor(desc.iManufacturer, manufacturer); | 347 return device_handle->GetStringDescriptor(desc.iManufacturer, manufacturer); |
348 } | 348 } |
349 return false; | 349 return false; |
350 #endif | 350 #endif |
351 } | 351 } |
352 | 352 |
353 bool UsbDeviceImpl::GetProduct(base::string16* product) { | 353 bool UsbDeviceImpl::GetProduct(base::string16* product) { |
354 DCHECK(thread_checker_.CalledOnValidThread()); | 354 DCHECK(thread_checker_.CalledOnValidThread()); |
355 | 355 |
356 #if defined(OS_LINUX) | 356 #if defined(USE_UDEV) |
357 if (product_.empty()) { | 357 if (product_.empty()) { |
358 return false; | 358 return false; |
359 } | 359 } |
360 *product = base::UTF8ToUTF16(product_); | 360 *product = base::UTF8ToUTF16(product_); |
361 return true; | 361 return true; |
362 #else | 362 #else |
363 // This is a non-blocking call as libusb has the descriptor in memory. | 363 // This is a non-blocking call as libusb has the descriptor in memory. |
364 libusb_device_descriptor desc; | 364 libusb_device_descriptor desc; |
365 const int rv = libusb_get_device_descriptor(platform_device_, &desc); | 365 const int rv = libusb_get_device_descriptor(platform_device_, &desc); |
366 if (rv != LIBUSB_SUCCESS) { | 366 if (rv != LIBUSB_SUCCESS) { |
(...skipping 10 matching lines...) Expand all Loading... |
377 if (device_handle.get()) { | 377 if (device_handle.get()) { |
378 return device_handle->GetStringDescriptor(desc.iProduct, product); | 378 return device_handle->GetStringDescriptor(desc.iProduct, product); |
379 } | 379 } |
380 return false; | 380 return false; |
381 #endif | 381 #endif |
382 } | 382 } |
383 | 383 |
384 bool UsbDeviceImpl::GetSerialNumber(base::string16* serial_number) { | 384 bool UsbDeviceImpl::GetSerialNumber(base::string16* serial_number) { |
385 DCHECK(thread_checker_.CalledOnValidThread()); | 385 DCHECK(thread_checker_.CalledOnValidThread()); |
386 | 386 |
387 #if defined(OS_LINUX) | 387 #if defined(USE_UDEV) |
388 if (serial_number_.empty()) { | 388 if (serial_number_.empty()) { |
389 return false; | 389 return false; |
390 } | 390 } |
391 *serial_number = base::UTF8ToUTF16(serial_number_); | 391 *serial_number = base::UTF8ToUTF16(serial_number_); |
392 return true; | 392 return true; |
393 #else | 393 #else |
394 // This is a non-blocking call as libusb has the descriptor in memory. | 394 // This is a non-blocking call as libusb has the descriptor in memory. |
395 libusb_device_descriptor desc; | 395 libusb_device_descriptor desc; |
396 const int rv = libusb_get_device_descriptor(platform_device_, &desc); | 396 const int rv = libusb_get_device_descriptor(platform_device_, &desc); |
397 if (rv != LIBUSB_SUCCESS) { | 397 if (rv != LIBUSB_SUCCESS) { |
(...skipping 17 matching lines...) Expand all Loading... |
415 | 415 |
416 void UsbDeviceImpl::OnDisconnect() { | 416 void UsbDeviceImpl::OnDisconnect() { |
417 DCHECK(thread_checker_.CalledOnValidThread()); | 417 DCHECK(thread_checker_.CalledOnValidThread()); |
418 HandlesVector handles; | 418 HandlesVector handles; |
419 swap(handles, handles_); | 419 swap(handles, handles_); |
420 for (HandlesVector::iterator it = handles.begin(); it != handles.end(); ++it) | 420 for (HandlesVector::iterator it = handles.begin(); it != handles.end(); ++it) |
421 (*it)->InternalClose(); | 421 (*it)->InternalClose(); |
422 } | 422 } |
423 | 423 |
424 } // namespace device | 424 } // namespace device |
OLD | NEW |