Make castv2 performance test work.
[chromium-blink-merge.git] / chrome / browser / devtools / device / usb / android_usb_device.h
blob3400a977dcaabdc5b5edca1d311582c63b5bd55a
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 MessageLoop;
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 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,
79 int inbound_address,
80 int outbound_address,
81 int zero_mask,
82 int interface_id);
84 void InitOnCallerThread();
86 net::StreamSocket* CreateSocket(const std::string& command);
88 void Send(uint32 command,
89 uint32 arg0,
90 uint32 arg1,
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_; }
99 private:
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,
107 size_t result);
109 void ReadHeader();
110 void ParseHeader(device::UsbTransferStatus status,
111 scoped_refptr<net::IOBuffer> buffer,
112 size_t result);
114 void ReadBody(scoped_ptr<AdbMessage> message,
115 uint32 data_length,
116 uint32 data_check);
117 void ParseBody(scoped_ptr<AdbMessage> message,
118 uint32 data_length,
119 uint32 data_check,
120 device::UsbTransferStatus status,
121 scoped_refptr<net::IOBuffer> buffer,
122 size_t result);
124 void HandleIncoming(scoped_ptr<AdbMessage> message);
126 void TransferError(device::UsbTransferStatus status);
128 void TerminateIfReleased(scoped_refptr<device::UsbDeviceHandle> usb_handle);
129 void Terminate();
131 void SocketDeleted(uint32 socket_id);
133 base::MessageLoop* message_loop_;
135 scoped_ptr<crypto::RSAPrivateKey> rsa_key_;
137 // Device info
138 scoped_refptr<device::UsbDeviceHandle> usb_handle_;
139 std::string serial_;
140 int inbound_address_;
141 int outbound_address_;
142 int zero_mask_;
143 int interface_id_;
145 bool is_connected_;
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_