#ifndef SERVICES_DEVICE_USB_USB_SERVICE_IMPL_H_
#define SERVICES_DEVICE_USB_USB_SERVICE_IMPL_H_
#include <stddef.h>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <vector>
#include "base/containers/queue.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "build/build_config.h"
#include "services/device/usb/scoped_libusb_device_ref.h"
#include "services/device/usb/usb_context.h"
#include "services/device/usb/usb_device_impl.h"
#include "services/device/usb/usb_service.h"
#include "third_party/libusb/src/libusb/libusb.h"
struct libusb_context;
namespace device {
typedef struct libusb_context* PlatformUsbContext;
class UsbDeviceImpl;
class UsbServiceImpl final : public UsbService {
public:
UsbServiceImpl();
UsbServiceImpl(const UsbServiceImpl&) = delete;
UsbServiceImpl& operator=(const UsbServiceImpl&) = delete;
~UsbServiceImpl() override;
private:
void GetDevices(GetDevicesCallback callback) override;
void OnUsbContext(scoped_refptr<UsbContext> context);
void RefreshDevices();
void OnDeviceList(
std::optional<std::vector<ScopedLibusbDeviceRef>> platform_devices);
void RefreshDevicesComplete();
void EnumerateDevice(ScopedLibusbDeviceRef platform_device,
base::OnceClosure refresh_complete);
void AddDevice(scoped_refptr<UsbDeviceImpl> device);
void RemoveDevice(scoped_refptr<UsbDeviceImpl> device);
static int LIBUSB_CALL HotplugCallback(libusb_context* context,
libusb_device* device,
libusb_hotplug_event event,
void* user_data);
void OnPlatformDeviceAdded(ScopedLibusbDeviceRef platform_device);
void OnPlatformDeviceRemoved(ScopedLibusbDeviceRef platform_device);
void EnumerationFailed(ScopedLibusbDeviceRef platform_device);
scoped_refptr<base::SequencedTaskRunner> task_runner_;
scoped_refptr<UsbContext> context_;
bool usb_unavailable_ = false;
libusb_hotplug_callback_handle hotplug_handle_;
bool enumeration_ready_ = false;
bool enumeration_in_progress_ = false;
std::vector<GetDevicesCallback> pending_enumeration_callbacks_;
typedef std::map<libusb_device*, scoped_refptr<UsbDeviceImpl>>
PlatformDeviceMap;
PlatformDeviceMap platform_devices_;
std::vector<ScopedLibusbDeviceRef> ignored_devices_;
std::set<libusb_device*> devices_being_enumerated_;
base::WeakPtr<UsbServiceImpl> weak_self_;
base::WeakPtrFactory<UsbServiceImpl> weak_factory_{this};
};
}
#endif