android_device_manager.h revision 010d83a9304c5a91596085d917d248abff47903a
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
15namespace net {
16class StreamSocket;
17}
18
19class AndroidDeviceManager
20    : public base::RefCountedThreadSafe<AndroidDeviceManager>,
21      public base::NonThreadSafe {
22 public:
23  typedef base::Callback<void(int, const std::string&)> CommandCallback;
24  typedef base::Callback<void(int result, net::StreamSocket*)> SocketCallback;
25
26  class Device : public base::RefCounted<Device>,
27                 public base::NonThreadSafe {
28   protected:
29    friend class AndroidDeviceManager;
30
31    typedef AndroidDeviceManager::CommandCallback CommandCallback;
32    typedef AndroidDeviceManager::SocketCallback SocketCallback;
33
34    Device(const std::string& serial, bool is_connected);
35
36    virtual void RunCommand(const std::string& command,
37                            const CommandCallback& callback) = 0;
38    virtual void OpenSocket(const std::string& socket_name,
39                            const SocketCallback& callback) = 0;
40
41    std::string serial() { return serial_; }
42    bool is_connected() { return is_connected_; }
43
44    friend class base::RefCounted<Device>;
45    virtual ~Device();
46
47   private:
48    const std::string serial_;
49    const bool is_connected_;
50
51    DISALLOW_COPY_AND_ASSIGN(Device);
52  };
53
54  typedef std::vector<scoped_refptr<Device> > Devices;
55
56  class DeviceProvider
57      : public base::RefCountedThreadSafe<
58            DeviceProvider,
59            content::BrowserThread::DeleteOnUIThread> {
60   protected:
61    friend class AndroidDeviceManager;
62
63    typedef base::Callback<void(const Devices&)> QueryDevicesCallback;
64
65    virtual void QueryDevices(const QueryDevicesCallback& callback) = 0;
66
67   protected:
68    friend struct
69        content::BrowserThread::DeleteOnThread<content::BrowserThread::UI>;
70    friend class base::DeleteHelper<DeviceProvider>;
71
72    DeviceProvider();
73    virtual ~DeviceProvider();
74  };
75
76 public:
77  static scoped_refptr<AndroidDeviceManager> Create();
78
79  typedef std::vector<scoped_refptr<DeviceProvider> > DeviceProviders;
80  typedef base::Callback<void (const std::vector<std::string>&)>
81      QueryDevicesCallback;
82
83  void QueryDevices(const DeviceProviders& providers,
84                    const QueryDevicesCallback& callback);
85
86  void Stop();
87
88  bool IsConnected(const std::string& serial);
89
90  void RunCommand(const std::string& serial,
91                  const std::string& command,
92                  const CommandCallback& callback);
93
94  void OpenSocket(const std::string& serial,
95                  const std::string& socket_name,
96                  const SocketCallback& callback);
97
98  void HttpQuery(const std::string& serial,
99                 const std::string& socket_name,
100                 const std::string& request,
101                 const CommandCallback& callback);
102
103  void HttpUpgrade(const std::string& serial,
104                   const std::string& socket_name,
105                   const std::string& url,
106                   const SocketCallback& callback);
107
108 private:
109  AndroidDeviceManager();
110
111  friend class base::RefCountedThreadSafe<AndroidDeviceManager>;
112
113  virtual ~AndroidDeviceManager();
114
115  void QueryNextProvider(
116      const QueryDevicesCallback& callback,
117      const DeviceProviders& providers,
118      const Devices& total_devices,
119      const Devices& new_devices);
120
121  Device* FindDevice(const std::string& serial);
122
123  typedef std::map<std::string, scoped_refptr<Device> > DeviceMap;
124  DeviceMap devices_;
125
126  bool stopped_;
127};
128
129#endif  // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_
130