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

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

Issue 16316004: Separate usb device handle from usb device. (deprecate) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Windows compile and a bug. Created 7 years, 6 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_handle.h
diff --git a/chrome/browser/usb/usb_device.h b/chrome/browser/usb/usb_device_handle.h
similarity index 83%
rename from chrome/browser/usb/usb_device.h
rename to chrome/browser/usb/usb_device_handle.h
index 0625e74cd02ca78a59cfe56438ec5ae0daeb448b..26698937534c2d70b54f5926ac5fe039bde7d1c2 100644
--- a/chrome/browser/usb/usb_device.h
+++ b/chrome/browser/usb/usb_device_handle.h
@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CHROME_BROWSER_USB_USB_DEVICE_H_
-#define CHROME_BROWSER_USB_USB_DEVICE_H_
+#ifndef CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_
+#define CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_
#include <map>
#include <vector>
@@ -14,15 +14,9 @@
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
-struct libusb_device;
-struct libusb_device_handle;
-struct libusb_iso_packet_descriptor;
-struct libusb_transfer;
-
-typedef libusb_device* PlatformUsbDevice;
-typedef libusb_device_handle* PlatformUsbDeviceHandle;
-typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
-typedef libusb_transfer* PlatformUsbTransferHandle;
+typedef struct libusb_device_handle* PlatformUsbDeviceHandle;
+typedef struct libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
+typedef struct libusb_transfer* PlatformUsbTransferHandle;
class UsbService;
@@ -48,7 +42,7 @@ typedef base::Callback<void(bool)> UsbInterfaceCallback;
// A UsbDevice wraps the platform's underlying representation of what a USB
// device actually is, and provides accessors for performing many of the
// standard USB operations.
-class UsbDevice : public base::RefCounted<UsbDevice> {
+class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> {
public:
enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED };
enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER };
@@ -56,9 +50,11 @@ class UsbDevice : public base::RefCounted<UsbDevice> {
// 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);
+ UsbDeviceHandle(UsbService* service, int device,
+ PlatformUsbDeviceHandle handle);
PlatformUsbDeviceHandle handle() { return handle_; }
+ int device() { return device_; }
// Close the USB device and release the underlying platform device. |callback|
// is invoked after the device has been closed.
@@ -122,10 +118,10 @@ class UsbDevice : public base::RefCounted<UsbDevice> {
protected:
// This constructor variant is for use in testing only.
- UsbDevice();
+ UsbDeviceHandle();
- friend class base::RefCounted<UsbDevice>;
- virtual ~UsbDevice();
+ friend class base::RefCountedThreadSafe<UsbDeviceHandle>;
+ virtual ~UsbDeviceHandle();
private:
struct Transfer {
@@ -138,8 +134,10 @@ class UsbDevice : public base::RefCounted<UsbDevice> {
UsbTransferCallback callback;
};
- // Checks that the device has not yet been closed.
- void CheckDevice();
+ friend class UsbDevice;
+
+ // This only called from UsbDevice, thus always from FILE thread.
+ void InternalClose();
// Submits a transfer and starts tracking it. Retains the buffer and copies
// the completion callback until the transfer finishes, whereupon it invokes
@@ -155,15 +153,24 @@ 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_;
+ int const device_;
+
+ // Lock the handle for the race condition of InternalClose and
+ // TransferComplete.
+ base::Lock handle_lock_;
PlatformUsbDeviceHandle handle_;
// transfers_ tracks all in-flight transfers associated with this device,
// allowing the device to retain the buffer and callback associated with a
- // transfer until such time that it completes. It is protected by lock_.
- base::Lock lock_;
+ // transfer until such time that it completes. It is protected by
+ // transfer_lock_.
+ //
+ // If both locks are used, the locking order must be first handle_lock_, then
+ // transfer_lock_.
+ base::Lock transfer_lock_;
std::map<PlatformUsbTransferHandle, Transfer> transfers_;
- DISALLOW_COPY_AND_ASSIGN(UsbDevice);
+ DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle);
};
-#endif // CHROME_BROWSER_USB_USB_DEVICE_H_
+#endif // CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_

Powered by Google App Engine
This is Rietveld 408576698