// Copyright 2015 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "device/usb/usb_service.h" #include #include #include #include "base/memory/weak_ptr.h" #include "device/usb/usb_context.h" #include "device/usb/usb_device_impl.h" #include "third_party/libusb/src/libusb/libusb.h" #if defined(OS_WIN) #include "base/scoped_observer.h" #include "device/core/device_monitor_win.h" #endif // OS_WIN struct libusb_device; struct libusb_context; namespace base { class SequencedTaskRunner; class SingleThreadTaskRunner; } namespace device { typedef struct libusb_device* PlatformUsbDevice; typedef struct libusb_context* PlatformUsbContext; class UsbServiceImpl : #if defined(OS_WIN) public DeviceMonitorWin::Observer, #endif // OS_WIN public UsbService { public: explicit UsbServiceImpl( scoped_refptr blocking_task_runner); ~UsbServiceImpl() override; private: // device::UsbService implementation scoped_refptr GetDevice(const std::string& guid) override; void GetDevices(const GetDevicesCallback& callback) override; #if defined(OS_WIN) // device::DeviceMonitorWin::Observer implementation void OnDeviceAdded(const GUID& class_guid, const std::string& device_path) override; void OnDeviceRemoved(const GUID& class_guid, const std::string& device_path) override; #endif // OS_WIN // Enumerate USB devices from OS and update devices_ map. void RefreshDevices(); void OnDeviceList(libusb_device** platform_devices, size_t device_count); void RefreshDevicesComplete(); // Creates a new UsbDevice based on the given libusb device. void EnumerateDevice(PlatformUsbDevice platform_device, const base::Closure& refresh_complete); void AddDevice(const base::Closure& refresh_complete, scoped_refptr device); void RemoveDevice(scoped_refptr device); // Handle hotplug events from libusb. static int LIBUSB_CALL HotplugCallback(libusb_context* context, PlatformUsbDevice device, libusb_hotplug_event event, void* user_data); // These functions release a reference to the provided platform device. void OnPlatformDeviceAdded(PlatformUsbDevice platform_device); void OnPlatformDeviceRemoved(PlatformUsbDevice platform_device); scoped_refptr context_; scoped_refptr task_runner_; scoped_refptr blocking_task_runner_; // When available the device list will be updated when new devices are // connected instead of only when a full enumeration is requested. // TODO(reillyg): Support this on all platforms. crbug.com/411715 bool hotplug_enabled_ = false; libusb_hotplug_callback_handle hotplug_handle_; // Enumeration callbacks are queued until an enumeration completes. bool enumeration_ready_ = false; bool enumeration_in_progress_ = false; std::queue pending_path_enumerations_; std::vector pending_enumeration_callbacks_; // The map from unique IDs to UsbDevices. typedef std::map> DeviceMap; DeviceMap devices_; // The map from PlatformUsbDevices to UsbDevices. typedef std::map> PlatformDeviceMap; PlatformDeviceMap platform_devices_; // Tracks PlatformUsbDevices that might be removed while they are being // enumerated. std::set devices_being_enumerated_; #if defined(OS_WIN) ScopedObserver device_observer_; #endif // OS_WIN base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl); }; } // namespace device