Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / devtools / adb / android_usb_device.h
blob3a9d9b76c5147a3d79f7549fc306cc9c2c793a60
1 // Copyright (c) 2013 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_ADB_ANDROID_USB_DEVICE_H_
6 #define CHROME_BROWSER_DEVTOOLS_ADB_ANDROID_USB_DEVICE_H_
8 #include <map>
9 #include <queue>
10 #include <vector>
11 #include "base/memory/ref_counted.h"
12 #include "chrome/browser/usb/usb_device_handle.h"
14 namespace base {
15 class MessageLoop;
18 namespace crypto {
19 class RSAPrivateKey;
22 namespace net {
23 class StreamSocket;
26 class AndroidUsbSocket;
28 class AdbMessage : public base::RefCounted<AdbMessage> {
29 public:
30 enum Command {
31 kCommandSYNC = 0x434e5953,
32 kCommandCNXN = 0x4e584e43,
33 kCommandOPEN = 0x4e45504f,
34 kCommandOKAY = 0x59414b4f,
35 kCommandCLSE = 0x45534c43,
36 kCommandWRTE = 0x45545257,
37 kCommandAUTH = 0x48545541
40 enum Auth {
41 kAuthToken = 1,
42 kAuthSignature = 2,
43 kAuthRSAPublicKey = 3
46 AdbMessage(uint32 command,
47 uint32 arg0,
48 uint32 arg1,
49 const std::string& body);
51 uint32 command;
52 uint32 arg0;
53 uint32 arg1;
54 std::string body;
55 private:
56 friend class base::RefCounted<AdbMessage>;
57 ~AdbMessage();
59 DISALLOW_COPY_AND_ASSIGN(AdbMessage);
62 class AndroidUsbDevice;
63 typedef std::vector<scoped_refptr<AndroidUsbDevice> > AndroidUsbDevices;
64 typedef base::Callback<void(const AndroidUsbDevices&)>
65 AndroidUsbDevicesCallback;
67 class AndroidUsbDevice : public base::RefCountedThreadSafe<AndroidUsbDevice> {
68 public:
69 static void Enumerate(crypto::RSAPrivateKey* rsa_key,
70 const AndroidUsbDevicesCallback& callback);
72 static void CountDevices(const base::Callback<void(int)>& callback);
74 AndroidUsbDevice(crypto::RSAPrivateKey* rsa_key,
75 scoped_refptr<UsbDeviceHandle> device,
76 const std::string& serial,
77 int inbound_address,
78 int outbound_address,
79 int zero_mask);
81 void InitOnCallerThread();
83 net::StreamSocket* CreateSocket(const std::string& command);
85 void Send(uint32 command,
86 uint32 arg0,
87 uint32 arg1,
88 const std::string& body);
90 scoped_refptr<UsbDeviceHandle> usb_device() { return usb_device_; }
92 std::string serial() { return serial_; }
94 bool terminated() { return terminated_; }
96 bool is_connected() { return is_connected_; }
98 private:
99 friend class base::RefCountedThreadSafe<AndroidUsbDevice>;
100 virtual ~AndroidUsbDevice();
102 void Queue(scoped_refptr<AdbMessage> message);
103 void ProcessOutgoing();
104 void OutgoingMessageSent(UsbTransferStatus status,
105 scoped_refptr<net::IOBuffer> buffer,
106 size_t result);
108 void ReadHeader(bool initial);
109 void ParseHeader(UsbTransferStatus status,
110 scoped_refptr<net::IOBuffer> buffer,
111 size_t result);
113 void ReadBody(scoped_refptr<AdbMessage> message,
114 uint32 data_length,
115 uint32 data_check);
116 void ParseBody(scoped_refptr<AdbMessage> message,
117 uint32 data_length,
118 uint32 data_check,
119 UsbTransferStatus status,
120 scoped_refptr<net::IOBuffer> buffer,
121 size_t result);
123 void HandleIncoming(scoped_refptr<AdbMessage> message);
125 void TransferError(UsbTransferStatus status);
127 void Terminate();
129 void SocketDeleted(uint32 socket_id);
131 base::MessageLoop* message_loop_;
133 scoped_ptr<crypto::RSAPrivateKey> rsa_key_;
135 // Device info
136 scoped_refptr<UsbDeviceHandle> usb_device_;
137 std::string serial_;
138 int inbound_address_;
139 int outbound_address_;
140 int zero_mask_;
142 bool is_connected_;
143 bool signature_sent_;
145 // Created sockets info
146 uint32 last_socket_id_;
147 bool terminated_;
148 typedef std::map<uint32, AndroidUsbSocket*> AndroidUsbSockets;
149 AndroidUsbSockets sockets_;
151 // Outgoing bulk queue
152 typedef std::pair<scoped_refptr<net::IOBuffer>, size_t> BulkMessage;
153 std::queue<BulkMessage> outgoing_queue_;
155 // Outgoing messages pending connect
156 typedef std::vector<scoped_refptr<AdbMessage> > PendingMessages;
157 PendingMessages pending_messages_;
159 DISALLOW_COPY_AND_ASSIGN(AndroidUsbDevice);
162 #endif // CHROME_BROWSER_DEVTOOLS_ADB_ANDROID_USB_DEVICE_H_