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