Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Unified Diff: chrome/browser/usb/usb_device.h

Issue 18593002: Usb backend refactor step 2: Separate Usb Device Handle and Usb Device (deprecate) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/usb/usb_device.h
diff --git a/chrome/browser/usb/usb_device.h b/chrome/browser/usb/usb_device.h
index 0625e74cd02ca78a59cfe56438ec5ae0daeb448b..51e833346cff1ef855d754be39168f607623712f 100644
--- a/chrome/browser/usb/usb_device.h
+++ b/chrome/browser/usb/usb_device.h
@@ -9,7 +9,8 @@
#include <vector>
#include "base/memory/ref_counted.h"
-#include "base/synchronization/lock.h"
+#include "base/port.h"
+#include "base/threading/thread_checker.h"
#include "chrome/browser/usb/usb_interface.h"
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
@@ -41,43 +42,41 @@ enum UsbTransferStatus {
USB_TRANSFER_LENGTH_SHORT,
};
-typedef base::Callback<void(UsbTransferStatus, scoped_refptr<net::IOBuffer>,
+typedef base::Callback<void(
+ UsbTransferStatus,
+ scoped_refptr<net::IOBuffer>,
size_t)> UsbTransferCallback;
-typedef base::Callback<void(bool)> UsbInterfaceCallback;
+typedef base::Callback<void(bool success)> UsbInterfaceCallback;
+typedef base::Callback<void(bool success)> UsbResetDeviceCallback;
// A UsbDevice wraps the platform's underlying representation of what a USB
// device actually is, and provides accessors for performing many of the
// standard USB operations.
-class UsbDevice : public base::RefCounted<UsbDevice> {
+class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> {
public:
enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED };
enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER };
- // Usually you will not want to directly create a UsbDevice, favoring to let
- // the UsbService take care of the logistics of getting a platform device
- // handle and handling events for it.
- UsbDevice(UsbService* service, PlatformUsbDeviceHandle handle);
-
- PlatformUsbDeviceHandle handle() { return handle_; }
+ int device() const { return device_; }
+ PlatformUsbDeviceHandle handle() const { return handle_; }
// Close the USB device and release the underlying platform device. |callback|
// is invoked after the device has been closed.
virtual void Close(const base::Callback<void()>& callback);
+ // FILE thread:
virtual void ListInterfaces(UsbConfigDescriptor* config,
const UsbInterfaceCallback& callback);
-
virtual void ClaimInterface(const int interface_number,
const UsbInterfaceCallback& callback);
-
virtual void ReleaseInterface(const int interface_number,
const UsbInterfaceCallback& callback);
-
virtual void SetInterfaceAlternateSetting(
const int interface_number,
const int alternate_setting,
const UsbInterfaceCallback& callback);
+ // Async IO. Can be called on any thread.
virtual void ControlTransfer(const UsbEndpointDirection direction,
const TransferRequestType request_type,
const TransferRecipient recipient,
@@ -88,21 +87,18 @@ class UsbDevice : public base::RefCounted<UsbDevice> {
const size_t length,
const unsigned int timeout,
const UsbTransferCallback& callback);
-
virtual void BulkTransfer(const UsbEndpointDirection direction,
const uint8 endpoint,
net::IOBuffer* buffer,
const size_t length,
const unsigned int timeout,
const UsbTransferCallback& callback);
-
virtual void InterruptTransfer(const UsbEndpointDirection direction,
const uint8 endpoint,
net::IOBuffer* buffer,
const size_t length,
const unsigned int timeout,
const UsbTransferCallback& callback);
-
virtual void IsochronousTransfer(const UsbEndpointDirection direction,
const uint8 endpoint,
net::IOBuffer* buffer,
@@ -112,34 +108,31 @@ class UsbDevice : public base::RefCounted<UsbDevice> {
const unsigned int timeout,
const UsbTransferCallback& callback);
- virtual void ResetDevice(const base::Callback<void(bool)>& callback);
-
- // Normal code should not call this function. It is called by the platform's
- // callback mechanism in such a way that it cannot be made private. Invokes
- // the callbacks associated with a given transfer, and removes it from the
- // in-flight transfer set.
- void TransferComplete(PlatformUsbTransferHandle transfer);
+ // FILE thread:
+ virtual void ResetDevice(const UsbResetDeviceCallback& callback);
protected:
// This constructor variant is for use in testing only.
UsbDevice();
- friend class base::RefCounted<UsbDevice>;
+ friend class base::RefCountedThreadSafe<UsbDevice>;
virtual ~UsbDevice();
private:
- struct Transfer {
- Transfer();
- ~Transfer();
+ // UsbDeviceHandle should only be created from UsbDevice class.
+ UsbDevice(UsbService* service, int device, PlatformUsbDeviceHandle handle);
+
+ friend class UsbDeviceStub;
- UsbTransferType transfer_type;
- scoped_refptr<net::IOBuffer> buffer;
- size_t length;
- UsbTransferCallback callback;
- };
+ static void HandleTransferCompletionFileThread(
+ PlatformUsbTransferHandle transfer);
+
+ static void API_CALL HandleTransferCompletion(
pfeldman 2013/07/22 18:23:13 Why API_CALL?
Bei Zhang 2013/07/23 17:58:44 This callback needs __stdcall on windows. On 2013
+ PlatformUsbTransferHandle transfer);
+
+ void TransferComplete(PlatformUsbTransferHandle transfer);
- // Checks that the device has not yet been closed.
- void CheckDevice();
+ void InternalClose();
// Submits a transfer and starts tracking it. Retains the buffer and copies
// the completion callback until the transfer finishes, whereupon it invokes
@@ -155,13 +148,15 @@ class UsbDevice : public base::RefCounted<UsbDevice> {
// responsible for its destruction, there is no case where a UsbDevice can
// have outlived its originating UsbService.
UsbService* const service_;
+ const int device_;
PlatformUsbDeviceHandle handle_;
+ base::ThreadChecker thread_checker_;
// transfers_ tracks all in-flight transfers associated with this device,
// allowing the device to retain the buffer and callback associated with a
- // transfer until such time that it completes. It is protected by lock_.
- base::Lock lock_;
- std::map<PlatformUsbTransferHandle, Transfer> transfers_;
+ // transfer until such time that it completes.
+ struct TransferInfo;
+ std::map<PlatformUsbTransferHandle, TransferInfo> transfers_;
DISALLOW_COPY_AND_ASSIGN(UsbDevice);
};

Powered by Google App Engine
This is Rietveld 408576698