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 "device/usb/usb_device.h"
14 #include "device/usb/usb_device_filter.h"
15 #include "device/usb/usb_device_handle.h"
16 #include "extensions/browser/api/api_resource_manager.h"
17 #include "extensions/browser/extension_function.h"
18 #include "extensions/common/api/usb.h"
19 #include "net/base/io_buffer.h"
21 namespace extensions
{
23 class DevicePermissionEntry
;
24 class DevicePermissions
;
25 class DevicePermissionsPrompt
;
26 class DevicePermissionsManager
;
27 class UsbDeviceResource
;
29 class UsbPermissionCheckingFunction
: public UIThreadExtensionFunction
{
31 UsbPermissionCheckingFunction();
32 ~UsbPermissionCheckingFunction() override
;
34 bool HasDevicePermission(scoped_refptr
<device::UsbDevice
> device
);
35 void RecordDeviceLastUsed();
38 DevicePermissionsManager
* device_permissions_manager_
;
39 scoped_refptr
<DevicePermissionEntry
> permission_entry_
;
42 class UsbConnectionFunction
: public UIThreadExtensionFunction
{
44 UsbConnectionFunction();
45 ~UsbConnectionFunction() override
;
47 scoped_refptr
<device::UsbDeviceHandle
> GetDeviceHandle(
48 const extensions::core_api::usb::ConnectionHandle
& handle
);
49 void ReleaseDeviceHandle(
50 const extensions::core_api::usb::ConnectionHandle
& handle
);
53 class UsbTransferFunction
: public UsbConnectionFunction
{
55 UsbTransferFunction();
56 ~UsbTransferFunction() override
;
58 void OnCompleted(device::UsbTransferStatus status
,
59 scoped_refptr
<net::IOBuffer
> data
,
63 class UsbFindDevicesFunction
: public UIThreadExtensionFunction
{
65 DECLARE_EXTENSION_FUNCTION("usb.findDevices", USB_FINDDEVICES
)
67 UsbFindDevicesFunction();
70 ~UsbFindDevicesFunction() override
;
73 ResponseAction
Run() override
;
75 void OnGetDevicesComplete(
76 const std::vector
<scoped_refptr
<device::UsbDevice
>>& devices
);
77 void OnDeviceOpened(scoped_refptr
<device::UsbDeviceHandle
> device_handle
);
82 scoped_ptr
<base::ListValue
> result_
;
83 base::Closure barrier_
;
85 DISALLOW_COPY_AND_ASSIGN(UsbFindDevicesFunction
);
88 class UsbGetDevicesFunction
: public UsbPermissionCheckingFunction
{
90 DECLARE_EXTENSION_FUNCTION("usb.getDevices", USB_GETDEVICES
)
92 UsbGetDevicesFunction();
95 ~UsbGetDevicesFunction() override
;
98 ResponseAction
Run() override
;
100 void OnGetDevicesComplete(
101 const std::vector
<scoped_refptr
<device::UsbDevice
>>& devices
);
103 std::vector
<device::UsbDeviceFilter
> filters_
;
105 DISALLOW_COPY_AND_ASSIGN(UsbGetDevicesFunction
);
108 class UsbGetUserSelectedDevicesFunction
: public UIThreadExtensionFunction
{
110 DECLARE_EXTENSION_FUNCTION("usb.getUserSelectedDevices",
111 USB_GETUSERSELECTEDDEVICES
)
113 UsbGetUserSelectedDevicesFunction();
116 ~UsbGetUserSelectedDevicesFunction() override
;
118 // ExtensionFunction:
119 ResponseAction
Run() override
;
121 void OnDevicesChosen(
122 const std::vector
<scoped_refptr
<device::UsbDevice
>>& devices
);
124 scoped_ptr
<DevicePermissionsPrompt
> prompt_
;
126 DISALLOW_COPY_AND_ASSIGN(UsbGetUserSelectedDevicesFunction
);
129 class UsbRequestAccessFunction
: public UIThreadExtensionFunction
{
131 DECLARE_EXTENSION_FUNCTION("usb.requestAccess", USB_REQUESTACCESS
)
133 UsbRequestAccessFunction();
136 ~UsbRequestAccessFunction() override
;
138 // ExtensionFunction:
139 ResponseAction
Run() override
;
141 DISALLOW_COPY_AND_ASSIGN(UsbRequestAccessFunction
);
144 class UsbOpenDeviceFunction
: public UsbPermissionCheckingFunction
{
146 DECLARE_EXTENSION_FUNCTION("usb.openDevice", USB_OPENDEVICE
)
148 UsbOpenDeviceFunction();
151 ~UsbOpenDeviceFunction() override
;
153 // ExtensionFunction:
154 ResponseAction
Run() override
;
156 void OnDeviceOpened(scoped_refptr
<device::UsbDeviceHandle
> device_handle
);
158 DISALLOW_COPY_AND_ASSIGN(UsbOpenDeviceFunction
);
161 class UsbSetConfigurationFunction
: public UsbConnectionFunction
{
163 DECLARE_EXTENSION_FUNCTION("usb.setConfiguration", USB_SETCONFIGURATION
)
165 UsbSetConfigurationFunction();
168 ~UsbSetConfigurationFunction() override
;
170 // ExtensionFunction:
171 ResponseAction
Run() override
;
173 void OnComplete(bool success
);
175 DISALLOW_COPY_AND_ASSIGN(UsbSetConfigurationFunction
);
178 class UsbGetConfigurationFunction
: public UsbConnectionFunction
{
180 DECLARE_EXTENSION_FUNCTION("usb.getConfiguration", USB_GETCONFIGURATION
)
182 UsbGetConfigurationFunction();
185 ~UsbGetConfigurationFunction() override
;
187 // ExtensionFunction:
188 ResponseAction
Run() override
;
190 DISALLOW_COPY_AND_ASSIGN(UsbGetConfigurationFunction
);
193 class UsbListInterfacesFunction
: public UsbConnectionFunction
{
195 DECLARE_EXTENSION_FUNCTION("usb.listInterfaces", USB_LISTINTERFACES
)
197 UsbListInterfacesFunction();
200 ~UsbListInterfacesFunction() override
;
202 // ExtensionFunction:
203 ResponseAction
Run() override
;
205 DISALLOW_COPY_AND_ASSIGN(UsbListInterfacesFunction
);
208 class UsbCloseDeviceFunction
: public UsbConnectionFunction
{
210 DECLARE_EXTENSION_FUNCTION("usb.closeDevice", USB_CLOSEDEVICE
)
212 UsbCloseDeviceFunction();
215 ~UsbCloseDeviceFunction() override
;
217 // ExtensionFunction:
218 ResponseAction
Run() override
;
220 DISALLOW_COPY_AND_ASSIGN(UsbCloseDeviceFunction
);
223 class UsbClaimInterfaceFunction
: public UsbConnectionFunction
{
225 DECLARE_EXTENSION_FUNCTION("usb.claimInterface", USB_CLAIMINTERFACE
)
227 UsbClaimInterfaceFunction();
230 ~UsbClaimInterfaceFunction() override
;
232 // ExtensionFunction:
233 ResponseAction
Run() override
;
235 void OnComplete(bool success
);
237 DISALLOW_COPY_AND_ASSIGN(UsbClaimInterfaceFunction
);
240 class UsbReleaseInterfaceFunction
: public UsbConnectionFunction
{
242 DECLARE_EXTENSION_FUNCTION("usb.releaseInterface", USB_RELEASEINTERFACE
)
244 UsbReleaseInterfaceFunction();
247 ~UsbReleaseInterfaceFunction() override
;
249 // ExtensionFunction:
250 ResponseAction
Run() override
;
252 DISALLOW_COPY_AND_ASSIGN(UsbReleaseInterfaceFunction
);
255 class UsbSetInterfaceAlternateSettingFunction
: public UsbConnectionFunction
{
257 DECLARE_EXTENSION_FUNCTION("usb.setInterfaceAlternateSetting",
258 USB_SETINTERFACEALTERNATESETTING
)
260 UsbSetInterfaceAlternateSettingFunction();
263 ~UsbSetInterfaceAlternateSettingFunction() override
;
265 // ExtensionFunction:
266 ResponseAction
Run() override
;
268 void OnComplete(bool success
);
270 DISALLOW_COPY_AND_ASSIGN(UsbSetInterfaceAlternateSettingFunction
);
273 class UsbControlTransferFunction
: public UsbTransferFunction
{
275 DECLARE_EXTENSION_FUNCTION("usb.controlTransfer", USB_CONTROLTRANSFER
)
277 UsbControlTransferFunction();
280 ~UsbControlTransferFunction() override
;
282 // ExtensionFunction:
283 ResponseAction
Run() override
;
285 DISALLOW_COPY_AND_ASSIGN(UsbControlTransferFunction
);
288 class UsbBulkTransferFunction
: public UsbTransferFunction
{
290 DECLARE_EXTENSION_FUNCTION("usb.bulkTransfer", USB_BULKTRANSFER
)
292 UsbBulkTransferFunction();
295 ~UsbBulkTransferFunction() override
;
297 // ExtensionFunction:
298 ResponseAction
Run() override
;
300 DISALLOW_COPY_AND_ASSIGN(UsbBulkTransferFunction
);
303 class UsbInterruptTransferFunction
: public UsbTransferFunction
{
305 DECLARE_EXTENSION_FUNCTION("usb.interruptTransfer", USB_INTERRUPTTRANSFER
)
307 UsbInterruptTransferFunction();
310 ~UsbInterruptTransferFunction() override
;
312 // ExtensionFunction:
313 ResponseAction
Run() override
;
315 DISALLOW_COPY_AND_ASSIGN(UsbInterruptTransferFunction
);
318 class UsbIsochronousTransferFunction
: public UsbTransferFunction
{
320 DECLARE_EXTENSION_FUNCTION("usb.isochronousTransfer", USB_ISOCHRONOUSTRANSFER
)
322 UsbIsochronousTransferFunction();
325 ~UsbIsochronousTransferFunction() override
;
327 // ExtensionFunction:
328 ResponseAction
Run() override
;
330 DISALLOW_COPY_AND_ASSIGN(UsbIsochronousTransferFunction
);
333 class UsbResetDeviceFunction
: public UsbConnectionFunction
{
335 DECLARE_EXTENSION_FUNCTION("usb.resetDevice", USB_RESETDEVICE
)
337 UsbResetDeviceFunction();
340 ~UsbResetDeviceFunction() override
;
342 // ExtensionFunction:
343 ResponseAction
Run() override
;
345 void OnComplete(bool success
);
347 scoped_ptr
<extensions::core_api::usb::ResetDevice::Params
> parameters_
;
349 DISALLOW_COPY_AND_ASSIGN(UsbResetDeviceFunction
);
351 } // namespace extensions
353 #endif // EXTENSIONS_BROWSER_API_USB_USB_API_H_