Chromium Code Reviews| Index: chrome/browser/usb/usb_device_handle.h |
| diff --git a/chrome/browser/usb/usb_device.h b/chrome/browser/usb/usb_device_handle.h |
| similarity index 64% |
| rename from chrome/browser/usb/usb_device.h |
| rename to chrome/browser/usb/usb_device_handle.h |
| index 0625e74cd02ca78a59cfe56438ec5ae0daeb448b..b63ed98229914c13ddb621d50d621d1de58930cd 100644 |
| --- a/chrome/browser/usb/usb_device.h |
| +++ b/chrome/browser/usb/usb_device_handle.h |
| @@ -2,27 +2,21 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#ifndef CHROME_BROWSER_USB_USB_DEVICE_H_ |
| -#define CHROME_BROWSER_USB_USB_DEVICE_H_ |
| +#ifndef CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_ |
| +#define CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_ |
| #include <map> |
| #include <vector> |
| #include "base/memory/ref_counted.h" |
| -#include "base/synchronization/lock.h" |
| +#include "base/threading/non_thread_safe.h" |
| #include "chrome/browser/usb/usb_interface.h" |
| #include "net/base/completion_callback.h" |
| #include "net/base/io_buffer.h" |
| -struct libusb_device; |
| -struct libusb_device_handle; |
| -struct libusb_iso_packet_descriptor; |
| -struct libusb_transfer; |
| - |
| -typedef libusb_device* PlatformUsbDevice; |
| -typedef libusb_device_handle* PlatformUsbDeviceHandle; |
| -typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; |
| -typedef libusb_transfer* PlatformUsbTransferHandle; |
| +typedef struct libusb_device_handle* PlatformUsbDeviceHandle; |
| +typedef struct libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; |
| +typedef struct libusb_transfer* PlatformUsbTransferHandle; |
| class UsbService; |
| @@ -42,23 +36,34 @@ enum UsbTransferStatus { |
| }; |
| typedef base::Callback<void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, |
| - size_t)> UsbTransferCallback; |
| + size_t)> UsbTransferCallback; |
| typedef base::Callback<void(bool)> UsbInterfaceCallback; |
| // A UsbDevice wraps the platform's underlying representation of what a USB |
| // device actually is, and provides accessors for performing many of the |
| // standard USB operations. |
| -class UsbDevice : public base::RefCounted<UsbDevice> { |
| +// |
| +// This class should be used on FILE thread. |
| +class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle>, |
|
akalin
2013/06/13 21:53:12
This doesn't make sense.
|
| + public base::NonThreadSafe { |
| public: |
| - enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; |
| - enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; |
| - |
| - // Usually you will not want to directly create a UsbDevice, favoring to let |
| - // the UsbService take care of the logistics of getting a platform device |
| - // handle and handling events for it. |
| - UsbDevice(UsbService* service, PlatformUsbDeviceHandle handle); |
| + enum TransferRequestType { |
| + STANDARD, |
| + CLASS, |
| + VENDOR, |
| + RESERVED |
| + }; |
| + enum TransferRecipient { |
| + DEVICE, |
| + INTERFACE, |
| + ENDPOINT, |
| + OTHER |
| + }; |
| PlatformUsbDeviceHandle handle() { return handle_; } |
| + int device() const { return device_; } |
| + uint16 vendor_id() const { return vendor_id_; } |
| + uint16 product_id() const { return product_id_; } |
| // Close the USB device and release the underlying platform device. |callback| |
| // is invoked after the device has been closed. |
| @@ -74,38 +79,31 @@ class UsbDevice : public base::RefCounted<UsbDevice> { |
| const UsbInterfaceCallback& callback); |
| virtual void SetInterfaceAlternateSetting( |
| - const int interface_number, |
| - const int alternate_setting, |
| + const int interface_number, const int alternate_setting, |
| const UsbInterfaceCallback& callback); |
| + // The following four methods can be called on any thread. |
| virtual void ControlTransfer(const UsbEndpointDirection direction, |
| const TransferRequestType request_type, |
| const TransferRecipient recipient, |
| - const uint8 request, |
| - const uint16 value, |
| - const uint16 index, |
| - net::IOBuffer* buffer, |
| - const size_t length, |
| - const unsigned int timeout, |
| + const uint8 request, const uint16 value, |
| + const uint16 index, net::IOBuffer* buffer, |
| + const size_t length, const unsigned int timeout, |
| const UsbTransferCallback& callback); |
| virtual void BulkTransfer(const UsbEndpointDirection direction, |
| - const uint8 endpoint, |
| - net::IOBuffer* buffer, |
| - const size_t length, |
| - const unsigned int timeout, |
| + const uint8 endpoint, net::IOBuffer* buffer, |
| + const size_t length, const unsigned int timeout, |
| const UsbTransferCallback& callback); |
| virtual void InterruptTransfer(const UsbEndpointDirection direction, |
| - const uint8 endpoint, |
| - net::IOBuffer* buffer, |
| + const uint8 endpoint, net::IOBuffer* buffer, |
| const size_t length, |
| const unsigned int timeout, |
| const UsbTransferCallback& callback); |
| virtual void IsochronousTransfer(const UsbEndpointDirection direction, |
| - const uint8 endpoint, |
| - net::IOBuffer* buffer, |
| + const uint8 endpoint, net::IOBuffer* buffer, |
| const size_t length, |
| const unsigned int packets, |
| const unsigned int packet_length, |
| @@ -122,10 +120,10 @@ class UsbDevice : public base::RefCounted<UsbDevice> { |
| protected: |
| // This constructor variant is for use in testing only. |
| - UsbDevice(); |
| + UsbDeviceHandle(); |
| - friend class base::RefCounted<UsbDevice>; |
| - virtual ~UsbDevice(); |
| + friend class base::RefCountedThreadSafe<UsbDeviceHandle>; |
| + virtual ~UsbDeviceHandle(); |
| private: |
| struct Transfer { |
| @@ -138,32 +136,39 @@ class UsbDevice : public base::RefCounted<UsbDevice> { |
| UsbTransferCallback callback; |
| }; |
| - // Checks that the device has not yet been closed. |
| - void CheckDevice(); |
| + // UsbDeviceHandle should only be created from UsbDevice class. |
| + UsbDeviceHandle(UsbService* service, int device, const uint16 vendor_id, |
| + const uint16 product_id, PlatformUsbDeviceHandle handle); |
| + |
| + friend class UsbDevice; |
| + |
| + // This only called from UsbDevice, thus always from FILE thread. |
| + void InternalClose(); |
| // Submits a transfer and starts tracking it. Retains the buffer and copies |
| // the completion callback until the transfer finishes, whereupon it invokes |
| // the callback then releases the buffer. |
| void SubmitTransfer(PlatformUsbTransferHandle handle, |
| - UsbTransferType transfer_type, |
| - net::IOBuffer* buffer, |
| - const size_t length, |
| - const UsbTransferCallback& callback); |
| + UsbTransferType transfer_type, net::IOBuffer* buffer, |
| + const size_t length, const UsbTransferCallback& callback); |
| // The UsbService isn't referenced here to prevent a dependency cycle between |
| // the service and the devices. Since a service owns every device, and is |
| // responsible for its destruction, there is no case where a UsbDevice can |
| // have outlived its originating UsbService. |
| UsbService* const service_; |
| + const int device_; |
| + const uint16 vendor_id_; |
| + const uint16 product_id_; |
| + |
| PlatformUsbDeviceHandle handle_; |
| // transfers_ tracks all in-flight transfers associated with this device, |
| // allowing the device to retain the buffer and callback associated with a |
| - // transfer until such time that it completes. It is protected by lock_. |
| - base::Lock lock_; |
| + // transfer until such time that it completes. |
| std::map<PlatformUsbTransferHandle, Transfer> transfers_; |
| - DISALLOW_COPY_AND_ASSIGN(UsbDevice); |
| + DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle); |
| }; |
| -#endif // CHROME_BROWSER_USB_USB_DEVICE_H_ |
| +#endif // CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_ |