1 // Copyright (c) 2013 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_DEVTOOLS_ADB_BRIDGE_H_ 6 #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/callback.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "chrome/browser/devtools/android_device.h" 15 #include "chrome/browser/devtools/refcounted_adb_thread.h" 16 #include "components/browser_context_keyed_service/browser_context_keyed_service.h" 17 #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h" 18 #include "content/public/browser/browser_thread.h" 19 #include "net/socket/tcp_client_socket.h" 20 #include "ui/gfx/size.h" 21 22 template<typename T> struct DefaultSingletonTraits; 23 24 namespace base { 25 class MessageLoop; 26 class DictionaryValue; 27 class ListValue; 28 class Thread; 29 } 30 31 namespace content { 32 class BrowserContext; 33 } 34 35 namespace crypto { 36 class RSAPrivateKey; 37 } 38 39 class DevToolsTargetImpl; 40 class Profile; 41 42 // The format used for constructing DevTools server socket names. 43 extern const char kDevToolsChannelNameFormat[]; 44 45 class DevToolsAdbBridge 46 : public base::RefCountedThreadSafe< 47 DevToolsAdbBridge, 48 content::BrowserThread::DeleteOnUIThread> { 49 public: 50 typedef base::Callback<void(int result, 51 const std::string& response)> Callback; 52 typedef std::vector<scoped_refptr<AndroidDeviceProvider> > DeviceProviders; 53 54 55 class Wrapper : public BrowserContextKeyedService { 56 public: 57 Wrapper(); 58 virtual ~Wrapper(); 59 60 DevToolsAdbBridge* Get(); 61 private: 62 scoped_refptr<DevToolsAdbBridge> bridge_; 63 }; 64 65 class Factory : public BrowserContextKeyedServiceFactory { 66 public: 67 // Returns singleton instance of DevToolsAdbBridge. 68 static Factory* GetInstance(); 69 70 // Returns DevToolsAdbBridge associated with |profile|. 71 static DevToolsAdbBridge* GetForProfile(Profile* profile); 72 73 private: 74 friend struct DefaultSingletonTraits<Factory>; 75 76 Factory(); 77 virtual ~Factory(); 78 79 // BrowserContextKeyedServiceFactory overrides: 80 virtual BrowserContextKeyedService* BuildServiceInstanceFor( 81 content::BrowserContext* context) const OVERRIDE; 82 DISALLOW_COPY_AND_ASSIGN(Factory); 83 }; 84 85 class RemoteBrowser : public base::RefCounted<RemoteBrowser> { 86 public: 87 RemoteBrowser( 88 scoped_refptr<RefCountedAdbThread> adb_thread, 89 scoped_refptr<AndroidDevice> device, 90 const std::string& socket); 91 92 scoped_refptr<RefCountedAdbThread> adb_thread() { return adb_thread_; } 93 94 scoped_refptr<AndroidDevice> device() { return device_; } 95 std::string socket() { return socket_; } 96 97 std::string display_name() { return display_name_; } 98 void set_display_name(const std::string& name) { display_name_ = name; } 99 100 std::string version() { return version_; } 101 void set_version(const std::string& version) { version_ = version; } 102 103 bool IsChrome() const; 104 105 typedef std::vector<int> ParsedVersion; 106 ParsedVersion GetParsedVersion() const; 107 108 std::vector<DevToolsTargetImpl*> CreatePageTargets(); 109 void SetPageDescriptors(const base::ListValue&); 110 111 void SendJsonRequest(const std::string& request, base::Closure callback); 112 void SendProtocolCommand(const std::string& debug_url, 113 const std::string& method, 114 base::DictionaryValue* params); 115 116 void Open(const std::string& url); 117 118 private: 119 friend class base::RefCounted<RemoteBrowser>; 120 virtual ~RemoteBrowser(); 121 122 void PageCreatedOnHandlerThread( 123 const std::string& url, int result, const std::string& response); 124 125 void PageCreatedOnUIThread( 126 const std::string& response, const std::string& url); 127 128 scoped_refptr<RefCountedAdbThread> adb_thread_; 129 scoped_refptr<AndroidDevice> device_; 130 const std::string socket_; 131 std::string display_name_; 132 std::string version_; 133 scoped_ptr<base::ListValue> page_descriptors_; 134 135 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser); 136 }; 137 138 typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers; 139 140 class RemoteDevice : public base::RefCounted<RemoteDevice> { 141 public: 142 explicit RemoteDevice(scoped_refptr<AndroidDevice> device); 143 144 std::string GetSerial(); 145 std::string GetModel(); 146 bool IsConnected(); 147 void AddBrowser(scoped_refptr<RemoteBrowser> browser); 148 149 scoped_refptr<AndroidDevice> device() { return device_; } 150 RemoteBrowsers& browsers() { return browsers_; } 151 gfx::Size screen_size() { return screen_size_; } 152 void set_screen_size(const gfx::Size& size) { screen_size_ = size; } 153 154 private: 155 friend class base::RefCounted<RemoteDevice>; 156 virtual ~RemoteDevice(); 157 158 scoped_refptr<AndroidDevice> device_; 159 RemoteBrowsers browsers_; 160 gfx::Size screen_size_; 161 162 DISALLOW_COPY_AND_ASSIGN(RemoteDevice); 163 }; 164 165 typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices; 166 167 typedef std::vector<scoped_refptr<AndroidDevice> > AndroidDevices; 168 typedef base::Callback<void(const AndroidDevices&)> AndroidDevicesCallback; 169 170 class Listener { 171 public: 172 virtual void RemoteDevicesChanged(RemoteDevices* devices) = 0; 173 protected: 174 virtual ~Listener() {} 175 }; 176 177 DevToolsAdbBridge(); 178 void AddListener(Listener* listener); 179 void RemoveListener(Listener* listener); 180 181 void set_device_providers(DeviceProviders device_providers) { 182 device_providers_ = device_providers; 183 } 184 static bool HasDevToolsWindow(const std::string& agent_id); 185 186 private: 187 friend struct content::BrowserThread::DeleteOnThread< 188 content::BrowserThread::UI>; 189 friend class base::DeleteHelper<DevToolsAdbBridge>; 190 191 virtual ~DevToolsAdbBridge(); 192 193 void RequestRemoteDevices(); 194 void ReceivedRemoteDevices(RemoteDevices* devices); 195 196 scoped_refptr<RefCountedAdbThread> adb_thread_; 197 bool has_message_loop_; 198 typedef std::vector<Listener*> Listeners; 199 Listeners listeners_; 200 DeviceProviders device_providers_; 201 DISALLOW_COPY_AND_ASSIGN(DevToolsAdbBridge); 202 }; 203 204 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_ADB_BRIDGE_H_ 205