Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / device / usb / usb_device_handle_impl.h
blob1bea4fb8fbe6f1d7ea84345e85cdfe7d09df831f
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 struct EndpointMapValue {
31 int interface_number;
32 UsbTransferType transfer_type;
35 class UsbContext;
36 struct UsbConfigDescriptor;
37 class UsbDeviceImpl;
39 typedef libusb_device_handle* PlatformUsbDeviceHandle;
40 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
41 typedef libusb_transfer* PlatformUsbTransferHandle;
43 // UsbDeviceHandle class provides basic I/O related functionalities.
44 class UsbDeviceHandleImpl : public UsbDeviceHandle {
45 public:
46 scoped_refptr<UsbDevice> GetDevice() const override;
47 void Close() override;
48 void SetConfiguration(int configuration_value,
49 const ResultCallback& callback) override;
50 void ClaimInterface(int interface_number,
51 const ResultCallback& callback) override;
52 bool ReleaseInterface(int interface_number) override;
53 void SetInterfaceAlternateSetting(int interface_number,
54 int alternate_setting,
55 const ResultCallback& callback) override;
56 void ResetDevice(const ResultCallback& callback) override;
57 void ClearHalt(uint8 endpoint, const ResultCallback& callback) override;
59 void ControlTransfer(UsbEndpointDirection direction,
60 TransferRequestType request_type,
61 TransferRecipient recipient,
62 uint8 request,
63 uint16 value,
64 uint16 index,
65 scoped_refptr<net::IOBuffer> buffer,
66 size_t length,
67 unsigned int timeout,
68 const TransferCallback& callback) override;
70 void IsochronousTransfer(UsbEndpointDirection direction,
71 uint8 endpoint,
72 scoped_refptr<net::IOBuffer> buffer,
73 size_t length,
74 unsigned int packets,
75 unsigned int packet_length,
76 unsigned int timeout,
77 const TransferCallback& callback) override;
79 void GenericTransfer(UsbEndpointDirection direction,
80 uint8 endpoint,
81 scoped_refptr<net::IOBuffer> buffer,
82 size_t length,
83 unsigned int timeout,
84 const TransferCallback& callback) override;
86 protected:
87 friend class UsbDeviceImpl;
89 // This constructor is called by UsbDeviceImpl.
90 UsbDeviceHandleImpl(
91 scoped_refptr<UsbContext> context,
92 scoped_refptr<UsbDeviceImpl> device,
93 PlatformUsbDeviceHandle handle,
94 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
96 ~UsbDeviceHandleImpl() override;
98 PlatformUsbDeviceHandle handle() const { return handle_; }
100 private:
101 class InterfaceClaimer;
102 class Transfer;
104 void SetConfigurationOnBlockingThread(int configuration_value,
105 const ResultCallback& callback);
106 void SetConfigurationComplete(bool success, const ResultCallback& callback);
107 void ClaimInterfaceOnBlockingThread(int interface_number,
108 const ResultCallback& callback);
109 void ClaimInterfaceComplete(int interface_number,
110 bool success,
111 const ResultCallback& callback);
112 void SetInterfaceAlternateSettingOnBlockingThread(
113 int interface_number,
114 int alternate_setting,
115 const ResultCallback& callback);
116 void SetInterfaceAlternateSettingComplete(int interface_number,
117 int alternate_setting,
118 bool success,
119 const ResultCallback& callback);
120 void ResetDeviceOnBlockingThread(const ResultCallback& callback);
121 void ClearHaltOnBlockingThread(uint8 endpoint,
122 const ResultCallback& callback);
124 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
125 // SetInterfaceAlternateSetting.
126 void RefreshEndpointMap();
128 // Look up the claimed interface by endpoint. Return NULL if the interface
129 // of the endpoint is not found.
130 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
131 uint8 endpoint);
133 void ControlTransferInternal(
134 UsbEndpointDirection direction,
135 TransferRequestType request_type,
136 TransferRecipient recipient,
137 uint8 request,
138 uint16 value,
139 uint16 index,
140 scoped_refptr<net::IOBuffer> buffer,
141 size_t length,
142 unsigned int timeout,
143 scoped_refptr<base::TaskRunner> callback_task_runner,
144 const TransferCallback& callback);
146 void BulkTransferInternal(
147 UsbEndpointDirection direction,
148 uint8 endpoint,
149 scoped_refptr<net::IOBuffer> buffer,
150 size_t length,
151 unsigned int timeout,
152 scoped_refptr<base::TaskRunner> callback_task_runner,
153 const TransferCallback& callback);
155 void InterruptTransferInternal(
156 UsbEndpointDirection direction,
157 uint8 endpoint,
158 scoped_refptr<net::IOBuffer> buffer,
159 size_t length,
160 unsigned int timeout,
161 scoped_refptr<base::TaskRunner> callback_task_runner,
162 const TransferCallback& callback);
164 void IsochronousTransferInternal(
165 UsbEndpointDirection direction,
166 uint8 endpoint,
167 scoped_refptr<net::IOBuffer> buffer,
168 size_t length,
169 unsigned int packets,
170 unsigned int packet_length,
171 unsigned int timeout,
172 scoped_refptr<base::TaskRunner> callback_task_runner,
173 const TransferCallback& callback);
175 void GenericTransferInternal(
176 UsbEndpointDirection direction,
177 uint8 endpoint,
178 scoped_refptr<net::IOBuffer> buffer,
179 size_t length,
180 unsigned int timeout,
181 scoped_refptr<base::TaskRunner> callback_task_runner,
182 const TransferCallback& callback);
184 // Submits a transfer and starts tracking it. Retains the buffer and copies
185 // the completion callback until the transfer finishes, whereupon it invokes
186 // the callback then releases the buffer.
187 void SubmitTransfer(scoped_ptr<Transfer> transfer);
189 // Removes the transfer from the in-flight transfer set and invokes the
190 // completion callback.
191 void TransferComplete(Transfer* transfer, const base::Closure& callback);
193 // Informs the object to drop internal references.
194 void InternalClose();
196 scoped_refptr<UsbDeviceImpl> device_;
198 PlatformUsbDeviceHandle handle_;
200 typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap;
201 ClaimedInterfaceMap claimed_interfaces_;
203 // This set holds weak pointers to pending transfers.
204 std::set<Transfer*> transfers_;
206 // A map from endpoints to EndpointMapValue
207 typedef std::map<int, EndpointMapValue> EndpointMap;
208 EndpointMap endpoint_map_;
210 // Retain the UsbContext so that the platform context will not be destroyed
211 // before this handle.
212 scoped_refptr<UsbContext> context_;
214 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
215 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
217 base::ThreadChecker thread_checker_;
219 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl);
222 } // namespace device
224 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_