Roll src/third_party/skia 57a48a7:de7665a
[chromium-blink-merge.git] / device / usb / usb_device_handle_impl.h
blob688f8b9cf8db090b15be22aae83bfb32d293bb6c
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/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/threading/thread_checker.h"
17 #include "device/usb/usb_device_handle.h"
18 #include "net/base/io_buffer.h"
19 #include "third_party/libusb/src/libusb/libusb.h"
21 namespace base {
22 class SingleThreadTaskRunner;
25 namespace device {
27 class UsbContext;
28 struct UsbConfigDescriptor;
29 class UsbDeviceImpl;
31 typedef libusb_device_handle* PlatformUsbDeviceHandle;
32 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
33 typedef libusb_transfer* PlatformUsbTransferHandle;
35 // UsbDeviceHandle class provides basic I/O related functionalities.
36 class UsbDeviceHandleImpl : public UsbDeviceHandle {
37 public:
38 scoped_refptr<UsbDevice> GetDevice() const override;
39 void Close() override;
40 bool SetConfiguration(int configuration_value) override;
41 bool ClaimInterface(int interface_number) override;
42 bool ReleaseInterface(int interface_number) override;
43 bool SetInterfaceAlternateSetting(int interface_number,
44 int alternate_setting) override;
45 bool ResetDevice() override;
46 bool GetStringDescriptor(uint8 string_id, base::string16* string) override;
48 void ControlTransfer(UsbEndpointDirection direction,
49 TransferRequestType request_type,
50 TransferRecipient recipient,
51 uint8 request,
52 uint16 value,
53 uint16 index,
54 net::IOBuffer* buffer,
55 size_t length,
56 unsigned int timeout,
57 const UsbTransferCallback& callback) override;
59 void BulkTransfer(UsbEndpointDirection direction,
60 uint8 endpoint,
61 net::IOBuffer* buffer,
62 size_t length,
63 unsigned int timeout,
64 const UsbTransferCallback& callback) override;
66 void InterruptTransfer(UsbEndpointDirection direction,
67 uint8 endpoint,
68 net::IOBuffer* buffer,
69 size_t length,
70 unsigned int timeout,
71 const UsbTransferCallback& callback) override;
73 void IsochronousTransfer(UsbEndpointDirection direction,
74 uint8 endpoint,
75 net::IOBuffer* buffer,
76 size_t length,
77 unsigned int packets,
78 unsigned int packet_length,
79 unsigned int timeout,
80 const UsbTransferCallback& callback) override;
82 PlatformUsbDeviceHandle handle() const { return handle_; }
84 protected:
85 friend class UsbDeviceImpl;
87 // This constructor is called by UsbDevice.
88 UsbDeviceHandleImpl(scoped_refptr<UsbContext> context,
89 scoped_refptr<UsbDeviceImpl> device,
90 PlatformUsbDeviceHandle handle);
92 ~UsbDeviceHandleImpl() override;
94 private:
95 friend class Transfer;
97 class InterfaceClaimer;
98 class Transfer;
100 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
101 // SetInterfaceAlternateSetting.
102 void RefreshEndpointMap();
104 // Look up the claimed interface by endpoint. Return NULL if the interface
105 // of the endpoint is not found.
106 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
107 unsigned char endpoint);
109 // If the device's task runner is on the current thread then the transfer will
110 // be submitted directly, otherwise a task to do so it posted. The callback
111 // will be called on the current message loop of the thread where this
112 // function was called.
113 void PostOrSubmitTransfer(scoped_ptr<Transfer> transfer);
115 // Submits a transfer and starts tracking it. Retains the buffer and copies
116 // the completion callback until the transfer finishes, whereupon it invokes
117 // the callback then releases the buffer.
118 void SubmitTransfer(scoped_ptr<Transfer> transfer);
120 // Invokes the callbacks associated with a given transfer, and removes it from
121 // the in-flight transfer set.
122 void CompleteTransfer(scoped_ptr<Transfer> transfer);
124 bool GetSupportedLanguages();
126 // Informs the object to drop internal references.
127 void InternalClose();
129 scoped_refptr<UsbDeviceImpl> device_;
131 PlatformUsbDeviceHandle handle_;
133 std::vector<uint16> languages_;
134 std::map<uint8, base::string16> strings_;
136 typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap;
137 ClaimedInterfaceMap claimed_interfaces_;
139 // This set holds weak pointers to pending transfers.
140 std::set<Transfer*> transfers_;
142 // A map from endpoints to interfaces
143 typedef std::map<int, int> EndpointMap;
144 EndpointMap endpoint_map_;
146 // Retain the UsbContext so that the platform context will not be destroyed
147 // before this handle.
148 scoped_refptr<UsbContext> context_;
150 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
152 base::ThreadChecker thread_checker_;
153 base::WeakPtrFactory<UsbDeviceHandleImpl> weak_factory_;
155 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl);
158 } // namespace device
160 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_