diff options
author | rockot@chromium.org <rockot@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-22 17:10:01 +0000 |
---|---|---|
committer | rockot@chromium.org <rockot@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-22 17:10:01 +0000 |
commit | b5508682f4eaa41e0dec82d01b4e2046adff41c8 (patch) | |
tree | a93e3e357daed1675ac6cf6a8a2cfeeafadecf73 | |
parent | cbd6d7038b8af7c86cdf875d2697e0ca81a40b95 (diff) | |
download | chromium_src-b5508682f4eaa41e0dec82d01b4e2046adff41c8.zip chromium_src-b5508682f4eaa41e0dec82d01b4e2046adff41c8.tar.gz chromium_src-b5508682f4eaa41e0dec82d01b4e2046adff41c8.tar.bz2 |
Move UsbService to its own component.
This establishes a new usb_service component from
the former contents of //chrome/browser/usb so that
non-Chrome browser code may also depend upon it.
BUG=361000
R=pfeldman@chromium.org
TBR=derat@chromium.org for DEPS +chromeos
TBR=cbentzel@chromium.org for DEPS +net/base
Review URL: https://codereview.chromium.org/236203019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@265290 0039d316-1c4b-4281-b951-d872f2087c98
30 files changed, 586 insertions, 367 deletions
diff --git a/chrome/browser/devtools/device/usb/DEPS b/chrome/browser/devtools/device/usb/DEPS new file mode 100644 index 0000000..fc9564a --- /dev/null +++ b/chrome/browser/devtools/device/usb/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+components/usb_service" +] diff --git a/chrome/browser/devtools/device/usb/android_usb_device.cc b/chrome/browser/devtools/device/usb/android_usb_device.cc index cd2845e..debde2e 100644 --- a/chrome/browser/devtools/device/usb/android_usb_device.cc +++ b/chrome/browser/devtools/device/usb/android_usb_device.cc @@ -16,15 +16,24 @@ #include "base/strings/utf_string_conversions.h" #include "chrome/browser/devtools/device/usb/android_rsa.h" #include "chrome/browser/devtools/device/usb/android_usb_socket.h" -#include "chrome/browser/usb/usb_device.h" -#include "chrome/browser/usb/usb_interface.h" -#include "chrome/browser/usb/usb_service.h" +#include "components/usb_service/usb_device.h" +#include "components/usb_service/usb_interface.h" +#include "components/usb_service/usb_service.h" #include "content/public/browser/browser_thread.h" #include "crypto/rsa_private_key.h" #include "net/base/ip_endpoint.h" #include "net/base/net_errors.h" #include "net/socket/stream_socket.h" +using usb_service::UsbConfigDescriptor; +using usb_service::UsbDevice; +using usb_service::UsbDeviceHandle; +using usb_service::UsbInterfaceAltSettingDescriptor; +using usb_service::UsbInterfaceDescriptor; +using usb_service::UsbEndpointDescriptor; +using usb_service::UsbService; +using usb_service::UsbTransferStatus; + namespace { const size_t kHeaderSize = 24; @@ -50,7 +59,7 @@ base::LazyInstance<std::vector<AndroidUsbDevice*> >::Leaky g_devices = LAZY_INSTANCE_INITIALIZER; bool IsAndroidInterface( - scoped_refptr<const UsbInterfaceDescriptor> interface) { + scoped_refptr<const usb_service::UsbInterfaceDescriptor> interface) { if (interface->GetNumAltSettings() == 0) return false; @@ -81,9 +90,9 @@ scoped_refptr<AndroidUsbDevice> ClaimInterface( for (size_t i = 0; i < idesc->GetNumEndpoints(); ++i) { scoped_refptr<const UsbEndpointDescriptor> edesc = idesc->GetEndpoint(i); - if (edesc->GetTransferType() != USB_TRANSFER_BULK) + if (edesc->GetTransferType() != usb_service::USB_TRANSFER_BULK) continue; - if (edesc->GetDirection() == USB_DIRECTION_INBOUND) + if (edesc->GetDirection() == usb_service::USB_DIRECTION_INBOUND) inbound_address = edesc->GetAddress(); else outbound_address = edesc->GetAddress(); @@ -419,7 +428,8 @@ void AndroidUsbDevice::ProcessOutgoing() { BulkMessage message = outgoing_queue_.front(); outgoing_queue_.pop(); DumpMessage(true, message.first->data(), message.second); - usb_handle_->BulkTransfer(USB_DIRECTION_OUTBOUND, outbound_address_, + usb_handle_->BulkTransfer( + usb_service::USB_DIRECTION_OUTBOUND, outbound_address_, message.first, message.second, kUsbTimeout, base::Bind(&AndroidUsbDevice::OutgoingMessageSent, weak_factory_.GetWeakPtr())); @@ -430,7 +440,7 @@ void AndroidUsbDevice::OutgoingMessageSent(UsbTransferStatus status, size_t result) { DCHECK(message_loop_ == base::MessageLoop::current()); - if (status != USB_TRANSFER_COMPLETED) + if (status != usb_service::USB_TRANSFER_COMPLETED) return; message_loop_->PostTask(FROM_HERE, base::Bind(&AndroidUsbDevice::ProcessOutgoing, this)); @@ -442,7 +452,8 @@ void AndroidUsbDevice::ReadHeader() { if (!usb_handle_) return; scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kHeaderSize); - usb_handle_->BulkTransfer(USB_DIRECTION_INBOUND, inbound_address_, + usb_handle_->BulkTransfer( + usb_service::USB_DIRECTION_INBOUND, inbound_address_, buffer, kHeaderSize, kUsbTimeout, base::Bind(&AndroidUsbDevice::ParseHeader, weak_factory_.GetWeakPtr())); @@ -453,13 +464,13 @@ void AndroidUsbDevice::ParseHeader(UsbTransferStatus status, size_t result) { DCHECK(message_loop_ == base::MessageLoop::current()); - if (status == USB_TRANSFER_TIMEOUT) { + if (status == usb_service::USB_TRANSFER_TIMEOUT) { message_loop_->PostTask(FROM_HERE, base::Bind(&AndroidUsbDevice::ReadHeader, this)); return; } - if (status != USB_TRANSFER_COMPLETED || result != kHeaderSize) { + if (status != usb_service::USB_TRANSFER_COMPLETED || result != kHeaderSize) { TransferError(status); return; } @@ -473,7 +484,7 @@ void AndroidUsbDevice::ParseHeader(UsbTransferStatus status, uint32 data_check = header[4]; uint32 magic = header[5]; if ((message->command ^ 0xffffffff) != magic) { - TransferError(USB_TRANSFER_ERROR); + TransferError(usb_service::USB_TRANSFER_ERROR); return; } @@ -497,7 +508,8 @@ void AndroidUsbDevice::ReadBody(scoped_refptr<AdbMessage> message, if (!usb_handle_) return; scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data_length); - usb_handle_->BulkTransfer(USB_DIRECTION_INBOUND, inbound_address_, + usb_handle_->BulkTransfer( + usb_service::USB_DIRECTION_INBOUND, inbound_address_, buffer, data_length, kUsbTimeout, base::Bind(&AndroidUsbDevice::ParseBody, weak_factory_.GetWeakPtr(), message, data_length, data_check)); @@ -511,14 +523,14 @@ void AndroidUsbDevice::ParseBody(scoped_refptr<AdbMessage> message, size_t result) { DCHECK(message_loop_ == base::MessageLoop::current()); - if (status == USB_TRANSFER_TIMEOUT) { + if (status == usb_service::USB_TRANSFER_TIMEOUT) { message_loop_->PostTask(FROM_HERE, base::Bind(&AndroidUsbDevice::ReadBody, this, message, data_length, data_check)); return; } - if (status != USB_TRANSFER_COMPLETED || + if (status != usb_service::USB_TRANSFER_COMPLETED || static_cast<uint32>(result) != data_length) { TransferError(status); return; @@ -527,7 +539,7 @@ void AndroidUsbDevice::ParseBody(scoped_refptr<AdbMessage> message, DumpMessage(false, buffer->data(), data_length); message->body = std::string(buffer->data(), result); if (Checksum(message->body) != data_check) { - TransferError(USB_TRANSFER_ERROR); + TransferError(usb_service::USB_TRANSFER_ERROR); return; } diff --git a/chrome/browser/devtools/device/usb/android_usb_device.h b/chrome/browser/devtools/device/usb/android_usb_device.h index e2a744f..f27df16 100644 --- a/chrome/browser/devtools/device/usb/android_usb_device.h +++ b/chrome/browser/devtools/device/usb/android_usb_device.h @@ -10,7 +10,7 @@ #include <vector> #include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" -#include "chrome/browser/usb/usb_device_handle.h" +#include "components/usb_service/usb_device_handle.h" namespace base { class MessageLoop; @@ -73,7 +73,7 @@ class AndroidUsbDevice : public base::RefCountedThreadSafe<AndroidUsbDevice> { static void CountDevices(const base::Callback<void(int)>& callback); AndroidUsbDevice(crypto::RSAPrivateKey* rsa_key, - scoped_refptr<UsbDeviceHandle> device, + scoped_refptr<usb_service::UsbDeviceHandle> device, const std::string& serial, int inbound_address, int outbound_address, @@ -89,7 +89,9 @@ class AndroidUsbDevice : public base::RefCountedThreadSafe<AndroidUsbDevice> { uint32 arg1, const std::string& body); - scoped_refptr<UsbDeviceHandle> usb_device() { return usb_handle_; } + scoped_refptr<usb_service::UsbDeviceHandle> usb_device() { + return usb_handle_; + } std::string serial() { return serial_; } @@ -101,12 +103,12 @@ class AndroidUsbDevice : public base::RefCountedThreadSafe<AndroidUsbDevice> { void Queue(scoped_refptr<AdbMessage> message); void ProcessOutgoing(); - void OutgoingMessageSent(UsbTransferStatus status, + void OutgoingMessageSent(usb_service::UsbTransferStatus status, scoped_refptr<net::IOBuffer> buffer, size_t result); void ReadHeader(); - void ParseHeader(UsbTransferStatus status, + void ParseHeader(usb_service::UsbTransferStatus status, scoped_refptr<net::IOBuffer> buffer, size_t result); @@ -116,15 +118,16 @@ class AndroidUsbDevice : public base::RefCountedThreadSafe<AndroidUsbDevice> { void ParseBody(scoped_refptr<AdbMessage> message, uint32 data_length, uint32 data_check, - UsbTransferStatus status, + usb_service::UsbTransferStatus status, scoped_refptr<net::IOBuffer> buffer, size_t result); void HandleIncoming(scoped_refptr<AdbMessage> message); - void TransferError(UsbTransferStatus status); + void TransferError(usb_service::UsbTransferStatus status); - void TerminateIfReleased(scoped_refptr<UsbDeviceHandle> usb_handle); + void TerminateIfReleased( + scoped_refptr<usb_service::UsbDeviceHandle> usb_handle); void Terminate(); void SocketDeleted(uint32 socket_id); @@ -134,7 +137,7 @@ class AndroidUsbDevice : public base::RefCountedThreadSafe<AndroidUsbDevice> { scoped_ptr<crypto::RSAPrivateKey> rsa_key_; // Device info - scoped_refptr<UsbDeviceHandle> usb_handle_; + scoped_refptr<usb_service::UsbDeviceHandle> usb_handle_; std::string serial_; int inbound_address_; int outbound_address_; diff --git a/chrome/browser/extensions/api/usb/DEPS b/chrome/browser/extensions/api/usb/DEPS new file mode 100644 index 0000000..70ac41d --- /dev/null +++ b/chrome/browser/extensions/api/usb/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+components/usb_service", +] diff --git a/chrome/browser/extensions/api/usb/usb_api.cc b/chrome/browser/extensions/api/usb/usb_api.cc index 0d23ae9..2df4c20 100644 --- a/chrome/browser/extensions/api/usb/usb_api.cc +++ b/chrome/browser/extensions/api/usb/usb_api.cc @@ -10,9 +10,9 @@ #include "base/memory/scoped_ptr.h" #include "base/message_loop/message_loop_proxy.h" #include "chrome/browser/extensions/api/usb/usb_device_resource.h" -#include "chrome/browser/usb/usb_device_handle.h" -#include "chrome/browser/usb/usb_service.h" #include "chrome/common/extensions/api/usb.h" +#include "components/usb_service/usb_device_handle.h" +#include "components/usb_service/usb_service.h" #include "extensions/browser/extension_system.h" #include "extensions/common/permissions/permissions_data.h" #include "extensions/common/permissions/usb_device_permission.h" @@ -49,6 +49,18 @@ using usb::RequestType; using usb::SynchronizationType; using usb::TransferType; using usb::UsageType; +using usb_service::UsbConfigDescriptor; +using usb_service::UsbDevice; +using usb_service::UsbDeviceHandle; +using usb_service::UsbEndpointDescriptor; +using usb_service::UsbEndpointDirection; +using usb_service::UsbInterfaceAltSettingDescriptor; +using usb_service::UsbInterfaceDescriptor; +using usb_service::UsbService; +using usb_service::UsbSynchronizationType; +using usb_service::UsbTransferStatus; +using usb_service::UsbTransferType; +using usb_service::UsbUsageType; typedef std::vector<scoped_refptr<UsbDevice> > DeviceVector; typedef scoped_ptr<DeviceVector> ScopedDeviceVector; @@ -105,10 +117,10 @@ UsbDevice* g_device_for_test = NULL; bool ConvertDirectionToApi(const UsbEndpointDirection& input, Direction* output) { switch (input) { - case USB_DIRECTION_INBOUND: + case usb_service::USB_DIRECTION_INBOUND: *output = usb::DIRECTION_IN; return true; - case USB_DIRECTION_OUTBOUND: + case usb_service::USB_DIRECTION_OUTBOUND: *output = usb::DIRECTION_OUT; return true; default: @@ -120,16 +132,16 @@ bool ConvertDirectionToApi(const UsbEndpointDirection& input, bool ConvertSynchronizationTypeToApi(const UsbSynchronizationType& input, usb::SynchronizationType* output) { switch (input) { - case USB_SYNCHRONIZATION_NONE: + case usb_service::USB_SYNCHRONIZATION_NONE: *output = usb::SYNCHRONIZATION_TYPE_NONE; return true; - case USB_SYNCHRONIZATION_ASYNCHRONOUS: + case usb_service::USB_SYNCHRONIZATION_ASYNCHRONOUS: *output = usb::SYNCHRONIZATION_TYPE_ASYNCHRONOUS; return true; - case USB_SYNCHRONIZATION_ADAPTIVE: + case usb_service::USB_SYNCHRONIZATION_ADAPTIVE: *output = usb::SYNCHRONIZATION_TYPE_ADAPTIVE; return true; - case USB_SYNCHRONIZATION_SYNCHRONOUS: + case usb_service::USB_SYNCHRONIZATION_SYNCHRONOUS: *output = usb::SYNCHRONIZATION_TYPE_SYNCHRONOUS; return true; default: @@ -142,16 +154,16 @@ bool ConvertTransferTypeToApi( const UsbTransferType& input, usb::TransferType* output) { switch (input) { - case USB_TRANSFER_CONTROL: + case usb_service::USB_TRANSFER_CONTROL: *output = usb::TRANSFER_TYPE_CONTROL; return true; - case USB_TRANSFER_INTERRUPT: + case usb_service::USB_TRANSFER_INTERRUPT: *output = usb::TRANSFER_TYPE_INTERRUPT; return true; - case USB_TRANSFER_ISOCHRONOUS: + case usb_service::USB_TRANSFER_ISOCHRONOUS: *output = usb::TRANSFER_TYPE_ISOCHRONOUS; return true; - case USB_TRANSFER_BULK: + case usb_service::USB_TRANSFER_BULK: *output = usb::TRANSFER_TYPE_BULK; return true; default: @@ -162,13 +174,13 @@ bool ConvertTransferTypeToApi( bool ConvertUsageTypeToApi(const UsbUsageType& input, usb::UsageType* output) { switch (input) { - case USB_USAGE_DATA: + case usb_service::USB_USAGE_DATA: *output = usb::USAGE_TYPE_DATA; return true; - case USB_USAGE_FEEDBACK: + case usb_service::USB_USAGE_FEEDBACK: *output = usb::USAGE_TYPE_FEEDBACK; return true; - case USB_USAGE_EXPLICIT_FEEDBACK: + case usb_service::USB_USAGE_EXPLICIT_FEEDBACK: *output = usb::USAGE_TYPE_EXPLICITFEEDBACK; return true; default: @@ -181,10 +193,10 @@ bool ConvertDirection(const Direction& input, UsbEndpointDirection* output) { switch (input) { case usb::DIRECTION_IN: - *output = USB_DIRECTION_INBOUND; + *output = usb_service::USB_DIRECTION_INBOUND; return true; case usb::DIRECTION_OUT: - *output = USB_DIRECTION_OUTBOUND; + *output = usb_service::USB_DIRECTION_OUTBOUND; return true; default: NOTREACHED(); @@ -265,9 +277,9 @@ scoped_refptr<net::IOBuffer> CreateBufferForTransfer( scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(std::max( static_cast<size_t>(1), size)); - if (direction == USB_DIRECTION_INBOUND) { + if (direction == usb_service::USB_DIRECTION_INBOUND) { return buffer; - } else if (direction == USB_DIRECTION_OUTBOUND) { + } else if (direction == usb_service::USB_DIRECTION_OUTBOUND) { if (input.data.get() && size <= input.data->size()) { memcpy(buffer->data(), input.data->data(), size); return buffer; @@ -279,21 +291,21 @@ scoped_refptr<net::IOBuffer> CreateBufferForTransfer( const char* ConvertTransferStatusToErrorString(const UsbTransferStatus status) { switch (status) { - case USB_TRANSFER_COMPLETED: + case usb_service::USB_TRANSFER_COMPLETED: return ""; - case USB_TRANSFER_ERROR: + case usb_service::USB_TRANSFER_ERROR: return kErrorGeneric; - case USB_TRANSFER_TIMEOUT: + case usb_service::USB_TRANSFER_TIMEOUT: return kErrorTimeout; - case USB_TRANSFER_CANCELLED: + case usb_service::USB_TRANSFER_CANCELLED: return kErrorCancelled; - case USB_TRANSFER_STALLED: + case usb_service::USB_TRANSFER_STALLED: return kErrorStalled; - case USB_TRANSFER_DISCONNECT: + case usb_service::USB_TRANSFER_DISCONNECT: return kErrorDisconnect; - case USB_TRANSFER_OVERFLOW: + case usb_service::USB_TRANSFER_OVERFLOW: return kErrorOverflow; - case USB_TRANSFER_LENGTH_SHORT: + case usb_service::USB_TRANSFER_LENGTH_SHORT: return kErrorTransferLength; default: NOTREACHED(); @@ -489,7 +501,7 @@ UsbAsyncApiTransferFunction::~UsbAsyncApiTransferFunction() {} void UsbAsyncApiTransferFunction::OnCompleted(UsbTransferStatus status, scoped_refptr<net::IOBuffer> data, size_t length) { - if (status != USB_TRANSFER_COMPLETED) + if (status != usb_service::USB_TRANSFER_COMPLETED) SetError(ConvertTransferStatusToErrorString(status)); SetResult(CreateTransferInfo(status, data, length)); diff --git a/chrome/browser/extensions/api/usb/usb_api.h b/chrome/browser/extensions/api/usb/usb_api.h index 9262eca..12e572c 100644 --- a/chrome/browser/extensions/api/usb/usb_api.h +++ b/chrome/browser/extensions/api/usb/usb_api.h @@ -10,17 +10,13 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" -#include "chrome/browser/usb/usb_device.h" -#include "chrome/browser/usb/usb_device_handle.h" #include "chrome/common/extensions/api/usb.h" +#include "components/usb_service/usb_device.h" +#include "components/usb_service/usb_device_handle.h" #include "extensions/browser/api/api_resource_manager.h" #include "extensions/browser/api/async_api_function.h" #include "net/base/io_buffer.h" -class UsbDevice; -class UsbDeviceHandle; -class UsbService; - namespace extensions { class UsbDeviceResource; @@ -35,11 +31,12 @@ class UsbAsyncApiFunction : public AsyncApiFunction { virtual bool PrePrepare() OVERRIDE; virtual bool Respond() OVERRIDE; - scoped_refptr<UsbDevice> GetDeviceOrOrCompleteWithError( + scoped_refptr<usb_service::UsbDevice> GetDeviceOrOrCompleteWithError( const extensions::api::usb::Device& input_device); - scoped_refptr<UsbDeviceHandle> GetDeviceHandleOrCompleteWithError( - const extensions::api::usb::ConnectionHandle& input_device_handle); + scoped_refptr<usb_service::UsbDeviceHandle> + GetDeviceHandleOrCompleteWithError( + const extensions::api::usb::ConnectionHandle& input_device_handle); void RemoveUsbDeviceResource(int api_resource_id); @@ -54,13 +51,15 @@ class UsbAsyncApiTransferFunction : public UsbAsyncApiFunction { virtual ~UsbAsyncApiTransferFunction(); bool ConvertDirectionSafely(const extensions::api::usb::Direction& input, - UsbEndpointDirection* output); - bool ConvertRequestTypeSafely(const extensions::api::usb::RequestType& input, - UsbDeviceHandle::TransferRequestType* output); - bool ConvertRecipientSafely(const extensions::api::usb::Recipient& input, - UsbDeviceHandle::TransferRecipient* output); - - void OnCompleted(UsbTransferStatus status, + usb_service::UsbEndpointDirection* output); + bool ConvertRequestTypeSafely( + const extensions::api::usb::RequestType& input, + usb_service::UsbDeviceHandle::TransferRequestType* output); + bool ConvertRecipientSafely( + const extensions::api::usb::Recipient& input, + usb_service::UsbDeviceHandle::TransferRecipient* output); + + void OnCompleted(usb_service::UsbTransferStatus status, scoped_refptr<net::IOBuffer> data, size_t length); }; @@ -78,9 +77,10 @@ class UsbFindDevicesFunction : public UsbAsyncApiFunction { virtual void AsyncWorkStart() OVERRIDE; private: - void OpenDevices(scoped_ptr<std::vector<scoped_refptr<UsbDevice> > > devices); + void OpenDevices( + scoped_ptr<std::vector<scoped_refptr<usb_service::UsbDevice> > > devices); - std::vector<scoped_refptr<UsbDeviceHandle> > device_handles_; + std::vector<scoped_refptr<usb_service::UsbDeviceHandle> > device_handles_; scoped_ptr<extensions::api::usb::FindDevices::Params> parameters_; }; @@ -90,7 +90,7 @@ class UsbGetDevicesFunction : public UsbAsyncApiFunction { UsbGetDevicesFunction(); - static void SetDeviceForTest(UsbDevice* device); + static void SetDeviceForTest(usb_service::UsbDevice* device); virtual bool Prepare() OVERRIDE; virtual void AsyncWorkStart() OVERRIDE; @@ -100,7 +100,7 @@ class UsbGetDevicesFunction : public UsbAsyncApiFunction { private: void EnumerationCompletedFileThread( - scoped_ptr<std::vector<scoped_refptr<UsbDevice> > > devices); + scoped_ptr<std::vector<scoped_refptr<usb_service::UsbDevice> > > devices); scoped_ptr<extensions::api::usb::GetDevices::Params> parameters_; }; @@ -136,7 +136,7 @@ class UsbOpenDeviceFunction : public UsbAsyncApiFunction { virtual ~UsbOpenDeviceFunction(); private: - scoped_refptr<UsbDeviceHandle> handle_; + scoped_refptr<usb_service::UsbDeviceHandle> handle_; scoped_ptr<extensions::api::usb::OpenDevice::Params> parameters_; }; @@ -153,14 +153,14 @@ class UsbListInterfacesFunction : public UsbAsyncApiFunction { virtual void AsyncWorkStart() OVERRIDE; private: - bool ConvertDirectionSafely(const UsbEndpointDirection& input, + bool ConvertDirectionSafely(const usb_service::UsbEndpointDirection& input, extensions::api::usb::Direction* output); bool ConvertSynchronizationTypeSafely( - const UsbSynchronizationType& input, + const usb_service::UsbSynchronizationType& input, extensions::api::usb::SynchronizationType* output); - bool ConvertTransferTypeSafely(const UsbTransferType& input, + bool ConvertTransferTypeSafely(const usb_service::UsbTransferType& input, extensions::api::usb::TransferType* output); - bool ConvertUsageTypeSafely(const UsbUsageType& input, + bool ConvertUsageTypeSafely(const usb_service::UsbUsageType& input, extensions::api::usb::UsageType* output); scoped_ptr<base::ListValue> result_; diff --git a/chrome/browser/extensions/api/usb/usb_apitest.cc b/chrome/browser/extensions/api/usb/usb_apitest.cc index d5dc252..4f2d72f 100644 --- a/chrome/browser/extensions/api/usb/usb_apitest.cc +++ b/chrome/browser/extensions/api/usb/usb_apitest.cc @@ -13,6 +13,11 @@ using testing::AnyNumber; using testing::_; using testing::Return; using content::BrowserThread; +using usb_service::UsbConfigDescriptor; +using usb_service::UsbDevice; +using usb_service::UsbDeviceHandle; +using usb_service::UsbEndpointDirection; +using usb_service::UsbTransferCallback; namespace { @@ -116,9 +121,11 @@ IN_PROC_BROWSER_TEST_F(UsbApiTest, ResetDevice) { EXPECT_CALL(*mock_device_handle_.get(), ResetDevice()) .WillOnce(Return(true)) .WillOnce(Return(false)); - EXPECT_CALL(*mock_device_handle_.get(), - InterruptTransfer(USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) - .WillOnce(InvokeUsbTransferCallback<5>(USB_TRANSFER_COMPLETED)); + EXPECT_CALL( + *mock_device_handle_.get(), + InterruptTransfer(usb_service::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) + .WillOnce( + InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_COMPLETED)); ASSERT_TRUE(RunExtensionTest("usb/reset_device")); } @@ -131,42 +138,45 @@ IN_PROC_BROWSER_TEST_F(UsbApiTest, ListInterfaces) { IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferEvent) { EXPECT_CALL(*mock_device_handle_.get(), - ControlTransfer(USB_DIRECTION_OUTBOUND, + ControlTransfer(usb_service::USB_DIRECTION_OUTBOUND, UsbDeviceHandle::STANDARD, UsbDeviceHandle::DEVICE, - 1, - 2, - 3, - _, - 1, - _, - _)) - .WillOnce(InvokeUsbTransferCallback<9>(USB_TRANSFER_COMPLETED)); - EXPECT_CALL(*mock_device_handle_.get(), - BulkTransfer(USB_DIRECTION_OUTBOUND, 1, _, 1, _, _)) - .WillOnce(InvokeUsbTransferCallback<5>(USB_TRANSFER_COMPLETED)); + 1, 2, 3, _, 1, _, _)) + .WillOnce( + InvokeUsbTransferCallback<9>(usb_service::USB_TRANSFER_COMPLETED)); EXPECT_CALL(*mock_device_handle_.get(), - InterruptTransfer(USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) - .WillOnce(InvokeUsbTransferCallback<5>(USB_TRANSFER_COMPLETED)); + BulkTransfer(usb_service::USB_DIRECTION_OUTBOUND, 1, _, 1, _, _)) + .WillOnce( + InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_COMPLETED)); + EXPECT_CALL( + *mock_device_handle_.get(), + InterruptTransfer(usb_service::USB_DIRECTION_OUTBOUND, 2, _, 1, _, _)) + .WillOnce( + InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_COMPLETED)); EXPECT_CALL(*mock_device_handle_.get(), - IsochronousTransfer(USB_DIRECTION_OUTBOUND, 3, _, 1, 1, 1, _, _)) - .WillOnce(InvokeUsbTransferCallback<7>(USB_TRANSFER_COMPLETED)); + IsochronousTransfer( + usb_service::USB_DIRECTION_OUTBOUND, 3, _, 1, 1, 1, _, _)) + .WillOnce( + InvokeUsbTransferCallback<7>(usb_service::USB_TRANSFER_COMPLETED)); EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); ASSERT_TRUE(RunExtensionTest("usb/transfer_event")); } IN_PROC_BROWSER_TEST_F(UsbApiTest, ZeroLengthTransfer) { EXPECT_CALL(*mock_device_handle_.get(), BulkTransfer(_, _, _, 0, _, _)) - .WillOnce(InvokeUsbTransferCallback<5>(USB_TRANSFER_COMPLETED)); + .WillOnce( + InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_COMPLETED)); EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); ASSERT_TRUE(RunExtensionTest("usb/zero_length_transfer")); } IN_PROC_BROWSER_TEST_F(UsbApiTest, TransferFailure) { EXPECT_CALL(*mock_device_handle_.get(), BulkTransfer(_, _, _, _, _, _)) - .WillOnce(InvokeUsbTransferCallback<5>(USB_TRANSFER_COMPLETED)) - .WillOnce(InvokeUsbTransferCallback<5>(USB_TRANSFER_ERROR)) - .WillOnce(InvokeUsbTransferCallback<5>(USB_TRANSFER_TIMEOUT)); + .WillOnce( + InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_COMPLETED)) + .WillOnce(InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_ERROR)) + .WillOnce( + InvokeUsbTransferCallback<5>(usb_service::USB_TRANSFER_TIMEOUT)); EXPECT_CALL(*mock_device_handle_.get(), Close()).Times(AnyNumber()); ASSERT_TRUE(RunExtensionTest("usb/transfer_failure")); } diff --git a/chrome/browser/extensions/api/usb/usb_device_resource.cc b/chrome/browser/extensions/api/usb/usb_device_resource.cc index c359a6b..633635d 100644 --- a/chrome/browser/extensions/api/usb/usb_device_resource.cc +++ b/chrome/browser/extensions/api/usb/usb_device_resource.cc @@ -10,12 +10,13 @@ #include "base/bind.h" #include "base/bind_helpers.h" #include "base/synchronization/lock.h" -#include "chrome/browser/usb/usb_device_handle.h" #include "chrome/common/extensions/api/usb.h" +#include "components/usb_service/usb_device_handle.h" #include "content/public/browser/browser_thread.h" #include "extensions/browser/api/api_resource.h" using content::BrowserThread; +using usb_service::UsbDeviceHandle; namespace extensions { diff --git a/chrome/browser/extensions/api/usb/usb_device_resource.h b/chrome/browser/extensions/api/usb/usb_device_resource.h index b959daf..f07f2dc 100644 --- a/chrome/browser/extensions/api/usb/usb_device_resource.h +++ b/chrome/browser/extensions/api/usb/usb_device_resource.h @@ -12,14 +12,12 @@ #include "base/memory/linked_ptr.h" #include "base/memory/ref_counted.h" #include "base/synchronization/lock.h" -#include "chrome/browser/usb/usb_device_handle.h" #include "chrome/common/extensions/api/usb.h" +#include "components/usb_service/usb_device_handle.h" #include "content/public/browser/browser_thread.h" #include "extensions/browser/api/api_resource.h" #include "extensions/browser/api/api_resource_manager.h" -class UsbDeviceHandle; - namespace net { class IOBuffer; } // namespace net @@ -30,10 +28,10 @@ namespace extensions { class UsbDeviceResource : public ApiResource { public: UsbDeviceResource(const std::string& owner_extension_id, - scoped_refptr<UsbDeviceHandle> device); + scoped_refptr<usb_service::UsbDeviceHandle> device); virtual ~UsbDeviceResource(); - scoped_refptr<UsbDeviceHandle> device() { return device_; } + scoped_refptr<usb_service::UsbDeviceHandle> device() { return device_; } static const content::BrowserThread::ID kThreadId = content::BrowserThread::FILE; @@ -42,7 +40,7 @@ class UsbDeviceResource : public ApiResource { friend class ApiResourceManager<UsbDeviceResource>; static const char* service_name() { return "UsbDeviceResourceManager"; } - scoped_refptr<UsbDeviceHandle> device_; + scoped_refptr<usb_service::UsbDeviceHandle> device_; DISALLOW_COPY_AND_ASSIGN(UsbDeviceResource); }; diff --git a/chrome/browser/usb/DEPS b/chrome/browser/usb/DEPS deleted file mode 100644 index e3044a5..0000000 --- a/chrome/browser/usb/DEPS +++ /dev/null @@ -1,3 +0,0 @@ -include_rules = [ - "+third_party/libusb", -] diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 966bfb3..46c364b 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -2516,16 +2516,6 @@ 'browser/upgrade_detector_impl.h', 'browser/upload_list.cc', 'browser/upload_list.h', - 'browser/usb/usb_device.cc', - 'browser/usb/usb_device.h', - 'browser/usb/usb_device_handle.cc', - 'browser/usb/usb_device_handle.h', - 'browser/usb/usb_interface.cc', - 'browser/usb/usb_interface.h', - 'browser/usb/usb_context.cc', - 'browser/usb/usb_context.h', - 'browser/usb/usb_service.cc', - 'browser/usb/usb_service.h', 'browser/web_applications/update_shortcut_worker_win.cc', 'browser/web_applications/update_shortcut_worker_win.h', 'browser/web_applications/web_app.cc', @@ -2594,6 +2584,7 @@ '../components/components.gyp:storage_monitor', '../components/components.gyp:translate_content_browser', '../components/components.gyp:url_matcher', + '../components/components.gyp:usb_service', '../components/components.gyp:visitedlink_browser', '../components/components.gyp:visitedlink_common', '../components/components.gyp:web_modal', @@ -2607,7 +2598,6 @@ '../third_party/hunspell/hunspell.gyp:hunspell', '../third_party/leveldatabase/leveldatabase.gyp:leveldatabase', '../third_party/libaddressinput/libaddressinput.gyp:libaddressinput', - '../third_party/libusb/libusb.gyp:libusb', '../third_party/libyuv/libyuv.gyp:libyuv', '../third_party/npapi/npapi.gyp:npapi', '../third_party/smhasher/smhasher.gyp:cityhash', @@ -3153,9 +3143,9 @@ ], 'dependencies!': [ '../components/components.gyp:storage_monitor', + '../components/components.gyp:usb_service', '../components/components.gyp:web_modal', '../third_party/libaddressinput/libaddressinput.gyp:libaddressinput', - '../third_party/libusb/libusb.gyp:libusb', ], 'sources': [ 'browser/sessions/in_memory_tab_restore_service.cc', @@ -3277,7 +3267,6 @@ ['exclude', '^browser/sync/glue/app_'], ['exclude', '^browser/sync/glue/extension_'], ['exclude', '^browser/sync/glue/theme_'], - ['exclude', '^browser/usb/'], ['exclude', '^third_party/mozilla_security_manager/'], ], }], diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi index 17bd11b..3686860 100644 --- a/chrome/chrome_tests_unit.gypi +++ b/chrome/chrome_tests_unit.gypi @@ -1755,7 +1755,6 @@ 'browser/chrome_content_browser_client_unittest.cc', 'browser/undo/bookmark_undo_service_test.cc', 'browser/undo/undo_manager_test.cc', - 'browser/usb/usb_context_unittest.cc', 'browser/web_applications/web_app_mac_unittest.mm', 'browser/web_applications/web_app_unittest.cc', 'browser/web_resource/eula_accepted_notifier_unittest.cc', @@ -2525,9 +2524,6 @@ # The importer code is not used on Android. 'common/importer/firefox_importer_utils_unittest.cc', - # USB service is not supported on Android. - 'browser/usb/usb_context_unittest.cc', - # Bookmark undo is not used on Android. 'browser/undo/bookmark_undo_service_test.cc', 'browser/undo/undo_manager_test.cc', diff --git a/components/OWNERS b/components/OWNERS index b6d2608..56f8cff 100644 --- a/components/OWNERS +++ b/components/OWNERS @@ -123,6 +123,10 @@ per-file translate.gypi=toyoshim@chromium.org per-file startup_metric_utils.gypi=jeremy@chromium.org +per-file usb_service.gypi=pfeldman@chromium.org +per-file usb_service.gypi=rockot@chromium.org +per-file usb_service.gypi=rpaquay@chromium.org + per-file user_prefs.gypi=battre@chromium.org per-file user_prefs.gypi=bauerb@chromium.org per-file user_prefs.gypi=mnissler@chromium.org diff --git a/components/components.gyp b/components/components.gyp index aa4c7ac..182c1be 100644 --- a/components/components.gyp +++ b/components/components.gyp @@ -55,6 +55,11 @@ 'wifi.gypi', ], }], + ['OS != "ios" and OS != "android"', { + 'includes': [ + 'usb_service.gypi', + ] + }], ['android_webview_build == 0', { # Android WebView fails to build if a dependency on sync.gyp:sync is # introduced. diff --git a/components/components_tests.gyp b/components/components_tests.gyp index cd07b05..33c354d 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -252,6 +252,7 @@ 'sources': [ 'autofill/content/renderer/renderer_save_password_progress_logger_unittest.cc', 'dom_distiller/content/dom_distiller_viewer_source_unittest.cc', + 'usb_service/usb_context_unittest.cc', ], 'dependencies': [ # Dependencies of autofill @@ -285,6 +286,10 @@ # Dependencies of url_matcher. 'components.gyp:url_matcher', + # Dependencies of usb_service tests. + 'components.gyp:usb_service', + '../third_party/libusb/libusb.gyp:libusb', + # Dependencies of visitedlink 'components.gyp:visitedlink_browser', 'components.gyp:visitedlink_renderer', @@ -380,13 +385,16 @@ 'storage_monitor/media_storage_util_unittest.cc', 'storage_monitor/storage_info_unittest.cc', 'storage_monitor/storage_monitor_unittest.cc', + 'usb_service/usb_context_unittest.cc', 'web_modal/web_contents_modal_dialog_manager_unittest.cc', ], 'dependencies!': [ 'components.gyp:storage_monitor', 'components.gyp:storage_monitor_test_support', + 'components.gyp:usb_service', 'components.gyp:web_modal', 'components.gyp:web_modal_test_support', + '../third_party/libusb/libusb.gyp:libusb', ], }], ['OS == "android" and gtest_target_type == "shared_library"', { diff --git a/components/usb_service.gypi b/components/usb_service.gypi new file mode 100644 index 0000000..97f4a61 --- /dev/null +++ b/components/usb_service.gypi @@ -0,0 +1,35 @@ +# 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. + +{ + 'targets': [{ + 'target_name': 'usb_service', + 'type': '<(component)', + 'dependencies': [ + '../base/base.gyp:base', + '../base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations', + '../content/content.gyp:content_browser', + '../net/net.gyp:net', + '../third_party/libusb/libusb.gyp:libusb', + ], + 'defines': [ + 'USB_SERVICE_IMPLEMENTATION', + ], + 'include_dirs': [ + '..', + ], + 'sources': [ + 'usb_service/usb_context.cc', + 'usb_service/usb_context.h', + 'usb_service/usb_device.cc', + 'usb_service/usb_device.h', + 'usb_service/usb_device_handle.cc', + 'usb_service/usb_device_handle.h', + 'usb_service/usb_interface.cc', + 'usb_service/usb_interface.h', + 'usb_service/usb_service.cc', + 'usb_service/usb_service.h', + ] + }], +} diff --git a/components/usb_service/DEPS b/components/usb_service/DEPS new file mode 100644 index 0000000..9013632 --- /dev/null +++ b/components/usb_service/DEPS @@ -0,0 +1,11 @@ +include_rules = [ + "+chromeos", + + "-content", + "+content/public/browser", + + "-net", + "+net/base", + + "+third_party/libusb", +] diff --git a/chrome/browser/usb/OWNERS b/components/usb_service/OWNERS index 28d05d3..28d05d3 100644 --- a/chrome/browser/usb/OWNERS +++ b/components/usb_service/OWNERS diff --git a/chrome/browser/usb/usb_context.cc b/components/usb_service/usb_context.cc index 27971e4..be8313e 100644 --- a/chrome/browser/usb/usb_context.cc +++ b/components/usb_service/usb_context.cc @@ -1,8 +1,8 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// 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. -#include "chrome/browser/usb/usb_context.h" +#include "components/usb_service/usb_context.h" #include "base/logging.h" #include "base/synchronization/waitable_event.h" @@ -10,6 +10,8 @@ #include "third_party/libusb/src/libusb/interrupt.h" #include "third_party/libusb/src/libusb/libusb.h" +namespace usb_service { + // The UsbEventHandler works around a design flaw in the libusb interface. There // is currently no way to signal to libusb that any caller into one of the event // handler calls should return without handling any events. @@ -71,3 +73,5 @@ UsbContext::~UsbContext() { event_handler_ = NULL; libusb_exit(context_); } + +} // namespace usb_service diff --git a/chrome/browser/usb/usb_context.h b/components/usb_service/usb_context.h index c914206..432617b3 100644 --- a/chrome/browser/usb/usb_context.h +++ b/components/usb_service/usb_context.h @@ -1,24 +1,28 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// 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 CHROME_BROWSER_USB_USB_CONTEXT_H_ -#define CHROME_BROWSER_USB_USB_CONTEXT_H_ +#ifndef COMPONENTS_USB_SERVICE_USB_CONTEXT_H_ +#define COMPONENTS_USB_SERVICE_USB_CONTEXT_H_ #include "base/basictypes.h" #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "base/threading/thread_checker.h" +#include "components/usb_service/usb_service_export.h" struct libusb_context; +namespace usb_service { + typedef libusb_context* PlatformUsbContext; // Ref-counted wrapper for libusb_context*. // It also manages the life-cycle of UsbEventHandler. // It is a blocking operation to delete UsbContext. // Destructor must be called on FILE thread. -class UsbContext : public base::RefCountedThreadSafe<UsbContext> { +class USB_SERVICE_EXPORT UsbContext + : public base::RefCountedThreadSafe<UsbContext> { public: PlatformUsbContext context() const { return context_; } @@ -38,4 +42,6 @@ class UsbContext : public base::RefCountedThreadSafe<UsbContext> { DISALLOW_COPY_AND_ASSIGN(UsbContext); }; -#endif // CHROME_BROWSER_USB_USB_CONTEXT_H_ +} // namespace usb_service + +#endif // COMPONENTS_USB_SERVICE_USB_CONTEXT_H_ diff --git a/chrome/browser/usb/usb_context_unittest.cc b/components/usb_service/usb_context_unittest.cc index dce79f4..2722cc5 100644 --- a/chrome/browser/usb/usb_context_unittest.cc +++ b/components/usb_service/usb_context_unittest.cc @@ -1,14 +1,16 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// 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. -#include "chrome/browser/usb/usb_context.h" - #include "base/threading/platform_thread.h" #include "build/build_config.h" +#include "components/usb_service/usb_context.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/libusb/src/libusb/libusb.h" +using usb_service::UsbContext; +using usb_service::PlatformUsbContext; + namespace { class UsbContextTest : public testing::Test { @@ -17,6 +19,7 @@ class UsbContextTest : public testing::Test { public: explicit UsbContextForTest(PlatformUsbContext context) : UsbContext(context) {} + private: virtual ~UsbContextForTest() {} DISALLOW_COPY_AND_ASSIGN(UsbContextForTest); diff --git a/chrome/browser/usb/usb_device.cc b/components/usb_service/usb_device.cc index 8f71d50..301e2e3 100644 --- a/chrome/browser/usb/usb_device.cc +++ b/components/usb_service/usb_device.cc @@ -1,14 +1,14 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// 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. -#include "chrome/browser/usb/usb_device.h" +#include "components/usb_service/usb_device.h" #include <algorithm> #include "base/stl_util.h" -#include "chrome/browser/usb/usb_context.h" -#include "chrome/browser/usb/usb_device_handle.h" +#include "components/usb_service/usb_context.h" +#include "components/usb_service/usb_device_handle.h" #include "content/public/browser/browser_thread.h" #include "third_party/libusb/src/libusb/libusb.h" @@ -26,19 +26,20 @@ namespace { void OnRequestUsbAccessReplied( const base::Callback<void(bool success)>& callback, bool success) { - BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, - base::Bind(callback, success)); + BrowserThread::PostTask( + BrowserThread::FILE, FROM_HERE, base::Bind(callback, success)); } #endif // defined(OS_CHROMEOS) } // namespace -UsbDevice::UsbDevice( - scoped_refptr<UsbContext> context, - PlatformUsbDevice platform_device, - uint16 vendor_id, - uint16 product_id, - uint32 unique_id) +namespace usb_service { + +UsbDevice::UsbDevice(scoped_refptr<UsbContext> context, + PlatformUsbDevice platform_device, + uint16 vendor_id, + uint16 product_id, + uint32 unique_id) : platform_device_(platform_device), vendor_id_(vendor_id), product_id_(product_id), @@ -58,9 +59,8 @@ UsbDevice::UsbDevice() UsbDevice::~UsbDevice() { DCHECK(thread_checker_.CalledOnValidThread()); - for (HandlesVector::iterator it = handles_.begin(); - it != handles_.end(); - ++it) { + for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); + ++it) { (*it)->InternalClose(); } STLClearObject(&handles_); @@ -86,7 +86,8 @@ void UsbDevice::RequestUsbAcess( } BrowserThread::PostTask( - BrowserThread::UI, FROM_HERE, + BrowserThread::UI, + FROM_HERE, base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess, base::Unretained(client), this->vendor_id_, @@ -117,9 +118,8 @@ scoped_refptr<UsbDeviceHandle> UsbDevice::Open() { bool UsbDevice::Close(scoped_refptr<UsbDeviceHandle> handle) { DCHECK(thread_checker_.CalledOnValidThread()); - for (HandlesVector::iterator it = handles_.begin(); - it != handles_.end(); - ++it) { + for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); + ++it) { if (*it == handle) { (*it)->InternalClose(); handles_.erase(it); @@ -146,9 +146,11 @@ void UsbDevice::OnDisconnect() { HandlesVector handles; swap(handles, handles_); for (std::vector<scoped_refptr<UsbDeviceHandle> >::iterator it = - handles.begin(); - it != handles.end(); - ++it) { + handles.begin(); + it != handles.end(); + ++it) { (*it)->InternalClose(); } } + +} // namespace usb_service diff --git a/chrome/browser/usb/usb_device.h b/components/usb_service/usb_device.h index a8bd0a3..2de4193 100644 --- a/chrome/browser/usb/usb_device.h +++ b/components/usb_service/usb_device.h @@ -1,10 +1,9 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// 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 CHROME_BROWSER_USB_USB_DEVICE_H_ -#define CHROME_BROWSER_USB_USB_DEVICE_H_ +#ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_H_ +#define COMPONENTS_USB_SERVICE_USB_DEVICE_H_ #include <vector> @@ -12,9 +11,13 @@ #include "base/callback.h" #include "base/memory/ref_counted.h" #include "base/threading/thread_checker.h" -#include "chrome/browser/usb/usb_interface.h" +#include "components/usb_service/usb_interface.h" +#include "components/usb_service/usb_service_export.h" struct libusb_device; + +namespace usb_service { + class UsbDeviceHandle; class UsbContext; @@ -23,7 +26,8 @@ typedef libusb_device* PlatformUsbDevice; // A UsbDevice object represents a detected USB device, providing basic // information about it. For further manipulation of the device, a // UsbDeviceHandle must be created from Open() method. -class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> { +class USB_SERVICE_EXPORT UsbDevice + : public base::RefCountedThreadSafe<UsbDevice> { public: // Accessors to basic information. PlatformUsbDevice platform_device() const { return platform_device_; } @@ -37,7 +41,8 @@ class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> { // interfaces can be used. If this argument is missing, permission broker will // not be used and this method fails if the device is claimed. virtual void RequestUsbAcess( - int interface_id, const base::Callback<void(bool success)>& callback); + int interface_id, + const base::Callback<void(bool success)>& callback); #endif // OS_CHROMEOS // Creates a UsbDeviceHandle for further manipulation. @@ -91,4 +96,6 @@ class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> { DISALLOW_COPY_AND_ASSIGN(UsbDevice); }; -#endif // CHROME_BROWSER_USB_USB_DEVICE_H_ +} // namespace usb_service + +#endif // COMPONENTS_USB_SERVICE_USB_DEVICE_H_ diff --git a/chrome/browser/usb/usb_device_handle.cc b/components/usb_service/usb_device_handle.cc index 367ecfd..e9b9fcce 100644 --- a/chrome/browser/usb/usb_device_handle.cc +++ b/components/usb_service/usb_device_handle.cc @@ -1,8 +1,8 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// 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. -#include "chrome/browser/usb/usb_device_handle.h" +#include "components/usb_service/usb_device_handle.h" #include <algorithm> #include <vector> @@ -11,20 +11,22 @@ #include "base/stl_util.h" #include "base/strings/string16.h" #include "base/synchronization/lock.h" -#include "chrome/browser/usb/usb_context.h" -#include "chrome/browser/usb/usb_device.h" -#include "chrome/browser/usb/usb_interface.h" -#include "chrome/browser/usb/usb_service.h" +#include "components/usb_service/usb_context.h" +#include "components/usb_service/usb_device.h" +#include "components/usb_service/usb_interface.h" +#include "components/usb_service/usb_service.h" #include "content/public/browser/browser_thread.h" #include "third_party/libusb/src/libusb/libusb.h" using content::BrowserThread; -void HandleTransferCompletion(PlatformUsbTransferHandle transfer); + +namespace usb_service { + +void HandleTransferCompletion(usb_service::PlatformUsbTransferHandle transfer); namespace { -static uint8 ConvertTransferDirection( - const UsbEndpointDirection direction) { +static uint8 ConvertTransferDirection(const UsbEndpointDirection direction) { switch (direction) { case USB_DIRECTION_INBOUND: return LIBUSB_ENDPOINT_IN; @@ -36,7 +38,8 @@ static uint8 ConvertTransferDirection( } } -static uint8 CreateRequestType(const UsbEndpointDirection direction, +static uint8 CreateRequestType( + const UsbEndpointDirection direction, const UsbDeviceHandle::TransferRequestType request_type, const UsbDeviceHandle::TransferRecipient recipient) { uint8 result = ConvertTransferDirection(direction); @@ -97,8 +100,8 @@ static UsbTransferStatus ConvertTransferStatus( } } -static void LIBUSB_CALL PlatformTransferCompletionCallback( - PlatformUsbTransferHandle transfer) { +static void LIBUSB_CALL +PlatformTransferCompletionCallback(PlatformUsbTransferHandle transfer) { BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(HandleTransferCompletion, transfer)); @@ -115,7 +118,6 @@ void HandleTransferCompletion(PlatformUsbTransferHandle transfer) { libusb_free_transfer(transfer); } - class UsbDeviceHandle::InterfaceClaimer : public base::RefCountedThreadSafe<UsbDeviceHandle::InterfaceClaimer> { public: @@ -142,7 +144,8 @@ class UsbDeviceHandle::InterfaceClaimer }; UsbDeviceHandle::InterfaceClaimer::InterfaceClaimer( - const scoped_refptr<UsbDeviceHandle> handle, const int interface_number) + const scoped_refptr<UsbDeviceHandle> handle, + const int interface_number) : handle_(handle), interface_number_(interface_number), alternate_setting_(0) { @@ -169,17 +172,16 @@ struct UsbDeviceHandle::Transfer { }; UsbDeviceHandle::Transfer::Transfer() - : transfer_type(USB_TRANSFER_CONTROL), - length(0) { + : transfer_type(USB_TRANSFER_CONTROL), length(0) { } -UsbDeviceHandle::Transfer::~Transfer() {} +UsbDeviceHandle::Transfer::~Transfer() { +} -UsbDeviceHandle::UsbDeviceHandle( - scoped_refptr<UsbContext> context, - UsbDevice* device, - PlatformUsbDeviceHandle handle, - scoped_refptr<UsbConfigDescriptor> interfaces) +UsbDeviceHandle::UsbDeviceHandle(scoped_refptr<UsbContext> context, + UsbDevice* device, + PlatformUsbDeviceHandle handle, + scoped_refptr<UsbConfigDescriptor> interfaces) : device_(device), handle_(handle), interfaces_(interfaces), @@ -219,8 +221,8 @@ void UsbDeviceHandle::TransferComplete(PlatformUsbTransferHandle handle) { size_t actual_length = static_cast<size_t>(std::max(handle->actual_length, 0)); - DCHECK(transfer.length >= actual_length) << - "data too big for our buffer (libusb failure?)"; + DCHECK(transfer.length >= actual_length) + << "data too big for our buffer (libusb failure?)"; scoped_refptr<net::IOBuffer> buffer = transfer.buffer; switch (transfer.transfer_type) { @@ -229,15 +231,16 @@ void UsbDeviceHandle::TransferComplete(PlatformUsbTransferHandle handle) { // setup header to the caller. This logic strips off the header if // present before invoking the callback provided with the transfer. if (actual_length > 0) { - CHECK(transfer.length >= LIBUSB_CONTROL_SETUP_SIZE) << - "buffer was not correctly set: too small for the control header"; + CHECK(transfer.length >= LIBUSB_CONTROL_SETUP_SIZE) + << "buffer was not correctly set: too small for the control header"; if (transfer.length >= actual_length && actual_length >= LIBUSB_CONTROL_SETUP_SIZE) { // 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. - scoped_refptr<net::IOBuffer> resized_buffer = new net::IOBuffer( - std::max(actual_length, static_cast<size_t>(1))); + scoped_refptr<net::IOBuffer> resized_buffer = + new net::IOBuffer(static_cast<int>( + std::max(actual_length, static_cast<size_t>(1)))); memcpy(resized_buffer->data(), buffer->data() + LIBUSB_CONTROL_SETUP_SIZE, actual_length); @@ -295,14 +298,16 @@ void UsbDeviceHandle::TransferComplete(PlatformUsbTransferHandle handle) { bool UsbDeviceHandle::ClaimInterface(const int interface_number) { DCHECK(thread_checker_.CalledOnValidThread()); - if (!device_) return false; - if (ContainsKey(claimed_interfaces_, interface_number)) return true; + if (!device_) + return false; + if (ContainsKey(claimed_interfaces_, interface_number)) + return true; scoped_refptr<InterfaceClaimer> claimer = new InterfaceClaimer(this, interface_number); if (claimer->Claim()) { - claimed_interfaces_[interface_number]= claimer; + claimed_interfaces_[interface_number] = claimer; RefreshEndpointMap(); return true; } @@ -311,14 +316,16 @@ bool UsbDeviceHandle::ClaimInterface(const int interface_number) { bool UsbDeviceHandle::ReleaseInterface(const int interface_number) { DCHECK(thread_checker_.CalledOnValidThread()); - if (!device_) return false; - if (!ContainsKey(claimed_interfaces_, interface_number)) return false; + if (!device_) + return false; + if (!ContainsKey(claimed_interfaces_, interface_number)) + return false; // Cancel all the transfers on that interface. InterfaceClaimer* interface_claimer = claimed_interfaces_[interface_number].get(); - for (TransferMap::iterator it = transfers_.begin(); - it != transfers_.end(); ++it) { + for (TransferMap::iterator it = transfers_.begin(); it != transfers_.end(); + ++it) { if (it->second.claimed_interface.get() == interface_claimer) libusb_cancel_transfer(it->first); } @@ -332,13 +339,15 @@ bool UsbDeviceHandle::SetInterfaceAlternateSetting( const int interface_number, const int alternate_setting) { DCHECK(thread_checker_.CalledOnValidThread()); - if (!device_) return false; - if (!ContainsKey(claimed_interfaces_, interface_number)) return false; - const int rv = libusb_set_interface_alt_setting(handle_, - interface_number, alternate_setting); + if (!device_) + return false; + if (!ContainsKey(claimed_interfaces_, interface_number)) + return false; + const int rv = libusb_set_interface_alt_setting( + handle_, interface_number, alternate_setting); if (rv == 0) { - claimed_interfaces_[interface_number]-> - set_alternate_setting(alternate_setting); + claimed_interfaces_[interface_number]->set_alternate_setting( + alternate_setting); RefreshEndpointMap(); return true; } @@ -347,7 +356,8 @@ bool UsbDeviceHandle::SetInterfaceAlternateSetting( bool UsbDeviceHandle::ResetDevice() { DCHECK(thread_checker_.CalledOnValidThread()); - if (!device_) return false; + if (!device_) + return false; return libusb_reset_device(handle_) == 0; } @@ -364,11 +374,14 @@ bool UsbDeviceHandle::GetSerial(base::string16* serial) { return false; // Getting supported language ID. - uint16 langid[128] = { 0 }; - - int size = libusb_get_string_descriptor( - handle_, 0, 0, - reinterpret_cast<unsigned char*>(&langid[0]), sizeof(langid)); + uint16 langid[128] = {0}; + + int size = + libusb_get_string_descriptor(handle_, + 0, + 0, + reinterpret_cast<unsigned char*>(&langid[0]), + sizeof(langid)); if (size < 0) return false; @@ -376,10 +389,13 @@ bool UsbDeviceHandle::GetSerial(base::string16* serial) { for (int i = 1; i <= language_count; ++i) { // Get the string using language ID. - base::char16 text[256] = { 0 }; - size = libusb_get_string_descriptor( - handle_, desc.iSerialNumber, langid[i], - reinterpret_cast<unsigned char*>(&text[0]), sizeof(text)); + base::char16 text[256] = {0}; + size = + libusb_get_string_descriptor(handle_, + desc.iSerialNumber, + langid[i], + reinterpret_cast<unsigned char*>(&text[0]), + sizeof(text)); if (size <= 2) continue; if ((text[0] >> 8) != LIBUSB_DT_STRING) @@ -395,50 +411,65 @@ bool UsbDeviceHandle::GetSerial(base::string16* serial) { } void UsbDeviceHandle::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) { + 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) { if (!device_) { callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); return; } const size_t resized_length = LIBUSB_CONTROL_SETUP_SIZE + length; - scoped_refptr<net::IOBuffer> resized_buffer(new net::IOBufferWithSize( - resized_length)); - memcpy(resized_buffer->data() + LIBUSB_CONTROL_SETUP_SIZE, buffer->data(), - length); + scoped_refptr<net::IOBuffer> resized_buffer( + new net::IOBufferWithSize(static_cast<int>(resized_length))); + if (!resized_buffer) { + callback.Run(USB_TRANSFER_ERROR, buffer, 0); + return; + } + memcpy(resized_buffer->data() + LIBUSB_CONTROL_SETUP_SIZE, + buffer->data(), + static_cast<int>(length)); PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); - const uint8 converted_type = CreateRequestType(direction, request_type, - recipient); + const uint8 converted_type = + CreateRequestType(direction, request_type, recipient); libusb_fill_control_setup(reinterpret_cast<uint8*>(resized_buffer->data()), - converted_type, request, value, index, length); - libusb_fill_control_transfer( - transfer, - handle_, - reinterpret_cast<uint8*>(resized_buffer->data()), - PlatformTransferCompletionCallback, - this, - timeout); - - BrowserThread::PostTask( - BrowserThread::FILE, - FROM_HERE, - base::Bind(&UsbDeviceHandle::SubmitTransfer, - this, - transfer, - USB_TRANSFER_CONTROL, - resized_buffer, - resized_length, - base::MessageLoopProxy::current(), - callback)); + converted_type, + request, + value, + index, + static_cast<int16>(length)); + libusb_fill_control_transfer(transfer, + handle_, + reinterpret_cast<uint8*>(resized_buffer->data()), + PlatformTransferCompletionCallback, + this, + timeout); + + BrowserThread::PostTask(BrowserThread::FILE, + FROM_HERE, + base::Bind(&UsbDeviceHandle::SubmitTransfer, + this, + transfer, + USB_TRANSFER_CONTROL, + resized_buffer, + resized_length, + base::MessageLoopProxy::current(), + callback)); } void UsbDeviceHandle::BulkTransfer(const UsbEndpointDirection direction, - const uint8 endpoint, net::IOBuffer* buffer, const size_t length, - const unsigned int timeout, const UsbTransferCallback& callback) { + const uint8 endpoint, + net::IOBuffer* buffer, + const size_t length, + const unsigned int timeout, + const UsbTransferCallback& callback) { if (!device_) { callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); return; @@ -446,26 +477,33 @@ void UsbDeviceHandle::BulkTransfer(const UsbEndpointDirection direction, PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; - libusb_fill_bulk_transfer(transfer, handle_, new_endpoint, - reinterpret_cast<uint8*>(buffer->data()), length, - PlatformTransferCompletionCallback, this, timeout); + libusb_fill_bulk_transfer(transfer, + handle_, + new_endpoint, + reinterpret_cast<uint8*>(buffer->data()), + static_cast<int>(length), + PlatformTransferCompletionCallback, + this, + timeout); - BrowserThread::PostTask( - BrowserThread::FILE, - FROM_HERE, - base::Bind(&UsbDeviceHandle::SubmitTransfer, - this, - transfer, - USB_TRANSFER_BULK, - make_scoped_refptr(buffer), - length, - base::MessageLoopProxy::current(), - callback)); + BrowserThread::PostTask(BrowserThread::FILE, + FROM_HERE, + base::Bind(&UsbDeviceHandle::SubmitTransfer, + this, + transfer, + USB_TRANSFER_BULK, + make_scoped_refptr(buffer), + length, + base::MessageLoopProxy::current(), + callback)); } void UsbDeviceHandle::InterruptTransfer(const UsbEndpointDirection direction, - const uint8 endpoint, net::IOBuffer* buffer, const size_t length, - const unsigned int timeout, const UsbTransferCallback& callback) { + const uint8 endpoint, + net::IOBuffer* buffer, + const size_t length, + const unsigned int timeout, + const UsbTransferCallback& callback) { if (!device_) { callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); return; @@ -473,63 +511,77 @@ void UsbDeviceHandle::InterruptTransfer(const UsbEndpointDirection direction, PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; - libusb_fill_interrupt_transfer(transfer, handle_, new_endpoint, - reinterpret_cast<uint8*>(buffer->data()), length, - PlatformTransferCompletionCallback, this, timeout); - BrowserThread::PostTask( - BrowserThread::FILE, - FROM_HERE, - base::Bind(&UsbDeviceHandle::SubmitTransfer, - this, - transfer, - USB_TRANSFER_INTERRUPT, - make_scoped_refptr(buffer), - length, - base::MessageLoopProxy::current(), - callback)); + libusb_fill_interrupt_transfer(transfer, + handle_, + new_endpoint, + reinterpret_cast<uint8*>(buffer->data()), + static_cast<int>(length), + PlatformTransferCompletionCallback, + this, + timeout); + BrowserThread::PostTask(BrowserThread::FILE, + FROM_HERE, + base::Bind(&UsbDeviceHandle::SubmitTransfer, + this, + transfer, + USB_TRANSFER_INTERRUPT, + make_scoped_refptr(buffer), + length, + base::MessageLoopProxy::current(), + callback)); } void UsbDeviceHandle::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) { + 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) { if (!device_) { callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); return; } const uint64 total_length = packets * packet_length; - CHECK(packets <= length && total_length <= length) << - "transfer length is too small"; + CHECK(packets <= length && total_length <= length) + << "transfer length is too small"; PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(packets); const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; - libusb_fill_iso_transfer(transfer, handle_, new_endpoint, - reinterpret_cast<uint8*>(buffer->data()), length, packets, - PlatformTransferCompletionCallback, this, timeout); + libusb_fill_iso_transfer(transfer, + handle_, + new_endpoint, + reinterpret_cast<uint8*>(buffer->data()), + static_cast<int>(length), + packets, + PlatformTransferCompletionCallback, + this, + timeout); libusb_set_iso_packet_lengths(transfer, packet_length); - BrowserThread::PostTask( - BrowserThread::FILE, - FROM_HERE, - base::Bind(&UsbDeviceHandle::SubmitTransfer, - this, - transfer, - USB_TRANSFER_ISOCHRONOUS, - make_scoped_refptr(buffer), - length, - base::MessageLoopProxy::current(), - callback)); + BrowserThread::PostTask(BrowserThread::FILE, + FROM_HERE, + base::Bind(&UsbDeviceHandle::SubmitTransfer, + this, + transfer, + USB_TRANSFER_ISOCHRONOUS, + make_scoped_refptr(buffer), + length, + base::MessageLoopProxy::current(), + callback)); } void UsbDeviceHandle::RefreshEndpointMap() { DCHECK(thread_checker_.CalledOnValidThread()); endpoint_map_.clear(); for (ClaimedInterfaceMap::iterator it = claimed_interfaces_.begin(); - it != claimed_interfaces_.end(); ++it) { + it != claimed_interfaces_.end(); + ++it) { scoped_refptr<const UsbInterfaceAltSettingDescriptor> interface_desc = - interfaces_->GetInterface(it->first)->GetAltSetting( - it->second->alternate_setting()); + interfaces_->GetInterface(it->first) + ->GetAltSetting(it->second->alternate_setting()); for (size_t i = 0; i < interface_desc->GetNumEndpoints(); i++) { scoped_refptr<const UsbEndpointDescriptor> endpoint = interface_desc->GetEndpoint(i); @@ -539,7 +591,7 @@ void UsbDeviceHandle::RefreshEndpointMap() { } scoped_refptr<UsbDeviceHandle::InterfaceClaimer> - UsbDeviceHandle::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { +UsbDeviceHandle::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; if (ContainsKey(endpoint_map_, address)) return claimed_interfaces_[endpoint_map_[address]]; @@ -557,8 +609,8 @@ void UsbDeviceHandle::SubmitTransfer( if (!device_) { message_loop_proxy->PostTask( FROM_HERE, - base::Bind(callback, USB_TRANSFER_DISCONNECT, - make_scoped_refptr(buffer), 0)); + base::Bind( + callback, USB_TRANSFER_DISCONNECT, make_scoped_refptr(buffer), 0)); } Transfer transfer; @@ -577,18 +629,19 @@ void UsbDeviceHandle::SubmitTransfer( } else { message_loop_proxy->PostTask( FROM_HERE, - base::Bind(callback, USB_TRANSFER_ERROR, - make_scoped_refptr(buffer), 0)); + base::Bind( + callback, USB_TRANSFER_ERROR, make_scoped_refptr(buffer), 0)); } } void UsbDeviceHandle::InternalClose() { DCHECK(thread_checker_.CalledOnValidThread()); - if (!device_) return; + if (!device_) + return; // Cancel all the transfers. - for (TransferMap::iterator it = transfers_.begin(); - it != transfers_.end(); ++it) { + for (TransferMap::iterator it = transfers_.begin(); it != transfers_.end(); + ++it) { // The callback will be called some time later. libusb_cancel_transfer(it->first); } @@ -601,3 +654,5 @@ void UsbDeviceHandle::InternalClose() { // finish. device_ = NULL; } + +} // namespace usb_service diff --git a/chrome/browser/usb/usb_device_handle.h b/components/usb_service/usb_device_handle.h index 09cd101..60b7c64 100644 --- a/chrome/browser/usb/usb_device_handle.h +++ b/components/usb_service/usb_device_handle.h @@ -1,41 +1,38 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// 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 CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_ -#define CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_ +#ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_ +#define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_ #include <map> #include <vector> +#include "base/callback.h" #include "base/memory/ref_counted.h" #include "base/strings/string16.h" -#include "base/synchronization/lock.h" #include "base/threading/thread_checker.h" -#include "chrome/browser/usb/usb_interface.h" -#include "net/base/completion_callback.h" +#include "components/usb_service/usb_interface.h" +#include "components/usb_service/usb_service_export.h" #include "net/base/io_buffer.h" struct libusb_device_handle; struct libusb_iso_packet_descriptor; struct libusb_transfer; -typedef libusb_device_handle* PlatformUsbDeviceHandle; -typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; -typedef libusb_transfer* PlatformUsbTransferHandle; +namespace base { +class MessageLoopProxy; +} + +namespace usb_service { class UsbContext; class UsbConfigDescriptor; class UsbDevice; -class UsbInterfaceDescriptor; -namespace base { - class MessageLoopProxy; -} // namespace base - -namespace net { -class IOBuffer; -} // namespace net +typedef libusb_device_handle* PlatformUsbDeviceHandle; +typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; +typedef libusb_transfer* PlatformUsbTransferHandle; enum UsbTransferStatus { USB_TRANSFER_COMPLETED = 0, @@ -48,11 +45,13 @@ enum UsbTransferStatus { USB_TRANSFER_LENGTH_SHORT, }; -typedef base::Callback<void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, - size_t)> UsbTransferCallback; +typedef base::Callback< + void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, size_t)> + UsbTransferCallback; // UsbDeviceHandle class provides basic I/O related functionalities. -class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> { +class USB_SERVICE_EXPORT UsbDeviceHandle + : public base::RefCountedThreadSafe<UsbDeviceHandle> { public: enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; @@ -71,9 +70,8 @@ class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> { // called on FILE thread. virtual bool ClaimInterface(const int interface_number); virtual bool ReleaseInterface(const int interface_number); - virtual bool SetInterfaceAlternateSetting( - const int interface_number, - const int alternate_setting); + virtual bool SetInterfaceAlternateSetting(const int interface_number, + const int alternate_setting); virtual bool ResetDevice(); virtual bool GetSerial(base::string16* serial); @@ -118,7 +116,8 @@ class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> { // This constructor is called by UsbDevice. UsbDeviceHandle(scoped_refptr<UsbContext> context, - UsbDevice* device, PlatformUsbDeviceHandle handle, + UsbDevice* device, + PlatformUsbDeviceHandle handle, scoped_refptr<UsbConfigDescriptor> interfaces); // This constructor variant is for use in testing only. @@ -182,4 +181,6 @@ class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> { DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle); }; -#endif // CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_ +} // namespace usb_service + +#endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_H_ diff --git a/chrome/browser/usb/usb_interface.cc b/components/usb_service/usb_interface.cc index d5a1e0f..67f1a9d 100644 --- a/chrome/browser/usb/usb_interface.cc +++ b/components/usb_service/usb_interface.cc @@ -1,19 +1,22 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// 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. -#include "chrome/browser/usb/usb_interface.h" +#include "components/usb_service/usb_interface.h" #include "base/logging.h" #include "third_party/libusb/src/libusb/libusb.h" +namespace usb_service { + UsbEndpointDescriptor::UsbEndpointDescriptor( scoped_refptr<const UsbConfigDescriptor> config, PlatformUsbEndpointDescriptor descriptor) : config_(config), descriptor_(descriptor) { } -UsbEndpointDescriptor::~UsbEndpointDescriptor() {} +UsbEndpointDescriptor::~UsbEndpointDescriptor() { +} int UsbEndpointDescriptor::GetAddress() const { return descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK; @@ -91,14 +94,15 @@ UsbInterfaceAltSettingDescriptor::UsbInterfaceAltSettingDescriptor( : config_(config), descriptor_(descriptor) { } -UsbInterfaceAltSettingDescriptor::~UsbInterfaceAltSettingDescriptor() {} +UsbInterfaceAltSettingDescriptor::~UsbInterfaceAltSettingDescriptor() { +} size_t UsbInterfaceAltSettingDescriptor::GetNumEndpoints() const { return descriptor_->bNumEndpoints; } scoped_refptr<const UsbEndpointDescriptor> - UsbInterfaceAltSettingDescriptor::GetEndpoint(size_t index) const { +UsbInterfaceAltSettingDescriptor::GetEndpoint(size_t index) const { return new UsbEndpointDescriptor(config_, &descriptor_->endpoint[index]); } @@ -128,14 +132,15 @@ UsbInterfaceDescriptor::UsbInterfaceDescriptor( : config_(config), interface_(usbInterface) { } -UsbInterfaceDescriptor::~UsbInterfaceDescriptor() {} +UsbInterfaceDescriptor::~UsbInterfaceDescriptor() { +} size_t UsbInterfaceDescriptor::GetNumAltSettings() const { return interface_->num_altsetting; } scoped_refptr<const UsbInterfaceAltSettingDescriptor> - UsbInterfaceDescriptor::GetAltSetting(size_t index) const { +UsbInterfaceDescriptor::GetAltSetting(size_t index) const { return new UsbInterfaceAltSettingDescriptor(config_, &interface_->altsetting[index]); } @@ -155,7 +160,9 @@ size_t UsbConfigDescriptor::GetNumInterfaces() const { return config_->bNumInterfaces; } -scoped_refptr<const UsbInterfaceDescriptor> - UsbConfigDescriptor::GetInterface(size_t index) const { +scoped_refptr<const UsbInterfaceDescriptor> UsbConfigDescriptor::GetInterface( + size_t index) const { return new UsbInterfaceDescriptor(this, &config_->interface[index]); } + +} // namespace usb_service diff --git a/chrome/browser/usb/usb_interface.h b/components/usb_service/usb_interface.h index e73b044..31e58a2 100644 --- a/chrome/browser/usb/usb_interface.h +++ b/components/usb_service/usb_interface.h @@ -1,17 +1,20 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// 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 CHROME_BROWSER_USB_USB_INTERFACE_H_ -#define CHROME_BROWSER_USB_USB_INTERFACE_H_ +#ifndef COMPONENTS_USB_SERVICE_USB_INTERFACE_H_ +#define COMPONENTS_USB_SERVICE_USB_INTERFACE_H_ #include "base/memory/ref_counted.h" +#include "components/usb_service/usb_service_export.h" struct libusb_config_descriptor; struct libusb_endpoint_descriptor; struct libusb_interface; struct libusb_interface_descriptor; +namespace usb_service { + typedef libusb_config_descriptor* PlatformUsbConfigDescriptor; typedef const libusb_endpoint_descriptor* PlatformUsbEndpointDescriptor; typedef const libusb_interface* PlatformUsbInterface; @@ -47,7 +50,7 @@ class UsbConfigDescriptor; class UsbInterfaceDescriptor; class UsbInterfaceAltSettingDescriptor; -class UsbEndpointDescriptor +class USB_SERVICE_EXPORT UsbEndpointDescriptor : public base::RefCounted<const UsbEndpointDescriptor> { public: int GetAddress() const; @@ -62,9 +65,8 @@ class UsbEndpointDescriptor friend class base::RefCounted<const UsbEndpointDescriptor>; friend class UsbInterfaceAltSettingDescriptor; - UsbEndpointDescriptor( - scoped_refptr<const UsbConfigDescriptor> config, - PlatformUsbEndpointDescriptor descriptor); + UsbEndpointDescriptor(scoped_refptr<const UsbConfigDescriptor> config, + PlatformUsbEndpointDescriptor descriptor); ~UsbEndpointDescriptor(); scoped_refptr<const UsbConfigDescriptor> config_; @@ -73,7 +75,7 @@ class UsbEndpointDescriptor DISALLOW_COPY_AND_ASSIGN(UsbEndpointDescriptor); }; -class UsbInterfaceAltSettingDescriptor +class USB_SERVICE_EXPORT UsbInterfaceAltSettingDescriptor : public base::RefCounted<const UsbInterfaceAltSettingDescriptor> { public: size_t GetNumEndpoints() const; @@ -100,7 +102,7 @@ class UsbInterfaceAltSettingDescriptor DISALLOW_COPY_AND_ASSIGN(UsbInterfaceAltSettingDescriptor); }; -class UsbInterfaceDescriptor +class USB_SERVICE_EXPORT UsbInterfaceDescriptor : public base::RefCounted<const UsbInterfaceDescriptor> { public: size_t GetNumAltSettings() const; @@ -112,7 +114,7 @@ class UsbInterfaceDescriptor friend class UsbConfigDescriptor; UsbInterfaceDescriptor(scoped_refptr<const UsbConfigDescriptor> config, - PlatformUsbInterface usbInterface); + PlatformUsbInterface usbInterface); ~UsbInterfaceDescriptor(); scoped_refptr<const UsbConfigDescriptor> config_; @@ -121,7 +123,8 @@ class UsbInterfaceDescriptor DISALLOW_COPY_AND_ASSIGN(UsbInterfaceDescriptor); }; -class UsbConfigDescriptor : public base::RefCounted<UsbConfigDescriptor> { +class USB_SERVICE_EXPORT UsbConfigDescriptor + : public base::RefCounted<UsbConfigDescriptor> { public: size_t GetNumInterfaces() const; scoped_refptr<const UsbInterfaceDescriptor> GetInterface(size_t index) const; @@ -138,4 +141,6 @@ class UsbConfigDescriptor : public base::RefCounted<UsbConfigDescriptor> { DISALLOW_COPY_AND_ASSIGN(UsbConfigDescriptor); }; -#endif // CHROME_BROWSER_USB_USB_INTERFACE_H_ +} // namespace usb_service; + +#endif // COMPONENTS_USB_SERVICE_USB_INTERFACE_H_ diff --git a/chrome/browser/usb/usb_service.cc b/components/usb_service/usb_service.cc index 5916457..89b884f 100644 --- a/chrome/browser/usb/usb_service.cc +++ b/components/usb_service/usb_service.cc @@ -1,19 +1,21 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// 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. -#include "chrome/browser/usb/usb_service.h" +#include "components/usb_service/usb_service.h" #include <set> #include <vector> #include "base/lazy_instance.h" #include "base/stl_util.h" -#include "chrome/browser/usb/usb_context.h" -#include "chrome/browser/usb/usb_device.h" +#include "components/usb_service/usb_context.h" +#include "components/usb_service/usb_device.h" #include "content/public/browser/browser_thread.h" #include "third_party/libusb/src/libusb/libusb.h" +namespace usb_service { + namespace { base::LazyInstance<scoped_ptr<UsbService> >::Leaky g_usb_service_instance = @@ -120,3 +122,5 @@ void UsbService::RefreshDevices() { libusb_free_device_list(platform_devices, true); } + +} // namespace usb_service diff --git a/chrome/browser/usb/usb_service.h b/components/usb_service/usb_service.h index c83ed5b..3bf0a40 100644 --- a/chrome/browser/usb/usb_service.h +++ b/components/usb_service/usb_service.h @@ -1,9 +1,9 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// 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 CHROME_BROWSER_USB_USB_SERVICE_H_ -#define CHROME_BROWSER_USB_USB_SERVICE_H_ +#ifndef COMPONENTS_USB_SERVICE_USB_SERVICE_H_ +#define COMPONENTS_USB_SERVICE_USB_SERVICE_H_ #include <map> #include <utility> @@ -14,6 +14,12 @@ #include "base/memory/scoped_ptr.h" #include "base/message_loop/message_loop.h" #include "base/threading/non_thread_safe.h" +#include "components/usb_service/usb_service_export.h" + +struct libusb_device; +struct libusb_context; + +namespace usb_service { typedef struct libusb_device* PlatformUsbDevice; typedef struct libusb_context* PlatformUsbContext; @@ -25,8 +31,9 @@ class UsbDevice; // used to manage and dispatch USB events. It is also responsible for device // discovery on the system, which allows it to re-use device handles to prevent // competition for the same USB device. -class UsbService : public base::MessageLoop::DestructionObserver, - public base::NonThreadSafe { +class USB_SERVICE_EXPORT UsbService + : public base::MessageLoop::DestructionObserver, + public base::NonThreadSafe { public: typedef scoped_ptr<std::vector<scoped_refptr<UsbDevice> > > ScopedDeviceVector; @@ -66,4 +73,6 @@ class UsbService : public base::MessageLoop::DestructionObserver, DISALLOW_COPY_AND_ASSIGN(UsbService); }; -#endif // CHROME_BROWSER_USB_USB_SERVICE_H_ +} // namespace usb_service + +#endif // COMPONENTS_USB_SERVICE_USB_SERVICE_H_ diff --git a/components/usb_service/usb_service_export.h b/components/usb_service/usb_service_export.h new file mode 100644 index 0000000..7465597 --- /dev/null +++ b/components/usb_service/usb_service_export.h @@ -0,0 +1,29 @@ +// 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_SERVICE_EXPORT_H_ +#define COMPONENTS_USB_SERVICE_USB_SERVICE_EXPORT_H_ + +#if defined(COMPONENT_BUILD) +#if defined(WIN32) + +#if defined(USB_SERVICE_IMPLEMENTATION) +#define USB_SERVICE_EXPORT __declspec(dllexport) +#else +#define USB_SERVICE_EXPORT __declspec(dllimport) +#endif // defined(USB_SERVICE_EXPORT) + +#else // defined(WIN32) +#if defined(USB_SERVICE_IMPLEMENTATION) +#define USB_SERVICE_EXPORT __attribute__((visibility("default"))) +#else +#define USB_SERVICE_EXPORT +#endif +#endif + +#else // defined(COMPONENT_BUILD) +#define USB_SERVICE_EXPORT +#endif + +#endif // COMPONENTS_USB_SERVICE_USB_SERVICE_EXPORT_H_ |