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.
5 #ifndef EXTENSIONS_BROWSER_API_USB_USB_API_H_
6 #define EXTENSIONS_BROWSER_API_USB_USB_API_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "components/usb_service/usb_device.h"
14 #include "components/usb_service/usb_device_handle.h"
15 #include "extensions/browser/api/api_resource_manager.h"
16 #include "extensions/browser/api/async_api_function.h"
17 #include "extensions/common/api/usb.h"
18 #include "net/base/io_buffer.h"
20 namespace extensions
{
22 class UsbDeviceResource
;
24 class UsbAsyncApiFunction
: public AsyncApiFunction
{
26 UsbAsyncApiFunction();
29 virtual ~UsbAsyncApiFunction();
31 virtual bool PrePrepare() OVERRIDE
;
32 virtual bool Respond() OVERRIDE
;
34 scoped_refptr
<usb_service::UsbDevice
> GetDeviceOrOrCompleteWithError(
35 const extensions::core_api::usb::Device
& input_device
);
37 scoped_refptr
<usb_service::UsbDeviceHandle
>
38 GetDeviceHandleOrCompleteWithError(
39 const extensions::core_api::usb::ConnectionHandle
&
42 void RemoveUsbDeviceResource(int api_resource_id
);
44 void CompleteWithError(const std::string
& error
);
46 ApiResourceManager
<UsbDeviceResource
>* manager_
;
49 class UsbAsyncApiTransferFunction
: public UsbAsyncApiFunction
{
51 UsbAsyncApiTransferFunction();
52 virtual ~UsbAsyncApiTransferFunction();
54 bool ConvertDirectionSafely(const extensions::core_api::usb::Direction
& input
,
55 usb_service::UsbEndpointDirection
* output
);
56 bool ConvertRequestTypeSafely(
57 const extensions::core_api::usb::RequestType
& input
,
58 usb_service::UsbDeviceHandle::TransferRequestType
* output
);
59 bool ConvertRecipientSafely(
60 const extensions::core_api::usb::Recipient
& input
,
61 usb_service::UsbDeviceHandle::TransferRecipient
* output
);
63 void OnCompleted(usb_service::UsbTransferStatus status
,
64 scoped_refptr
<net::IOBuffer
> data
,
68 class UsbFindDevicesFunction
: public UsbAsyncApiFunction
{
70 DECLARE_EXTENSION_FUNCTION("usb.findDevices", USB_FINDDEVICES
)
72 UsbFindDevicesFunction();
75 virtual ~UsbFindDevicesFunction();
77 virtual bool Prepare() OVERRIDE
;
78 virtual void AsyncWorkStart() OVERRIDE
;
82 scoped_ptr
<std::vector
<scoped_refptr
<usb_service::UsbDevice
> > > devices
);
84 std::vector
<scoped_refptr
<usb_service::UsbDeviceHandle
> > device_handles_
;
85 scoped_ptr
<extensions::core_api::usb::FindDevices::Params
> parameters_
;
88 class UsbGetDevicesFunction
: public UsbAsyncApiFunction
{
90 DECLARE_EXTENSION_FUNCTION("usb.getDevices", USB_GETDEVICES
)
92 UsbGetDevicesFunction();
94 virtual bool Prepare() OVERRIDE
;
95 virtual void AsyncWorkStart() OVERRIDE
;
98 virtual ~UsbGetDevicesFunction();
101 void EnumerationCompletedFileThread(
102 scoped_ptr
<std::vector
<scoped_refptr
<usb_service::UsbDevice
> > > devices
);
104 scoped_ptr
<extensions::core_api::usb::GetDevices::Params
> parameters_
;
107 class UsbRequestAccessFunction
: public UsbAsyncApiFunction
{
109 DECLARE_EXTENSION_FUNCTION("usb.requestAccess", USB_REQUESTACCESS
)
111 UsbRequestAccessFunction();
113 virtual bool Prepare() OVERRIDE
;
114 virtual void AsyncWorkStart() OVERRIDE
;
117 virtual ~UsbRequestAccessFunction();
119 void OnCompleted(bool success
);
122 scoped_ptr
<extensions::core_api::usb::RequestAccess::Params
> parameters_
;
125 class UsbOpenDeviceFunction
: public UsbAsyncApiFunction
{
127 DECLARE_EXTENSION_FUNCTION("usb.openDevice", USB_OPENDEVICE
)
129 UsbOpenDeviceFunction();
131 virtual bool Prepare() OVERRIDE
;
132 virtual void AsyncWorkStart() OVERRIDE
;
135 virtual ~UsbOpenDeviceFunction();
138 scoped_refptr
<usb_service::UsbDeviceHandle
> handle_
;
139 scoped_ptr
<extensions::core_api::usb::OpenDevice::Params
> parameters_
;
142 class UsbListInterfacesFunction
: public UsbAsyncApiFunction
{
144 DECLARE_EXTENSION_FUNCTION("usb.listInterfaces", USB_LISTINTERFACES
)
146 UsbListInterfacesFunction();
149 virtual ~UsbListInterfacesFunction();
151 virtual bool Prepare() OVERRIDE
;
152 virtual void AsyncWorkStart() OVERRIDE
;
155 bool ConvertDirectionSafely(const usb_service::UsbEndpointDirection
& input
,
156 extensions::core_api::usb::Direction
* output
);
157 bool ConvertSynchronizationTypeSafely(
158 const usb_service::UsbSynchronizationType
& input
,
159 extensions::core_api::usb::SynchronizationType
* output
);
160 bool ConvertTransferTypeSafely(
161 const usb_service::UsbTransferType
& input
,
162 extensions::core_api::usb::TransferType
* output
);
163 bool ConvertUsageTypeSafely(const usb_service::UsbUsageType
& input
,
164 extensions::core_api::usb::UsageType
* output
);
166 scoped_ptr
<base::ListValue
> result_
;
167 scoped_ptr
<extensions::core_api::usb::ListInterfaces::Params
> parameters_
;
170 class UsbCloseDeviceFunction
: public UsbAsyncApiFunction
{
172 DECLARE_EXTENSION_FUNCTION("usb.closeDevice", USB_CLOSEDEVICE
)
174 UsbCloseDeviceFunction();
177 virtual ~UsbCloseDeviceFunction();
179 virtual bool Prepare() OVERRIDE
;
180 virtual void AsyncWorkStart() OVERRIDE
;
183 scoped_ptr
<extensions::core_api::usb::CloseDevice::Params
> parameters_
;
186 class UsbClaimInterfaceFunction
: public UsbAsyncApiFunction
{
188 DECLARE_EXTENSION_FUNCTION("usb.claimInterface", USB_CLAIMINTERFACE
)
190 UsbClaimInterfaceFunction();
193 virtual ~UsbClaimInterfaceFunction();
195 virtual bool Prepare() OVERRIDE
;
196 virtual void AsyncWorkStart() OVERRIDE
;
199 scoped_ptr
<extensions::core_api::usb::ClaimInterface::Params
> parameters_
;
202 class UsbReleaseInterfaceFunction
: public UsbAsyncApiFunction
{
204 DECLARE_EXTENSION_FUNCTION("usb.releaseInterface", USB_RELEASEINTERFACE
)
206 UsbReleaseInterfaceFunction();
209 virtual ~UsbReleaseInterfaceFunction();
211 virtual bool Prepare() OVERRIDE
;
212 virtual void AsyncWorkStart() OVERRIDE
;
215 scoped_ptr
<extensions::core_api::usb::ReleaseInterface::Params
> parameters_
;
218 class UsbSetInterfaceAlternateSettingFunction
: public UsbAsyncApiFunction
{
220 DECLARE_EXTENSION_FUNCTION("usb.setInterfaceAlternateSetting",
221 USB_SETINTERFACEALTERNATESETTING
)
223 UsbSetInterfaceAlternateSettingFunction();
226 virtual ~UsbSetInterfaceAlternateSettingFunction();
228 virtual bool Prepare() OVERRIDE
;
229 virtual void AsyncWorkStart() OVERRIDE
;
231 scoped_ptr
<extensions::core_api::usb::SetInterfaceAlternateSetting::Params
>
235 class UsbControlTransferFunction
: public UsbAsyncApiTransferFunction
{
237 DECLARE_EXTENSION_FUNCTION("usb.controlTransfer", USB_CONTROLTRANSFER
)
239 UsbControlTransferFunction();
242 virtual ~UsbControlTransferFunction();
244 virtual bool Prepare() OVERRIDE
;
245 virtual void AsyncWorkStart() OVERRIDE
;
248 scoped_ptr
<extensions::core_api::usb::ControlTransfer::Params
> parameters_
;
251 class UsbBulkTransferFunction
: public UsbAsyncApiTransferFunction
{
253 DECLARE_EXTENSION_FUNCTION("usb.bulkTransfer", USB_BULKTRANSFER
)
255 UsbBulkTransferFunction();
258 virtual ~UsbBulkTransferFunction();
260 virtual bool Prepare() OVERRIDE
;
261 virtual void AsyncWorkStart() OVERRIDE
;
264 scoped_ptr
<extensions::core_api::usb::BulkTransfer::Params
> parameters_
;
267 class UsbInterruptTransferFunction
: public UsbAsyncApiTransferFunction
{
269 DECLARE_EXTENSION_FUNCTION("usb.interruptTransfer", USB_INTERRUPTTRANSFER
)
271 UsbInterruptTransferFunction();
274 virtual ~UsbInterruptTransferFunction();
276 virtual bool Prepare() OVERRIDE
;
277 virtual void AsyncWorkStart() OVERRIDE
;
280 scoped_ptr
<extensions::core_api::usb::InterruptTransfer::Params
> parameters_
;
283 class UsbIsochronousTransferFunction
: public UsbAsyncApiTransferFunction
{
285 DECLARE_EXTENSION_FUNCTION("usb.isochronousTransfer", USB_ISOCHRONOUSTRANSFER
)
287 UsbIsochronousTransferFunction();
290 virtual ~UsbIsochronousTransferFunction();
292 virtual bool Prepare() OVERRIDE
;
293 virtual void AsyncWorkStart() OVERRIDE
;
296 scoped_ptr
<extensions::core_api::usb::IsochronousTransfer::Params
>
300 class UsbResetDeviceFunction
: public UsbAsyncApiFunction
{
302 DECLARE_EXTENSION_FUNCTION("usb.resetDevice", USB_RESETDEVICE
)
304 UsbResetDeviceFunction();
307 virtual ~UsbResetDeviceFunction();
309 virtual bool Prepare() OVERRIDE
;
310 virtual void AsyncWorkStart() OVERRIDE
;
313 scoped_ptr
<extensions::core_api::usb::ResetDevice::Params
> parameters_
;
315 } // namespace extensions
317 #endif // EXTENSIONS_BROWSER_API_USB_USB_API_H_