Patch 3/3 to get WebScheduler via WebThread
[chromium-blink-merge.git] / device / usb / usb_device_handle_impl.h
bloba60f502b7eac7b5fcbab06a60359dedf19f63772
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(PlatformUsbDeviceHandle handle,
106 int configuration_value,
107 const ResultCallback& callback);
108 void SetConfigurationComplete(bool success, const ResultCallback& callback);
109 void ClaimInterfaceOnBlockingThread(PlatformUsbDeviceHandle handle,
110 int interface_number,
111 const ResultCallback& callback);
112 void ClaimInterfaceComplete(int interface_number,
113 bool success,
114 const ResultCallback& callback);
115 void SetInterfaceAlternateSettingOnBlockingThread(
116 PlatformUsbDeviceHandle handle,
117 int interface_number,
118 int alternate_setting,
119 const ResultCallback& callback);
120 void SetInterfaceAlternateSettingComplete(int interface_number,
121 int alternate_setting,
122 bool success,
123 const ResultCallback& callback);
124 void ResetDeviceOnBlockingThread(PlatformUsbDeviceHandle handle,
125 const ResultCallback& callback);
126 void ResetDeviceComplete(bool success, const ResultCallback& callback);
128 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
129 // SetInterfaceAlternateSetting.
130 void RefreshEndpointMap();
132 // Look up the claimed interface by endpoint. Return NULL if the interface
133 // of the endpoint is not found.
134 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
135 unsigned char endpoint);
137 void ControlTransferInternal(
138 UsbEndpointDirection direction,
139 TransferRequestType request_type,
140 TransferRecipient recipient,
141 uint8 request,
142 uint16 value,
143 uint16 index,
144 scoped_refptr<net::IOBuffer> buffer,
145 size_t length,
146 unsigned int timeout,
147 scoped_refptr<base::TaskRunner> callback_task_runner,
148 const TransferCallback& callback);
150 void BulkTransferInternal(
151 UsbEndpointDirection direction,
152 uint8 endpoint,
153 scoped_refptr<net::IOBuffer> buffer,
154 size_t length,
155 unsigned int timeout,
156 scoped_refptr<base::TaskRunner> callback_task_runner,
157 const TransferCallback& callback);
159 void InterruptTransferInternal(
160 UsbEndpointDirection direction,
161 uint8 endpoint,
162 scoped_refptr<net::IOBuffer> buffer,
163 size_t length,
164 unsigned int timeout,
165 scoped_refptr<base::TaskRunner> callback_task_runner,
166 const TransferCallback& callback);
168 void IsochronousTransferInternal(
169 UsbEndpointDirection direction,
170 uint8 endpoint,
171 scoped_refptr<net::IOBuffer> buffer,
172 size_t length,
173 unsigned int packets,
174 unsigned int packet_length,
175 unsigned int timeout,
176 scoped_refptr<base::TaskRunner> callback_task_runner,
177 const TransferCallback& callback);
179 // Submits a transfer and starts tracking it. Retains the buffer and copies
180 // the completion callback until the transfer finishes, whereupon it invokes
181 // the callback then releases the buffer.
182 void SubmitTransfer(scoped_ptr<Transfer> transfer);
184 // Removes the transfer from the in-flight transfer set and invokes the
185 // completion callback.
186 void TransferComplete(Transfer* transfer, const base::Closure& callback);
188 // Informs the object to drop internal references.
189 void InternalClose();
191 scoped_refptr<UsbDeviceImpl> device_;
193 PlatformUsbDeviceHandle handle_;
195 typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap;
196 ClaimedInterfaceMap claimed_interfaces_;
198 // This set holds weak pointers to pending transfers.
199 std::set<Transfer*> transfers_;
201 // A map from endpoints to interfaces
202 typedef std::map<int, int> EndpointMap;
203 EndpointMap endpoint_map_;
205 // Retain the UsbContext so that the platform context will not be destroyed
206 // before this handle.
207 scoped_refptr<UsbContext> context_;
209 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
210 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
212 base::ThreadChecker thread_checker_;
214 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl);
217 } // namespace device
219 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_