Update V8 to version 4.6.55.
[chromium-blink-merge.git] / device / usb / usb_device_handle_impl.h
blob6e88fc910a36a411d46a5a7204ed093c8bccb1cd
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;
52 void ClearHalt(uint8 endpoint, const ResultCallback& callback) override;
54 void ControlTransfer(UsbEndpointDirection direction,
55 TransferRequestType request_type,
56 TransferRecipient recipient,
57 uint8 request,
58 uint16 value,
59 uint16 index,
60 scoped_refptr<net::IOBuffer> buffer,
61 size_t length,
62 unsigned int timeout,
63 const TransferCallback& callback) override;
65 void BulkTransfer(UsbEndpointDirection direction,
66 uint8 endpoint,
67 scoped_refptr<net::IOBuffer> buffer,
68 size_t length,
69 unsigned int timeout,
70 const TransferCallback& callback) override;
72 void InterruptTransfer(UsbEndpointDirection direction,
73 uint8 endpoint,
74 scoped_refptr<net::IOBuffer> buffer,
75 size_t length,
76 unsigned int timeout,
77 const TransferCallback& callback) override;
79 void IsochronousTransfer(UsbEndpointDirection direction,
80 uint8 endpoint,
81 scoped_refptr<net::IOBuffer> buffer,
82 size_t length,
83 unsigned int packets,
84 unsigned int packet_length,
85 unsigned int timeout,
86 const TransferCallback& callback) override;
88 protected:
89 friend class UsbDeviceImpl;
91 // This constructor is called by UsbDeviceImpl.
92 UsbDeviceHandleImpl(
93 scoped_refptr<UsbContext> context,
94 scoped_refptr<UsbDeviceImpl> device,
95 PlatformUsbDeviceHandle handle,
96 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
98 ~UsbDeviceHandleImpl() override;
100 PlatformUsbDeviceHandle handle() const { return handle_; }
102 private:
103 class InterfaceClaimer;
104 class Transfer;
106 void SetConfigurationOnBlockingThread(int configuration_value,
107 const ResultCallback& callback);
108 void SetConfigurationComplete(bool success, const ResultCallback& callback);
109 void ClaimInterfaceOnBlockingThread(int interface_number,
110 const ResultCallback& callback);
111 void ClaimInterfaceComplete(int interface_number,
112 bool success,
113 const ResultCallback& callback);
114 void SetInterfaceAlternateSettingOnBlockingThread(
115 int interface_number,
116 int alternate_setting,
117 const ResultCallback& callback);
118 void SetInterfaceAlternateSettingComplete(int interface_number,
119 int alternate_setting,
120 bool success,
121 const ResultCallback& callback);
122 void ResetDeviceOnBlockingThread(const ResultCallback& callback);
123 void ClearHaltOnBlockingThread(uint8 endpoint,
124 const ResultCallback& callback);
126 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
127 // SetInterfaceAlternateSetting.
128 void RefreshEndpointMap();
130 // Look up the claimed interface by endpoint. Return NULL if the interface
131 // of the endpoint is not found.
132 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
133 uint8 endpoint);
135 void ControlTransferInternal(
136 UsbEndpointDirection direction,
137 TransferRequestType request_type,
138 TransferRecipient recipient,
139 uint8 request,
140 uint16 value,
141 uint16 index,
142 scoped_refptr<net::IOBuffer> buffer,
143 size_t length,
144 unsigned int timeout,
145 scoped_refptr<base::TaskRunner> callback_task_runner,
146 const TransferCallback& callback);
148 void BulkTransferInternal(
149 UsbEndpointDirection direction,
150 uint8 endpoint,
151 scoped_refptr<net::IOBuffer> buffer,
152 size_t length,
153 unsigned int timeout,
154 scoped_refptr<base::TaskRunner> callback_task_runner,
155 const TransferCallback& callback);
157 void InterruptTransferInternal(
158 UsbEndpointDirection direction,
159 uint8 endpoint,
160 scoped_refptr<net::IOBuffer> buffer,
161 size_t length,
162 unsigned int timeout,
163 scoped_refptr<base::TaskRunner> callback_task_runner,
164 const TransferCallback& callback);
166 void IsochronousTransferInternal(
167 UsbEndpointDirection direction,
168 uint8 endpoint,
169 scoped_refptr<net::IOBuffer> buffer,
170 size_t length,
171 unsigned int packets,
172 unsigned int packet_length,
173 unsigned int timeout,
174 scoped_refptr<base::TaskRunner> callback_task_runner,
175 const TransferCallback& callback);
177 // Submits a transfer and starts tracking it. Retains the buffer and copies
178 // the completion callback until the transfer finishes, whereupon it invokes
179 // the callback then releases the buffer.
180 void SubmitTransfer(scoped_ptr<Transfer> transfer);
182 // Removes the transfer from the in-flight transfer set and invokes the
183 // completion callback.
184 void TransferComplete(Transfer* transfer, const base::Closure& callback);
186 // Informs the object to drop internal references.
187 void InternalClose();
189 scoped_refptr<UsbDeviceImpl> device_;
191 PlatformUsbDeviceHandle handle_;
193 typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap;
194 ClaimedInterfaceMap claimed_interfaces_;
196 // This set holds weak pointers to pending transfers.
197 std::set<Transfer*> transfers_;
199 // A map from endpoints to interfaces
200 typedef std::map<int, int> EndpointMap;
201 EndpointMap endpoint_map_;
203 // Retain the UsbContext so that the platform context will not be destroyed
204 // before this handle.
205 scoped_refptr<UsbContext> context_;
207 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
208 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
210 base::ThreadChecker thread_checker_;
212 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl);
215 } // namespace device
217 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_