• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_BROWSER_DEVTOOLS_DEVICE_USB_ANDROID_USB_DEVICE_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVICE_USB_ANDROID_USB_DEVICE_H_
7 
8 #include <map>
9 #include <queue>
10 #include <vector>
11 
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "device/usb/usb_device_handle.h"
15 
16 namespace base {
17 class MessageLoop;
18 }
19 
20 namespace crypto {
21 class RSAPrivateKey;
22 }
23 
24 namespace net {
25 class StreamSocket;
26 }
27 
28 class AndroidUsbSocket;
29 
30 class AdbMessage : public base::RefCounted<AdbMessage> {
31  public:
32   enum Command {
33     kCommandSYNC = 0x434e5953,
34     kCommandCNXN = 0x4e584e43,
35     kCommandOPEN = 0x4e45504f,
36     kCommandOKAY = 0x59414b4f,
37     kCommandCLSE = 0x45534c43,
38     kCommandWRTE = 0x45545257,
39     kCommandAUTH = 0x48545541
40   };
41 
42   enum Auth {
43     kAuthToken = 1,
44     kAuthSignature = 2,
45     kAuthRSAPublicKey = 3
46   };
47 
48   AdbMessage(uint32 command,
49              uint32 arg0,
50              uint32 arg1,
51              const std::string& body);
52 
53   uint32 command;
54   uint32 arg0;
55   uint32 arg1;
56   std::string body;
57  private:
58   friend class base::RefCounted<AdbMessage>;
59   ~AdbMessage();
60 
61   DISALLOW_COPY_AND_ASSIGN(AdbMessage);
62 };
63 
64 class AndroidUsbDevice;
65 typedef std::vector<scoped_refptr<AndroidUsbDevice> > AndroidUsbDevices;
66 typedef base::Callback<void(const AndroidUsbDevices&)>
67     AndroidUsbDevicesCallback;
68 
69 class AndroidUsbDevice : public base::RefCountedThreadSafe<AndroidUsbDevice> {
70  public:
71   static void Enumerate(crypto::RSAPrivateKey* rsa_key,
72                         const AndroidUsbDevicesCallback& callback);
73 
74   static void CountDevices(const base::Callback<void(int)>& callback);
75 
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);
83 
84   void InitOnCallerThread();
85 
86   net::StreamSocket* CreateSocket(const std::string& command);
87 
88   void Send(uint32 command,
89             uint32 arg0,
90             uint32 arg1,
91             const std::string& body);
92 
usb_device()93   scoped_refptr<device::UsbDeviceHandle> usb_device() { return usb_handle_; }
94 
serial()95   std::string serial() { return serial_; }
96 
is_connected()97   bool is_connected() { return is_connected_; }
98 
99  private:
100   friend class base::RefCountedThreadSafe<AndroidUsbDevice>;
101   virtual ~AndroidUsbDevice();
102 
103   void Queue(scoped_refptr<AdbMessage> message);
104   void ProcessOutgoing();
105   void OutgoingMessageSent(device::UsbTransferStatus status,
106                            scoped_refptr<net::IOBuffer> buffer,
107                            size_t result);
108 
109   void ReadHeader();
110   void ParseHeader(device::UsbTransferStatus status,
111                    scoped_refptr<net::IOBuffer> buffer,
112                    size_t result);
113 
114   void ReadBody(scoped_refptr<AdbMessage> message,
115                 uint32 data_length,
116                 uint32 data_check);
117   void ParseBody(scoped_refptr<AdbMessage> message,
118                  uint32 data_length,
119                  uint32 data_check,
120                  device::UsbTransferStatus status,
121                  scoped_refptr<net::IOBuffer> buffer,
122                  size_t result);
123 
124   void HandleIncoming(scoped_refptr<AdbMessage> message);
125 
126   void TransferError(device::UsbTransferStatus status);
127 
128   void TerminateIfReleased(scoped_refptr<device::UsbDeviceHandle> usb_handle);
129   void Terminate();
130 
131   void SocketDeleted(uint32 socket_id);
132 
133   base::MessageLoop* message_loop_;
134 
135   scoped_ptr<crypto::RSAPrivateKey> rsa_key_;
136 
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_;
144 
145   bool is_connected_;
146   bool signature_sent_;
147 
148   // Created sockets info
149   uint32 last_socket_id_;
150   typedef std::map<uint32, AndroidUsbSocket*> AndroidUsbSockets;
151   AndroidUsbSockets sockets_;
152 
153   // Outgoing bulk queue
154   typedef scoped_refptr<net::IOBufferWithSize> BulkMessage;
155   std::queue<BulkMessage> outgoing_queue_;
156 
157   // Outgoing messages pending connect
158   typedef std::vector<scoped_refptr<AdbMessage> > PendingMessages;
159   PendingMessages pending_messages_;
160 
161   base::WeakPtrFactory<AndroidUsbDevice> weak_factory_;
162 
163   DISALLOW_COPY_AND_ASSIGN(AndroidUsbDevice);
164 };
165 
166 #endif  // CHROME_BROWSER_DEVTOOLS_DEVICE_USB_ANDROID_USB_DEVICE_H_
167