android_device_manager.h revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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 virtual 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 // Implemented in browser_tests. 95 static scoped_refptr<DeviceProvider> GetMockDeviceProviderForTest(); 96 97 static scoped_refptr<AndroidDeviceManager> Create(); 98 99 typedef std::vector<scoped_refptr<DeviceProvider> > DeviceProviders; 100 typedef base::Callback<void (const std::vector<std::string>&)> 101 QueryDevicesCallback; 102 103 void QueryDevices(const DeviceProviders& providers, 104 const QueryDevicesCallback& callback); 105 106 void Stop(); 107 108 bool IsConnected(const std::string& serial); 109 110 void RunCommand(const std::string& serial, 111 const std::string& command, 112 const CommandCallback& callback); 113 114 void OpenSocket(const std::string& serial, 115 const std::string& socket_name, 116 const SocketCallback& callback); 117 118 void HttpQuery(const std::string& serial, 119 const std::string& la_name, 120 const std::string& request, 121 const CommandCallback& callback); 122 123 void HttpUpgrade(const std::string& serial, 124 const std::string& la_name, 125 const std::string& request, 126 const SocketCallback& callback); 127 128 private: 129 AndroidDeviceManager(); 130 131 friend class base::RefCountedThreadSafe<AndroidDeviceManager>; 132 133 virtual ~AndroidDeviceManager(); 134 135 void QueryNextProvider( 136 const QueryDevicesCallback& callback, 137 const DeviceProviders& providers, 138 const Devices& total_devices, 139 const Devices& new_devices); 140 141 Device* FindDevice(const std::string& serial); 142 143 typedef std::map<std::string, scoped_refptr<Device> > DeviceMap; 144 DeviceMap devices_; 145 146 bool stopped_; 147}; 148 149#endif // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_ 150