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