android_device_manager.h revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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_ANDROID_DEVICE_MANAGER_H_
6#define CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_
7
8#include <vector>
9
10#include "base/memory/ref_counted.h"
11#include "base/threading/non_thread_safe.h"
12#include "chrome/browser/profiles/profile.h"
13#include "content/public/browser/browser_thread.h"
14#include "crypto/rsa_private_key.h"
15#include "net/socket/stream_socket.h"
16
17class AndroidDeviceManager
18    : public base::RefCountedThreadSafe<AndroidDeviceManager>,
19      public base::NonThreadSafe {
20 public:
21  typedef base::Callback<void(int, const std::string&)> CommandCallback;
22  typedef base::Callback<void(int result, net::StreamSocket*)> SocketCallback;
23
24  class Device : public base::RefCounted<Device>,
25                 public base::NonThreadSafe {
26   protected:
27    friend class AndroidDeviceManager;
28
29    typedef AndroidDeviceManager::CommandCallback CommandCallback;
30    typedef AndroidDeviceManager::SocketCallback SocketCallback;
31
32    Device(const std::string& serial, bool is_connected);
33
34    virtual void RunCommand(const std::string& command,
35                            const CommandCallback& callback) = 0;
36    virtual void OpenSocket(const std::string& socket_name,
37                            const SocketCallback& callback) = 0;
38    void HttpQuery(const std::string& la_name,
39                   const std::string& request,
40                   const CommandCallback& callback);
41    void HttpUpgrade(const std::string& la_name,
42                     const std::string& request,
43                     const SocketCallback& callback);
44
45    std::string serial() { return serial_; }
46    bool is_connected() { return is_connected_; }
47
48    friend class base::RefCounted<Device>;
49    virtual ~Device();
50
51   private:
52    void OnHttpSocketOpened(const std::string& request,
53                            const CommandCallback& callback,
54                            int result,
55                            net::StreamSocket* socket);
56    void OnHttpSocketOpened2(const std::string& request,
57                             const SocketCallback& callback,
58                             int result,
59                             net::StreamSocket* socket);
60
61    const std::string serial_;
62    const bool is_connected_;
63
64    DISALLOW_COPY_AND_ASSIGN(Device);
65  };
66
67  typedef std::vector<scoped_refptr<Device> > Devices;
68
69  class DeviceProvider
70      : public base::RefCountedThreadSafe<
71            DeviceProvider,
72            content::BrowserThread::DeleteOnUIThread> {
73   protected:
74    friend class AndroidDeviceManager;
75
76    typedef base::Callback<void(const Devices&)> QueryDevicesCallback;
77
78    virtual void QueryDevices(const QueryDevicesCallback& callback) = 0;
79
80   protected:
81    friend struct
82        content::BrowserThread::DeleteOnThread<content::BrowserThread::UI>;
83    friend class base::DeleteHelper<DeviceProvider>;
84
85    DeviceProvider();
86    virtual ~DeviceProvider();
87  };
88
89 public:
90  static scoped_refptr<DeviceProvider> GetAdbDeviceProvider();
91  static scoped_refptr<DeviceProvider> GetUsbDeviceProvider(Profile* profile);
92  // Use only in a test and/or when DEBUG_DEVTOOLS is defined.
93  static scoped_refptr<DeviceProvider> GetSelfAsDeviceProvider(int port);
94
95  static scoped_refptr<AndroidDeviceManager> Create();
96
97  typedef std::vector<scoped_refptr<DeviceProvider> > DeviceProviders;
98  typedef base::Callback<void (const std::vector<std::string>&)>
99      QueryDevicesCallback;
100
101  void QueryDevices(const DeviceProviders& providers,
102                    const QueryDevicesCallback& callback);
103
104  void Stop();
105
106  bool IsConnected(const std::string& serial);
107
108  void RunCommand(const std::string& serial,
109                  const std::string& command,
110                  const CommandCallback& callback);
111
112  void OpenSocket(const std::string& serial,
113                  const std::string& socket_name,
114                  const SocketCallback& callback);
115
116  void HttpQuery(const std::string& serial,
117                 const std::string& la_name,
118                 const std::string& request,
119                 const CommandCallback& callback);
120
121  void HttpUpgrade(const std::string& serial,
122                   const std::string& la_name,
123                   const std::string& request,
124                   const SocketCallback& callback);
125
126 private:
127  AndroidDeviceManager();
128
129  friend class base::RefCountedThreadSafe<AndroidDeviceManager>;
130
131  virtual ~AndroidDeviceManager();
132
133  void QueryNextProvider(
134      const QueryDevicesCallback& callback,
135      const DeviceProviders& providers,
136      const Devices& total_devices,
137      const Devices& new_devices);
138
139  Device* FindDevice(const std::string& serial);
140
141  typedef std::map<std::string, scoped_refptr<Device> > DeviceMap;
142  DeviceMap devices_;
143
144  bool stopped_;
145};
146
147#endif  // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_
148