OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_ | 5 #ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_ |
6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_ | 6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "components/usb_service/usb_device_handle.h" | 15 #include "components/usb_service/usb_device_handle.h" |
16 #include "components/usb_service/usb_interface.h" | 16 #include "components/usb_service/usb_interface.h" |
17 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
18 | 18 #include "third_party/libusb/src/libusb/libusb.h" |
19 struct libusb_device_handle; | |
20 struct libusb_iso_packet_descriptor; | |
21 struct libusb_transfer; | |
22 | 19 |
23 namespace base { | 20 namespace base { |
24 class MessageLoopProxy; | 21 class SingleThreadTaskRunner; |
25 } | 22 } |
26 | 23 |
27 namespace usb_service { | 24 namespace usb_service { |
28 | 25 |
29 class UsbContext; | 26 class UsbContext; |
30 class UsbConfigDescriptor; | 27 class UsbConfigDescriptor; |
31 class UsbDeviceImpl; | 28 class UsbDeviceImpl; |
32 | 29 |
33 typedef libusb_device_handle* PlatformUsbDeviceHandle; | 30 typedef libusb_device_handle* PlatformUsbDeviceHandle; |
34 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; | 31 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 87 |
91 // This constructor is called by UsbDevice. | 88 // This constructor is called by UsbDevice. |
92 UsbDeviceHandleImpl(scoped_refptr<UsbContext> context, | 89 UsbDeviceHandleImpl(scoped_refptr<UsbContext> context, |
93 UsbDeviceImpl* device, | 90 UsbDeviceImpl* device, |
94 PlatformUsbDeviceHandle handle, | 91 PlatformUsbDeviceHandle handle, |
95 scoped_refptr<UsbConfigDescriptor> interfaces); | 92 scoped_refptr<UsbConfigDescriptor> interfaces); |
96 | 93 |
97 virtual ~UsbDeviceHandleImpl(); | 94 virtual ~UsbDeviceHandleImpl(); |
98 | 95 |
99 private: | 96 private: |
100 friend void HandleTransferCompletion(PlatformUsbTransferHandle handle); | |
101 | |
102 class InterfaceClaimer; | 97 class InterfaceClaimer; |
103 struct Transfer; | 98 struct Transfer; |
104 | 99 |
105 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and | 100 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and |
106 // SetInterfaceAlternateSetting. | 101 // SetInterfaceAlternateSetting. |
107 void RefreshEndpointMap(); | 102 void RefreshEndpointMap(); |
108 | 103 |
109 // Look up the claimed interface by endpoint. Return NULL if the interface | 104 // Look up the claimed interface by endpoint. Return NULL if the interface |
110 // of the endpoint is not found. | 105 // of the endpoint is not found. |
111 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint( | 106 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint( |
112 unsigned char endpoint); | 107 unsigned char endpoint); |
113 | 108 |
| 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(PlatformUsbTransferHandle handle, |
| 114 UsbTransferType transfer_type, |
| 115 net::IOBuffer* buffer, |
| 116 size_t length, |
| 117 const UsbTransferCallback& callback); |
| 118 |
114 // Submits a transfer and starts tracking it. Retains the buffer and copies | 119 // Submits a transfer and starts tracking it. Retains the buffer and copies |
115 // the completion callback until the transfer finishes, whereupon it invokes | 120 // the completion callback until the transfer finishes, whereupon it invokes |
116 // the callback then releases the buffer. | 121 // the callback then releases the buffer. |
117 void SubmitTransfer(PlatformUsbTransferHandle handle, | 122 void SubmitTransfer(PlatformUsbTransferHandle handle, |
118 UsbTransferType transfer_type, | 123 UsbTransferType transfer_type, |
119 net::IOBuffer* buffer, | 124 net::IOBuffer* buffer, |
120 const size_t length, | 125 const size_t length, |
121 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 126 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
122 const UsbTransferCallback& callback); | 127 const UsbTransferCallback& callback); |
123 | 128 |
| 129 static void LIBUSB_CALL |
| 130 PlatformTransferCallback(PlatformUsbTransferHandle handle); |
| 131 |
124 // Invokes the callbacks associated with a given transfer, and removes it from | 132 // Invokes the callbacks associated with a given transfer, and removes it from |
125 // the in-flight transfer set. | 133 // the in-flight transfer set. |
126 void TransferComplete(PlatformUsbTransferHandle transfer); | 134 void CompleteTransfer(PlatformUsbTransferHandle transfer); |
127 | 135 |
128 bool GetSupportedLanguages(); | 136 bool GetSupportedLanguages(); |
129 bool GetStringDescriptor(uint8 string_id, base::string16* string); | 137 bool GetStringDescriptor(uint8 string_id, base::string16* string); |
130 | 138 |
131 // Informs the object to drop internal references. | 139 // Informs the object to drop internal references. |
132 void InternalClose(); | 140 void InternalClose(); |
133 | 141 |
134 UsbDeviceImpl* device_; | 142 UsbDeviceImpl* device_; |
135 | 143 |
136 PlatformUsbDeviceHandle handle_; | 144 PlatformUsbDeviceHandle handle_; |
(...skipping 10 matching lines...) Expand all Loading... |
147 TransferMap transfers_; | 155 TransferMap transfers_; |
148 | 156 |
149 // A map from endpoints to interfaces | 157 // A map from endpoints to interfaces |
150 typedef std::map<int, int> EndpointMap; | 158 typedef std::map<int, int> EndpointMap; |
151 EndpointMap endpoint_map_; | 159 EndpointMap endpoint_map_; |
152 | 160 |
153 // Retain the UsbContext so that the platform context will not be destroyed | 161 // Retain the UsbContext so that the platform context will not be destroyed |
154 // before this handle. | 162 // before this handle. |
155 scoped_refptr<UsbContext> context_; | 163 scoped_refptr<UsbContext> context_; |
156 | 164 |
| 165 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 166 |
157 base::ThreadChecker thread_checker_; | 167 base::ThreadChecker thread_checker_; |
158 | 168 |
159 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl); | 169 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl); |
160 }; | 170 }; |
161 | 171 |
162 } // namespace usb_service | 172 } // namespace usb_service |
163 | 173 |
164 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_ | 174 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_ |
OLD | NEW |