Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / devtools / device / usb / android_usb_device.h
blobaf976a0c0b12247181d1c70c9fc32e5c414f4f98
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_
8 #include <map>
9 #include <queue>
10 #include <vector>
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"
17 namespace base {
18 class SingleThreadTaskRunner;
21 namespace crypto {
22 class RSAPrivateKey;
25 namespace net {
26 class StreamSocket;
29 class AndroidUsbSocket;
31 class AdbMessage {
32 public:
33 enum Command {
34 kCommandSYNC = 0x434e5953,
35 kCommandCNXN = 0x4e584e43,
36 kCommandOPEN = 0x4e45504f,
37 kCommandOKAY = 0x59414b4f,
38 kCommandCLSE = 0x45534c43,
39 kCommandWRTE = 0x45545257,
40 kCommandAUTH = 0x48545541
43 enum Auth {
44 kAuthToken = 1,
45 kAuthSignature = 2,
46 kAuthRSAPublicKey = 3
49 AdbMessage(uint32 command,
50 uint32 arg0,
51 uint32 arg1,
52 const std::string& body);
53 ~AdbMessage();
55 uint32 command;
56 uint32 arg0;
57 uint32 arg1;
58 std::string body;
60 private:
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> {
70 public:
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,
78 int inbound_address,
79 int outbound_address,
80 int zero_mask,
81 int interface_id);
83 void InitOnCallerThread();
85 net::StreamSocket* CreateSocket(const std::string& command);
87 void Send(uint32 command,
88 uint32 arg0,
89 uint32 arg1,
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_; }
98 private:
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,
106 size_t result);
108 void ReadHeader();
109 void ParseHeader(device::UsbTransferStatus status,
110 scoped_refptr<net::IOBuffer> buffer,
111 size_t result);
113 void ReadBody(scoped_ptr<AdbMessage> message,
114 uint32 data_length,
115 uint32 data_check);
116 void ParseBody(scoped_ptr<AdbMessage> message,
117 uint32 data_length,
118 uint32 data_check,
119 device::UsbTransferStatus status,
120 scoped_refptr<net::IOBuffer> buffer,
121 size_t result);
123 void HandleIncoming(scoped_ptr<AdbMessage> message);
125 void TransferError(device::UsbTransferStatus status);
127 void TerminateIfReleased(scoped_refptr<device::UsbDeviceHandle> usb_handle);
128 void Terminate();
130 void SocketDeleted(uint32 socket_id);
132 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
134 scoped_ptr<crypto::RSAPrivateKey> rsa_key_;
136 // Device info
137 scoped_refptr<device::UsbDeviceHandle> usb_handle_;
138 std::string serial_;
139 int inbound_address_;
140 int outbound_address_;
141 int zero_mask_;
142 int interface_id_;
144 bool is_connected_;
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_