Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / device / usb / usb_service_impl.h
blob3362e7796770e798796b4ac0e0b7ba7d84e57474
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 <queue>
9 #include <set>
11 #include "base/memory/weak_ptr.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 :
35 #if defined(OS_WIN)
36 public DeviceMonitorWin::Observer,
37 #endif // OS_WIN
38 public UsbService {
39 public:
40 explicit UsbServiceImpl(
41 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
42 ~UsbServiceImpl() override;
44 private:
45 // device::UsbService implementation
46 scoped_refptr<UsbDevice> GetDevice(const std::string& guid) override;
47 void GetDevices(const GetDevicesCallback& callback) override;
49 #if defined(OS_WIN)
50 // device::DeviceMonitorWin::Observer implementation
51 void OnDeviceAdded(const GUID& class_guid,
52 const std::string& device_path) override;
53 void OnDeviceRemoved(const GUID& class_guid,
54 const std::string& device_path) override;
55 #endif // OS_WIN
57 // Enumerate USB devices from OS and update devices_ map.
58 void RefreshDevices();
59 void OnDeviceList(libusb_device** platform_devices, size_t device_count);
60 void RefreshDevicesComplete();
62 // Creates a new UsbDevice based on the given libusb device.
63 void EnumerateDevice(PlatformUsbDevice platform_device,
64 const base::Closure& refresh_complete);
66 void AddDevice(const base::Closure& refresh_complete,
67 scoped_refptr<UsbDeviceImpl> device);
68 void RemoveDevice(scoped_refptr<UsbDeviceImpl> device);
70 // Handle hotplug events from libusb.
71 static int LIBUSB_CALL HotplugCallback(libusb_context* context,
72 PlatformUsbDevice device,
73 libusb_hotplug_event event,
74 void* user_data);
75 // These functions release a reference to the provided platform device.
76 void OnPlatformDeviceAdded(PlatformUsbDevice platform_device);
77 void OnPlatformDeviceRemoved(PlatformUsbDevice platform_device);
79 scoped_refptr<UsbContext> context_;
80 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
81 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
83 // When available the device list will be updated when new devices are
84 // connected instead of only when a full enumeration is requested.
85 // TODO(reillyg): Support this on all platforms. crbug.com/411715
86 bool hotplug_enabled_ = false;
87 libusb_hotplug_callback_handle hotplug_handle_;
89 // Enumeration callbacks are queued until an enumeration completes.
90 bool enumeration_ready_ = false;
91 bool enumeration_in_progress_ = false;
92 std::queue<std::string> pending_path_enumerations_;
93 std::vector<GetDevicesCallback> pending_enumeration_callbacks_;
95 // The map from unique IDs to UsbDevices.
96 typedef std::map<std::string, scoped_refptr<UsbDeviceImpl>> DeviceMap;
97 DeviceMap devices_;
99 // The map from PlatformUsbDevices to UsbDevices.
100 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDeviceImpl>>
101 PlatformDeviceMap;
102 PlatformDeviceMap platform_devices_;
104 // Tracks PlatformUsbDevices that might be removed while they are being
105 // enumerated.
106 std::set<PlatformUsbDevice> devices_being_enumerated_;
108 #if defined(OS_WIN)
109 ScopedObserver<DeviceMonitorWin, DeviceMonitorWin::Observer> device_observer_;
110 #endif // OS_WIN
112 base::WeakPtrFactory<UsbServiceImpl> weak_factory_;
114 DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl);
117 } // namespace device