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 DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ | 5 #ifndef DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ |
6 #define DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ | 6 #define DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/memory/ref_counted.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" | 14 #include "base/threading/thread_checker.h" |
17 #include "device/usb/usb_device_handle.h" | 15 #include "device/usb/usb_device_handle.h" |
18 #include "net/base/io_buffer.h" | |
19 #include "third_party/libusb/src/libusb/libusb.h" | 16 #include "third_party/libusb/src/libusb/libusb.h" |
20 | 17 |
21 namespace base { | 18 namespace base { |
22 class SingleThreadTaskRunner; | 19 class SequencedTaskRunner; |
| 20 } |
| 21 |
| 22 namespace net { |
| 23 class IOBuffer; |
23 } | 24 } |
24 | 25 |
25 namespace device { | 26 namespace device { |
26 | 27 |
27 class UsbContext; | 28 class UsbContext; |
28 struct UsbConfigDescriptor; | 29 struct UsbConfigDescriptor; |
29 class UsbDeviceImpl; | 30 class UsbDeviceImpl; |
30 | 31 |
31 typedef libusb_device_handle* PlatformUsbDeviceHandle; | 32 typedef libusb_device_handle* PlatformUsbDeviceHandle; |
32 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; | 33 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; |
33 typedef libusb_transfer* PlatformUsbTransferHandle; | 34 typedef libusb_transfer* PlatformUsbTransferHandle; |
34 | 35 |
35 // UsbDeviceHandle class provides basic I/O related functionalities. | 36 // UsbDeviceHandle class provides basic I/O related functionalities. |
36 class UsbDeviceHandleImpl : public UsbDeviceHandle { | 37 class UsbDeviceHandleImpl : public UsbDeviceHandle { |
37 public: | 38 public: |
38 scoped_refptr<UsbDevice> GetDevice() const override; | 39 scoped_refptr<UsbDevice> GetDevice() const override; |
39 void Close() override; | 40 void Close() override; |
40 bool SetConfiguration(int configuration_value) override; | 41 void SetConfiguration(int configuration_value, |
41 bool ClaimInterface(int interface_number) override; | 42 const ResultCallback& callback) override; |
| 43 void ClaimInterface(int interface_number, |
| 44 const ResultCallback& callback) override; |
42 bool ReleaseInterface(int interface_number) override; | 45 bool ReleaseInterface(int interface_number) override; |
43 bool SetInterfaceAlternateSetting(int interface_number, | 46 void SetInterfaceAlternateSetting(int interface_number, |
44 int alternate_setting) override; | 47 int alternate_setting, |
45 bool ResetDevice() override; | 48 const ResultCallback& callback) override; |
46 bool GetStringDescriptor(uint8 string_id, base::string16* string) override; | 49 void ResetDevice(const ResultCallback& callback) override; |
47 | 50 |
48 void ControlTransfer(UsbEndpointDirection direction, | 51 void ControlTransfer(UsbEndpointDirection direction, |
49 TransferRequestType request_type, | 52 TransferRequestType request_type, |
50 TransferRecipient recipient, | 53 TransferRecipient recipient, |
51 uint8 request, | 54 uint8 request, |
52 uint16 value, | 55 uint16 value, |
53 uint16 index, | 56 uint16 index, |
54 net::IOBuffer* buffer, | 57 net::IOBuffer* buffer, |
55 size_t length, | 58 size_t length, |
56 unsigned int timeout, | 59 unsigned int timeout, |
57 const UsbTransferCallback& callback) override; | 60 const TransferCallback& callback) override; |
58 | 61 |
59 void BulkTransfer(UsbEndpointDirection direction, | 62 void BulkTransfer(UsbEndpointDirection direction, |
60 uint8 endpoint, | 63 uint8 endpoint, |
61 net::IOBuffer* buffer, | 64 net::IOBuffer* buffer, |
62 size_t length, | 65 size_t length, |
63 unsigned int timeout, | 66 unsigned int timeout, |
64 const UsbTransferCallback& callback) override; | 67 const TransferCallback& callback) override; |
65 | 68 |
66 void InterruptTransfer(UsbEndpointDirection direction, | 69 void InterruptTransfer(UsbEndpointDirection direction, |
67 uint8 endpoint, | 70 uint8 endpoint, |
68 net::IOBuffer* buffer, | 71 net::IOBuffer* buffer, |
69 size_t length, | 72 size_t length, |
70 unsigned int timeout, | 73 unsigned int timeout, |
71 const UsbTransferCallback& callback) override; | 74 const TransferCallback& callback) override; |
72 | 75 |
73 void IsochronousTransfer(UsbEndpointDirection direction, | 76 void IsochronousTransfer(UsbEndpointDirection direction, |
74 uint8 endpoint, | 77 uint8 endpoint, |
75 net::IOBuffer* buffer, | 78 net::IOBuffer* buffer, |
76 size_t length, | 79 size_t length, |
77 unsigned int packets, | 80 unsigned int packets, |
78 unsigned int packet_length, | 81 unsigned int packet_length, |
79 unsigned int timeout, | 82 unsigned int timeout, |
80 const UsbTransferCallback& callback) override; | 83 const TransferCallback& callback) override; |
81 | 84 |
82 PlatformUsbDeviceHandle handle() const { return handle_; } | 85 PlatformUsbDeviceHandle handle() const { return handle_; } |
83 | 86 |
84 protected: | 87 protected: |
85 friend class UsbDeviceImpl; | 88 friend class UsbDeviceImpl; |
86 | 89 |
87 // This constructor is called by UsbDevice. | 90 // This constructor is called by UsbDeviceImpl. |
88 UsbDeviceHandleImpl(scoped_refptr<UsbContext> context, | 91 UsbDeviceHandleImpl( |
89 scoped_refptr<UsbDeviceImpl> device, | 92 scoped_refptr<UsbContext> context, |
90 PlatformUsbDeviceHandle handle); | 93 scoped_refptr<UsbDeviceImpl> device, |
| 94 PlatformUsbDeviceHandle handle, |
| 95 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
91 | 96 |
92 ~UsbDeviceHandleImpl() override; | 97 ~UsbDeviceHandleImpl() override; |
93 | 98 |
94 private: | 99 private: |
95 friend class Transfer; | 100 friend class Transfer; |
96 | 101 |
97 class InterfaceClaimer; | 102 class InterfaceClaimer; |
98 class Transfer; | 103 class Transfer; |
99 | 104 |
| 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); |
| 127 |
100 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and | 128 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and |
101 // SetInterfaceAlternateSetting. | 129 // SetInterfaceAlternateSetting. |
102 void RefreshEndpointMap(); | 130 void RefreshEndpointMap(); |
103 | 131 |
104 // Look up the claimed interface by endpoint. Return NULL if the interface | 132 // Look up the claimed interface by endpoint. Return NULL if the interface |
105 // of the endpoint is not found. | 133 // of the endpoint is not found. |
106 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint( | 134 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint( |
107 unsigned char endpoint); | 135 unsigned char endpoint); |
108 | 136 |
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); | |
114 | |
115 // Submits a transfer and starts tracking it. Retains the buffer and copies | 137 // Submits a transfer and starts tracking it. Retains the buffer and copies |
116 // the completion callback until the transfer finishes, whereupon it invokes | 138 // the completion callback until the transfer finishes, whereupon it invokes |
117 // the callback then releases the buffer. | 139 // the callback then releases the buffer. |
118 void SubmitTransfer(scoped_ptr<Transfer> transfer); | 140 void SubmitTransfer(scoped_ptr<Transfer> transfer); |
119 | 141 |
120 // Invokes the callbacks associated with a given transfer, and removes it from | 142 // Removes the transfer from the in-flight transfer set and invokes the |
121 // the in-flight transfer set. | 143 // completion callback. |
122 void CompleteTransfer(scoped_ptr<Transfer> transfer); | 144 void TransferComplete(Transfer* transfer, const base::Closure& callback); |
123 | |
124 bool GetSupportedLanguages(); | |
125 | 145 |
126 // Informs the object to drop internal references. | 146 // Informs the object to drop internal references. |
127 void InternalClose(); | 147 void InternalClose(); |
128 | 148 |
129 scoped_refptr<UsbDeviceImpl> device_; | 149 scoped_refptr<UsbDeviceImpl> device_; |
130 | 150 |
131 PlatformUsbDeviceHandle handle_; | 151 PlatformUsbDeviceHandle handle_; |
132 | 152 |
133 std::vector<uint16> languages_; | |
134 std::map<uint8, base::string16> strings_; | |
135 | |
136 typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap; | 153 typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap; |
137 ClaimedInterfaceMap claimed_interfaces_; | 154 ClaimedInterfaceMap claimed_interfaces_; |
138 | 155 |
139 // This set holds weak pointers to pending transfers. | 156 // This set holds weak pointers to pending transfers. |
140 std::set<Transfer*> transfers_; | 157 std::set<Transfer*> transfers_; |
141 | 158 |
142 // A map from endpoints to interfaces | 159 // A map from endpoints to interfaces |
143 typedef std::map<int, int> EndpointMap; | 160 typedef std::map<int, int> EndpointMap; |
144 EndpointMap endpoint_map_; | 161 EndpointMap endpoint_map_; |
145 | 162 |
146 // Retain the UsbContext so that the platform context will not be destroyed | 163 // Retain the UsbContext so that the platform context will not be destroyed |
147 // before this handle. | 164 // before this handle. |
148 scoped_refptr<UsbContext> context_; | 165 scoped_refptr<UsbContext> context_; |
149 | 166 |
150 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 167 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 168 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
151 | 169 |
152 base::ThreadChecker thread_checker_; | 170 base::ThreadChecker thread_checker_; |
153 base::WeakPtrFactory<UsbDeviceHandleImpl> weak_factory_; | |
154 | 171 |
155 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl); | 172 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl); |
156 }; | 173 }; |
157 | 174 |
158 } // namespace device | 175 } // namespace device |
159 | 176 |
160 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ | 177 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ |
OLD | NEW |