Process Alt-Svc headers.
[chromium-blink-merge.git] / device / usb / usb_device_handle_impl.h
blobf8510bc35676b635b34e9e25e6bb48ab9ffe1893
1 // Copyright 2014 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 #ifndef DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_
6 #define DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_
8 #include <map>
9 #include <set>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/threading/thread_checker.h"
15 #include "device/usb/usb_device_handle.h"
16 #include "third_party/libusb/src/libusb/libusb.h"
18 namespace base {
19 class SequencedTaskRunner;
20 class SingleThreadTaskRunner;
21 class TaskRunner;
24 namespace net {
25 class IOBuffer;
28 namespace device {
30 class UsbContext;
31 struct UsbConfigDescriptor;
32 class UsbDeviceImpl;
34 typedef libusb_device_handle* PlatformUsbDeviceHandle;
35 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
36 typedef libusb_transfer* PlatformUsbTransferHandle;
38 // UsbDeviceHandle class provides basic I/O related functionalities.
39 class UsbDeviceHandleImpl : public UsbDeviceHandle {
40 public:
41 scoped_refptr<UsbDevice> GetDevice() const override;
42 void Close() override;
43 void SetConfiguration(int configuration_value,
44 const ResultCallback& callback) override;
45 void ClaimInterface(int interface_number,
46 const ResultCallback& callback) override;
47 bool ReleaseInterface(int interface_number) override;
48 void SetInterfaceAlternateSetting(int interface_number,
49 int alternate_setting,
50 const ResultCallback& callback) override;
51 void ResetDevice(const ResultCallback& callback) override;
53 void ControlTransfer(UsbEndpointDirection direction,
54 TransferRequestType request_type,
55 TransferRecipient recipient,
56 uint8 request,
57 uint16 value,
58 uint16 index,
59 scoped_refptr<net::IOBuffer> buffer,
60 size_t length,
61 unsigned int timeout,
62 const TransferCallback& callback) override;
64 void BulkTransfer(UsbEndpointDirection direction,
65 uint8 endpoint,
66 scoped_refptr<net::IOBuffer> buffer,
67 size_t length,
68 unsigned int timeout,
69 const TransferCallback& callback) override;
71 void InterruptTransfer(UsbEndpointDirection direction,
72 uint8 endpoint,
73 scoped_refptr<net::IOBuffer> buffer,
74 size_t length,
75 unsigned int timeout,
76 const TransferCallback& callback) override;
78 void IsochronousTransfer(UsbEndpointDirection direction,
79 uint8 endpoint,
80 scoped_refptr<net::IOBuffer> buffer,
81 size_t length,
82 unsigned int packets,
83 unsigned int packet_length,
84 unsigned int timeout,
85 const TransferCallback& callback) override;
87 protected:
88 friend class UsbDeviceImpl;
90 // This constructor is called by UsbDeviceImpl.
91 UsbDeviceHandleImpl(
92 scoped_refptr<UsbContext> context,
93 scoped_refptr<UsbDeviceImpl> device,
94 PlatformUsbDeviceHandle handle,
95 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
97 ~UsbDeviceHandleImpl() override;
99 PlatformUsbDeviceHandle handle() const { return handle_; }
101 private:
102 class InterfaceClaimer;
103 class Transfer;
105 void SetConfigurationOnBlockingThread(int configuration_value,
106 const ResultCallback& callback);
107 void SetConfigurationComplete(bool success, const ResultCallback& callback);
108 void ClaimInterfaceOnBlockingThread(int interface_number,
109 const ResultCallback& callback);
110 void ClaimInterfaceComplete(int interface_number,
111 bool success,
112 const ResultCallback& callback);
113 void SetInterfaceAlternateSettingOnBlockingThread(
114 int interface_number,
115 int alternate_setting,
116 const ResultCallback& callback);
117 void SetInterfaceAlternateSettingComplete(int interface_number,
118 int alternate_setting,
119 bool success,
120 const ResultCallback& callback);
121 void ResetDeviceOnBlockingThread(const ResultCallback& callback);
123 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
124 // SetInterfaceAlternateSetting.
125 void RefreshEndpointMap();
127 // Look up the claimed interface by endpoint. Return NULL if the interface
128 // of the endpoint is not found.
129 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
130 unsigned char endpoint);
132 void ControlTransferInternal(
133 UsbEndpointDirection direction,
134 TransferRequestType request_type,
135 TransferRecipient recipient,
136 uint8 request,
137 uint16 value,
138 uint16 index,
139 scoped_refptr<net::IOBuffer> buffer,
140 size_t length,
141 unsigned int timeout,
142 scoped_refptr<base::TaskRunner> callback_task_runner,
143 const TransferCallback& callback);
145 void BulkTransferInternal(
146 UsbEndpointDirection direction,
147 uint8 endpoint,
148 scoped_refptr<net::IOBuffer> buffer,
149 size_t length,
150 unsigned int timeout,
151 scoped_refptr<base::TaskRunner> callback_task_runner,
152 const TransferCallback& callback);
154 void InterruptTransferInternal(
155 UsbEndpointDirection direction,
156 uint8 endpoint,
157 scoped_refptr<net::IOBuffer> buffer,
158 size_t length,
159 unsigned int timeout,
160 scoped_refptr<base::TaskRunner> callback_task_runner,
161 const TransferCallback& callback);
163 void IsochronousTransferInternal(
164 UsbEndpointDirection direction,
165 uint8 endpoint,
166 scoped_refptr<net::IOBuffer> buffer,
167 size_t length,
168 unsigned int packets,
169 unsigned int packet_length,
170 unsigned int timeout,
171 scoped_refptr<base::TaskRunner> callback_task_runner,
172 const TransferCallback& callback);
174 // Submits a transfer and starts tracking it. Retains the buffer and copies
175 // the completion callback until the transfer finishes, whereupon it invokes
176 // the callback then releases the buffer.
177 void SubmitTransfer(scoped_ptr<Transfer> transfer);
179 // Removes the transfer from the in-flight transfer set and invokes the
180 // completion callback.
181 void TransferComplete(Transfer* transfer, const base::Closure& callback);
183 // Informs the object to drop internal references.
184 void InternalClose();
186 scoped_refptr<UsbDeviceImpl> device_;
188 PlatformUsbDeviceHandle handle_;
190 typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap;
191 ClaimedInterfaceMap claimed_interfaces_;
193 // This set holds weak pointers to pending transfers.
194 std::set<Transfer*> transfers_;
196 // A map from endpoints to interfaces
197 typedef std::map<int, int> EndpointMap;
198 EndpointMap endpoint_map_;
200 // Retain the UsbContext so that the platform context will not be destroyed
201 // before this handle.
202 scoped_refptr<UsbContext> context_;
204 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
205 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
207 base::ThreadChecker thread_checker_;
209 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl);
212 } // namespace device
214 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_