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_EXTENSIONS_API_USB_USB_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_USB_USB_API_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/memory/ref_counted.h"
     12 #include "chrome/browser/extensions/api/api_function.h"
     13 #include "chrome/browser/extensions/api/api_resource_manager.h"
     14 #include "chrome/browser/usb/usb_device.h"
     15 #include "chrome/browser/usb/usb_device_handle.h"
     16 #include "chrome/common/extensions/api/usb.h"
     17 #include "net/base/io_buffer.h"
     18 
     19 class UsbDeviceHandle;
     20 class UsbService;
     21 
     22 namespace extensions {
     23 
     24 class UsbDeviceResource;
     25 
     26 class UsbAsyncApiFunction : public AsyncApiFunction {
     27  public:
     28   UsbAsyncApiFunction();
     29 
     30  protected:
     31   virtual ~UsbAsyncApiFunction();
     32 
     33   virtual bool PrePrepare() OVERRIDE;
     34   virtual bool Respond() OVERRIDE;
     35 
     36   UsbDeviceResource* GetUsbDeviceResource(int api_resource_id);
     37   void RemoveUsbDeviceResource(int api_resource_id);
     38 
     39   void CompleteWithError(const std::string& error);
     40 
     41   ApiResourceManager<UsbDeviceResource>* manager_;
     42 };
     43 
     44 class UsbAsyncApiTransferFunction : public UsbAsyncApiFunction {
     45  protected:
     46   UsbAsyncApiTransferFunction();
     47   virtual ~UsbAsyncApiTransferFunction();
     48 
     49   bool ConvertDirectionSafely(const extensions::api::usb::Direction& input,
     50                               UsbEndpointDirection* output);
     51   bool ConvertRequestTypeSafely(const extensions::api::usb::RequestType& input,
     52                               UsbDeviceHandle::TransferRequestType* output);
     53   bool ConvertRecipientSafely(const extensions::api::usb::Recipient& input,
     54                               UsbDeviceHandle::TransferRecipient* output);
     55 
     56   void OnCompleted(UsbTransferStatus status,
     57                    scoped_refptr<net::IOBuffer> data,
     58                    size_t length);
     59 };
     60 
     61 class UsbFindDevicesFunction : public UsbAsyncApiFunction {
     62  public:
     63   DECLARE_EXTENSION_FUNCTION("usb.findDevices", USB_FINDDEVICES)
     64 
     65   UsbFindDevicesFunction();
     66 
     67   static void SetDeviceForTest(UsbDevice* device);
     68 
     69  protected:
     70   virtual ~UsbFindDevicesFunction();
     71 
     72   virtual bool Prepare() OVERRIDE;
     73   virtual void AsyncWorkStart() OVERRIDE;
     74 
     75  private:
     76   typedef scoped_ptr<std::vector<scoped_refptr<UsbDevice> > >
     77       ScopedDeviceVector;
     78 
     79   // This should be run on the FILE thread.
     80   // Wait for GetDeviceService to return and start enumeration on FILE thread.
     81   void EnumerateDevices(uint16_t vendor_id,
     82                         uint16_t product_id,
     83                         int interface_id,
     84                         UsbService* service);
     85 
     86   // Relay the result on IO thread to OnCompleted.
     87   void OnEnumerationCompleted(ScopedDeviceVector devices);
     88 
     89   // This should be run on the IO thread.
     90   // Create ApiResources and reply.
     91   void OnCompleted();
     92 
     93   scoped_ptr<base::ListValue> result_;
     94   std::vector<scoped_refptr<UsbDeviceHandle> > device_handles_;
     95   scoped_ptr<extensions::api::usb::FindDevices::Params> parameters_;
     96 };
     97 
     98 class UsbListInterfacesFunction : public UsbAsyncApiFunction {
     99  public:
    100   DECLARE_EXTENSION_FUNCTION("usb.listInterfaces", USB_LISTINTERFACES)
    101 
    102   UsbListInterfacesFunction();
    103 
    104  protected:
    105   virtual ~UsbListInterfacesFunction();
    106 
    107   virtual bool Prepare() OVERRIDE;
    108   virtual void AsyncWorkStart() OVERRIDE;
    109 
    110  private:
    111   void OnCompleted(bool success);
    112 
    113   bool ConvertDirectionSafely(const UsbEndpointDirection& input,
    114                               extensions::api::usb::Direction* output);
    115   bool ConvertSynchronizationTypeSafely(
    116       const UsbSynchronizationType& input,
    117       extensions::api::usb::SynchronizationType* output);
    118   bool ConvertTransferTypeSafely(const UsbTransferType& input,
    119                               extensions::api::usb::TransferType* output);
    120   bool ConvertUsageTypeSafely(const UsbUsageType& input,
    121                               extensions::api::usb::UsageType* output);
    122 
    123   scoped_ptr<base::ListValue> result_;
    124   scoped_refptr<UsbConfigDescriptor> config_;
    125   scoped_ptr<extensions::api::usb::ListInterfaces::Params> parameters_;
    126 };
    127 
    128 class UsbCloseDeviceFunction : public UsbAsyncApiFunction {
    129  public:
    130   DECLARE_EXTENSION_FUNCTION("usb.closeDevice", USB_CLOSEDEVICE)
    131 
    132   UsbCloseDeviceFunction();
    133 
    134  protected:
    135   virtual ~UsbCloseDeviceFunction();
    136 
    137   virtual bool Prepare() OVERRIDE;
    138   virtual void AsyncWorkStart() OVERRIDE;
    139 
    140   void OnCompleted();
    141 
    142  private:
    143   scoped_ptr<extensions::api::usb::CloseDevice::Params> parameters_;
    144 };
    145 
    146 class UsbClaimInterfaceFunction : public UsbAsyncApiFunction {
    147  public:
    148   DECLARE_EXTENSION_FUNCTION("usb.claimInterface", USB_CLAIMINTERFACE)
    149 
    150   UsbClaimInterfaceFunction();
    151 
    152  protected:
    153   virtual ~UsbClaimInterfaceFunction();
    154 
    155   virtual bool Prepare() OVERRIDE;
    156   virtual void AsyncWorkStart() OVERRIDE;
    157 
    158  private:
    159   void OnCompleted(bool success);
    160 
    161   scoped_ptr<extensions::api::usb::ClaimInterface::Params> parameters_;
    162 };
    163 
    164 class UsbReleaseInterfaceFunction : public UsbAsyncApiFunction {
    165  public:
    166   DECLARE_EXTENSION_FUNCTION("usb.releaseInterface", USB_RELEASEINTERFACE)
    167 
    168   UsbReleaseInterfaceFunction();
    169 
    170  protected:
    171   virtual ~UsbReleaseInterfaceFunction();
    172 
    173   virtual bool Prepare() OVERRIDE;
    174   virtual void AsyncWorkStart() OVERRIDE;
    175 
    176  private:
    177   void OnCompleted(bool success);
    178 
    179   scoped_ptr<extensions::api::usb::ReleaseInterface::Params> parameters_;
    180 };
    181 
    182 class UsbSetInterfaceAlternateSettingFunction : public UsbAsyncApiFunction {
    183  public:
    184   DECLARE_EXTENSION_FUNCTION("usb.setInterfaceAlternateSetting",
    185                              USB_SETINTERFACEALTERNATESETTING)
    186 
    187   UsbSetInterfaceAlternateSettingFunction();
    188 
    189  private:
    190   virtual ~UsbSetInterfaceAlternateSettingFunction();
    191 
    192   virtual bool Prepare() OVERRIDE;
    193   virtual void AsyncWorkStart() OVERRIDE;
    194 
    195   void OnCompleted(bool success);
    196 
    197   scoped_ptr<extensions::api::usb::SetInterfaceAlternateSetting::Params>
    198       parameters_;
    199 };
    200 
    201 class UsbControlTransferFunction : public UsbAsyncApiTransferFunction {
    202  public:
    203   DECLARE_EXTENSION_FUNCTION("usb.controlTransfer", USB_CONTROLTRANSFER)
    204 
    205   UsbControlTransferFunction();
    206 
    207  protected:
    208   virtual ~UsbControlTransferFunction();
    209 
    210   virtual bool Prepare() OVERRIDE;
    211   virtual void AsyncWorkStart() OVERRIDE;
    212 
    213  private:
    214   scoped_ptr<extensions::api::usb::ControlTransfer::Params> parameters_;
    215 };
    216 
    217 class UsbBulkTransferFunction : public UsbAsyncApiTransferFunction {
    218  public:
    219   DECLARE_EXTENSION_FUNCTION("usb.bulkTransfer", USB_BULKTRANSFER)
    220 
    221   UsbBulkTransferFunction();
    222 
    223  protected:
    224   virtual ~UsbBulkTransferFunction();
    225 
    226   virtual bool Prepare() OVERRIDE;
    227   virtual void AsyncWorkStart() OVERRIDE;
    228 
    229  private:
    230   scoped_ptr<extensions::api::usb::BulkTransfer::Params>
    231       parameters_;
    232 };
    233 
    234 class UsbInterruptTransferFunction : public UsbAsyncApiTransferFunction {
    235  public:
    236   DECLARE_EXTENSION_FUNCTION("usb.interruptTransfer", USB_INTERRUPTTRANSFER)
    237 
    238   UsbInterruptTransferFunction();
    239 
    240  protected:
    241   virtual ~UsbInterruptTransferFunction();
    242 
    243   virtual bool Prepare() OVERRIDE;
    244   virtual void AsyncWorkStart() OVERRIDE;
    245 
    246  private:
    247   scoped_ptr<extensions::api::usb::InterruptTransfer::Params> parameters_;
    248 };
    249 
    250 class UsbIsochronousTransferFunction : public UsbAsyncApiTransferFunction {
    251  public:
    252   DECLARE_EXTENSION_FUNCTION("usb.isochronousTransfer", USB_ISOCHRONOUSTRANSFER)
    253 
    254   UsbIsochronousTransferFunction();
    255 
    256  protected:
    257   virtual ~UsbIsochronousTransferFunction();
    258 
    259   virtual bool Prepare() OVERRIDE;
    260   virtual void AsyncWorkStart() OVERRIDE;
    261 
    262  private:
    263   scoped_ptr<extensions::api::usb::IsochronousTransfer::Params> parameters_;
    264 };
    265 
    266 class UsbResetDeviceFunction : public UsbAsyncApiFunction {
    267  public:
    268   DECLARE_EXTENSION_FUNCTION("usb.resetDevice", USB_RESETDEVICE)
    269 
    270   UsbResetDeviceFunction();
    271 
    272  protected:
    273   virtual ~UsbResetDeviceFunction();
    274 
    275   virtual bool Prepare() OVERRIDE;
    276   virtual void AsyncWorkStart() OVERRIDE;
    277 
    278  private:
    279   // This should be run on the FILE thread.
    280   void OnStartResest(UsbDeviceResource* resource);
    281   void OnCompletedFileThread(bool success);
    282 
    283   // This should be run on the IO thread.
    284   void OnCompleted(bool success);
    285   void OnError();
    286 
    287   scoped_ptr<extensions::api::usb::ResetDevice::Params> parameters_;
    288 };
    289 }  // namespace extensions
    290 
    291 #endif  // CHROME_BROWSER_EXTENSIONS_API_USB_USB_API_H_
    292