Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / usb / usb_api.h
blobabc5c89d5cbab486aeaaa288dd4bb682563a09c4
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_
8 #include <string>
9 #include <vector>
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 {
30 protected:
31 UsbPermissionCheckingFunction();
32 ~UsbPermissionCheckingFunction() override;
34 bool HasDevicePermission(scoped_refptr<device::UsbDevice> device);
35 void RecordDeviceLastUsed();
37 private:
38 DevicePermissionsManager* device_permissions_manager_;
39 scoped_refptr<DevicePermissionEntry> permission_entry_;
42 class UsbConnectionFunction : public UIThreadExtensionFunction {
43 protected:
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 {
54 protected:
55 UsbTransferFunction();
56 ~UsbTransferFunction() override;
58 void OnCompleted(device::UsbTransferStatus status,
59 scoped_refptr<net::IOBuffer> data,
60 size_t length);
63 class UsbFindDevicesFunction : public UIThreadExtensionFunction {
64 public:
65 DECLARE_EXTENSION_FUNCTION("usb.findDevices", USB_FINDDEVICES)
67 UsbFindDevicesFunction();
69 private:
70 ~UsbFindDevicesFunction() override;
72 // ExtensionFunction:
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);
78 void OpenComplete();
80 uint16_t vendor_id_;
81 uint16_t product_id_;
82 scoped_ptr<base::ListValue> result_;
83 base::Closure barrier_;
85 DISALLOW_COPY_AND_ASSIGN(UsbFindDevicesFunction);
88 class UsbGetDevicesFunction : public UsbPermissionCheckingFunction {
89 public:
90 DECLARE_EXTENSION_FUNCTION("usb.getDevices", USB_GETDEVICES)
92 UsbGetDevicesFunction();
94 private:
95 ~UsbGetDevicesFunction() override;
97 // ExtensionFunction:
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 {
109 public:
110 DECLARE_EXTENSION_FUNCTION("usb.getUserSelectedDevices",
111 USB_GETUSERSELECTEDDEVICES)
113 UsbGetUserSelectedDevicesFunction();
115 private:
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 {
130 public:
131 DECLARE_EXTENSION_FUNCTION("usb.getConfigurations", USB_GETCONFIGURATIONS);
133 UsbGetConfigurationsFunction();
135 private:
136 ~UsbGetConfigurationsFunction() override;
138 // ExtensionFunction:
139 ResponseAction Run() override;
141 DISALLOW_COPY_AND_ASSIGN(UsbGetConfigurationsFunction);
144 class UsbRequestAccessFunction : public UIThreadExtensionFunction {
145 public:
146 DECLARE_EXTENSION_FUNCTION("usb.requestAccess", USB_REQUESTACCESS)
148 UsbRequestAccessFunction();
150 private:
151 ~UsbRequestAccessFunction() override;
153 // ExtensionFunction:
154 ResponseAction Run() override;
156 DISALLOW_COPY_AND_ASSIGN(UsbRequestAccessFunction);
159 class UsbOpenDeviceFunction : public UsbPermissionCheckingFunction {
160 public:
161 DECLARE_EXTENSION_FUNCTION("usb.openDevice", USB_OPENDEVICE)
163 UsbOpenDeviceFunction();
165 private:
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 {
177 public:
178 DECLARE_EXTENSION_FUNCTION("usb.setConfiguration", USB_SETCONFIGURATION)
180 UsbSetConfigurationFunction();
182 private:
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 {
194 public:
195 DECLARE_EXTENSION_FUNCTION("usb.getConfiguration", USB_GETCONFIGURATION)
197 UsbGetConfigurationFunction();
199 private:
200 ~UsbGetConfigurationFunction() override;
202 // ExtensionFunction:
203 ResponseAction Run() override;
205 DISALLOW_COPY_AND_ASSIGN(UsbGetConfigurationFunction);
208 class UsbListInterfacesFunction : public UsbConnectionFunction {
209 public:
210 DECLARE_EXTENSION_FUNCTION("usb.listInterfaces", USB_LISTINTERFACES)
212 UsbListInterfacesFunction();
214 private:
215 ~UsbListInterfacesFunction() override;
217 // ExtensionFunction:
218 ResponseAction Run() override;
220 DISALLOW_COPY_AND_ASSIGN(UsbListInterfacesFunction);
223 class UsbCloseDeviceFunction : public UsbConnectionFunction {
224 public:
225 DECLARE_EXTENSION_FUNCTION("usb.closeDevice", USB_CLOSEDEVICE)
227 UsbCloseDeviceFunction();
229 private:
230 ~UsbCloseDeviceFunction() override;
232 // ExtensionFunction:
233 ResponseAction Run() override;
235 DISALLOW_COPY_AND_ASSIGN(UsbCloseDeviceFunction);
238 class UsbClaimInterfaceFunction : public UsbConnectionFunction {
239 public:
240 DECLARE_EXTENSION_FUNCTION("usb.claimInterface", USB_CLAIMINTERFACE)
242 UsbClaimInterfaceFunction();
244 protected:
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 {
256 public:
257 DECLARE_EXTENSION_FUNCTION("usb.releaseInterface", USB_RELEASEINTERFACE)
259 UsbReleaseInterfaceFunction();
261 private:
262 ~UsbReleaseInterfaceFunction() override;
264 // ExtensionFunction:
265 ResponseAction Run() override;
267 DISALLOW_COPY_AND_ASSIGN(UsbReleaseInterfaceFunction);
270 class UsbSetInterfaceAlternateSettingFunction : public UsbConnectionFunction {
271 public:
272 DECLARE_EXTENSION_FUNCTION("usb.setInterfaceAlternateSetting",
273 USB_SETINTERFACEALTERNATESETTING)
275 UsbSetInterfaceAlternateSettingFunction();
277 private:
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 {
289 public:
290 DECLARE_EXTENSION_FUNCTION("usb.controlTransfer", USB_CONTROLTRANSFER)
292 UsbControlTransferFunction();
294 private:
295 ~UsbControlTransferFunction() override;
297 // ExtensionFunction:
298 ResponseAction Run() override;
300 DISALLOW_COPY_AND_ASSIGN(UsbControlTransferFunction);
303 class UsbBulkTransferFunction : public UsbTransferFunction {
304 public:
305 DECLARE_EXTENSION_FUNCTION("usb.bulkTransfer", USB_BULKTRANSFER)
307 UsbBulkTransferFunction();
309 private:
310 ~UsbBulkTransferFunction() override;
312 // ExtensionFunction:
313 ResponseAction Run() override;
315 DISALLOW_COPY_AND_ASSIGN(UsbBulkTransferFunction);
318 class UsbInterruptTransferFunction : public UsbTransferFunction {
319 public:
320 DECLARE_EXTENSION_FUNCTION("usb.interruptTransfer", USB_INTERRUPTTRANSFER)
322 UsbInterruptTransferFunction();
324 private:
325 ~UsbInterruptTransferFunction() override;
327 // ExtensionFunction:
328 ResponseAction Run() override;
330 DISALLOW_COPY_AND_ASSIGN(UsbInterruptTransferFunction);
333 class UsbIsochronousTransferFunction : public UsbTransferFunction {
334 public:
335 DECLARE_EXTENSION_FUNCTION("usb.isochronousTransfer", USB_ISOCHRONOUSTRANSFER)
337 UsbIsochronousTransferFunction();
339 private:
340 ~UsbIsochronousTransferFunction() override;
342 // ExtensionFunction:
343 ResponseAction Run() override;
345 DISALLOW_COPY_AND_ASSIGN(UsbIsochronousTransferFunction);
348 class UsbResetDeviceFunction : public UsbConnectionFunction {
349 public:
350 DECLARE_EXTENSION_FUNCTION("usb.resetDevice", USB_RESETDEVICE)
352 UsbResetDeviceFunction();
354 private:
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_