Commit d738857a authored by zvorygin@chromium.org's avatar zvorygin@chromium.org

Extracted UsbDeviceHandle as interface.

BUG=367094

Review URL: https://codereview.chromium.org/278633003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269803 0039d316-1c4b-4281-b951-d872f2087c98
parent 2ebf2604
...@@ -610,7 +610,7 @@ void AndroidUsbDevice::TransferError(UsbTransferStatus status) { ...@@ -610,7 +610,7 @@ void AndroidUsbDevice::TransferError(UsbTransferStatus status) {
void AndroidUsbDevice::TerminateIfReleased( void AndroidUsbDevice::TerminateIfReleased(
scoped_refptr<UsbDeviceHandle> usb_handle) { scoped_refptr<UsbDeviceHandle> usb_handle) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
if (usb_handle->device()) if (usb_handle->GetDevice())
return; return;
message_loop_->PostTask(FROM_HERE, message_loop_->PostTask(FROM_HERE,
base::Bind(&AndroidUsbDevice::Terminate, this)); base::Bind(&AndroidUsbDevice::Terminate, this));
......
...@@ -25,7 +25,8 @@ ...@@ -25,7 +25,8 @@
'usb_service/usb_device_impl.cc', 'usb_service/usb_device_impl.cc',
'usb_service/usb_device_impl.h', 'usb_service/usb_device_impl.h',
'usb_service/usb_device.h', 'usb_service/usb_device.h',
'usb_service/usb_device_handle.cc', 'usb_service/usb_device_handle_impl.cc',
'usb_service/usb_device_handle_impl.h',
'usb_service/usb_device_handle.h', 'usb_service/usb_device_handle.h',
'usb_service/usb_interface.h', 'usb_service/usb_interface.h',
'usb_service/usb_interface_impl.cc', 'usb_service/usb_interface_impl.cc',
......
...@@ -16,24 +16,10 @@ ...@@ -16,24 +16,10 @@
#include "components/usb_service/usb_service_export.h" #include "components/usb_service/usb_service_export.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
struct libusb_device_handle;
struct libusb_iso_packet_descriptor;
struct libusb_transfer;
namespace base {
class MessageLoopProxy;
}
namespace usb_service { namespace usb_service {
class UsbContext;
class UsbConfigDescriptor;
class UsbDevice; class UsbDevice;
typedef libusb_device_handle* PlatformUsbDeviceHandle;
typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
typedef libusb_transfer* PlatformUsbTransferHandle;
enum UsbTransferStatus { enum UsbTransferStatus {
USB_TRANSFER_COMPLETED = 0, USB_TRANSFER_COMPLETED = 0,
USB_TRANSFER_ERROR, USB_TRANSFER_ERROR,
...@@ -56,24 +42,23 @@ class USB_SERVICE_EXPORT UsbDeviceHandle ...@@ -56,24 +42,23 @@ class USB_SERVICE_EXPORT UsbDeviceHandle
enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED };
enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER };
scoped_refptr<UsbDevice> device() const; virtual scoped_refptr<UsbDevice> GetDevice() const = 0;
PlatformUsbDeviceHandle handle() const { return handle_; }
// Notifies UsbDevice to drop the reference of this object; cancels all the // Notifies UsbDevice to drop the reference of this object; cancels all the
// flying transfers. // flying transfers.
// It is possible that the object has no other reference after this call. So // It is possible that the object has no other reference after this call. So
// if it is called using a raw pointer, it could be invalidated. // if it is called using a raw pointer, it could be invalidated.
// The platform device handle will be closed when UsbDeviceHandle destructs. // The platform device handle will be closed when UsbDeviceHandle destructs.
virtual void Close(); virtual void Close() = 0;
// Device manipulation operations. These methods are blocking and must be // Device manipulation operations. These methods are blocking and must be
// called on FILE thread. // called on FILE thread.
virtual bool ClaimInterface(const int interface_number); virtual bool ClaimInterface(const int interface_number) = 0;
virtual bool ReleaseInterface(const int interface_number); virtual bool ReleaseInterface(const int interface_number) = 0;
virtual bool SetInterfaceAlternateSetting(const int interface_number, virtual bool SetInterfaceAlternateSetting(const int interface_number,
const int alternate_setting); const int alternate_setting) = 0;
virtual bool ResetDevice(); virtual bool ResetDevice() = 0;
virtual bool GetSerial(base::string16* serial); virtual bool GetSerial(base::string16* serial) = 0;
// Async IO. Can be called on any thread. // Async IO. Can be called on any thread.
virtual void ControlTransfer(const UsbEndpointDirection direction, virtual void ControlTransfer(const UsbEndpointDirection direction,
...@@ -85,21 +70,21 @@ class USB_SERVICE_EXPORT UsbDeviceHandle ...@@ -85,21 +70,21 @@ class USB_SERVICE_EXPORT UsbDeviceHandle
net::IOBuffer* buffer, net::IOBuffer* buffer,
const size_t length, const size_t length,
const unsigned int timeout, const unsigned int timeout,
const UsbTransferCallback& callback); const UsbTransferCallback& callback) = 0;
virtual void BulkTransfer(const UsbEndpointDirection direction, virtual void BulkTransfer(const UsbEndpointDirection direction,
const uint8 endpoint, const uint8 endpoint,
net::IOBuffer* buffer, net::IOBuffer* buffer,
const size_t length, const size_t length,
const unsigned int timeout, const unsigned int timeout,
const UsbTransferCallback& callback); const UsbTransferCallback& callback) = 0;
virtual void InterruptTransfer(const UsbEndpointDirection direction, virtual void InterruptTransfer(const UsbEndpointDirection direction,
const uint8 endpoint, const uint8 endpoint,
net::IOBuffer* buffer, net::IOBuffer* buffer,
const size_t length, const size_t length,
const unsigned int timeout, const unsigned int timeout,
const UsbTransferCallback& callback); const UsbTransferCallback& callback) = 0;
virtual void IsochronousTransfer(const UsbEndpointDirection direction, virtual void IsochronousTransfer(const UsbEndpointDirection direction,
const uint8 endpoint, const uint8 endpoint,
...@@ -108,75 +93,14 @@ class USB_SERVICE_EXPORT UsbDeviceHandle ...@@ -108,75 +93,14 @@ class USB_SERVICE_EXPORT UsbDeviceHandle
const unsigned int packets, const unsigned int packets,
const unsigned int packet_length, const unsigned int packet_length,
const unsigned int timeout, const unsigned int timeout,
const UsbTransferCallback& callback); const UsbTransferCallback& callback) = 0;
protected: protected:
friend class base::RefCountedThreadSafe<UsbDeviceHandle>; friend class base::RefCountedThreadSafe<UsbDeviceHandle>;
friend class UsbDeviceImpl;
// This constructor is called by UsbDevice.
UsbDeviceHandle(scoped_refptr<UsbContext> context,
UsbDevice* device,
PlatformUsbDeviceHandle handle,
scoped_refptr<UsbConfigDescriptor> interfaces);
// This constructor variant is for use in testing only.
UsbDeviceHandle();
virtual ~UsbDeviceHandle();
UsbDevice* device_;
private:
friend void HandleTransferCompletion(PlatformUsbTransferHandle handle);
class InterfaceClaimer;
struct Transfer;
// Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
// SetInterfaceAlternateSetting.
void RefreshEndpointMap();
// Look up the claimed interface by endpoint. Return NULL if the interface
// of the endpoint is not found.
scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
unsigned char endpoint);
// 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,
scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
const UsbTransferCallback& callback);
// Invokes the callbacks associated with a given transfer, and removes it from
// the in-flight transfer set.
void TransferComplete(PlatformUsbTransferHandle transfer);
// Informs the object to drop internal references.
void InternalClose();
PlatformUsbDeviceHandle handle_;
scoped_refptr<UsbConfigDescriptor> interfaces_;
typedef std::map<int, scoped_refptr<InterfaceClaimer> > ClaimedInterfaceMap;
ClaimedInterfaceMap claimed_interfaces_;
typedef std::map<PlatformUsbTransferHandle, Transfer> TransferMap;
TransferMap transfers_;
// A map from endpoints to interfaces
typedef std::map<int, int> EndpointMap;
EndpointMap endpoint_map_;
// Retain the UsbContext so that the platform context will not be destroyed UsbDeviceHandle() {};
// before this handle.
scoped_refptr<UsbContext> context_;
base::ThreadChecker thread_checker_; virtual ~UsbDeviceHandle() {};
DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle); DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle);
}; };
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "components/usb_service/usb_device_handle.h" #include "components/usb_service/usb_device_handle_impl.h"
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
#include "components/usb_service/usb_context.h" #include "components/usb_service/usb_context.h"
#include "components/usb_service/usb_device.h" #include "components/usb_service/usb_device_impl.h"
#include "components/usb_service/usb_interface.h" #include "components/usb_service/usb_interface.h"
#include "components/usb_service/usb_service.h" #include "components/usb_service/usb_service.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
...@@ -113,17 +113,17 @@ PlatformTransferCompletionCallback(PlatformUsbTransferHandle transfer) { ...@@ -113,17 +113,17 @@ PlatformTransferCompletionCallback(PlatformUsbTransferHandle transfer) {
void HandleTransferCompletion(PlatformUsbTransferHandle transfer) { void HandleTransferCompletion(PlatformUsbTransferHandle transfer) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
UsbDeviceHandle* const device_handle = UsbDeviceHandleImpl* const device_handle =
reinterpret_cast<UsbDeviceHandle*>(transfer->user_data); reinterpret_cast<UsbDeviceHandleImpl*>(transfer->user_data);
CHECK(device_handle) << "Device handle is closed before transfer finishes."; CHECK(device_handle) << "Device handle is closed before transfer finishes.";
device_handle->TransferComplete(transfer); device_handle->TransferComplete(transfer);
libusb_free_transfer(transfer); libusb_free_transfer(transfer);
} }
class UsbDeviceHandle::InterfaceClaimer class UsbDeviceHandleImpl::InterfaceClaimer
: public base::RefCountedThreadSafe<UsbDeviceHandle::InterfaceClaimer> { : public base::RefCountedThreadSafe<UsbDeviceHandleImpl::InterfaceClaimer> {
public: public:
InterfaceClaimer(const scoped_refptr<UsbDeviceHandle> handle, InterfaceClaimer(const scoped_refptr<UsbDeviceHandleImpl> handle,
const int interface_number); const int interface_number);
bool Claim() const; bool Claim() const;
...@@ -138,52 +138,53 @@ class UsbDeviceHandle::InterfaceClaimer ...@@ -138,52 +138,53 @@ class UsbDeviceHandle::InterfaceClaimer
friend class base::RefCountedThreadSafe<InterfaceClaimer>; friend class base::RefCountedThreadSafe<InterfaceClaimer>;
~InterfaceClaimer(); ~InterfaceClaimer();
const scoped_refptr<UsbDeviceHandle> handle_; const scoped_refptr<UsbDeviceHandleImpl> handle_;
const int interface_number_; const int interface_number_;
int alternate_setting_; int alternate_setting_;
DISALLOW_COPY_AND_ASSIGN(InterfaceClaimer); DISALLOW_COPY_AND_ASSIGN(InterfaceClaimer);
}; };
UsbDeviceHandle::InterfaceClaimer::InterfaceClaimer( UsbDeviceHandleImpl::InterfaceClaimer::InterfaceClaimer(
const scoped_refptr<UsbDeviceHandle> handle, const scoped_refptr<UsbDeviceHandleImpl> handle,
const int interface_number) const int interface_number)
: handle_(handle), : handle_(handle),
interface_number_(interface_number), interface_number_(interface_number),
alternate_setting_(0) { alternate_setting_(0) {
} }
UsbDeviceHandle::InterfaceClaimer::~InterfaceClaimer() { UsbDeviceHandleImpl::InterfaceClaimer::~InterfaceClaimer() {
libusb_release_interface(handle_->handle(), interface_number_); libusb_release_interface(handle_->handle(), interface_number_);
} }
bool UsbDeviceHandle::InterfaceClaimer::Claim() const { bool UsbDeviceHandleImpl::InterfaceClaimer::Claim() const {
return libusb_claim_interface(handle_->handle(), interface_number_) == 0; return libusb_claim_interface(handle_->handle(), interface_number_) == 0;
} }
struct UsbDeviceHandle::Transfer { struct UsbDeviceHandleImpl::Transfer {
Transfer(); Transfer();
~Transfer(); ~Transfer();
UsbTransferType transfer_type; UsbTransferType transfer_type;
scoped_refptr<net::IOBuffer> buffer; scoped_refptr<net::IOBuffer> buffer;
scoped_refptr<UsbDeviceHandle::InterfaceClaimer> claimed_interface; scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> claimed_interface;
scoped_refptr<base::MessageLoopProxy> message_loop_proxy; scoped_refptr<base::MessageLoopProxy> message_loop_proxy;
size_t length; size_t length;
UsbTransferCallback callback; UsbTransferCallback callback;
}; };
UsbDeviceHandle::Transfer::Transfer() UsbDeviceHandleImpl::Transfer::Transfer()
: transfer_type(USB_TRANSFER_CONTROL), length(0) { : transfer_type(USB_TRANSFER_CONTROL), length(0) {
} }
UsbDeviceHandle::Transfer::~Transfer() { UsbDeviceHandleImpl::Transfer::~Transfer() {
} }
UsbDeviceHandle::UsbDeviceHandle(scoped_refptr<UsbContext> context, UsbDeviceHandleImpl::UsbDeviceHandleImpl(
UsbDevice* device, scoped_refptr<UsbContext> context,
PlatformUsbDeviceHandle handle, UsbDeviceImpl* device,
scoped_refptr<UsbConfigDescriptor> interfaces) PlatformUsbDeviceHandle handle,
scoped_refptr<UsbConfigDescriptor> interfaces)
: device_(device), : device_(device),
handle_(handle), handle_(handle),
interfaces_(interfaces), interfaces_(interfaces),
...@@ -193,27 +194,24 @@ UsbDeviceHandle::UsbDeviceHandle(scoped_refptr<UsbContext> context, ...@@ -193,27 +194,24 @@ UsbDeviceHandle::UsbDeviceHandle(scoped_refptr<UsbContext> context,
DCHECK(interfaces_) << "Unabled to list interfaces"; DCHECK(interfaces_) << "Unabled to list interfaces";
} }
UsbDeviceHandle::UsbDeviceHandle() : device_(NULL), handle_(NULL) { UsbDeviceHandleImpl::~UsbDeviceHandleImpl() {
}
UsbDeviceHandle::~UsbDeviceHandle() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
libusb_close(handle_); libusb_close(handle_);
handle_ = NULL; handle_ = NULL;
} }
scoped_refptr<UsbDevice> UsbDeviceHandle::device() const { scoped_refptr<UsbDevice> UsbDeviceHandleImpl::GetDevice() const {
return device_; return static_cast<UsbDevice*>(device_);
} }
void UsbDeviceHandle::Close() { void UsbDeviceHandleImpl::Close() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
if (device_) if (device_)
device_->Close(this); device_->Close(this);
} }
void UsbDeviceHandle::TransferComplete(PlatformUsbTransferHandle handle) { void UsbDeviceHandleImpl::TransferComplete(PlatformUsbTransferHandle handle) {
DCHECK(ContainsKey(transfers_, handle)) << "Missing transfer completed"; DCHECK(ContainsKey(transfers_, handle)) << "Missing transfer completed";
Transfer transfer = transfers_[handle]; Transfer transfer = transfers_[handle];
...@@ -236,7 +234,7 @@ void UsbDeviceHandle::TransferComplete(PlatformUsbTransferHandle handle) { ...@@ -236,7 +234,7 @@ void UsbDeviceHandle::TransferComplete(PlatformUsbTransferHandle handle) {
CHECK(transfer.length >= LIBUSB_CONTROL_SETUP_SIZE) CHECK(transfer.length >= LIBUSB_CONTROL_SETUP_SIZE)
<< "buffer was not correctly set: too small for the control header"; << "buffer was not correctly set: too small for the control header";
if (transfer.length >= (LIBUSB_CONTROL_SETUP_SIZE + actual_length)) { if (transfer.length >= (LIBUSB_CONTROL_SETUP_SIZE + actual_length)) {
// If the payload is zero bytes long, pad out the allocated buffer // If the payload is zero bytes long, pad out the allocated buffer
// size to one byte so that an IOBuffer of that size can be allocated. // size to one byte so that an IOBuffer of that size can be allocated.
scoped_refptr<net::IOBuffer> resized_buffer = scoped_refptr<net::IOBuffer> resized_buffer =
...@@ -297,7 +295,7 @@ void UsbDeviceHandle::TransferComplete(PlatformUsbTransferHandle handle) { ...@@ -297,7 +295,7 @@ void UsbDeviceHandle::TransferComplete(PlatformUsbTransferHandle handle) {
transfer.claimed_interface = NULL; transfer.claimed_interface = NULL;
} }
bool UsbDeviceHandle::ClaimInterface(const int interface_number) { bool UsbDeviceHandleImpl::ClaimInterface(const int interface_number) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
if (!device_) if (!device_)
return false; return false;
...@@ -315,7 +313,7 @@ bool UsbDeviceHandle::ClaimInterface(const int interface_number) { ...@@ -315,7 +313,7 @@ bool UsbDeviceHandle::ClaimInterface(const int interface_number) {
return false; return false;
} }
bool UsbDeviceHandle::ReleaseInterface(const int interface_number) { bool UsbDeviceHandleImpl::ReleaseInterface(const int interface_number) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
if (!device_) if (!device_)
return false; return false;
...@@ -336,7 +334,7 @@ bool UsbDeviceHandle::ReleaseInterface(const int interface_number) { ...@@ -336,7 +334,7 @@ bool UsbDeviceHandle::ReleaseInterface(const int interface_number) {
return true; return true;
} }
bool UsbDeviceHandle::SetInterfaceAlternateSetting( bool UsbDeviceHandleImpl::SetInterfaceAlternateSetting(
const int interface_number, const int interface_number,
const int alternate_setting) { const int alternate_setting) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
...@@ -355,7 +353,7 @@ bool UsbDeviceHandle::SetInterfaceAlternateSetting( ...@@ -355,7 +353,7 @@ bool UsbDeviceHandle::SetInterfaceAlternateSetting(
return false; return false;
} }
bool UsbDeviceHandle::ResetDevice() { bool UsbDeviceHandleImpl::ResetDevice() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
if (!device_) if (!device_)
return false; return false;
...@@ -363,7 +361,7 @@ bool UsbDeviceHandle::ResetDevice() { ...@@ -363,7 +361,7 @@ bool UsbDeviceHandle::ResetDevice() {
return libusb_reset_device(handle_) == 0; return libusb_reset_device(handle_) == 0;
} }
bool UsbDeviceHandle::GetSerial(base::string16* serial) { bool UsbDeviceHandleImpl::GetSerial(base::string16* serial) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
PlatformUsbDevice device = libusb_get_device(handle_); PlatformUsbDevice device = libusb_get_device(handle_);
libusb_device_descriptor desc; libusb_device_descriptor desc;
...@@ -411,16 +409,17 @@ bool UsbDeviceHandle::GetSerial(base::string16* serial) { ...@@ -411,16 +409,17 @@ bool UsbDeviceHandle::GetSerial(base::string16* serial) {
return false; return false;
} }
void UsbDeviceHandle::ControlTransfer(const UsbEndpointDirection direction, void UsbDeviceHandleImpl::ControlTransfer(
const TransferRequestType request_type, const UsbEndpointDirection direction,
const TransferRecipient recipient, const TransferRequestType request_type,
const uint8 request, const TransferRecipient recipient,
const uint16 value, const uint8 request,
const uint16 index, const uint16 value,
net::IOBuffer* buffer, const uint16 index,
const size_t length, net::IOBuffer* buffer,
const unsigned int timeout, const size_t length,
const UsbTransferCallback& callback) { const unsigned int timeout,
const UsbTransferCallback& callback) {
if (!device_) { if (!device_) {
callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0);
return; return;
...@@ -455,7 +454,7 @@ void UsbDeviceHandle::ControlTransfer(const UsbEndpointDirection direction, ...@@ -455,7 +454,7 @@ void UsbDeviceHandle::ControlTransfer(const UsbEndpointDirection direction,
BrowserThread::PostTask(BrowserThread::FILE, BrowserThread::PostTask(BrowserThread::FILE,
FROM_HERE, FROM_HERE,
base::Bind(&UsbDeviceHandle::SubmitTransfer, base::Bind(&UsbDeviceHandleImpl::SubmitTransfer,
this, this,
transfer, transfer,
USB_TRANSFER_CONTROL, USB_TRANSFER_CONTROL,
...@@ -465,12 +464,12 @@ void UsbDeviceHandle::ControlTransfer(const UsbEndpointDirection direction, ...@@ -465,12 +464,12 @@ void UsbDeviceHandle::ControlTransfer(const UsbEndpointDirection direction,
callback)); callback));
} }
void UsbDeviceHandle::BulkTransfer(const UsbEndpointDirection direction, void UsbDeviceHandleImpl::BulkTransfer(const UsbEndpointDirection direction,
const uint8 endpoint, const uint8 endpoint,
net::IOBuffer* buffer, net::IOBuffer* buffer,
const size_t length, const size_t length,
const unsigned int timeout, const unsigned int timeout,
const UsbTransferCallback& callback) { const UsbTransferCallback& callback) {
if (!device_) { if (!device_) {
callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0);
return; return;
...@@ -489,7 +488,7 @@ void UsbDeviceHandle::BulkTransfer(const UsbEndpointDirection direction, ...@@ -489,7 +488,7 @@ void UsbDeviceHandle::BulkTransfer(const UsbEndpointDirection direction,
BrowserThread::PostTask(BrowserThread::FILE, BrowserThread::PostTask(BrowserThread::FILE,
FROM_HERE, FROM_HERE,
base::Bind(&UsbDeviceHandle::SubmitTransfer, base::Bind(&UsbDeviceHandleImpl::SubmitTransfer,
this, this,
transfer, transfer,
USB_TRANSFER_BULK, USB_TRANSFER_BULK,
...@@ -499,12 +498,13 @@ void UsbDeviceHandle::BulkTransfer(const UsbEndpointDirection direction, ...@@ -499,12 +498,13 @@ void UsbDeviceHandle::BulkTransfer(const UsbEndpointDirection direction,
callback)); callback));
} }
void UsbDeviceHandle::InterruptTransfer(const UsbEndpointDirection direction, void UsbDeviceHandleImpl::InterruptTransfer(
const uint8 endpoint, const UsbEndpointDirection direction,
net::IOBuffer* buffer, const uint8 endpoint,
const size_t length, net::IOBuffer* buffer,
const unsigned int timeout, const size_t length,
const UsbTransferCallback& callback) { const unsigned int timeout,
const UsbTransferCallback& callback) {
if (!device_) { if (!device_) {
callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0);
return; return;
...@@ -522,7 +522,7 @@ void UsbDeviceHandle::InterruptTransfer(const UsbEndpointDirection direction, ...@@ -522,7 +522,7 @@ void UsbDeviceHandle::InterruptTransfer(const UsbEndpointDirection direction,
timeout); timeout);
BrowserThread::PostTask(BrowserThread::FILE, BrowserThread::PostTask(BrowserThread::FILE,
FROM_HERE, FROM_HERE,
base::Bind(&UsbDeviceHandle::SubmitTransfer, base::Bind(&UsbDeviceHandleImpl::SubmitTransfer,
this, this,
transfer, transfer,
USB_TRANSFER_INTERRUPT, USB_TRANSFER_INTERRUPT,
...@@ -532,14 +532,15 @@ void UsbDeviceHandle::InterruptTransfer(const UsbEndpointDirection direction, ...@@ -532,14 +532,15 @@ void UsbDeviceHandle::InterruptTransfer(const UsbEndpointDirection direction,
callback)); callback));
} }
void UsbDeviceHandle::IsochronousTransfer(const UsbEndpointDirection direction, void UsbDeviceHandleImpl::IsochronousTransfer(
const uint8 endpoint, const UsbEndpointDirection direction,
net::IOBuffer* buffer, const uint8 endpoint,
const size_t length, net::IOBuffer* buffer,
const unsigned int packets, const size_t length,
const unsigned int packet_length, const unsigned int packets,
const unsigned int timeout, const unsigned int packet_length,
const UsbTransferCallback& callback) { const unsigned int timeout,
const UsbTransferCallback& callback) {
if (!device_) { if (!device_) {
callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0);
return; return;
...@@ -564,7 +565,7 @@ void UsbDeviceHandle::IsochronousTransfer(const UsbEndpointDirection direction, ...@@ -564,7 +565,7 @@ void UsbDeviceHandle::IsochronousTransfer(const UsbEndpointDirection direction,
BrowserThread::PostTask(BrowserThread::FILE, BrowserThread::PostTask(BrowserThread::FILE,
FROM_HERE, FROM_HERE,
base::Bind(&UsbDeviceHandle::SubmitTransfer, base::Bind(&UsbDeviceHandleImpl::SubmitTransfer,
this, this,
transfer, transfer,
USB_TRANSFER_ISOCHRONOUS, USB_TRANSFER_ISOCHRONOUS,
...@@ -574,7 +575,7 @@ void UsbDeviceHandle::IsochronousTransfer(const UsbEndpointDirection direction, ...@@ -574,7 +575,7 @@ void UsbDeviceHandle::IsochronousTransfer(const UsbEndpointDirection direction,
callback)); callback));
} }
void UsbDeviceHandle::RefreshEndpointMap() { void UsbDeviceHandleImpl::RefreshEndpointMap() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
endpoint_map_.clear(); endpoint_map_.clear();
for (ClaimedInterfaceMap::iterator it = claimed_interfaces_.begin(); for (ClaimedInterfaceMap::iterator it = claimed_interfaces_.begin();
...@@ -591,15 +592,15 @@ void UsbDeviceHandle::RefreshEndpointMap() { ...@@ -591,15 +592,15 @@ void UsbDeviceHandle::RefreshEndpointMap() {
} }
} }
scoped_refptr<UsbDeviceHandle::InterfaceClaimer> scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer>
UsbDeviceHandle::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { UsbDeviceHandleImpl::GetClaimedInterfaceForEndpoint(unsigned char endpoint) {
unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK;
if (ContainsKey(endpoint_map_, address)) if (ContainsKey(endpoint_map_, address))
return claimed_interfaces_[endpoint_map_[address]]; return claimed_interfaces_[endpoint_map_[address]];
return NULL; return NULL;
} }
void UsbDeviceHandle::SubmitTransfer( void UsbDeviceHandleImpl::SubmitTransfer(
PlatformUsbTransferHandle handle, PlatformUsbTransferHandle handle,
UsbTransferType transfer_type, UsbTransferType transfer_type,
net::IOBuffer* buffer, net::IOBuffer* buffer,
...@@ -635,7 +636,7 @@ void UsbDeviceHandle::SubmitTransfer( ...@@ -635,7 +636,7 @@ void UsbDeviceHandle::SubmitTransfer(
} }
} }
void UsbDeviceHandle::InternalClose() { void UsbDeviceHandleImpl::InternalClose() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
if (!device_) if (!device_)
return; return;
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_
#define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_
#include <map>
#include <vector>
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string16.h"
#include "base/threading/thread_checker.h"
#include "components/usb_service/usb_device_handle.h"
#include "components/usb_service/usb_interface.h"
#include "net/base/io_buffer.h"
struct libusb_device_handle;
struct libusb_iso_packet_descriptor;
struct libusb_transfer;
namespace base {
class MessageLoopProxy;
}
namespace usb_service {
class UsbContext;
class UsbConfigDescriptor;
class UsbDeviceImpl;
typedef libusb_device_handle* PlatformUsbDeviceHandle;
typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
typedef libusb_transfer* PlatformUsbTransferHandle;
// UsbDeviceHandle class provides basic I/O related functionalities.
class UsbDeviceHandleImpl : public UsbDeviceHandle {
public:
virtual scoped_refptr<UsbDevice> GetDevice() const OVERRIDE;
virtual void Close() OVERRIDE;
virtual bool ClaimInterface(const int interface_number) OVERRIDE;
virtual bool ReleaseInterface(const int interface_number) OVERRIDE;
virtual bool SetInterfaceAlternateSetting(
const int interface_number,
const int alternate_setting) OVERRIDE;
virtual bool ResetDevice() OVERRIDE;
virtual bool GetSerial(base::string16* serial) OVERRIDE;
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 UsbTransferCallback& callback) OVERRIDE;
virtual void BulkTransfer(const UsbEndpointDirection direction,
const uint8 endpoint,
net::IOBuffer* buffer,
const size_t length,
const unsigned int timeout,
const UsbTransferCallback& callback) OVERRIDE;
virtual void InterruptTransfer(const UsbEndpointDirection direction,
const uint8 endpoint,
net::IOBuffer* buffer,
const size_t length,
const unsigned int timeout,
const UsbTransferCallback& callback) OVERRIDE;
virtual void IsochronousTransfer(
const UsbEndpointDirection direction,
const uint8 endpoint,
net::IOBuffer* buffer,
const size_t length,
const unsigned int packets,
const unsigned int packet_length,
const unsigned int timeout,
const UsbTransferCallback& callback) OVERRIDE;
PlatformUsbDeviceHandle handle() const { return handle_; }
protected:
friend class UsbDeviceImpl;
// This constructor is called by UsbDevice.
UsbDeviceHandleImpl(scoped_refptr<UsbContext> context,
UsbDeviceImpl* device,
PlatformUsbDeviceHandle handle,
scoped_refptr<UsbConfigDescriptor> interfaces);
virtual ~UsbDeviceHandleImpl();
private:
friend void HandleTransferCompletion(PlatformUsbTransferHandle handle);
class InterfaceClaimer;
struct Transfer;
// Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
// SetInterfaceAlternateSetting.
void RefreshEndpointMap();
// Look up the claimed interface by endpoint. Return NULL if the interface
// of the endpoint is not found.
scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
unsigned char endpoint);
// 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,
scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
const UsbTransferCallback& callback);
// Invokes the callbacks associated with a given transfer, and removes it from
// the in-flight transfer set.
void TransferComplete(PlatformUsbTransferHandle transfer);
// Informs the object to drop internal references.
void InternalClose();
UsbDeviceImpl* device_;
PlatformUsbDeviceHandle handle_;
scoped_refptr<UsbConfigDescriptor> interfaces_;
typedef std::map<int, scoped_refptr<InterfaceClaimer> > ClaimedInterfaceMap;
ClaimedInterfaceMap claimed_interfaces_;
typedef std::map<PlatformUsbTransferHandle, Transfer> TransferMap;
TransferMap transfers_;
// A map from endpoints to interfaces
typedef std::map<int, int> EndpointMap;
EndpointMap endpoint_map_;
// Retain the UsbContext so that the platform context will not be destroyed
// before this handle.
scoped_refptr<UsbContext> context_;
base::ThreadChecker thread_checker_;
DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl);
};
} // namespace usb_service
#endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include "base/stl_util.h" #include "base/stl_util.h"
#include "components/usb_service/usb_context.h" #include "components/usb_service/usb_context.h"
#include "components/usb_service/usb_device_handle.h" #include "components/usb_service/usb_device_handle_impl.h"
#include "components/usb_service/usb_interface_impl.h" #include "components/usb_service/usb_interface_impl.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "third_party/libusb/src/libusb/libusb.h" #include "third_party/libusb/src/libusb/libusb.h"
...@@ -98,8 +98,8 @@ scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() { ...@@ -98,8 +98,8 @@ scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() {
scoped_refptr<UsbConfigDescriptor> interfaces = ListInterfaces(); scoped_refptr<UsbConfigDescriptor> interfaces = ListInterfaces();
if (!interfaces) if (!interfaces)
return NULL; return NULL;
scoped_refptr<UsbDeviceHandle> device_handle = scoped_refptr<UsbDeviceHandleImpl> device_handle =
new UsbDeviceHandle(context_, this, handle, interfaces); new UsbDeviceHandleImpl(context_, this, handle, interfaces);
handles_.push_back(device_handle); handles_.push_back(device_handle);
return device_handle; return device_handle;
} }
...@@ -136,12 +136,8 @@ void UsbDeviceImpl::OnDisconnect() { ...@@ -136,12 +136,8 @@ void UsbDeviceImpl::OnDisconnect() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
HandlesVector handles; HandlesVector handles;
swap(handles, handles_); swap(handles, handles_);
for (std::vector<scoped_refptr<UsbDeviceHandle> >::iterator it = for (HandlesVector::iterator it = handles.begin(); it != handles.end(); ++it)
handles.begin();
it != handles.end();
++it) {
(*it)->InternalClose(); (*it)->InternalClose();
}
} }
} // namespace usb_service } // namespace usb_service
...@@ -17,7 +17,7 @@ struct libusb_config_descriptor; ...@@ -17,7 +17,7 @@ struct libusb_config_descriptor;
namespace usb_service { namespace usb_service {
class UsbDeviceHandle; class UsbDeviceHandleImpl;
class UsbContext; class UsbContext;
typedef libusb_device* PlatformUsbDevice; typedef libusb_device* PlatformUsbDevice;
...@@ -58,7 +58,7 @@ class UsbDeviceImpl : public UsbDevice { ...@@ -58,7 +58,7 @@ class UsbDeviceImpl : public UsbDevice {
scoped_refptr<UsbContext> context_; scoped_refptr<UsbContext> context_;
// Opened handles. // Opened handles.
typedef std::vector<scoped_refptr<UsbDeviceHandle> > HandlesVector; typedef std::vector<scoped_refptr<UsbDeviceHandleImpl> > HandlesVector;
HandlesVector handles_; HandlesVector handles_;
DISALLOW_COPY_AND_ASSIGN(UsbDeviceImpl); DISALLOW_COPY_AND_ASSIGN(UsbDeviceImpl);
......
...@@ -464,15 +464,15 @@ UsbAsyncApiFunction::GetDeviceHandleOrCompleteWithError( ...@@ -464,15 +464,15 @@ UsbAsyncApiFunction::GetDeviceHandleOrCompleteWithError(
return NULL; return NULL;
} }
if (!resource->device() || !resource->device()->device()) { if (!resource->device() || !resource->device()->GetDevice()) {
CompleteWithError(kErrorDisconnect); CompleteWithError(kErrorDisconnect);
manager_->Remove(extension_->id(), input_device_handle.handle); manager_->Remove(extension_->id(), input_device_handle.handle);
return NULL; return NULL;
} }
if (resource->device()->device()->vendor_id() != if (resource->device()->GetDevice()->vendor_id() !=
input_device_handle.vendor_id || input_device_handle.vendor_id ||
resource->device()->device()->product_id() != resource->device()->GetDevice()->product_id() !=
input_device_handle.product_id) { input_device_handle.product_id) {
CompleteWithError(kErrorNoDevice); CompleteWithError(kErrorNoDevice);
return NULL; return NULL;
...@@ -721,8 +721,8 @@ void UsbOpenDeviceFunction::AsyncWorkStart() { ...@@ -721,8 +721,8 @@ void UsbOpenDeviceFunction::AsyncWorkStart() {
SetResult(PopulateConnectionHandle( SetResult(PopulateConnectionHandle(
manager_->Add(new UsbDeviceResource(extension_->id(), handle_)), manager_->Add(new UsbDeviceResource(extension_->id(), handle_)),
handle_->device()->vendor_id(), handle_->GetDevice()->vendor_id(),
handle_->device()->product_id())); handle_->GetDevice()->product_id()));
AsyncWorkCompleted(); AsyncWorkCompleted();
} }
...@@ -745,7 +745,7 @@ void UsbListInterfacesFunction::AsyncWorkStart() { ...@@ -745,7 +745,7 @@ void UsbListInterfacesFunction::AsyncWorkStart() {
return; return;
scoped_refptr<UsbConfigDescriptor> config = scoped_refptr<UsbConfigDescriptor> config =
device_handle->device()->ListInterfaces(); device_handle->GetDevice()->ListInterfaces();
if (!config) { if (!config) {
SetError(kErrorCannotListInterfaces); SetError(kErrorCannotListInterfaces);
......
...@@ -86,10 +86,21 @@ class MockUsbDeviceHandle : public UsbDeviceHandle { ...@@ -86,10 +86,21 @@ class MockUsbDeviceHandle : public UsbDeviceHandle {
const UsbTransferCallback& callback)); const UsbTransferCallback& callback));
MOCK_METHOD0(ResetDevice, bool()); MOCK_METHOD0(ResetDevice, bool());
MOCK_METHOD1(ClaimInterface, bool(const int interface_number));
MOCK_METHOD1(ReleaseInterface, bool(const int interface_number));
MOCK_METHOD2(SetInterfaceAlternateSetting,
bool(const int interface_number, const int alternate_setting));
MOCK_METHOD1(GetSerial, bool(base::string16* serial));
virtual scoped_refptr<UsbDevice> GetDevice() const OVERRIDE {
return device_;
}
void set_device(UsbDevice* device) { device_ = device; } void set_device(UsbDevice* device) { device_ = device; }
protected: protected:
UsbDevice* device_;
virtual ~MockUsbDeviceHandle() {} virtual ~MockUsbDeviceHandle() {}
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment