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::api::usb::ConnectionHandle
& handle
);
49 void ReleaseDeviceHandle(
50 const extensions::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 UsbGetConfigurationsFunction
: public UsbPermissionCheckingFunction
{
131 DECLARE_EXTENSION_FUNCTION("usb.getConfigurations", USB_GETCONFIGURATIONS
);
133 UsbGetConfigurationsFunction();
136 ~UsbGetConfigurationsFunction() override
;
138 // ExtensionFunction:
139 ResponseAction
Run() override
;
141 DISALLOW_COPY_AND_ASSIGN(UsbGetConfigurationsFunction
);
144 class UsbRequestAccessFunction
: public UIThreadExtensionFunction
{
146 DECLARE_EXTENSION_FUNCTION("usb.requestAccess", USB_REQUESTACCESS
)
148 UsbRequestAccessFunction();
151 ~UsbRequestAccessFunction() override
;
153 // ExtensionFunction:
154 ResponseAction
Run() override
;
156 DISALLOW_COPY_AND_ASSIGN(UsbRequestAccessFunction
);
159 class UsbOpenDeviceFunction
: public UsbPermissionCheckingFunction
{
161 DECLARE_EXTENSION_FUNCTION("usb.openDevice", USB_OPENDEVICE
)
163 UsbOpenDeviceFunction();
166 ~UsbOpenDeviceFunction() override
;
168 // ExtensionFunction:
169 ResponseAction
Run() override
;
171 void OnDeviceOpened(scoped_refptr
<device::UsbDeviceHandle
> device_handle
);
173 DISALLOW_COPY_AND_ASSIGN(UsbOpenDeviceFunction
);
176 class UsbSetConfigurationFunction
: public UsbConnectionFunction
{
178 DECLARE_EXTENSION_FUNCTION("usb.setConfiguration", USB_SETCONFIGURATION
)
180 UsbSetConfigurationFunction();
183 ~UsbSetConfigurationFunction() override
;
185 // ExtensionFunction:
186 ResponseAction
Run() override
;
188 void OnComplete(bool success
);
190 DISALLOW_COPY_AND_ASSIGN(UsbSetConfigurationFunction
);
193 class UsbGetConfigurationFunction
: public UsbConnectionFunction
{
195 DECLARE_EXTENSION_FUNCTION("usb.getConfiguration", USB_GETCONFIGURATION
)
197 UsbGetConfigurationFunction();
200 ~UsbGetConfigurationFunction() override
;
202 // ExtensionFunction:
203 ResponseAction
Run() override
;
205 DISALLOW_COPY_AND_ASSIGN(UsbGetConfigurationFunction
);
208 class UsbListInterfacesFunction
: public UsbConnectionFunction
{
210 DECLARE_EXTENSION_FUNCTION("usb.listInterfaces", USB_LISTINTERFACES
)
212 UsbListInterfacesFunction();
215 ~UsbListInterfacesFunction() override
;
217 // ExtensionFunction:
218 ResponseAction
Run() override
;
220 DISALLOW_COPY_AND_ASSIGN(UsbListInterfacesFunction
);
223 class UsbCloseDeviceFunction
: public UsbConnectionFunction
{
225 DECLARE_EXTENSION_FUNCTION("usb.closeDevice", USB_CLOSEDEVICE
)
227 UsbCloseDeviceFunction();
230 ~UsbCloseDeviceFunction() override
;
232 // ExtensionFunction:
233 ResponseAction
Run() override
;
235 DISALLOW_COPY_AND_ASSIGN(UsbCloseDeviceFunction
);
238 class UsbClaimInterfaceFunction
: public UsbConnectionFunction
{
240 DECLARE_EXTENSION_FUNCTION("usb.claimInterface", USB_CLAIMINTERFACE
)
242 UsbClaimInterfaceFunction();
245 ~UsbClaimInterfaceFunction() override
;
247 // ExtensionFunction:
248 ResponseAction
Run() override
;
250 void OnComplete(bool success
);
252 DISALLOW_COPY_AND_ASSIGN(UsbClaimInterfaceFunction
);
255 class UsbReleaseInterfaceFunction
: public UsbConnectionFunction
{
257 DECLARE_EXTENSION_FUNCTION("usb.releaseInterface", USB_RELEASEINTERFACE
)
259 UsbReleaseInterfaceFunction();
262 ~UsbReleaseInterfaceFunction() override
;
264 // ExtensionFunction:
265 ResponseAction
Run() override
;
267 DISALLOW_COPY_AND_ASSIGN(UsbReleaseInterfaceFunction
);
270 class UsbSetInterfaceAlternateSettingFunction
: public UsbConnectionFunction
{
272 DECLARE_EXTENSION_FUNCTION("usb.setInterfaceAlternateSetting",
273 USB_SETINTERFACEALTERNATESETTING
)
275 UsbSetInterfaceAlternateSettingFunction();
278 ~UsbSetInterfaceAlternateSettingFunction() override
;
280 // ExtensionFunction:
281 ResponseAction
Run() override
;
283 void OnComplete(bool success
);
285 DISALLOW_COPY_AND_ASSIGN(UsbSetInterfaceAlternateSettingFunction
);
288 class UsbControlTransferFunction
: public UsbTransferFunction
{
290 DECLARE_EXTENSION_FUNCTION("usb.controlTransfer", USB_CONTROLTRANSFER
)
292 UsbControlTransferFunction();
295 ~UsbControlTransferFunction() override
;
297 // ExtensionFunction:
298 ResponseAction
Run() override
;
300 DISALLOW_COPY_AND_ASSIGN(UsbControlTransferFunction
);
303 class UsbBulkTransferFunction
: public UsbTransferFunction
{
305 DECLARE_EXTENSION_FUNCTION("usb.bulkTransfer", USB_BULKTRANSFER
)
307 UsbBulkTransferFunction();
310 ~UsbBulkTransferFunction() override
;
312 // ExtensionFunction:
313 ResponseAction
Run() override
;
315 DISALLOW_COPY_AND_ASSIGN(UsbBulkTransferFunction
);
318 class UsbInterruptTransferFunction
: public UsbTransferFunction
{
320 DECLARE_EXTENSION_FUNCTION("usb.interruptTransfer", USB_INTERRUPTTRANSFER
)
322 UsbInterruptTransferFunction();
325 ~UsbInterruptTransferFunction() override
;
327 // ExtensionFunction:
328 ResponseAction
Run() override
;
330 DISALLOW_COPY_AND_ASSIGN(UsbInterruptTransferFunction
);
333 class UsbIsochronousTransferFunction
: public UsbTransferFunction
{
335 DECLARE_EXTENSION_FUNCTION("usb.isochronousTransfer", USB_ISOCHRONOUSTRANSFER
)
337 UsbIsochronousTransferFunction();
340 ~UsbIsochronousTransferFunction() override
;
342 // ExtensionFunction:
343 ResponseAction
Run() override
;
345 DISALLOW_COPY_AND_ASSIGN(UsbIsochronousTransferFunction
);
348 class UsbResetDeviceFunction
: public UsbConnectionFunction
{
350 DECLARE_EXTENSION_FUNCTION("usb.resetDevice", USB_RESETDEVICE
)
352 UsbResetDeviceFunction();
355 ~UsbResetDeviceFunction() override
;
357 // ExtensionFunction:
358 ResponseAction
Run() override
;
360 void OnComplete(bool success
);
362 scoped_ptr
<extensions::api::usb::ResetDevice::Params
> parameters_
;
364 DISALLOW_COPY_AND_ASSIGN(UsbResetDeviceFunction
);
366 } // namespace extensions
368 #endif // EXTENSIONS_BROWSER_API_USB_USB_API_H_