Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / device / usb / usb_device_handle.h
blob898284bf66ee5239290bdf708b33bff6276626b7
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 DEVICE_USB_USB_DEVICE_HANDLE_H_
6 #define DEVICE_USB_USB_DEVICE_HANDLE_H_
8 #include <map>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "base/threading/thread_checker.h"
15 #include "device/usb/usb_descriptors.h"
16 #include "net/base/io_buffer.h"
18 namespace device {
20 class UsbDevice;
22 enum UsbTransferStatus {
23 USB_TRANSFER_COMPLETED = 0,
24 USB_TRANSFER_ERROR,
25 USB_TRANSFER_TIMEOUT,
26 USB_TRANSFER_CANCELLED,
27 USB_TRANSFER_STALLED,
28 USB_TRANSFER_DISCONNECT,
29 USB_TRANSFER_OVERFLOW,
30 USB_TRANSFER_LENGTH_SHORT,
33 // UsbDeviceHandle class provides basic I/O related functionalities.
34 class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> {
35 public:
36 using ResultCallback = base::Callback<void(bool)>;
37 using TransferCallback = base::Callback<
38 void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, size_t)>;
40 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED };
41 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER };
43 virtual scoped_refptr<UsbDevice> GetDevice() const = 0;
45 // Notifies UsbDevice to drop the reference of this object; cancels all the
46 // flying transfers.
47 // It is possible that the object has no other reference after this call. So
48 // if it is called using a raw pointer, it could be invalidated.
49 // The platform device handle will be closed when UsbDeviceHandle destructs.
50 virtual void Close() = 0;
52 // Device manipulation operations.
53 virtual void SetConfiguration(int configuration_value,
54 const ResultCallback& callback) = 0;
55 virtual void ClaimInterface(int interface_number,
56 const ResultCallback& callback) = 0;
57 virtual bool ReleaseInterface(int interface_number) = 0;
58 virtual void SetInterfaceAlternateSetting(int interface_number,
59 int alternate_setting,
60 const ResultCallback& callback) = 0;
61 virtual void ResetDevice(const ResultCallback& callback) = 0;
62 virtual void ClearHalt(uint8 endpoint, const ResultCallback& callback) = 0;
64 // The transfer functions may be called from any thread. The provided callback
65 // will be run on the caller's thread.
66 virtual void ControlTransfer(UsbEndpointDirection direction,
67 TransferRequestType request_type,
68 TransferRecipient recipient,
69 uint8 request,
70 uint16 value,
71 uint16 index,
72 scoped_refptr<net::IOBuffer> buffer,
73 size_t length,
74 unsigned int timeout,
75 const TransferCallback& callback) = 0;
77 virtual void IsochronousTransfer(UsbEndpointDirection direction,
78 uint8 endpoint,
79 scoped_refptr<net::IOBuffer> buffer,
80 size_t length,
81 unsigned int packets,
82 unsigned int packet_length,
83 unsigned int timeout,
84 const TransferCallback& callback) = 0;
86 virtual void GenericTransfer(UsbEndpointDirection direction,
87 uint8 endpoint,
88 scoped_refptr<net::IOBuffer> buffer,
89 size_t length,
90 unsigned int timeout,
91 const TransferCallback& callback) = 0;
93 protected:
94 friend class base::RefCountedThreadSafe<UsbDeviceHandle>;
96 UsbDeviceHandle();
98 virtual ~UsbDeviceHandle();
100 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle);
103 } // namespace device
105 #endif // DEVICE_USB_USB_DEVICE_HANDLE_H_