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 OnRequestAccessComplete(scoped_refptr
<device::UsbDevice
> device
,
79 void OnDeviceOpened(scoped_refptr
<device::UsbDeviceHandle
> device_handle
);
85 scoped_ptr
<base::ListValue
> result_
;
86 base::Closure barrier_
;
88 DISALLOW_COPY_AND_ASSIGN(UsbFindDevicesFunction
);
91 class UsbGetDevicesFunction
: public UsbPermissionCheckingFunction
{
93 DECLARE_EXTENSION_FUNCTION("usb.getDevices", USB_GETDEVICES
)
95 UsbGetDevicesFunction();
98 ~UsbGetDevicesFunction() override
;
100 // ExtensionFunction:
101 ResponseAction
Run() override
;
103 void OnGetDevicesComplete(
104 const std::vector
<scoped_refptr
<device::UsbDevice
>>& devices
);
106 std::vector
<device::UsbDeviceFilter
> filters_
;
108 DISALLOW_COPY_AND_ASSIGN(UsbGetDevicesFunction
);
111 class UsbGetUserSelectedDevicesFunction
: public UIThreadExtensionFunction
{
113 DECLARE_EXTENSION_FUNCTION("usb.getUserSelectedDevices",
114 USB_GETUSERSELECTEDDEVICES
)
116 UsbGetUserSelectedDevicesFunction();
119 ~UsbGetUserSelectedDevicesFunction() override
;
121 // ExtensionFunction:
122 ResponseAction
Run() override
;
124 void OnDevicesChosen(
125 const std::vector
<scoped_refptr
<device::UsbDevice
>>& devices
);
127 scoped_ptr
<DevicePermissionsPrompt
> prompt_
;
129 DISALLOW_COPY_AND_ASSIGN(UsbGetUserSelectedDevicesFunction
);
132 class UsbRequestAccessFunction
: public UIThreadExtensionFunction
{
134 DECLARE_EXTENSION_FUNCTION("usb.requestAccess", USB_REQUESTACCESS
)
136 UsbRequestAccessFunction();
139 ~UsbRequestAccessFunction() override
;
141 // ExtensionFunction:
142 ResponseAction
Run() override
;
144 DISALLOW_COPY_AND_ASSIGN(UsbRequestAccessFunction
);
147 class UsbOpenDeviceFunction
: public UsbPermissionCheckingFunction
{
149 DECLARE_EXTENSION_FUNCTION("usb.openDevice", USB_OPENDEVICE
)
151 UsbOpenDeviceFunction();
154 ~UsbOpenDeviceFunction() override
;
156 // ExtensionFunction:
157 ResponseAction
Run() override
;
159 void OnRequestAccessComplete(scoped_refptr
<device::UsbDevice
> device
,
161 void OnDeviceOpened(scoped_refptr
<device::UsbDeviceHandle
> device_handle
);
163 DISALLOW_COPY_AND_ASSIGN(UsbOpenDeviceFunction
);
166 class UsbSetConfigurationFunction
: public UsbConnectionFunction
{
168 DECLARE_EXTENSION_FUNCTION("usb.setConfiguration", USB_SETCONFIGURATION
)
170 UsbSetConfigurationFunction();
173 ~UsbSetConfigurationFunction() override
;
175 // ExtensionFunction:
176 ResponseAction
Run() override
;
178 void OnComplete(bool success
);
180 DISALLOW_COPY_AND_ASSIGN(UsbSetConfigurationFunction
);
183 class UsbGetConfigurationFunction
: public UsbConnectionFunction
{
185 DECLARE_EXTENSION_FUNCTION("usb.getConfiguration", USB_GETCONFIGURATION
)
187 UsbGetConfigurationFunction();
190 ~UsbGetConfigurationFunction() override
;
192 // ExtensionFunction:
193 ResponseAction
Run() override
;
195 DISALLOW_COPY_AND_ASSIGN(UsbGetConfigurationFunction
);
198 class UsbListInterfacesFunction
: public UsbConnectionFunction
{
200 DECLARE_EXTENSION_FUNCTION("usb.listInterfaces", USB_LISTINTERFACES
)
202 UsbListInterfacesFunction();
205 ~UsbListInterfacesFunction() override
;
207 // ExtensionFunction:
208 ResponseAction
Run() override
;
210 DISALLOW_COPY_AND_ASSIGN(UsbListInterfacesFunction
);
213 class UsbCloseDeviceFunction
: public UsbConnectionFunction
{
215 DECLARE_EXTENSION_FUNCTION("usb.closeDevice", USB_CLOSEDEVICE
)
217 UsbCloseDeviceFunction();
220 ~UsbCloseDeviceFunction() override
;
222 // ExtensionFunction:
223 ResponseAction
Run() override
;
225 DISALLOW_COPY_AND_ASSIGN(UsbCloseDeviceFunction
);
228 class UsbClaimInterfaceFunction
: public UsbConnectionFunction
{
230 DECLARE_EXTENSION_FUNCTION("usb.claimInterface", USB_CLAIMINTERFACE
)
232 UsbClaimInterfaceFunction();
235 ~UsbClaimInterfaceFunction() override
;
237 // ExtensionFunction:
238 ResponseAction
Run() override
;
240 void OnComplete(bool success
);
242 DISALLOW_COPY_AND_ASSIGN(UsbClaimInterfaceFunction
);
245 class UsbReleaseInterfaceFunction
: public UsbConnectionFunction
{
247 DECLARE_EXTENSION_FUNCTION("usb.releaseInterface", USB_RELEASEINTERFACE
)
249 UsbReleaseInterfaceFunction();
252 ~UsbReleaseInterfaceFunction() override
;
254 // ExtensionFunction:
255 ResponseAction
Run() override
;
257 DISALLOW_COPY_AND_ASSIGN(UsbReleaseInterfaceFunction
);
260 class UsbSetInterfaceAlternateSettingFunction
: public UsbConnectionFunction
{
262 DECLARE_EXTENSION_FUNCTION("usb.setInterfaceAlternateSetting",
263 USB_SETINTERFACEALTERNATESETTING
)
265 UsbSetInterfaceAlternateSettingFunction();
268 ~UsbSetInterfaceAlternateSettingFunction() override
;
270 // ExtensionFunction:
271 ResponseAction
Run() override
;
273 void OnComplete(bool success
);
275 DISALLOW_COPY_AND_ASSIGN(UsbSetInterfaceAlternateSettingFunction
);
278 class UsbControlTransferFunction
: public UsbTransferFunction
{
280 DECLARE_EXTENSION_FUNCTION("usb.controlTransfer", USB_CONTROLTRANSFER
)
282 UsbControlTransferFunction();
285 ~UsbControlTransferFunction() override
;
287 // ExtensionFunction:
288 ResponseAction
Run() override
;
290 DISALLOW_COPY_AND_ASSIGN(UsbControlTransferFunction
);
293 class UsbBulkTransferFunction
: public UsbTransferFunction
{
295 DECLARE_EXTENSION_FUNCTION("usb.bulkTransfer", USB_BULKTRANSFER
)
297 UsbBulkTransferFunction();
300 ~UsbBulkTransferFunction() override
;
302 // ExtensionFunction:
303 ResponseAction
Run() override
;
305 DISALLOW_COPY_AND_ASSIGN(UsbBulkTransferFunction
);
308 class UsbInterruptTransferFunction
: public UsbTransferFunction
{
310 DECLARE_EXTENSION_FUNCTION("usb.interruptTransfer", USB_INTERRUPTTRANSFER
)
312 UsbInterruptTransferFunction();
315 ~UsbInterruptTransferFunction() override
;
317 // ExtensionFunction:
318 ResponseAction
Run() override
;
320 DISALLOW_COPY_AND_ASSIGN(UsbInterruptTransferFunction
);
323 class UsbIsochronousTransferFunction
: public UsbTransferFunction
{
325 DECLARE_EXTENSION_FUNCTION("usb.isochronousTransfer", USB_ISOCHRONOUSTRANSFER
)
327 UsbIsochronousTransferFunction();
330 ~UsbIsochronousTransferFunction() override
;
332 // ExtensionFunction:
333 ResponseAction
Run() override
;
335 DISALLOW_COPY_AND_ASSIGN(UsbIsochronousTransferFunction
);
338 class UsbResetDeviceFunction
: public UsbConnectionFunction
{
340 DECLARE_EXTENSION_FUNCTION("usb.resetDevice", USB_RESETDEVICE
)
342 UsbResetDeviceFunction();
345 ~UsbResetDeviceFunction() override
;
347 // ExtensionFunction:
348 ResponseAction
Run() override
;
350 void OnComplete(bool success
);
352 scoped_ptr
<extensions::core_api::usb::ResetDevice::Params
> parameters_
;
354 DISALLOW_COPY_AND_ASSIGN(UsbResetDeviceFunction
);
356 } // namespace extensions
358 #endif // EXTENSIONS_BROWSER_API_USB_USB_API_H_