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_service_impl.h" | 5 #include "device/usb/usb_service_impl.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
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/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
15 #include "device/usb/usb_error.h" | 15 #include "device/usb/usb_error.h" |
| 16 #include "device/usb/usb_log.h" |
16 | 17 |
17 #if defined(OS_WIN) | 18 #if defined(OS_WIN) |
18 #include <usbiodef.h> | 19 #include <usbiodef.h> |
19 | 20 |
20 #include "base/scoped_observer.h" | 21 #include "base/scoped_observer.h" |
21 #include "device/core/device_monitor_win.h" | 22 #include "device/core/device_monitor_win.h" |
22 #endif // OS_WIN | 23 #endif // OS_WIN |
23 | 24 |
24 namespace device { | 25 namespace device { |
25 | 26 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 ScopedObserver<DeviceMonitorWin, DeviceMonitorWin::Observer> device_observer_; | 63 ScopedObserver<DeviceMonitorWin, DeviceMonitorWin::Observer> device_observer_; |
63 }; | 64 }; |
64 #endif | 65 #endif |
65 | 66 |
66 // static | 67 // static |
67 UsbService* UsbServiceImpl::Create( | 68 UsbService* UsbServiceImpl::Create( |
68 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { | 69 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { |
69 PlatformUsbContext context = NULL; | 70 PlatformUsbContext context = NULL; |
70 const int rv = libusb_init(&context); | 71 const int rv = libusb_init(&context); |
71 if (rv != LIBUSB_SUCCESS) { | 72 if (rv != LIBUSB_SUCCESS) { |
72 VLOG(1) << "Failed to initialize libusb: " | 73 USB_LOG(ERROR) << "Failed to initialize libusb: " |
73 << ConvertPlatformUsbErrorToString(rv); | 74 << ConvertPlatformUsbErrorToString(rv); |
74 return nullptr; | 75 return nullptr; |
75 } | 76 } |
76 if (!context) { | 77 if (!context) { |
77 return nullptr; | 78 return nullptr; |
78 } | 79 } |
79 | 80 |
80 return new UsbServiceImpl(context, ui_task_runner); | 81 return new UsbServiceImpl(context, ui_task_runner); |
81 } | 82 } |
82 | 83 |
83 scoped_refptr<UsbDevice> UsbServiceImpl::GetDeviceById(uint32 unique_id) { | 84 scoped_refptr<UsbDevice> UsbServiceImpl::GetDeviceById(uint32 unique_id) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 } | 147 } |
147 } | 148 } |
148 | 149 |
149 void UsbServiceImpl::RefreshDevices() { | 150 void UsbServiceImpl::RefreshDevices() { |
150 DCHECK(CalledOnValidThread()); | 151 DCHECK(CalledOnValidThread()); |
151 | 152 |
152 libusb_device** platform_devices = NULL; | 153 libusb_device** platform_devices = NULL; |
153 const ssize_t device_count = | 154 const ssize_t device_count = |
154 libusb_get_device_list(context_->context(), &platform_devices); | 155 libusb_get_device_list(context_->context(), &platform_devices); |
155 if (device_count < 0) { | 156 if (device_count < 0) { |
156 VLOG(1) << "Failed to get device list: " | 157 USB_LOG(ERROR) << "Failed to get device list: " |
157 << ConvertPlatformUsbErrorToString(device_count); | 158 << ConvertPlatformUsbErrorToString(device_count); |
158 } | 159 } |
159 | 160 |
160 std::set<UsbDevice*> connected_devices; | 161 std::set<UsbDevice*> connected_devices; |
161 std::vector<PlatformUsbDevice> disconnected_devices; | 162 std::vector<PlatformUsbDevice> disconnected_devices; |
162 | 163 |
163 // Populates new devices. | 164 // Populates new devices. |
164 for (ssize_t i = 0; i < device_count; ++i) { | 165 for (ssize_t i = 0; i < device_count; ++i) { |
165 if (!ContainsKey(platform_devices_, platform_devices[i])) { | 166 if (!ContainsKey(platform_devices_, platform_devices[i])) { |
166 scoped_refptr<UsbDeviceImpl> new_device = AddDevice(platform_devices[i]); | 167 scoped_refptr<UsbDeviceImpl> new_device = AddDevice(platform_devices[i]); |
167 if (new_device) { | 168 if (new_device) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 } while (devices_.find(unique_id) != devices_.end()); | 207 } while (devices_.find(unique_id) != devices_.end()); |
207 | 208 |
208 scoped_refptr<UsbDeviceImpl> new_device(new UsbDeviceImpl( | 209 scoped_refptr<UsbDeviceImpl> new_device(new UsbDeviceImpl( |
209 context_, ui_task_runner_, platform_device, descriptor.idVendor, | 210 context_, ui_task_runner_, platform_device, descriptor.idVendor, |
210 descriptor.idProduct, unique_id)); | 211 descriptor.idProduct, unique_id)); |
211 platform_devices_[platform_device] = new_device; | 212 platform_devices_[platform_device] = new_device; |
212 devices_[unique_id] = new_device; | 213 devices_[unique_id] = new_device; |
213 NotifyDeviceAdded(new_device); | 214 NotifyDeviceAdded(new_device); |
214 return new_device; | 215 return new_device; |
215 } else { | 216 } else { |
216 VLOG(1) << "Failed to get device descriptor: " | 217 USB_LOG(DEBUG) << "Failed to get device descriptor: " |
217 << ConvertPlatformUsbErrorToString(rv); | 218 << ConvertPlatformUsbErrorToString(rv); |
218 return nullptr; | 219 return nullptr; |
219 } | 220 } |
220 } | 221 } |
221 | 222 |
222 // static | 223 // static |
223 int LIBUSB_CALL UsbServiceImpl::HotplugCallback(libusb_context* context, | 224 int LIBUSB_CALL UsbServiceImpl::HotplugCallback(libusb_context* context, |
224 PlatformUsbDevice device, | 225 PlatformUsbDevice device, |
225 libusb_hotplug_event event, | 226 libusb_hotplug_event event, |
226 void* user_data) { | 227 void* user_data) { |
227 // It is safe to access the UsbServiceImpl* here because libusb takes a lock | 228 // It is safe to access the UsbServiceImpl* here because libusb takes a lock |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 NotifyDeviceRemoved(device); | 283 NotifyDeviceRemoved(device); |
283 device->OnDisconnect(); | 284 device->OnDisconnect(); |
284 } else { | 285 } else { |
285 NOTREACHED(); | 286 NOTREACHED(); |
286 } | 287 } |
287 | 288 |
288 libusb_unref_device(platform_device); | 289 libusb_unref_device(platform_device); |
289 } | 290 } |
290 | 291 |
291 } // namespace device | 292 } // namespace device |
OLD | NEW |