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 CHROME_BROWSER_DEVTOOLS_DEVICE_USB_ANDROID_USB_DEVICE_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVICE_USB_ANDROID_USB_DEVICE_H_
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/weak_ptr.h"
15 #include "device/usb/usb_device_handle.h"
18 class SingleThreadTaskRunner
;
29 class AndroidUsbSocket
;
34 kCommandSYNC
= 0x434e5953,
35 kCommandCNXN
= 0x4e584e43,
36 kCommandOPEN
= 0x4e45504f,
37 kCommandOKAY
= 0x59414b4f,
38 kCommandCLSE
= 0x45534c43,
39 kCommandWRTE
= 0x45545257,
40 kCommandAUTH
= 0x48545541
49 AdbMessage(uint32 command
,
52 const std::string
& body
);
61 DISALLOW_COPY_AND_ASSIGN(AdbMessage
);
64 class AndroidUsbDevice
;
65 typedef std::vector
<scoped_refptr
<AndroidUsbDevice
> > AndroidUsbDevices
;
66 typedef base::Callback
<void(const AndroidUsbDevices
&)>
67 AndroidUsbDevicesCallback
;
69 class AndroidUsbDevice
: public base::RefCountedThreadSafe
<AndroidUsbDevice
> {
71 static void CountDevices(const base::Callback
<void(int)>& callback
);
72 static void Enumerate(crypto::RSAPrivateKey
* rsa_key
,
73 const AndroidUsbDevicesCallback
& callback
);
75 AndroidUsbDevice(crypto::RSAPrivateKey
* rsa_key
,
76 scoped_refptr
<device::UsbDeviceHandle
> device
,
77 const std::string
& serial
,
83 void InitOnCallerThread();
85 net::StreamSocket
* CreateSocket(const std::string
& command
);
87 void Send(uint32 command
,
90 const std::string
& body
);
92 scoped_refptr
<device::UsbDeviceHandle
> usb_device() { return usb_handle_
; }
94 std::string
serial() { return serial_
; }
96 bool is_connected() { return is_connected_
; }
99 friend class base::RefCountedThreadSafe
<AndroidUsbDevice
>;
100 virtual ~AndroidUsbDevice();
102 void Queue(scoped_ptr
<AdbMessage
> message
);
103 void ProcessOutgoing();
104 void OutgoingMessageSent(device::UsbTransferStatus status
,
105 scoped_refptr
<net::IOBuffer
> buffer
,
109 void ParseHeader(device::UsbTransferStatus status
,
110 scoped_refptr
<net::IOBuffer
> buffer
,
113 void ReadBody(scoped_ptr
<AdbMessage
> message
,
116 void ParseBody(scoped_ptr
<AdbMessage
> message
,
119 device::UsbTransferStatus status
,
120 scoped_refptr
<net::IOBuffer
> buffer
,
123 void HandleIncoming(scoped_ptr
<AdbMessage
> message
);
125 void TransferError(device::UsbTransferStatus status
);
127 void TerminateIfReleased(scoped_refptr
<device::UsbDeviceHandle
> usb_handle
);
130 void SocketDeleted(uint32 socket_id
);
132 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
134 scoped_ptr
<crypto::RSAPrivateKey
> rsa_key_
;
137 scoped_refptr
<device::UsbDeviceHandle
> usb_handle_
;
139 int inbound_address_
;
140 int outbound_address_
;
145 bool signature_sent_
;
147 // Created sockets info
148 uint32 last_socket_id_
;
149 typedef std::map
<uint32
, AndroidUsbSocket
*> AndroidUsbSockets
;
150 AndroidUsbSockets sockets_
;
152 // Outgoing bulk queue
153 typedef scoped_refptr
<net::IOBufferWithSize
> BulkMessage
;
154 std::queue
<BulkMessage
> outgoing_queue_
;
156 // Outgoing messages pending connect
157 typedef ScopedVector
<AdbMessage
> PendingMessages
;
158 PendingMessages pending_messages_
;
160 base::WeakPtrFactory
<AndroidUsbDevice
> weak_factory_
;
162 DISALLOW_COPY_AND_ASSIGN(AndroidUsbDevice
);
165 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_USB_ANDROID_USB_DEVICE_H_