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"
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 Enumerate(crypto::RSAPrivateKey
* rsa_key
,
72 const AndroidUsbDevicesCallback
& callback
);
74 static void CountDevices(const base::Callback
<void(int)>& callback
);
76 AndroidUsbDevice(crypto::RSAPrivateKey
* rsa_key
,
77 scoped_refptr
<device::UsbDeviceHandle
> device
,
78 const std::string
& serial
,
84 void InitOnCallerThread();
86 net::StreamSocket
* CreateSocket(const std::string
& command
);
88 void Send(uint32 command
,
91 const std::string
& body
);
93 scoped_refptr
<device::UsbDeviceHandle
> usb_device() { return usb_handle_
; }
95 std::string
serial() { return serial_
; }
97 bool is_connected() { return is_connected_
; }
100 friend class base::RefCountedThreadSafe
<AndroidUsbDevice
>;
101 virtual ~AndroidUsbDevice();
103 void Queue(scoped_ptr
<AdbMessage
> message
);
104 void ProcessOutgoing();
105 void OutgoingMessageSent(device::UsbTransferStatus status
,
106 scoped_refptr
<net::IOBuffer
> buffer
,
110 void ParseHeader(device::UsbTransferStatus status
,
111 scoped_refptr
<net::IOBuffer
> buffer
,
114 void ReadBody(scoped_ptr
<AdbMessage
> message
,
117 void ParseBody(scoped_ptr
<AdbMessage
> message
,
120 device::UsbTransferStatus status
,
121 scoped_refptr
<net::IOBuffer
> buffer
,
124 void HandleIncoming(scoped_ptr
<AdbMessage
> message
);
126 void TransferError(device::UsbTransferStatus status
);
128 void TerminateIfReleased(scoped_refptr
<device::UsbDeviceHandle
> usb_handle
);
131 void SocketDeleted(uint32 socket_id
);
133 base::MessageLoop
* message_loop_
;
135 scoped_ptr
<crypto::RSAPrivateKey
> rsa_key_
;
138 scoped_refptr
<device::UsbDeviceHandle
> usb_handle_
;
140 int inbound_address_
;
141 int outbound_address_
;
146 bool signature_sent_
;
148 // Created sockets info
149 uint32 last_socket_id_
;
150 typedef std::map
<uint32
, AndroidUsbSocket
*> AndroidUsbSockets
;
151 AndroidUsbSockets sockets_
;
153 // Outgoing bulk queue
154 typedef scoped_refptr
<net::IOBufferWithSize
> BulkMessage
;
155 std::queue
<BulkMessage
> outgoing_queue_
;
157 // Outgoing messages pending connect
158 typedef ScopedVector
<AdbMessage
> PendingMessages
;
159 PendingMessages pending_messages_
;
161 base::WeakPtrFactory
<AndroidUsbDevice
> weak_factory_
;
163 DISALLOW_COPY_AND_ASSIGN(AndroidUsbDevice
);
166 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_USB_ANDROID_USB_DEVICE_H_