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
3 // found in the LICENSE file.
5 #include "device/usb/usb_service.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "device/usb/usb_context.h"
13 #include "device/usb/usb_device_impl.h"
14 #include "third_party/libusb/src/libusb/libusb.h"
17 #include "base/scoped_observer.h"
18 #include "device/core/device_monitor_win.h"
22 struct libusb_context
;
25 class SequencedTaskRunner
;
26 class SingleThreadTaskRunner
;
31 typedef struct libusb_device
* PlatformUsbDevice
;
32 typedef struct libusb_context
* PlatformUsbContext
;
34 class UsbServiceImpl
: public UsbService
,
36 public DeviceMonitorWin::Observer
,
38 public base::MessageLoop::DestructionObserver
{
40 static UsbService
* Create(
41 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
44 explicit UsbServiceImpl(
45 PlatformUsbContext context
,
46 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
47 ~UsbServiceImpl() override
;
49 // device::UsbService implementation
50 scoped_refptr
<UsbDevice
> GetDeviceById(uint32 unique_id
) override
;
51 void GetDevices(const GetDevicesCallback
& callback
) override
;
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
;
61 // base::MessageLoop::DestructionObserver implementation
62 void WillDestroyCurrentMessageLoop() override
;
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
);
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
);
76 static void AddDeviceOnBlockingThread(
77 base::WeakPtr
<UsbServiceImpl
> usb_service
,
78 scoped_refptr
<base::SequencedTaskRunner
> task_runner
,
79 PlatformUsbDevice platform_device
);
81 void RefreshDevicesComplete(libusb_device
** platform_devices
,
82 ssize_t device_count
);
84 // Adds a new UsbDevice to the devices_ map based on the given libusb device.
85 void AddDevice(PlatformUsbDevice platform_device
,
88 base::string16 manufacturer_string
,
89 base::string16 product_string
,
90 base::string16 serial_number
,
91 std::string device_node
);
93 void RemoveDevice(scoped_refptr
<UsbDeviceImpl
> device
);
95 // Handle hotplug events from libusb.
96 static int LIBUSB_CALL
HotplugCallback(libusb_context
* context
,
97 PlatformUsbDevice device
,
98 libusb_hotplug_event event
,
100 // These functions release a reference to the provided platform device.
101 void OnPlatformDeviceAdded(PlatformUsbDevice platform_device
);
102 void OnPlatformDeviceRemoved(PlatformUsbDevice platform_device
);
104 scoped_refptr
<UsbContext
> context_
;
105 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
106 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
108 // TODO(reillyg): Figure out a better solution for device IDs.
109 uint32 next_unique_id_
= 0;
111 // When available the device list will be updated when new devices are
112 // connected instead of only when a full enumeration is requested.
113 // TODO(reillyg): Support this on all platforms. crbug.com/411715
114 bool hotplug_enabled_
= false;
115 libusb_hotplug_callback_handle hotplug_handle_
;
117 // Enumeration callbacks are queued until an enumeration completes.
118 bool enumeration_ready_
= false;
119 std::vector
<GetDevicesCallback
> pending_enumerations_
;
121 // The map from unique IDs to UsbDevices.
122 typedef std::map
<uint32
, scoped_refptr
<UsbDeviceImpl
>> DeviceMap
;
125 // The map from PlatformUsbDevices to UsbDevices.
126 typedef std::map
<PlatformUsbDevice
, scoped_refptr
<UsbDeviceImpl
>>
128 PlatformDeviceMap platform_devices_
;
131 ScopedObserver
<DeviceMonitorWin
, DeviceMonitorWin::Observer
> device_observer_
;
134 base::WeakPtrFactory
<UsbServiceImpl
> weak_factory_
;
136 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl
);
139 } // namespace device