Home | History | Annotate | Download | only in usb
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 
      6 #ifndef CHROME_BROWSER_USB_USB_DEVICE_H_
      7 #define CHROME_BROWSER_USB_USB_DEVICE_H_
      8 
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/threading/thread_checker.h"
     15 #include "chrome/browser/usb/usb_interface.h"
     16 
     17 struct libusb_device;
     18 class UsbDeviceHandle;
     19 class UsbContext;
     20 
     21 typedef libusb_device* PlatformUsbDevice;
     22 
     23 // A UsbDevice object represents a detected USB device, providing basic
     24 // information about it. For further manipulation of the device, a
     25 // UsbDeviceHandle must be created from Open() method.
     26 class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> {
     27  public:
     28   // Accessors to basic information.
     29   PlatformUsbDevice platform_device() const { return platform_device_; }
     30   uint16 vendor_id() const { return vendor_id_; }
     31   uint16 product_id() const { return product_id_; }
     32   uint32 unique_id() const { return unique_id_; }
     33 
     34 #if defined(OS_CHROMEOS)
     35   // On ChromeOS, if an interface of a claimed device is not claimed, the
     36   // permission broker can change the owner of the device so that the unclaimed
     37   // interfaces can be used. If this argument is missing, permission broker will
     38   // not be used and this method fails if the device is claimed.
     39   virtual void RequestUsbAcess(
     40       int interface_id, const base::Callback<void(bool success)>& callback);
     41 #endif  // OS_CHROMEOS
     42 
     43   // Creates a UsbDeviceHandle for further manipulation.
     44   // Blocking method. Must be called on FILE thread.
     45   virtual scoped_refptr<UsbDeviceHandle> Open();
     46 
     47   // Explicitly closes a device handle. This method will be automatically called
     48   // by the destructor of a UsbDeviceHandle as well.
     49   // Closing a closed handle is a safe
     50   // Blocking method. Must be called on FILE thread.
     51   virtual bool Close(scoped_refptr<UsbDeviceHandle> handle);
     52 
     53   // Lists the interfaces provided by the device and fills the given
     54   // UsbConfigDescriptor.
     55   // Blocking method. Must be called on FILE thread.
     56   virtual scoped_refptr<UsbConfigDescriptor> ListInterfaces();
     57 
     58  protected:
     59   friend class UsbService;
     60   friend class base::RefCountedThreadSafe<UsbDevice>;
     61 
     62   // Called by UsbService only;
     63   UsbDevice(scoped_refptr<UsbContext> context,
     64             PlatformUsbDevice platform_device,
     65             uint16 vendor_id,
     66             uint16 product_id,
     67             uint32 unique_id);
     68 
     69   // Constructor called in test only.
     70   UsbDevice();
     71   virtual ~UsbDevice();
     72 
     73   // Called only be UsbService.
     74   virtual void OnDisconnect();
     75 
     76  private:
     77   PlatformUsbDevice platform_device_;
     78   uint16 vendor_id_;
     79   uint16 product_id_;
     80   uint32 unique_id_;
     81 
     82   // Retain the context so that it will not be released before UsbDevice.
     83   scoped_refptr<UsbContext> context_;
     84 
     85   // Opened handles.
     86   typedef std::vector<scoped_refptr<UsbDeviceHandle> > HandlesVector;
     87   HandlesVector handles_;
     88 
     89   base::ThreadChecker thread_checker_;
     90 
     91   DISALLOW_COPY_AND_ASSIGN(UsbDevice);
     92 };
     93 
     94 #endif  // CHROME_BROWSER_USB_USB_DEVICE_H_
     95