OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_ | 5 #ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_ |
6 #define CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_ | 6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_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/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
13 #include "base/synchronization/lock.h" | |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "chrome/browser/usb/usb_interface.h" | 15 #include "components/usb_service/usb_interface.h" |
16 #include "net/base/completion_callback.h" | 16 #include "components/usb_service/usb_service_export.h" |
17 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
18 | 18 |
19 struct libusb_device_handle; | 19 struct libusb_device_handle; |
20 struct libusb_iso_packet_descriptor; | 20 struct libusb_iso_packet_descriptor; |
21 struct libusb_transfer; | 21 struct libusb_transfer; |
22 | 22 |
| 23 namespace base { |
| 24 class MessageLoopProxy; |
| 25 } |
| 26 |
| 27 namespace usb_service { |
| 28 |
| 29 class UsbContext; |
| 30 class UsbConfigDescriptor; |
| 31 class UsbDevice; |
| 32 |
23 typedef libusb_device_handle* PlatformUsbDeviceHandle; | 33 typedef libusb_device_handle* PlatformUsbDeviceHandle; |
24 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; | 34 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; |
25 typedef libusb_transfer* PlatformUsbTransferHandle; | 35 typedef libusb_transfer* PlatformUsbTransferHandle; |
26 | 36 |
27 class UsbContext; | |
28 class UsbConfigDescriptor; | |
29 class UsbDevice; | |
30 class UsbInterfaceDescriptor; | |
31 | |
32 namespace base { | |
33 class MessageLoopProxy; | |
34 } // namespace base | |
35 | |
36 namespace net { | |
37 class IOBuffer; | |
38 } // namespace net | |
39 | |
40 enum UsbTransferStatus { | 37 enum UsbTransferStatus { |
41 USB_TRANSFER_COMPLETED = 0, | 38 USB_TRANSFER_COMPLETED = 0, |
42 USB_TRANSFER_ERROR, | 39 USB_TRANSFER_ERROR, |
43 USB_TRANSFER_TIMEOUT, | 40 USB_TRANSFER_TIMEOUT, |
44 USB_TRANSFER_CANCELLED, | 41 USB_TRANSFER_CANCELLED, |
45 USB_TRANSFER_STALLED, | 42 USB_TRANSFER_STALLED, |
46 USB_TRANSFER_DISCONNECT, | 43 USB_TRANSFER_DISCONNECT, |
47 USB_TRANSFER_OVERFLOW, | 44 USB_TRANSFER_OVERFLOW, |
48 USB_TRANSFER_LENGTH_SHORT, | 45 USB_TRANSFER_LENGTH_SHORT, |
49 }; | 46 }; |
50 | 47 |
51 typedef base::Callback<void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, | 48 typedef base::Callback< |
52 size_t)> UsbTransferCallback; | 49 void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, size_t)> |
| 50 UsbTransferCallback; |
53 | 51 |
54 // UsbDeviceHandle class provides basic I/O related functionalities. | 52 // UsbDeviceHandle class provides basic I/O related functionalities. |
55 class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> { | 53 class USB_SERVICE_EXPORT UsbDeviceHandle |
| 54 : public base::RefCountedThreadSafe<UsbDeviceHandle> { |
56 public: | 55 public: |
57 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; | 56 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; |
58 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; | 57 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; |
59 | 58 |
60 scoped_refptr<UsbDevice> device() const; | 59 scoped_refptr<UsbDevice> device() const; |
61 PlatformUsbDeviceHandle handle() const { return handle_; } | 60 PlatformUsbDeviceHandle handle() const { return handle_; } |
62 | 61 |
63 // Notifies UsbDevice to drop the reference of this object; cancels all the | 62 // Notifies UsbDevice to drop the reference of this object; cancels all the |
64 // flying transfers. | 63 // flying transfers. |
65 // It is possible that the object has no other reference after this call. So | 64 // It is possible that the object has no other reference after this call. So |
66 // if it is called using a raw pointer, it could be invalidated. | 65 // if it is called using a raw pointer, it could be invalidated. |
67 // The platform device handle will be closed when UsbDeviceHandle destructs. | 66 // The platform device handle will be closed when UsbDeviceHandle destructs. |
68 virtual void Close(); | 67 virtual void Close(); |
69 | 68 |
70 // Device manipulation operations. These methods are blocking and must be | 69 // Device manipulation operations. These methods are blocking and must be |
71 // called on FILE thread. | 70 // called on FILE thread. |
72 virtual bool ClaimInterface(const int interface_number); | 71 virtual bool ClaimInterface(const int interface_number); |
73 virtual bool ReleaseInterface(const int interface_number); | 72 virtual bool ReleaseInterface(const int interface_number); |
74 virtual bool SetInterfaceAlternateSetting( | 73 virtual bool SetInterfaceAlternateSetting(const int interface_number, |
75 const int interface_number, | 74 const int alternate_setting); |
76 const int alternate_setting); | |
77 virtual bool ResetDevice(); | 75 virtual bool ResetDevice(); |
78 virtual bool GetSerial(base::string16* serial); | 76 virtual bool GetSerial(base::string16* serial); |
79 | 77 |
80 // Async IO. Can be called on any thread. | 78 // Async IO. Can be called on any thread. |
81 virtual void ControlTransfer(const UsbEndpointDirection direction, | 79 virtual void ControlTransfer(const UsbEndpointDirection direction, |
82 const TransferRequestType request_type, | 80 const TransferRequestType request_type, |
83 const TransferRecipient recipient, | 81 const TransferRecipient recipient, |
84 const uint8 request, | 82 const uint8 request, |
85 const uint16 value, | 83 const uint16 value, |
86 const uint16 index, | 84 const uint16 index, |
(...skipping 24 matching lines...) Expand all Loading... |
111 const unsigned int packet_length, | 109 const unsigned int packet_length, |
112 const unsigned int timeout, | 110 const unsigned int timeout, |
113 const UsbTransferCallback& callback); | 111 const UsbTransferCallback& callback); |
114 | 112 |
115 protected: | 113 protected: |
116 friend class base::RefCountedThreadSafe<UsbDeviceHandle>; | 114 friend class base::RefCountedThreadSafe<UsbDeviceHandle>; |
117 friend class UsbDevice; | 115 friend class UsbDevice; |
118 | 116 |
119 // This constructor is called by UsbDevice. | 117 // This constructor is called by UsbDevice. |
120 UsbDeviceHandle(scoped_refptr<UsbContext> context, | 118 UsbDeviceHandle(scoped_refptr<UsbContext> context, |
121 UsbDevice* device, PlatformUsbDeviceHandle handle, | 119 UsbDevice* device, |
| 120 PlatformUsbDeviceHandle handle, |
122 scoped_refptr<UsbConfigDescriptor> interfaces); | 121 scoped_refptr<UsbConfigDescriptor> interfaces); |
123 | 122 |
124 // This constructor variant is for use in testing only. | 123 // This constructor variant is for use in testing only. |
125 UsbDeviceHandle(); | 124 UsbDeviceHandle(); |
126 virtual ~UsbDeviceHandle(); | 125 virtual ~UsbDeviceHandle(); |
127 | 126 |
128 UsbDevice* device_; | 127 UsbDevice* device_; |
129 | 128 |
130 private: | 129 private: |
131 friend void HandleTransferCompletion(PlatformUsbTransferHandle handle); | 130 friend void HandleTransferCompletion(PlatformUsbTransferHandle handle); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 | 174 |
176 // Retain the UsbContext so that the platform context will not be destroyed | 175 // Retain the UsbContext so that the platform context will not be destroyed |
177 // before this handle. | 176 // before this handle. |
178 scoped_refptr<UsbContext> context_; | 177 scoped_refptr<UsbContext> context_; |
179 | 178 |
180 base::ThreadChecker thread_checker_; | 179 base::ThreadChecker thread_checker_; |
181 | 180 |
182 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle); | 181 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle); |
183 }; | 182 }; |
184 | 183 |
185 #endif // CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_ | 184 } // namespace usb_service |
| 185 |
| 186 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_ |
OLD | NEW |