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 UsbSuccessCallback& callback) override; |
| 43 void ClaimInterface(int interface_number, |
| 44 const UsbSuccessCallback& 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( |
44 int alternate_setting) override; | 47 int interface_number, |
45 bool ResetDevice() override; | 48 int alternate_setting, |
46 bool GetStringDescriptor(uint8 string_id, base::string16* string) override; | 49 const UsbSuccessCallback& callback) override; |
| 50 void ResetDevice(const UsbSuccessCallback& callback) override; |
47 | 51 |
48 void ControlTransfer(UsbEndpointDirection direction, | 52 void ControlTransfer(UsbEndpointDirection direction, |
49 TransferRequestType request_type, | 53 TransferRequestType request_type, |
50 TransferRecipient recipient, | 54 TransferRecipient recipient, |
51 uint8 request, | 55 uint8 request, |
52 uint16 value, | 56 uint16 value, |
53 uint16 index, | 57 uint16 index, |
54 net::IOBuffer* buffer, | 58 net::IOBuffer* buffer, |
55 size_t length, | 59 size_t length, |
56 unsigned int timeout, | 60 unsigned int timeout, |
(...skipping 20 matching lines...) Expand all Loading... |
77 unsigned int packets, | 81 unsigned int packets, |
78 unsigned int packet_length, | 82 unsigned int packet_length, |
79 unsigned int timeout, | 83 unsigned int timeout, |
80 const UsbTransferCallback& callback) override; | 84 const UsbTransferCallback& callback) override; |
81 | 85 |
82 PlatformUsbDeviceHandle handle() const { return handle_; } | 86 PlatformUsbDeviceHandle handle() const { return handle_; } |
83 | 87 |
84 protected: | 88 protected: |
85 friend class UsbDeviceImpl; | 89 friend class UsbDeviceImpl; |
86 | 90 |
87 // This constructor is called by UsbDevice. | 91 // This constructor is called by UsbDeviceImpl. |
88 UsbDeviceHandleImpl(scoped_refptr<UsbContext> context, | 92 UsbDeviceHandleImpl( |
89 scoped_refptr<UsbDeviceImpl> device, | 93 scoped_refptr<UsbContext> context, |
90 PlatformUsbDeviceHandle handle); | 94 scoped_refptr<UsbDeviceImpl> device, |
| 95 PlatformUsbDeviceHandle handle, |
| 96 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
91 | 97 |
92 ~UsbDeviceHandleImpl() override; | 98 ~UsbDeviceHandleImpl() override; |
93 | 99 |
94 private: | 100 private: |
95 friend class Transfer; | 101 friend class Transfer; |
96 | 102 |
97 class InterfaceClaimer; | 103 class InterfaceClaimer; |
98 class Transfer; | 104 class Transfer; |
99 | 105 |
| 106 void SetConfigurationOnBlockingThread(PlatformUsbDeviceHandle handle, |
| 107 int configuration_value, |
| 108 const UsbSuccessCallback& callback); |
| 109 void SetConfigurationComplete(bool success, |
| 110 const UsbSuccessCallback& callback); |
| 111 void ClaimInterfaceOnBlockingThread(PlatformUsbDeviceHandle handle, |
| 112 int interface_number, |
| 113 const UsbSuccessCallback& callback); |
| 114 void ClaimInterfaceComplete(int interface_number, |
| 115 bool success, |
| 116 const UsbSuccessCallback& callback); |
| 117 void SetInterfaceAlternateSettingOnBlockingThread( |
| 118 PlatformUsbDeviceHandle handle, |
| 119 int interface_number, |
| 120 int alternate_setting, |
| 121 const UsbSuccessCallback& callback); |
| 122 void SetInterfaceAlternateSettingComplete(int interface_number, |
| 123 int alternate_setting, |
| 124 bool success, |
| 125 const UsbSuccessCallback& callback); |
| 126 void ResetDeviceOnBlockingThread(PlatformUsbDeviceHandle handle, |
| 127 const UsbSuccessCallback& callback); |
| 128 void ResetDeviceComplete(bool success, const UsbSuccessCallback& callback); |
| 129 |
100 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and | 130 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and |
101 // SetInterfaceAlternateSetting. | 131 // SetInterfaceAlternateSetting. |
102 void RefreshEndpointMap(); | 132 void RefreshEndpointMap(); |
103 | 133 |
104 // Look up the claimed interface by endpoint. Return NULL if the interface | 134 // Look up the claimed interface by endpoint. Return NULL if the interface |
105 // of the endpoint is not found. | 135 // of the endpoint is not found. |
106 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint( | 136 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint( |
107 unsigned char endpoint); | 137 unsigned char endpoint); |
108 | 138 |
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 | 139 // Submits a transfer and starts tracking it. Retains the buffer and copies |
116 // the completion callback until the transfer finishes, whereupon it invokes | 140 // the completion callback until the transfer finishes, whereupon it invokes |
117 // the callback then releases the buffer. | 141 // the callback then releases the buffer. |
118 void SubmitTransfer(scoped_ptr<Transfer> transfer); | 142 void SubmitTransfer(scoped_ptr<Transfer> transfer); |
119 | 143 |
120 // Invokes the callbacks associated with a given transfer, and removes it from | 144 // Removes the transfer from the in-flight transfer set and invokes the |
121 // the in-flight transfer set. | 145 // completion callback. |
122 void CompleteTransfer(scoped_ptr<Transfer> transfer); | 146 void TransferComplete(Transfer* transfer, const base::Closure& callback); |
123 | |
124 bool GetSupportedLanguages(); | |
125 | 147 |
126 // Informs the object to drop internal references. | 148 // Informs the object to drop internal references. |
127 void InternalClose(); | 149 void InternalClose(); |
128 | 150 |
129 scoped_refptr<UsbDeviceImpl> device_; | 151 scoped_refptr<UsbDeviceImpl> device_; |
130 | 152 |
131 PlatformUsbDeviceHandle handle_; | 153 PlatformUsbDeviceHandle handle_; |
132 | 154 |
133 std::vector<uint16> languages_; | |
134 std::map<uint8, base::string16> strings_; | |
135 | |
136 typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap; | 155 typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap; |
137 ClaimedInterfaceMap claimed_interfaces_; | 156 ClaimedInterfaceMap claimed_interfaces_; |
138 | 157 |
139 // This set holds weak pointers to pending transfers. | 158 // This set holds weak pointers to pending transfers. |
140 std::set<Transfer*> transfers_; | 159 std::set<Transfer*> transfers_; |
141 | 160 |
142 // A map from endpoints to interfaces | 161 // A map from endpoints to interfaces |
143 typedef std::map<int, int> EndpointMap; | 162 typedef std::map<int, int> EndpointMap; |
144 EndpointMap endpoint_map_; | 163 EndpointMap endpoint_map_; |
145 | 164 |
146 // Retain the UsbContext so that the platform context will not be destroyed | 165 // Retain the UsbContext so that the platform context will not be destroyed |
147 // before this handle. | 166 // before this handle. |
148 scoped_refptr<UsbContext> context_; | 167 scoped_refptr<UsbContext> context_; |
149 | 168 |
150 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 169 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 170 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
151 | 171 |
152 base::ThreadChecker thread_checker_; | 172 base::ThreadChecker thread_checker_; |
153 base::WeakPtrFactory<UsbDeviceHandleImpl> weak_factory_; | |
154 | 173 |
155 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl); | 174 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl); |
156 }; | 175 }; |
157 | 176 |
158 } // namespace device | 177 } // namespace device |
159 | 178 |
160 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ | 179 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_ |
OLD | NEW |