diff options
author | zvorygin@chromium.org <zvorygin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-07 14:21:31 +0000 |
---|---|---|
committer | zvorygin@chromium.org <zvorygin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-07 14:21:31 +0000 |
commit | 42c39c991b61b51762ea631b0dd738300a870846 (patch) | |
tree | 14b81233cbfbadfea9655fea6c6ca50bb5651167 /components/usb_service | |
parent | 0b4b29c36b48de9f03badd9037280f4534970b95 (diff) | |
download | chromium_src-42c39c991b61b51762ea631b0dd738300a870846.zip chromium_src-42c39c991b61b51762ea631b0dd738300a870846.tar.gz chromium_src-42c39c991b61b51762ea631b0dd738300a870846.tar.bz2 |
Extracted UsbDevice as interface.
Introduced UsbDeviceImpl class.
BUG=367094
R=rockot@chromium.org
Review URL: https://codereview.chromium.org/266783004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@268745 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/usb_service')
-rw-r--r-- | components/usb_service/usb_device.h | 53 | ||||
-rw-r--r-- | components/usb_service/usb_device_handle.cc | 2 | ||||
-rw-r--r-- | components/usb_service/usb_device_handle.h | 2 | ||||
-rw-r--r-- | components/usb_service/usb_device_impl.cc (renamed from components/usb_service/usb_device.cc) | 42 | ||||
-rw-r--r-- | components/usb_service/usb_device_impl.h | 67 | ||||
-rw-r--r-- | components/usb_service/usb_interface.h | 2 | ||||
-rw-r--r-- | components/usb_service/usb_service_impl.cc | 14 |
7 files changed, 106 insertions, 76 deletions
diff --git a/components/usb_service/usb_device.h b/components/usb_service/usb_device.h index 6ca7a6e..076af18 100644 --- a/components/usb_service/usb_device.h +++ b/components/usb_service/usb_device.h @@ -5,23 +5,15 @@ #ifndef COMPONENTS_USB_SERVICE_USB_DEVICE_H_ #define COMPONENTS_USB_SERVICE_USB_DEVICE_H_ -#include <vector> - #include "base/basictypes.h" #include "base/callback.h" #include "base/memory/ref_counted.h" -#include "base/threading/thread_checker.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; - -typedef libusb_device* PlatformUsbDevice; +class UsbConfigDescriptor; // A UsbDevice object represents a detected USB device, providing basic // information about it. For further manipulation of the device, a @@ -30,7 +22,6 @@ class USB_SERVICE_EXPORT UsbDevice : public base::RefCountedThreadSafe<UsbDevice> { public: // Accessors to basic information. - PlatformUsbDevice platform_device() const { return platform_device_; } uint16 vendor_id() const { return vendor_id_; } uint16 product_id() const { return product_id_; } uint32 unique_id() const { return unique_id_; } @@ -42,56 +33,36 @@ class USB_SERVICE_EXPORT UsbDevice // 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); + const base::Callback<void(bool success)>& callback) = 0; #endif // OS_CHROMEOS // Creates a UsbDeviceHandle for further manipulation. // Blocking method. Must be called on FILE thread. - virtual scoped_refptr<UsbDeviceHandle> Open(); + virtual scoped_refptr<UsbDeviceHandle> Open() = 0; // Explicitly closes a device handle. This method will be automatically called // by the destructor of a UsbDeviceHandle as well. // Closing a closed handle is a safe // Blocking method. Must be called on FILE thread. - virtual bool Close(scoped_refptr<UsbDeviceHandle> handle); + virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) = 0; // Lists the interfaces provided by the device and fills the given // UsbConfigDescriptor. // Blocking method. Must be called on FILE thread. - virtual scoped_refptr<UsbConfigDescriptor> ListInterfaces(); + virtual scoped_refptr<UsbConfigDescriptor> ListInterfaces() = 0; protected: - friend class UsbServiceImpl; - friend class base::RefCountedThreadSafe<UsbDevice>; - - // Called by UsbService only; - UsbDevice(scoped_refptr<UsbContext> context, - PlatformUsbDevice platform_device, - uint16 vendor_id, - uint16 product_id, - uint32 unique_id); + UsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id) + : vendor_id_(vendor_id), product_id_(product_id), unique_id_(unique_id) {} - // Constructor called in test only. - UsbDevice(); - virtual ~UsbDevice(); - - // Called only be UsbService. - virtual void OnDisconnect(); + virtual ~UsbDevice() {} private: - PlatformUsbDevice platform_device_; - uint16 vendor_id_; - uint16 product_id_; - uint32 unique_id_; - - // Retain the context so that it will not be released before UsbDevice. - scoped_refptr<UsbContext> context_; - - // Opened handles. - typedef std::vector<scoped_refptr<UsbDeviceHandle> > HandlesVector; - HandlesVector handles_; + friend class base::RefCountedThreadSafe<UsbDevice>; - base::ThreadChecker thread_checker_; + const uint16 vendor_id_; + const uint16 product_id_; + const uint32 unique_id_; DISALLOW_COPY_AND_ASSIGN(UsbDevice); }; diff --git a/components/usb_service/usb_device_handle.cc b/components/usb_service/usb_device_handle.cc index b341368..f0f368e 100644 --- a/components/usb_service/usb_device_handle.cc +++ b/components/usb_service/usb_device_handle.cc @@ -22,6 +22,8 @@ using content::BrowserThread; namespace usb_service { +typedef libusb_device* PlatformUsbDevice; + void HandleTransferCompletion(usb_service::PlatformUsbTransferHandle transfer); namespace { diff --git a/components/usb_service/usb_device_handle.h b/components/usb_service/usb_device_handle.h index 60b7c64..2b03a8a 100644 --- a/components/usb_service/usb_device_handle.h +++ b/components/usb_service/usb_device_handle.h @@ -112,7 +112,7 @@ class USB_SERVICE_EXPORT UsbDeviceHandle protected: friend class base::RefCountedThreadSafe<UsbDeviceHandle>; - friend class UsbDevice; + friend class UsbDeviceImpl; // This constructor is called by UsbDevice. UsbDeviceHandle(scoped_refptr<UsbContext> context, diff --git a/components/usb_service/usb_device.cc b/components/usb_service/usb_device_impl.cc index 301e2e3..17c8337 100644 --- a/components/usb_service/usb_device.cc +++ b/components/usb_service/usb_device_impl.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/usb_service/usb_device.h" +#include "components/usb_service/usb_device_impl.h" #include <algorithm> @@ -35,29 +35,19 @@ void OnRequestUsbAccessReplied( 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), - unique_id_(unique_id), +UsbDeviceImpl::UsbDeviceImpl(scoped_refptr<UsbContext> context, + PlatformUsbDevice platform_device, + uint16 vendor_id, + uint16 product_id, + uint32 unique_id) + : UsbDevice(vendor_id, product_id, unique_id), + platform_device_(platform_device), context_(context) { CHECK(platform_device) << "platform_device cannot be NULL"; libusb_ref_device(platform_device); } -UsbDevice::UsbDevice() - : platform_device_(NULL), - vendor_id_(0), - product_id_(0), - unique_id_(0), - context_(NULL) { -} - -UsbDevice::~UsbDevice() { +UsbDeviceImpl::~UsbDeviceImpl() { DCHECK(thread_checker_.CalledOnValidThread()); for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); ++it) { @@ -69,7 +59,7 @@ UsbDevice::~UsbDevice() { #if defined(OS_CHROMEOS) -void UsbDevice::RequestUsbAcess( +void UsbDeviceImpl::RequestUsbAcess( int interface_id, const base::Callback<void(bool success)>& callback) { DCHECK(thread_checker_.CalledOnValidThread()); @@ -90,8 +80,8 @@ void UsbDevice::RequestUsbAcess( FROM_HERE, base::Bind(&chromeos::PermissionBrokerClient::RequestUsbAccess, base::Unretained(client), - this->vendor_id_, - this->product_id_, + vendor_id(), + product_id(), interface_id, base::Bind(&OnRequestUsbAccessReplied, callback))); } @@ -99,7 +89,7 @@ void UsbDevice::RequestUsbAcess( #endif -scoped_refptr<UsbDeviceHandle> UsbDevice::Open() { +scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() { DCHECK(thread_checker_.CalledOnValidThread()); PlatformUsbDeviceHandle handle; int rv = libusb_open(platform_device_, &handle); @@ -115,7 +105,7 @@ scoped_refptr<UsbDeviceHandle> UsbDevice::Open() { return NULL; } -bool UsbDevice::Close(scoped_refptr<UsbDeviceHandle> handle) { +bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) { DCHECK(thread_checker_.CalledOnValidThread()); for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); @@ -129,7 +119,7 @@ bool UsbDevice::Close(scoped_refptr<UsbDeviceHandle> handle) { return false; } -scoped_refptr<UsbConfigDescriptor> UsbDevice::ListInterfaces() { +scoped_refptr<UsbConfigDescriptor> UsbDeviceImpl::ListInterfaces() { DCHECK(thread_checker_.CalledOnValidThread()); PlatformUsbConfigDescriptor platform_config; @@ -141,7 +131,7 @@ scoped_refptr<UsbConfigDescriptor> UsbDevice::ListInterfaces() { return NULL; } -void UsbDevice::OnDisconnect() { +void UsbDeviceImpl::OnDisconnect() { DCHECK(thread_checker_.CalledOnValidThread()); HandlesVector handles; swap(handles, handles_); diff --git a/components/usb_service/usb_device_impl.h b/components/usb_service/usb_device_impl.h new file mode 100644 index 0000000..eafbe15 --- /dev/null +++ b/components/usb_service/usb_device_impl.h @@ -0,0 +1,67 @@ +// 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_IMPL_H_ +#define COMPONENTS_USB_SERVICE_USB_DEVICE_IMPL_H_ + +#include <vector> + +#include "base/basictypes.h" +#include "base/callback.h" +#include "base/threading/thread_checker.h" +#include "components/usb_service/usb_device.h" + +struct libusb_device; + +namespace usb_service { + +class UsbDeviceHandle; +class UsbContext; + +typedef libusb_device* PlatformUsbDevice; + +class UsbDeviceImpl : public UsbDevice { + public: +// UsbDevice implementation: +#if defined(OS_CHROMEOS) + virtual void RequestUsbAcess( + int interface_id, + const base::Callback<void(bool success)>& callback) OVERRIDE; +#endif // OS_CHROMEOS + virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE; + virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE; + virtual scoped_refptr<UsbConfigDescriptor> ListInterfaces() OVERRIDE; + + protected: + friend class UsbServiceImpl; + + // Called by UsbServiceImpl only; + UsbDeviceImpl(scoped_refptr<UsbContext> context, + PlatformUsbDevice platform_device, + uint16 vendor_id, + uint16 product_id, + uint32 unique_id); + + virtual ~UsbDeviceImpl(); + + // Called only be UsbService. + void OnDisconnect(); + + private: + base::ThreadChecker thread_checker_; + PlatformUsbDevice platform_device_; + + // Retain the context so that it will not be released before UsbDevice. + scoped_refptr<UsbContext> context_; + + // Opened handles. + typedef std::vector<scoped_refptr<UsbDeviceHandle> > HandlesVector; + HandlesVector handles_; + + DISALLOW_COPY_AND_ASSIGN(UsbDeviceImpl); +}; + +} // namespace usb_service + +#endif // COMPONENTS_USB_SERVICE_USB_DEVICE_IMPL_H_ diff --git a/components/usb_service/usb_interface.h b/components/usb_service/usb_interface.h index 1d9c8ee..dc5ad85 100644 --- a/components/usb_service/usb_interface.h +++ b/components/usb_service/usb_interface.h @@ -137,7 +137,7 @@ class USB_SERVICE_EXPORT UsbConfigDescriptor private: friend class base::RefCounted<UsbConfigDescriptor>; - friend class UsbDevice; + friend class UsbDeviceImpl; explicit UsbConfigDescriptor(PlatformUsbConfigDescriptor config); diff --git a/components/usb_service/usb_service_impl.cc b/components/usb_service/usb_service_impl.cc index 838461e..bafb51b 100644 --- a/components/usb_service/usb_service_impl.cc +++ b/components/usb_service/usb_service_impl.cc @@ -11,7 +11,7 @@ #include "base/message_loop/message_loop.h" #include "base/stl_util.h" #include "components/usb_service/usb_context.h" -#include "components/usb_service/usb_device.h" +#include "components/usb_service/usb_device_impl.h" #include "content/public/browser/browser_thread.h" #include "third_party/libusb/src/libusb/libusb.h" @@ -52,7 +52,7 @@ class UsbServiceImpl uint32 next_unique_id_; // The map from PlatformUsbDevices to UsbDevices. - typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDevice> > DeviceMap; + typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDeviceImpl> > DeviceMap; DeviceMap devices_; DISALLOW_COPY_AND_ASSIGN(UsbServiceImpl); @@ -113,11 +113,11 @@ void UsbServiceImpl::RefreshDevices() { // This test is needed. A valid vendor/produce pair is required. if (0 != libusb_get_device_descriptor(platform_devices[i], &descriptor)) continue; - UsbDevice* new_device = new UsbDevice(context_, - platform_devices[i], - descriptor.idVendor, - descriptor.idProduct, - ++next_unique_id_); + UsbDeviceImpl* new_device = new UsbDeviceImpl(context_, + platform_devices[i], + descriptor.idVendor, + descriptor.idProduct, + ++next_unique_id_); devices_[platform_devices[i]] = new_device; connected_devices.insert(new_device); } else { |