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"
11 #include "base/memory/weak_ptr.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
:
36 public DeviceMonitorWin::Observer
,
40 explicit UsbServiceImpl(
41 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
42 ~UsbServiceImpl() override
;
45 // device::UsbService implementation
46 scoped_refptr
<UsbDevice
> GetDevice(const std::string
& guid
) override
;
47 void GetDevices(const GetDevicesCallback
& callback
) override
;
50 // device::DeviceMonitorWin::Observer implementation
51 void OnDeviceAdded(const GUID
& class_guid
,
52 const std::string
& device_path
) override
;
53 void OnDeviceRemoved(const GUID
& class_guid
,
54 const std::string
& device_path
) override
;
57 // Enumerate USB devices from OS and update devices_ map.
58 void RefreshDevices();
59 void OnDeviceList(libusb_device
** platform_devices
, size_t device_count
);
60 void RefreshDevicesComplete();
62 // Creates a new UsbDevice based on the given libusb device.
63 void EnumerateDevice(PlatformUsbDevice platform_device
,
64 const base::Closure
& refresh_complete
);
66 void AddDevice(const base::Closure
& refresh_complete
,
67 scoped_refptr
<UsbDeviceImpl
> device
);
68 void RemoveDevice(scoped_refptr
<UsbDeviceImpl
> device
);
70 // Handle hotplug events from libusb.
71 static int LIBUSB_CALL
HotplugCallback(libusb_context
* context
,
72 PlatformUsbDevice device
,
73 libusb_hotplug_event event
,
75 // These functions release a reference to the provided platform device.
76 void OnPlatformDeviceAdded(PlatformUsbDevice platform_device
);
77 void OnPlatformDeviceRemoved(PlatformUsbDevice platform_device
);
79 scoped_refptr
<UsbContext
> context_
;
80 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
81 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
83 // When available the device list will be updated when new devices are
84 // connected instead of only when a full enumeration is requested.
85 // TODO(reillyg): Support this on all platforms. crbug.com/411715
86 bool hotplug_enabled_
= false;
87 libusb_hotplug_callback_handle hotplug_handle_
;
89 // Enumeration callbacks are queued until an enumeration completes.
90 bool enumeration_ready_
= false;
91 bool enumeration_in_progress_
= false;
92 std::queue
<std::string
> pending_path_enumerations_
;
93 std::vector
<GetDevicesCallback
> pending_enumeration_callbacks_
;
95 // The map from unique IDs to UsbDevices.
96 typedef std::map
<std::string
, scoped_refptr
<UsbDeviceImpl
>> DeviceMap
;
99 // The map from PlatformUsbDevices to UsbDevices.
100 typedef std::map
<PlatformUsbDevice
, scoped_refptr
<UsbDeviceImpl
>>
102 PlatformDeviceMap platform_devices_
;
104 // Tracks PlatformUsbDevices that might be removed while they are being
106 std::set
<PlatformUsbDevice
> devices_being_enumerated_
;
109 ScopedObserver
<DeviceMonitorWin
, DeviceMonitorWin::Observer
> device_observer_
;
112 base::WeakPtrFactory
<UsbServiceImpl
> weak_factory_
;
114 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl
);
117 } // namespace device