OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.h" | 5 #include "device/usb/usb_service.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
| 8 #include <set> |
8 | 9 |
9 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
10 #include "base/single_thread_task_runner.h" | 11 #include "base/message_loop/message_loop.h" |
11 #include "device/usb/usb_context.h" | 12 #include "device/usb/usb_context.h" |
12 #include "device/usb/usb_device_impl.h" | 13 #include "device/usb/usb_device_impl.h" |
13 #include "third_party/libusb/src/libusb/libusb.h" | 14 #include "third_party/libusb/src/libusb/libusb.h" |
14 | 15 |
| 16 #if defined(OS_WIN) |
| 17 #include "base/scoped_observer.h" |
| 18 #include "device/core/device_monitor_win.h" |
| 19 #endif // OS_WIN |
| 20 |
| 21 struct libusb_device; |
| 22 struct libusb_context; |
| 23 |
| 24 namespace base { |
| 25 class SequencedTaskRunner; |
| 26 class SingleThreadTaskRunner; |
| 27 } |
| 28 |
15 namespace device { | 29 namespace device { |
16 | 30 |
17 typedef struct libusb_device* PlatformUsbDevice; | 31 typedef struct libusb_device* PlatformUsbDevice; |
18 typedef struct libusb_context* PlatformUsbContext; | 32 typedef struct libusb_context* PlatformUsbContext; |
19 | 33 |
20 class UsbServiceImpl : public UsbService { | 34 class UsbServiceImpl : public UsbService, |
| 35 #if defined(OS_WIN) |
| 36 public DeviceMonitorWin::Observer, |
| 37 #endif // OS_WIN |
| 38 public base::MessageLoop::DestructionObserver { |
21 public: | 39 public: |
22 static UsbService* Create( | 40 static UsbService* Create( |
23 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); | 41 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
24 | 42 |
25 private: | 43 private: |
26 explicit UsbServiceImpl( | 44 explicit UsbServiceImpl( |
27 PlatformUsbContext context, | 45 PlatformUsbContext context, |
28 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); | 46 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
29 ~UsbServiceImpl() override; | 47 ~UsbServiceImpl() override; |
30 | 48 |
31 // device::UsbService implementation | 49 // device::UsbService implementation |
32 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override; | 50 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id) override; |
33 void GetDevices(std::vector<scoped_refptr<UsbDevice>>* devices) override; | 51 void GetDevices(const GetDevicesCallback& callback) override; |
34 | |
35 // Enumerate USB devices from OS and update devices_ map. | |
36 void RefreshDevices(); | |
37 | 52 |
38 #if defined(OS_WIN) | 53 #if defined(OS_WIN) |
39 void RefreshDevicesIfWinUsbDevice(const std::string& device_path); | 54 // device::DeviceMonitorWin::Observer implementation |
| 55 void OnDeviceAdded(const GUID& class_guid, |
| 56 const std::string& device_path) override; |
| 57 void OnDeviceRemoved(const GUID& class_guid, |
| 58 const std::string& device_path) override; |
40 #endif // OS_WIN | 59 #endif // OS_WIN |
41 | 60 |
| 61 // base::MessageLoop::DestructionObserver implementation |
| 62 void WillDestroyCurrentMessageLoop() override; |
| 63 |
| 64 // Enumerate USB devices from OS and update devices_ map. |new_device_path| is |
| 65 // an optional hint used on Windows to prevent enumerations before drivers for |
| 66 // a new device have been completely loaded. |
| 67 void RefreshDevices(const std::string& new_device_path); |
| 68 |
| 69 static void RefreshDevicesOnBlockingThread( |
| 70 base::WeakPtr<UsbServiceImpl> usb_service, |
| 71 const std::string& new_device_path, |
| 72 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 73 scoped_refptr<UsbContext> usb_context, |
| 74 const std::set<PlatformUsbDevice>& previous_devices); |
| 75 |
| 76 static void AddDeviceOnBlockingThread( |
| 77 base::WeakPtr<UsbServiceImpl> usb_service, |
| 78 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 79 PlatformUsbDevice platform_device); |
| 80 |
| 81 void RefreshDevicesComplete(libusb_device** platform_devices, |
| 82 ssize_t device_count); |
| 83 |
42 // Adds a new UsbDevice to the devices_ map based on the given libusb device. | 84 // Adds a new UsbDevice to the devices_ map based on the given libusb device. |
43 scoped_refptr<UsbDeviceImpl> AddDevice(PlatformUsbDevice platform_device); | 85 void AddDevice(PlatformUsbDevice platform_device, |
| 86 uint16 vendor_id, |
| 87 uint16 product_id, |
| 88 base::string16 manufacturer_string, |
| 89 base::string16 product_string, |
| 90 base::string16 serial_number, |
| 91 std::string device_node); |
| 92 |
| 93 void RemoveDevice(scoped_refptr<UsbDeviceImpl> device); |
44 | 94 |
45 // Handle hotplug events from libusb. | 95 // Handle hotplug events from libusb. |
46 static int LIBUSB_CALL HotplugCallback(libusb_context* context, | 96 static int LIBUSB_CALL HotplugCallback(libusb_context* context, |
47 PlatformUsbDevice device, | 97 PlatformUsbDevice device, |
48 libusb_hotplug_event event, | 98 libusb_hotplug_event event, |
49 void* user_data); | 99 void* user_data); |
50 // These functions release a reference to the provided platform device. | 100 // These functions release a reference to the provided platform device. |
51 void OnDeviceAdded(PlatformUsbDevice platform_device); | 101 void OnPlatformDeviceAdded(PlatformUsbDevice platform_device); |
52 void OnDeviceRemoved(PlatformUsbDevice platform_device); | 102 void OnPlatformDeviceRemoved(PlatformUsbDevice platform_device); |
53 | 103 |
54 scoped_refptr<UsbContext> context_; | 104 scoped_refptr<UsbContext> context_; |
55 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 105 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
56 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 106 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
57 | |
58 #if defined(OS_WIN) | |
59 class UIThreadHelper; | |
60 UIThreadHelper* ui_thread_helper_; | |
61 #endif // OS_WIN | |
62 | 107 |
63 // TODO(reillyg): Figure out a better solution for device IDs. | 108 // TODO(reillyg): Figure out a better solution for device IDs. |
64 uint32 next_unique_id_; | 109 uint32 next_unique_id_ = 0; |
65 | 110 |
66 // When available the device list will be updated when new devices are | 111 // When available the device list will be updated when new devices are |
67 // connected instead of only when a full enumeration is requested. | 112 // connected instead of only when a full enumeration is requested. |
68 // TODO(reillyg): Support this on all platforms. crbug.com/411715 | 113 // TODO(reillyg): Support this on all platforms. crbug.com/411715 |
69 bool hotplug_enabled_; | 114 bool hotplug_enabled_ = false; |
70 libusb_hotplug_callback_handle hotplug_handle_; | 115 libusb_hotplug_callback_handle hotplug_handle_; |
71 | 116 |
| 117 // Enumeration callbacks are queued until an enumeration completes. |
| 118 bool enumeration_ready_ = false; |
| 119 std::vector<GetDevicesCallback> pending_enumerations_; |
| 120 |
72 // The map from unique IDs to UsbDevices. | 121 // The map from unique IDs to UsbDevices. |
73 typedef std::map<uint32, scoped_refptr<UsbDeviceImpl>> DeviceMap; | 122 typedef std::map<uint32, scoped_refptr<UsbDeviceImpl>> DeviceMap; |
74 DeviceMap devices_; | 123 DeviceMap devices_; |
75 | 124 |
76 // The map from PlatformUsbDevices to UsbDevices. | 125 // The map from PlatformUsbDevices to UsbDevices. |
77 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDeviceImpl>> | 126 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDeviceImpl>> |
78 PlatformDeviceMap; | 127 PlatformDeviceMap; |
79 PlatformDeviceMap platform_devices_; | 128 PlatformDeviceMap platform_devices_; |
80 | 129 |
| 130 #if defined(OS_WIN) |
| 131 ScopedObserver<DeviceMonitorWin, DeviceMonitorWin::Observer> device_observer_; |
| 132 #endif // OS_WIN |
| 133 |
81 base::WeakPtrFactory<UsbServiceImpl> weak_factory_; | 134 base::WeakPtrFactory<UsbServiceImpl> weak_factory_; |
82 | 135 |
83 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl); | 136 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl); |
84 }; | 137 }; |
85 | 138 |
86 } // namespace device | 139 } // namespace device |
OLD | NEW |