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
> GetDevice(const std::string
& guid
) 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.
65 void RefreshDevices();
66 void OnDeviceList(libusb_device
** platform_devices
, size_t device_count
);
67 void RefreshDevicesComplete();
69 // Creates a new UsbDevice based on the given libusb device.
70 void EnumerateDevice(PlatformUsbDevice platform_device
,
71 const base::Closure
& refresh_complete
);
73 void AddDevice(const base::Closure
& refresh_complete
,
74 scoped_refptr
<UsbDeviceImpl
> device
);
75 void RemoveDevice(scoped_refptr
<UsbDeviceImpl
> device
);
77 // Handle hotplug events from libusb.
78 static int LIBUSB_CALL
HotplugCallback(libusb_context
* context
,
79 PlatformUsbDevice device
,
80 libusb_hotplug_event event
,
82 // These functions release a reference to the provided platform device.
83 void OnPlatformDeviceAdded(PlatformUsbDevice platform_device
);
84 void OnPlatformDeviceRemoved(PlatformUsbDevice platform_device
);
86 scoped_refptr
<UsbContext
> context_
;
87 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
88 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
90 // When available the device list will be updated when new devices are
91 // connected instead of only when a full enumeration is requested.
92 // TODO(reillyg): Support this on all platforms. crbug.com/411715
93 bool hotplug_enabled_
= false;
94 libusb_hotplug_callback_handle hotplug_handle_
;
96 // Enumeration callbacks are queued until an enumeration completes.
97 bool enumeration_ready_
= false;
98 bool enumeration_in_progress_
= false;
99 std::queue
<std::string
> pending_path_enumerations_
;
100 std::vector
<GetDevicesCallback
> pending_enumeration_callbacks_
;
102 // The map from unique IDs to UsbDevices.
103 typedef std::map
<std::string
, scoped_refptr
<UsbDeviceImpl
>> DeviceMap
;
106 // The map from PlatformUsbDevices to UsbDevices.
107 typedef std::map
<PlatformUsbDevice
, scoped_refptr
<UsbDeviceImpl
>>
109 PlatformDeviceMap platform_devices_
;
111 // Tracks PlatformUsbDevices that might be removed while they are being
113 std::set
<PlatformUsbDevice
> devices_being_enumerated_
;
116 ScopedObserver
<DeviceMonitorWin
, DeviceMonitorWin::Observer
> device_observer_
;
119 base::WeakPtrFactory
<UsbServiceImpl
> weak_factory_
;
121 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl
);
124 } // namespace device