Suppress -Woverloaded-virtual for mesa's glsl library.
[chromium-blink-merge.git] / device / usb / usb_service_impl.h
blob7e5a06c7b5af19437347f5dd00dd6a99d204c310
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"
7 #include <map>
8 #include <set>
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"
16 #if defined(OS_WIN)
17 #include "base/scoped_observer.h"
18 #include "device/core/device_monitor_win.h"
19 #endif // OS_WIN
21 struct libusb_device;
22 struct libusb_context;
24 namespace base {
25 class SequencedTaskRunner;
26 class SingleThreadTaskRunner;
29 namespace device {
31 typedef struct libusb_device* PlatformUsbDevice;
32 typedef struct libusb_context* PlatformUsbContext;
34 class UsbServiceImpl : public UsbService,
35 #if defined(OS_WIN)
36 public DeviceMonitorWin::Observer,
37 #endif // OS_WIN
38 public base::MessageLoop::DestructionObserver {
39 public:
40 explicit UsbServiceImpl(
41 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
43 private:
44 ~UsbServiceImpl() override;
46 // device::UsbService implementation
47 scoped_refptr<UsbDevice> GetDevice(const std::string& guid) override;
48 void GetDevices(const GetDevicesCallback& callback) override;
50 #if defined(OS_WIN)
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;
56 #endif // OS_WIN
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,
78 void* user_data);
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;
101 DeviceMap devices_;
103 // The map from PlatformUsbDevices to UsbDevices.
104 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDeviceImpl>>
105 PlatformDeviceMap;
106 PlatformDeviceMap platform_devices_;
108 // Tracks PlatformUsbDevices that might be removed while they are being
109 // enumerated.
110 std::set<PlatformUsbDevice> devices_being_enumerated_;
112 #if defined(OS_WIN)
113 ScopedObserver<DeviceMonitorWin, DeviceMonitorWin::Observer> device_observer_;
114 #endif // OS_WIN
116 base::WeakPtrFactory<UsbServiceImpl> weak_factory_;
118 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl);
121 } // namespace device