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 16 namespace net { 17 class StreamSocket; 18 } 19 20 class AndroidDeviceManager 21 : public base::RefCountedThreadSafe< 22 AndroidDeviceManager, 23 content::BrowserThread::DeleteOnUIThread>, 24 public base::NonThreadSafe { 25 public: 26 typedef base::Callback<void(int, const std::string&)> CommandCallback; 27 typedef base::Callback<void(int result, net::StreamSocket*)> SocketCallback; 28 typedef base::Callback<void(const std::vector<std::string>&)> SerialsCallback; 29 30 struct BrowserInfo { 31 BrowserInfo(); 32 33 enum Type { 34 kTypeChrome, 35 kTypeWebView, 36 kTypeOther 37 }; 38 39 std::string socket_name; 40 std::string display_name; 41 Type type; 42 }; 43 44 struct DeviceInfo { 45 DeviceInfo(); 46 ~DeviceInfo(); 47 48 std::string model; 49 bool connected; 50 gfx::Size screen_size; 51 std::vector<BrowserInfo> browser_info; 52 }; 53 54 typedef base::Callback<void(const DeviceInfo&)> DeviceInfoCallback; 55 56 class AndroidWebSocket : public base::RefCountedThreadSafe<AndroidWebSocket> { 57 public: 58 class Delegate { 59 public: 60 virtual void OnSocketOpened() = 0; 61 virtual void OnFrameRead(const std::string& message) = 0; 62 virtual void OnSocketClosed(bool closed_by_device) = 0; 63 64 protected: 65 virtual ~Delegate() {} 66 }; 67 68 AndroidWebSocket() {} 69 70 virtual void Connect() = 0; 71 virtual void Disconnect() = 0; 72 virtual void SendFrame(const std::string& message) = 0; 73 virtual void ClearDelegate() = 0; 74 75 protected: 76 virtual ~AndroidWebSocket() {} 77 78 private: 79 friend class base::RefCountedThreadSafe<AndroidWebSocket>; 80 81 DISALLOW_COPY_AND_ASSIGN(AndroidWebSocket); 82 }; 83 84 class DeviceProvider; 85 86 class Device : public base::RefCountedThreadSafe<Device>, 87 public base::NonThreadSafe { 88 public: 89 typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback; 90 typedef AndroidDeviceManager::CommandCallback CommandCallback; 91 typedef AndroidDeviceManager::SocketCallback SocketCallback; 92 93 void QueryDeviceInfo(const DeviceInfoCallback& callback); 94 95 void OpenSocket(const std::string& socket_name, 96 const SocketCallback& callback); 97 98 void SendJsonRequest(const std::string& socket_name, 99 const std::string& request, 100 const CommandCallback& callback); 101 102 void HttpUpgrade(const std::string& socket_name, 103 const std::string& url, 104 const SocketCallback& callback); 105 106 scoped_refptr<AndroidWebSocket> CreateWebSocket( 107 const std::string& socket_name, 108 const std::string& url, 109 AndroidWebSocket::Delegate* delegate); 110 111 std::string serial() { return serial_; } 112 113 private: 114 friend class AndroidDeviceManager; 115 Device(scoped_refptr<base::MessageLoopProxy> device_message_loop, 116 scoped_refptr<DeviceProvider> provider, 117 const std::string& serial); 118 119 friend class base::RefCountedThreadSafe<Device>; 120 virtual ~Device(); 121 122 scoped_refptr<base::MessageLoopProxy> device_message_loop_; 123 scoped_refptr<DeviceProvider> provider_; 124 std::string serial_; 125 base::WeakPtrFactory<Device> weak_factory_; 126 127 DISALLOW_COPY_AND_ASSIGN(Device); 128 }; 129 130 typedef std::vector<scoped_refptr<Device> > Devices; 131 typedef base::Callback<void(const Devices&)> DevicesCallback; 132 133 class DeviceProvider : public base::RefCountedThreadSafe<DeviceProvider> { 134 public: 135 typedef AndroidDeviceManager::SerialsCallback SerialsCallback; 136 typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback; 137 typedef AndroidDeviceManager::SocketCallback SocketCallback; 138 typedef AndroidDeviceManager::CommandCallback CommandCallback; 139 140 virtual void QueryDevices(const SerialsCallback& callback) = 0; 141 142 virtual void QueryDeviceInfo(const std::string& serial, 143 const DeviceInfoCallback& callback) = 0; 144 145 virtual void OpenSocket(const std::string& serial, 146 const std::string& socket_name, 147 const SocketCallback& callback) = 0; 148 149 virtual void SendJsonRequest(const std::string& serial, 150 const std::string& socket_name, 151 const std::string& request, 152 const CommandCallback& callback); 153 154 virtual void HttpUpgrade(const std::string& serial, 155 const std::string& socket_name, 156 const std::string& url, 157 const SocketCallback& callback); 158 159 virtual void ReleaseDevice(const std::string& serial); 160 161 protected: 162 friend class base::RefCountedThreadSafe<DeviceProvider>; 163 DeviceProvider(); 164 virtual ~DeviceProvider(); 165 }; 166 167 typedef std::vector<scoped_refptr<DeviceProvider> > DeviceProviders; 168 169 static scoped_refptr<AndroidDeviceManager> Create(); 170 171 void SetDeviceProviders(const DeviceProviders& providers); 172 173 void QueryDevices(const DevicesCallback& callback); 174 175 struct DeviceDescriptor { 176 DeviceDescriptor(); 177 ~DeviceDescriptor(); 178 179 scoped_refptr<DeviceProvider> provider; 180 std::string serial; 181 }; 182 183 typedef std::vector<DeviceDescriptor> DeviceDescriptors; 184 185 private: 186 class HandlerThread : public base::RefCountedThreadSafe<HandlerThread> { 187 public: 188 static scoped_refptr<HandlerThread> GetInstance(); 189 scoped_refptr<base::MessageLoopProxy> message_loop(); 190 191 private: 192 friend class base::RefCountedThreadSafe<HandlerThread>; 193 static HandlerThread* instance_; 194 static void StopThread(base::Thread* thread); 195 196 HandlerThread(); 197 virtual ~HandlerThread(); 198 base::Thread* thread_; 199 }; 200 201 friend struct content::BrowserThread::DeleteOnThread< 202 content::BrowserThread::UI>; 203 friend class base::DeleteHelper<AndroidDeviceManager>; 204 AndroidDeviceManager(); 205 virtual ~AndroidDeviceManager(); 206 207 void UpdateDevices(const DevicesCallback& callback, 208 DeviceDescriptors* descriptors); 209 210 typedef std::map<std::string, base::WeakPtr<Device> > DeviceWeakMap; 211 212 scoped_refptr<HandlerThread> handler_thread_; 213 DeviceProviders providers_; 214 DeviceWeakMap devices_; 215 }; 216 217 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_ 218