Home | History | Annotate | Download | only in usb
      1 // Copyright 2014 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 #ifndef DEVICE_USB_USB_DEVICE_H_
      6 #define DEVICE_USB_USB_DEVICE_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/callback.h"
     10 #include "base/memory/ref_counted.h"
     11 #include "base/observer_list.h"
     12 #include "base/strings/string16.h"
     13 
     14 namespace device {
     15 
     16 class UsbDeviceHandle;
     17 struct UsbConfigDescriptor;
     18 
     19 // A UsbDevice object represents a detected USB device, providing basic
     20 // information about it. For further manipulation of the device, a
     21 // UsbDeviceHandle must be created from Open() method.
     22 class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> {
     23  public:
     24   class Observer {
     25    public:
     26     virtual void OnDisconnect(scoped_refptr<UsbDevice> device) = 0;
     27   };
     28 
     29   // Accessors to basic information.
     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 RequestUsbAccess(
     40       int interface_id,
     41       const base::Callback<void(bool success)>& callback) = 0;
     42 #endif  // OS_CHROMEOS
     43 
     44   // Creates a UsbDeviceHandle for further manipulation.
     45   // Blocking method. Must be called on FILE thread.
     46   virtual scoped_refptr<UsbDeviceHandle> Open() = 0;
     47 
     48   // Explicitly closes a device handle. This method will be automatically called
     49   // by the destructor of a UsbDeviceHandle as well.
     50   // Closing a closed handle is a safe
     51   // Blocking method. Must be called on FILE thread.
     52   virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) = 0;
     53 
     54   // Gets the UsbConfigDescriptor for the active device configuration.
     55   // Blocking method. Must be called on FILE thread.
     56   virtual const UsbConfigDescriptor& GetConfiguration() = 0;
     57 
     58   // Gets the manufacturer string of the device, or returns false.
     59   // Blocking method. Must be called on FILE thread.
     60   virtual bool GetManufacturer(base::string16* manufacturer) = 0;
     61 
     62   // Gets the product string of the device, or returns false.
     63   // Blocking method. Must be called on FILE thread.
     64   virtual bool GetProduct(base::string16* product) = 0;
     65 
     66   // Gets the serial number string of the device, or returns false.
     67   // Blocking method. Must be called on FILE thread.
     68   virtual bool GetSerialNumber(base::string16* serial) = 0;
     69 
     70   void AddObserver(Observer* obs) { observer_list_.AddObserver(obs); }
     71   void RemoveObserver(Observer* obs) { observer_list_.RemoveObserver(obs); }
     72 
     73  protected:
     74   UsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id);
     75   virtual ~UsbDevice();
     76 
     77   void NotifyDisconnect();
     78 
     79  private:
     80   friend class base::RefCountedThreadSafe<UsbDevice>;
     81 
     82   const uint16 vendor_id_;
     83   const uint16 product_id_;
     84   const uint32 unique_id_;
     85 
     86   ObserverList<Observer> observer_list_;
     87 
     88   DISALLOW_COPY_AND_ASSIGN(UsbDevice);
     89 };
     90 
     91 }  // namespace device
     92 
     93 #endif  // DEVICE_USB_USB_DEVICE_H_
     94