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_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "components/usb_service/usb_device_handle.h"
27 class AndroidUsbSocket
;
29 class AdbMessage
: public base::RefCounted
<AdbMessage
> {
32 kCommandSYNC
= 0x434e5953,
33 kCommandCNXN
= 0x4e584e43,
34 kCommandOPEN
= 0x4e45504f,
35 kCommandOKAY
= 0x59414b4f,
36 kCommandCLSE
= 0x45534c43,
37 kCommandWRTE
= 0x45545257,
38 kCommandAUTH
= 0x48545541
47 AdbMessage(uint32 command
,
50 const std::string
& body
);
57 friend class base::RefCounted
<AdbMessage
>;
60 DISALLOW_COPY_AND_ASSIGN(AdbMessage
);
63 class AndroidUsbDevice
;
64 typedef std::vector
<scoped_refptr
<AndroidUsbDevice
> > AndroidUsbDevices
;
65 typedef base::Callback
<void(const AndroidUsbDevices
&)>
66 AndroidUsbDevicesCallback
;
68 class AndroidUsbDevice
: public base::RefCountedThreadSafe
<AndroidUsbDevice
> {
70 static void Enumerate(crypto::RSAPrivateKey
* rsa_key
,
71 const AndroidUsbDevicesCallback
& callback
);
73 static void CountDevices(const base::Callback
<void(int)>& callback
);
75 AndroidUsbDevice(crypto::RSAPrivateKey
* rsa_key
,
76 scoped_refptr
<usb_service::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
<usb_service::UsbDeviceHandle
> usb_device() {
96 std::string
serial() { return serial_
; }
98 bool is_connected() { return is_connected_
; }
101 friend class base::RefCountedThreadSafe
<AndroidUsbDevice
>;
102 virtual ~AndroidUsbDevice();
104 void Queue(scoped_refptr
<AdbMessage
> message
);
105 void ProcessOutgoing();
106 void OutgoingMessageSent(usb_service::UsbTransferStatus status
,
107 scoped_refptr
<net::IOBuffer
> buffer
,
111 void ParseHeader(usb_service::UsbTransferStatus status
,
112 scoped_refptr
<net::IOBuffer
> buffer
,
115 void ReadBody(scoped_refptr
<AdbMessage
> message
,
118 void ParseBody(scoped_refptr
<AdbMessage
> message
,
121 usb_service::UsbTransferStatus status
,
122 scoped_refptr
<net::IOBuffer
> buffer
,
125 void HandleIncoming(scoped_refptr
<AdbMessage
> message
);
127 void TransferError(usb_service::UsbTransferStatus status
);
129 void TerminateIfReleased(
130 scoped_refptr
<usb_service::UsbDeviceHandle
> usb_handle
);
133 void SocketDeleted(uint32 socket_id
);
135 base::MessageLoop
* message_loop_
;
137 scoped_ptr
<crypto::RSAPrivateKey
> rsa_key_
;
140 scoped_refptr
<usb_service::UsbDeviceHandle
> usb_handle_
;
142 int inbound_address_
;
143 int outbound_address_
;
148 bool signature_sent_
;
150 // Created sockets info
151 uint32 last_socket_id_
;
152 typedef std::map
<uint32
, AndroidUsbSocket
*> AndroidUsbSockets
;
153 AndroidUsbSockets sockets_
;
155 // Outgoing bulk queue
156 typedef std::pair
<scoped_refptr
<net::IOBuffer
>, size_t> BulkMessage
;
157 std::queue
<BulkMessage
> outgoing_queue_
;
159 // Outgoing messages pending connect
160 typedef std::vector
<scoped_refptr
<AdbMessage
> > PendingMessages
;
161 PendingMessages pending_messages_
;
163 base::WeakPtrFactory
<AndroidUsbDevice
> weak_factory_
;
165 DISALLOW_COPY_AND_ASSIGN(AndroidUsbDevice
);
168 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_USB_ANDROID_USB_DEVICE_H_