| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_H_ | 5 #ifndef CHROME_BROWSER_USB_USB_DEVICE_H_ |
| 6 #define CHROME_BROWSER_USB_USB_DEVICE_H_ | 6 #define CHROME_BROWSER_USB_USB_DEVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; | 48 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; |
| 49 | 49 |
| 50 // Usually you will not want to directly create a UsbDevice, favoring to let | 50 // Usually you will not want to directly create a UsbDevice, favoring to let |
| 51 // the UsbService take care of the logistics of getting a platform device | 51 // the UsbService take care of the logistics of getting a platform device |
| 52 // handle and handling events for it. | 52 // handle and handling events for it. |
| 53 UsbDevice(UsbService* service, PlatformUsbDeviceHandle handle); | 53 UsbDevice(UsbService* service, PlatformUsbDeviceHandle handle); |
| 54 | 54 |
| 55 PlatformUsbDeviceHandle handle() { return handle_; } | 55 PlatformUsbDeviceHandle handle() { return handle_; } |
| 56 | 56 |
| 57 // Close the USB device and release the underlying platform device. | 57 // Close the USB device and release the underlying platform device. |
| 58 void Close(); | 58 virtual void Close(); |
| 59 | 59 |
| 60 void ControlTransfer(const TransferDirection direction, | 60 virtual void ControlTransfer(const TransferDirection direction, |
| 61 const TransferRequestType request_type, | 61 const TransferRequestType request_type, |
| 62 const TransferRecipient recipient, | 62 const TransferRecipient recipient, |
| 63 const uint8 request, | 63 const uint8 request, |
| 64 const uint16 value, | 64 const uint16 value, |
| 65 const uint16 index, | 65 const uint16 index, |
| 66 net::IOBuffer* buffer, | 66 net::IOBuffer* buffer, |
| 67 const size_t length, | 67 const size_t length, |
| 68 const unsigned int timeout, | 68 const unsigned int timeout, |
| 69 const UsbTransferCallback& callback); | 69 const UsbTransferCallback& callback); |
| 70 | 70 |
| 71 void BulkTransfer(const TransferDirection direction, | 71 virtual void BulkTransfer(const TransferDirection direction, |
| 72 const uint8 endpoint, | 72 const uint8 endpoint, |
| 73 net::IOBuffer* buffer, | 73 net::IOBuffer* buffer, |
| 74 const size_t length, | 74 const size_t length, |
| 75 const unsigned int timeout, | 75 const unsigned int timeout, |
| 76 const UsbTransferCallback& callback); | 76 const UsbTransferCallback& callback); |
| 77 | 77 |
| 78 void InterruptTransfer(const TransferDirection direction, | 78 virtual void InterruptTransfer(const TransferDirection direction, |
| 79 const uint8 endpoint, | 79 const uint8 endpoint, |
| 80 net::IOBuffer* buffer, | 80 net::IOBuffer* buffer, |
| 81 const size_t length, | 81 const size_t length, |
| 82 const unsigned int timeout, | 82 const unsigned int timeout, |
| 83 const UsbTransferCallback& callback); | 83 const UsbTransferCallback& callback); |
| 84 | 84 |
| 85 void IsochronousTransfer(const TransferDirection direction, | 85 virtual void IsochronousTransfer(const TransferDirection direction, |
| 86 const uint8 endpoint, | 86 const uint8 endpoint, |
| 87 net::IOBuffer* buffer, | 87 net::IOBuffer* buffer, |
| 88 const size_t length, | 88 const size_t length, |
| 89 const unsigned int packets, | 89 const unsigned int packets, |
| 90 const unsigned int packet_length, | 90 const unsigned int packet_length, |
| 91 const unsigned int timeout, | 91 const unsigned int timeout, |
| 92 const UsbTransferCallback& callback); | 92 const UsbTransferCallback& callback); |
| 93 | 93 |
| 94 // Normal code should not call this function. It is called by the platform's | 94 // Normal code should not call this function. It is called by the platform's |
| 95 // callback mechanism in such a way that it cannot be made private. Invokes | 95 // callback mechanism in such a way that it cannot be made private. Invokes |
| 96 // the callbacks associated with a given transfer, and removes it from the | 96 // the callbacks associated with a given transfer, and removes it from the |
| 97 // in-flight transfer set. | 97 // in-flight transfer set. |
| 98 void TransferComplete(PlatformUsbTransferHandle transfer); | 98 void TransferComplete(PlatformUsbTransferHandle transfer); |
| 99 | 99 |
| 100 protected: |
| 101 // This constructor variant is for use in testing only. |
| 102 UsbDevice(); |
| 103 |
| 104 friend class base::RefCounted<UsbDevice>; |
| 105 virtual ~UsbDevice(); |
| 106 |
| 100 private: | 107 private: |
| 101 struct Transfer { | 108 struct Transfer { |
| 102 Transfer(); | 109 Transfer(); |
| 103 ~Transfer(); | 110 ~Transfer(); |
| 104 | 111 |
| 105 scoped_refptr<net::IOBuffer> buffer; | 112 scoped_refptr<net::IOBuffer> buffer; |
| 106 UsbTransferCallback callback; | 113 UsbTransferCallback callback; |
| 107 }; | 114 }; |
| 108 | 115 |
| 109 friend class base::RefCounted<UsbDevice>; | |
| 110 virtual ~UsbDevice(); | |
| 111 | |
| 112 // Checks that the device has not yet been closed. | 116 // Checks that the device has not yet been closed. |
| 113 void CheckDevice(); | 117 void CheckDevice(); |
| 114 | 118 |
| 115 // 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 |
| 116 // the completion callback until the transfer finishes, whereupon it invokes | 120 // the completion callback until the transfer finishes, whereupon it invokes |
| 117 // the callback then releases the buffer. | 121 // the callback then releases the buffer. |
| 118 void SubmitTransfer(PlatformUsbTransferHandle handle, net::IOBuffer* buffer, | 122 void SubmitTransfer(PlatformUsbTransferHandle handle, net::IOBuffer* buffer, |
| 119 const UsbTransferCallback& callback); | 123 const UsbTransferCallback& callback); |
| 120 | 124 |
| 121 // The UsbService isn't referenced here to prevent a dependency cycle between | 125 // The UsbService isn't referenced here to prevent a dependency cycle between |
| 122 // the service and the devices. Since a service owns every device, and is | 126 // the service and the devices. Since a service owns every device, and is |
| 123 // responsible for its destruction, there is no case where a UsbDevice can | 127 // responsible for its destruction, there is no case where a UsbDevice can |
| 124 // have outlived its originating UsbService. | 128 // have outlived its originating UsbService. |
| 125 UsbService* const service_; | 129 UsbService* const service_; |
| 126 PlatformUsbDeviceHandle handle_; | 130 PlatformUsbDeviceHandle handle_; |
| 127 | 131 |
| 128 // transfers_ tracks all in-flight transfers associated with this device, | 132 // transfers_ tracks all in-flight transfers associated with this device, |
| 129 // allowing the device to retain the buffer and callback associated with a | 133 // allowing the device to retain the buffer and callback associated with a |
| 130 // transfer until such time that it completes. It is protected by lock_. | 134 // transfer until such time that it completes. It is protected by lock_. |
| 131 base::Lock lock_; | 135 base::Lock lock_; |
| 132 std::map<PlatformUsbTransferHandle, Transfer> transfers_; | 136 std::map<PlatformUsbTransferHandle, Transfer> transfers_; |
| 133 | 137 |
| 134 DISALLOW_EVIL_CONSTRUCTORS(UsbDevice); | 138 DISALLOW_EVIL_CONSTRUCTORS(UsbDevice); |
| 135 }; | 139 }; |
| 136 | 140 |
| 137 #endif // CHROME_BROWSER_USB_USB_DEVICE_H_ | 141 #endif // CHROME_BROWSER_USB_USB_DEVICE_H_ |
| OLD | NEW |