android_device_manager.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 "ui/gfx/size.h"
15
16namespace net {
17class StreamSocket;
18}
19
20class AndroidDeviceManager
21    : public base::RefCountedThreadSafe<AndroidDeviceManager>,
22      public base::NonThreadSafe {
23 public:
24  typedef base::Callback<void(int, const std::string&)> CommandCallback;
25  typedef base::Callback<void(int result, net::StreamSocket*)> SocketCallback;
26
27  struct BrowserInfo {
28    BrowserInfo();
29
30    enum Type {
31      kTypeChrome,
32      kTypeWebView,
33      kTypeOther
34    };
35
36    std::string socket_name;
37    std::string display_name;
38    Type type;
39  };
40
41  struct DeviceInfo {
42    DeviceInfo();
43    ~DeviceInfo();
44
45    std::string model;
46    gfx::Size screen_size;
47    std::vector<BrowserInfo> browser_info;
48  };
49
50  typedef base::Callback<void(const DeviceInfo&)> DeviceInfoCallback;
51
52  class Device : public base::RefCounted<Device>,
53                 public base::NonThreadSafe {
54   protected:
55    friend class AndroidDeviceManager;
56
57    typedef AndroidDeviceManager::CommandCallback CommandCallback;
58    typedef AndroidDeviceManager::SocketCallback SocketCallback;
59    typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback;
60
61    Device(const std::string& serial, bool is_connected);
62
63    virtual void QueryDeviceInfo(const DeviceInfoCallback& callback) = 0;
64
65    virtual void OpenSocket(const std::string& socket_name,
66                            const SocketCallback& callback) = 0;
67
68    virtual void HttpQuery(const std::string& socket_name,
69                           const std::string& request,
70                           const CommandCallback& callback);
71
72    std::string serial() { return serial_; }
73    bool is_connected() { return is_connected_; }
74
75    friend class base::RefCounted<Device>;
76    virtual ~Device();
77
78   private:
79    const std::string serial_;
80    const bool is_connected_;
81
82    DISALLOW_COPY_AND_ASSIGN(Device);
83  };
84
85  typedef std::vector<scoped_refptr<Device> > Devices;
86
87  class DeviceProvider
88      : public base::RefCountedThreadSafe<
89            DeviceProvider,
90            content::BrowserThread::DeleteOnUIThread> {
91   protected:
92    friend class AndroidDeviceManager;
93
94    typedef base::Callback<void(const Devices&)> QueryDevicesCallback;
95
96    virtual void QueryDevices(const QueryDevicesCallback& callback) = 0;
97
98   protected:
99    friend struct
100        content::BrowserThread::DeleteOnThread<content::BrowserThread::UI>;
101    friend class base::DeleteHelper<DeviceProvider>;
102
103    DeviceProvider();
104    virtual ~DeviceProvider();
105  };
106
107 public:
108  static scoped_refptr<AndroidDeviceManager> Create();
109
110  typedef std::vector<scoped_refptr<DeviceProvider> > DeviceProviders;
111  typedef base::Callback<void (const std::vector<std::string>&)>
112      QueryDevicesCallback;
113
114  void QueryDevices(const DeviceProviders& providers,
115                    const QueryDevicesCallback& callback);
116
117  void Stop();
118
119  bool IsConnected(const std::string& serial);
120
121  void QueryDeviceInfo(const std::string& serial,
122                       const DeviceInfoCallback& callback);
123
124  void OpenSocket(const std::string& serial,
125                  const std::string& socket_name,
126                  const SocketCallback& callback);
127
128  void HttpQuery(const std::string& serial,
129                 const std::string& socket_name,
130                 const std::string& request,
131                 const CommandCallback& callback);
132
133  void HttpUpgrade(const std::string& serial,
134                   const std::string& socket_name,
135                   const std::string& url,
136                   const SocketCallback& callback);
137
138 private:
139  AndroidDeviceManager();
140
141  friend class base::RefCountedThreadSafe<AndroidDeviceManager>;
142
143  virtual ~AndroidDeviceManager();
144
145  void QueryNextProvider(
146      const QueryDevicesCallback& callback,
147      const DeviceProviders& providers,
148      const Devices& total_devices,
149      const Devices& new_devices);
150
151  Device* FindDevice(const std::string& serial);
152
153  typedef std::map<std::string, scoped_refptr<Device> > DeviceMap;
154  DeviceMap devices_;
155
156  bool stopped_;
157};
158
159#endif  // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_
160