Home | History | Annotate | Download | only in usb
      1 // Copyright (c) 2012 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 CHROME_BROWSER_USB_USB_SERVICE_H_
      6 #define CHROME_BROWSER_USB_USB_SERVICE_H_
      7 
      8 #include <map>
      9 #include <utility>
     10 #include <vector>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/memory/singleton.h"
     14 #include "chrome/browser/usb/usb_device.h"
     15 
     16 namespace base {
     17 
     18 template <class T> class DeleteHelper;
     19 
     20 }  // namespace base
     21 
     22 template <typename T> struct DefaultSingletonTraits;
     23 class UsbContext;
     24 
     25 // The USB service handles creating and managing an event handler thread that is
     26 // used to manage and dispatch USB events. It is also responsbile for device
     27 // discovery on the system, which allows it to re-use device handles to prevent
     28 // competition for the same USB device.
     29 class UsbService {
     30  public:
     31   typedef scoped_ptr<std::vector<scoped_refptr<UsbDevice> > >
     32       ScopedDeviceVector;
     33 
     34   // Must be called on FILE thread.
     35   static UsbService* GetInstance();
     36 
     37   // Find all of the devices attached to the system that are identified by
     38   // |vendor_id| and |product_id|, inserting them into |devices|. Clears
     39   // |devices| before use. Calls |callback| once |devices| is populated.
     40   // The result will be sorted by id in increasing order. Must be called on
     41   // FILE thread.
     42   void FindDevices(
     43       const uint16 vendor_id,
     44       const uint16 product_id,
     45       int interface_id,
     46       const base::Callback<void(ScopedDeviceVector vector)>& callback);
     47 
     48   // Get all of the devices attached to the system, inserting them into
     49   // |devices|. Clears |devices| before use. The result will be sorted by id
     50   // in increasing order. Must be called on FILE thread.
     51   void GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices);
     52 
     53  private:
     54   friend struct DefaultSingletonTraits<UsbService>;
     55   friend class base::DeleteHelper<UsbService>;
     56 
     57   UsbService();
     58   virtual ~UsbService();
     59 
     60   // Return true if |device|'s vendor and product identifiers match |vendor_id|
     61   // and |product_id|.
     62   static bool DeviceMatches(scoped_refptr<UsbDevice> device,
     63                             const uint16 vendor_id,
     64                             const uint16 product_id);
     65 
     66   // This method is called when permission broker replied our request.
     67   // We will simply relay it to FILE thread.
     68   void OnRequestUsbAccessReplied(
     69       const uint16 vendor_id,
     70       const uint16 product_id,
     71       const base::Callback<void(ScopedDeviceVector vector)>& callback,
     72       bool success);
     73 
     74   // FindDevicesImpl is called by FindDevices on ChromeOS after the permission
     75   // broker has signaled that permission has been granted to access the
     76   // underlying device nodes. On other platforms, it is called directly by
     77   // FindDevices.
     78   void FindDevicesImpl(
     79       const uint16 vendor_id,
     80       const uint16 product_id,
     81       const base::Callback<void(ScopedDeviceVector vector)>& callback,
     82       bool success);
     83 
     84   // Enumerate USB devices from OS and Update devices_ map.
     85   void RefreshDevices();
     86 
     87   scoped_refptr<UsbContext> context_;
     88 
     89   // The map from PlatformUsbDevices to UsbDevices.
     90   typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDevice> > DeviceMap;
     91   DeviceMap devices_;
     92 
     93   DISALLOW_COPY_AND_ASSIGN(UsbService);
     94 };
     95 
     96 #endif  // CHROME_BROWSER_USB_USB_SERVICE_H_
     97