Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / usb_service / usb_device_handle_impl.h
blob7d113377ee1ade3a358ea334dfe397537be8200b
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 COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_
6 #define COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_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 "components/usb_service/usb_device_handle.h"
16 #include "components/usb_service/usb_interface.h"
17 #include "net/base/io_buffer.h"
19 struct libusb_device_handle;
20 struct libusb_iso_packet_descriptor;
21 struct libusb_transfer;
23 namespace base {
24 class MessageLoopProxy;
27 namespace usb_service {
29 class UsbContext;
30 class UsbConfigDescriptor;
31 class UsbDeviceImpl;
33 typedef libusb_device_handle* PlatformUsbDeviceHandle;
34 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
35 typedef libusb_transfer* PlatformUsbTransferHandle;
37 // UsbDeviceHandle class provides basic I/O related functionalities.
38 class UsbDeviceHandleImpl : public UsbDeviceHandle {
39 public:
40 virtual scoped_refptr<UsbDevice> GetDevice() const OVERRIDE;
41 virtual void Close() OVERRIDE;
42 virtual bool ClaimInterface(const int interface_number) OVERRIDE;
43 virtual bool ReleaseInterface(const int interface_number) OVERRIDE;
44 virtual bool SetInterfaceAlternateSetting(
45 const int interface_number,
46 const int alternate_setting) OVERRIDE;
47 virtual bool ResetDevice() OVERRIDE;
48 virtual bool GetManufacturer(base::string16* manufacturer) OVERRIDE;
49 virtual bool GetProduct(base::string16* product) OVERRIDE;
50 virtual bool GetSerial(base::string16* serial) OVERRIDE;
51 virtual void ControlTransfer(const UsbEndpointDirection direction,
52 const TransferRequestType request_type,
53 const TransferRecipient recipient,
54 const uint8 request,
55 const uint16 value,
56 const uint16 index,
57 net::IOBuffer* buffer,
58 const size_t length,
59 const unsigned int timeout,
60 const UsbTransferCallback& callback) OVERRIDE;
62 virtual void BulkTransfer(const UsbEndpointDirection direction,
63 const uint8 endpoint,
64 net::IOBuffer* buffer,
65 const size_t length,
66 const unsigned int timeout,
67 const UsbTransferCallback& callback) OVERRIDE;
69 virtual void InterruptTransfer(const UsbEndpointDirection direction,
70 const uint8 endpoint,
71 net::IOBuffer* buffer,
72 const size_t length,
73 const unsigned int timeout,
74 const UsbTransferCallback& callback) OVERRIDE;
76 virtual void IsochronousTransfer(
77 const UsbEndpointDirection direction,
78 const uint8 endpoint,
79 net::IOBuffer* buffer,
80 const size_t length,
81 const unsigned int packets,
82 const unsigned int packet_length,
83 const unsigned int timeout,
84 const UsbTransferCallback& callback) OVERRIDE;
86 PlatformUsbDeviceHandle handle() const { return handle_; }
88 protected:
89 friend class UsbDeviceImpl;
91 // This constructor is called by UsbDevice.
92 UsbDeviceHandleImpl(scoped_refptr<UsbContext> context,
93 UsbDeviceImpl* device,
94 PlatformUsbDeviceHandle handle,
95 scoped_refptr<UsbConfigDescriptor> interfaces);
97 virtual ~UsbDeviceHandleImpl();
99 private:
100 friend void HandleTransferCompletion(PlatformUsbTransferHandle handle);
102 class InterfaceClaimer;
103 struct Transfer;
105 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
106 // SetInterfaceAlternateSetting.
107 void RefreshEndpointMap();
109 // Look up the claimed interface by endpoint. Return NULL if the interface
110 // of the endpoint is not found.
111 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
112 unsigned char endpoint);
114 // Submits a transfer and starts tracking it. Retains the buffer and copies
115 // the completion callback until the transfer finishes, whereupon it invokes
116 // the callback then releases the buffer.
117 void SubmitTransfer(PlatformUsbTransferHandle handle,
118 UsbTransferType transfer_type,
119 net::IOBuffer* buffer,
120 const size_t length,
121 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
122 const UsbTransferCallback& callback);
124 // Invokes the callbacks associated with a given transfer, and removes it from
125 // the in-flight transfer set.
126 void TransferComplete(PlatformUsbTransferHandle transfer);
128 bool GetSupportedLanguages();
129 bool GetStringDescriptor(uint8 string_id, base::string16* string);
131 // Informs the object to drop internal references.
132 void InternalClose();
134 UsbDeviceImpl* device_;
136 PlatformUsbDeviceHandle handle_;
138 scoped_refptr<UsbConfigDescriptor> interfaces_;
140 std::vector<uint16> languages_;
141 std::map<uint8, base::string16> strings_;
143 typedef std::map<int, scoped_refptr<InterfaceClaimer> > ClaimedInterfaceMap;
144 ClaimedInterfaceMap claimed_interfaces_;
146 typedef std::map<PlatformUsbTransferHandle, Transfer> TransferMap;
147 TransferMap transfers_;
149 // A map from endpoints to interfaces
150 typedef std::map<int, int> EndpointMap;
151 EndpointMap endpoint_map_;
153 // Retain the UsbContext so that the platform context will not be destroyed
154 // before this handle.
155 scoped_refptr<UsbContext> context_;
157 base::ThreadChecker thread_checker_;
159 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl);
162 } // namespace usb_service
164 #endif // COMPONENTS_USB_SERVICE_USB_DEVICE_HANDLE_IMPL_H_