OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/devtools/device/devtools_android_bridge.h" | 5 #include "chrome/browser/devtools/device/devtools_android_bridge.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "chrome/browser/devtools/devtools_target_impl.h" | 30 #include "chrome/browser/devtools/devtools_target_impl.h" |
31 #include "chrome/browser/devtools/devtools_window.h" | 31 #include "chrome/browser/devtools/devtools_window.h" |
32 #include "chrome/browser/profiles/profile.h" | 32 #include "chrome/browser/profiles/profile.h" |
33 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
34 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 34 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
35 #include "content/public/browser/devtools_agent_host.h" | 35 #include "content/public/browser/devtools_agent_host.h" |
36 #include "content/public/browser/devtools_external_agent_proxy.h" | 36 #include "content/public/browser/devtools_external_agent_proxy.h" |
37 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" | 37 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" |
38 #include "content/public/browser/user_metrics.h" | 38 #include "content/public/browser/user_metrics.h" |
39 #include "net/base/escape.h" | 39 #include "net/base/escape.h" |
| 40 #include "net/base/net_errors.h" |
40 | 41 |
41 using content::BrowserThread; | 42 using content::BrowserThread; |
42 | 43 |
43 namespace { | 44 namespace { |
44 | 45 |
45 const char kPageListRequest[] = "/json"; | 46 const char kPageListRequest[] = "/json"; |
46 const char kVersionRequest[] = "/json/version"; | 47 const char kVersionRequest[] = "/json/version"; |
47 const char kClosePageRequest[] = "/json/close/%s"; | 48 const char kClosePageRequest[] = "/json/close/%s"; |
48 const char kNewPageRequest[] = "/json/new"; | 49 const char kNewPageRequest[] = "/json/new"; |
49 const char kNewPageRequestWithURL[] = "/json/new?%s"; | 50 const char kNewPageRequestWithURL[] = "/json/new?%s"; |
50 const char kActivatePageRequest[] = "/json/activate/%s"; | 51 const char kActivatePageRequest[] = "/json/activate/%s"; |
51 const char kBrowserTargetSocket[] = "/devtools/browser"; | 52 const char kBrowserTargetSocket[] = "/devtools/browser"; |
52 const int kAdbPollingIntervalMs = 1000; | 53 const int kAdbPollingIntervalMs = 1000; |
53 | 54 |
54 const char kUrlParam[] = "url"; | 55 const char kUrlParam[] = "url"; |
55 const char kPageReloadCommand[] = "Page.reload"; | 56 const char kPageReloadCommand[] = "Page.reload"; |
56 const char kPageNavigateCommand[] = "Page.navigate"; | 57 const char kPageNavigateCommand[] = "Page.navigate"; |
57 | 58 |
58 const int kMinVersionNewWithURL = 32; | 59 const int kMinVersionNewWithURL = 32; |
59 const int kNewPageNavigateDelayMs = 500; | 60 const int kNewPageNavigateDelayMs = 500; |
60 | 61 |
| 62 } // namespace |
| 63 |
61 // DiscoveryRequest ----------------------------------------------------- | 64 // DiscoveryRequest ----------------------------------------------------- |
62 | 65 |
63 class DiscoveryRequest : public base::RefCountedThreadSafe< | 66 class DevToolsAndroidBridge::DiscoveryRequest |
64 DiscoveryRequest, | 67 : public base::RefCountedThreadSafe<DiscoveryRequest, |
65 BrowserThread::DeleteOnUIThread> { | 68 BrowserThread::DeleteOnUIThread> { |
66 public: | 69 public: |
67 typedef AndroidDeviceManager::Device Device; | 70 typedef base::Callback<void(const AndroidDeviceManager::Devices&, |
68 typedef AndroidDeviceManager::Devices Devices; | 71 const RemoteDevices&)> DiscoveryCallback; |
69 typedef AndroidDeviceManager::DeviceInfo DeviceInfo; | |
70 typedef DevToolsAndroidBridge::RemoteDevice RemoteDevice; | |
71 typedef DevToolsAndroidBridge::RemoteDevices RemoteDevices; | |
72 typedef DevToolsAndroidBridge::RemoteBrowser RemoteBrowser; | |
73 typedef DevToolsAndroidBridge::RemoteBrowsers RemoteBrowsers; | |
74 typedef base::Callback<void(const RemoteDevices&)> DiscoveryCallback; | |
75 | 72 |
76 DiscoveryRequest(AndroidDeviceManager* device_manager, | 73 DiscoveryRequest(AndroidDeviceManager* device_manager, |
77 const DiscoveryCallback& callback); | 74 const DiscoveryCallback& callback); |
78 private: | 75 private: |
79 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 76 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
80 friend class base::DeleteHelper<DiscoveryRequest>; | 77 friend class base::DeleteHelper<DiscoveryRequest>; |
81 virtual ~DiscoveryRequest(); | 78 virtual ~DiscoveryRequest(); |
82 | 79 |
83 void ReceivedDevices(const Devices& devices); | 80 void ReceivedDevices(const AndroidDeviceManager::Devices& devices); |
84 void ReceivedDeviceInfo(scoped_refptr<Device> device, | 81 void ReceivedDeviceInfo(scoped_refptr<AndroidDeviceManager::Device> device, |
85 const DeviceInfo& device_info); | 82 const AndroidDeviceManager::DeviceInfo& device_info); |
86 void ReceivedVersion(scoped_refptr<RemoteBrowser>, | 83 void ReceivedVersion(scoped_refptr<RemoteBrowser>, |
87 int result, | 84 int result, |
88 const std::string& response); | 85 const std::string& response); |
89 void ReceivedPages(scoped_refptr<RemoteBrowser>, | 86 void ReceivedPages(scoped_refptr<RemoteBrowser>, |
90 int result, | 87 int result, |
91 const std::string& response); | 88 const std::string& response); |
92 | 89 |
93 DiscoveryCallback callback_; | 90 DiscoveryCallback callback_; |
| 91 AndroidDeviceManager::Devices devices_; |
94 RemoteDevices remote_devices_; | 92 RemoteDevices remote_devices_; |
95 }; | 93 }; |
96 | 94 |
97 DiscoveryRequest::DiscoveryRequest( | 95 DevToolsAndroidBridge::DiscoveryRequest::DiscoveryRequest( |
98 AndroidDeviceManager* device_manager, | 96 AndroidDeviceManager* device_manager, |
99 const DiscoveryCallback& callback) | 97 const DiscoveryCallback& callback) |
100 : callback_(callback) { | 98 : callback_(callback) { |
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
102 device_manager->QueryDevices( | 100 device_manager->QueryDevices( |
103 base::Bind(&DiscoveryRequest::ReceivedDevices, this)); | 101 base::Bind(&DiscoveryRequest::ReceivedDevices, this)); |
104 } | 102 } |
105 | 103 |
106 DiscoveryRequest::~DiscoveryRequest() { | 104 DevToolsAndroidBridge::DiscoveryRequest::~DiscoveryRequest() { |
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
108 callback_.Run(remote_devices_); | 106 callback_.Run(devices_, remote_devices_); |
109 } | 107 } |
110 | 108 |
111 void DiscoveryRequest::ReceivedDevices(const Devices& devices) { | 109 void DevToolsAndroidBridge::DiscoveryRequest::ReceivedDevices( |
| 110 const AndroidDeviceManager::Devices& devices) { |
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
113 for (Devices::const_iterator it = devices.begin(); | 112 devices_ = devices; |
| 113 for (AndroidDeviceManager::Devices::const_iterator it = devices.begin(); |
114 it != devices.end(); ++it) { | 114 it != devices.end(); ++it) { |
115 (*it)->QueryDeviceInfo( | 115 (*it)->QueryDeviceInfo( |
116 base::Bind(&DiscoveryRequest::ReceivedDeviceInfo, this, *it)); | 116 base::Bind(&DiscoveryRequest::ReceivedDeviceInfo, this, *it)); |
117 } | 117 } |
118 } | 118 } |
119 | 119 |
120 void DiscoveryRequest::ReceivedDeviceInfo(scoped_refptr<Device> device, | 120 void DevToolsAndroidBridge::DiscoveryRequest::ReceivedDeviceInfo( |
121 const DeviceInfo& device_info) { | 121 scoped_refptr<AndroidDeviceManager::Device> device, |
| 122 const AndroidDeviceManager::DeviceInfo& device_info) { |
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
123 scoped_refptr<RemoteDevice> remote_device = | 124 scoped_refptr<RemoteDevice> remote_device = |
124 new RemoteDevice(device, device_info); | 125 new RemoteDevice(device->serial(), device_info); |
125 remote_devices_.push_back(remote_device); | 126 remote_devices_.push_back(remote_device); |
126 for (RemoteBrowsers::iterator it = remote_device->browsers().begin(); | 127 for (RemoteBrowsers::iterator it = remote_device->browsers().begin(); |
127 it != remote_device->browsers().end(); ++it) { | 128 it != remote_device->browsers().end(); ++it) { |
128 (*it)->SendJsonRequest( | 129 device->SendJsonRequest( |
| 130 (*it)->socket(), |
129 kVersionRequest, | 131 kVersionRequest, |
130 base::Bind(&DiscoveryRequest::ReceivedVersion, this, *it)); | 132 base::Bind(&DiscoveryRequest::ReceivedVersion, this, *it)); |
131 (*it)->SendJsonRequest( | 133 device->SendJsonRequest( |
| 134 (*it)->socket(), |
132 kPageListRequest, | 135 kPageListRequest, |
133 base::Bind(&DiscoveryRequest::ReceivedPages, this, *it)); | 136 base::Bind(&DiscoveryRequest::ReceivedPages, this, *it)); |
134 } | 137 } |
135 } | 138 } |
136 | 139 |
137 void DiscoveryRequest::ReceivedVersion(scoped_refptr<RemoteBrowser> browser, | 140 void DevToolsAndroidBridge::DiscoveryRequest::ReceivedVersion( |
138 int result, | 141 scoped_refptr<RemoteBrowser> browser, |
139 const std::string& response) { | 142 int result, |
| 143 const std::string& response) { |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
141 if (result < 0) | 145 if (result < 0) |
142 return; | 146 return; |
143 // Parse version, append to package name if available, | 147 // Parse version, append to package name if available, |
144 scoped_ptr<base::Value> value(base::JSONReader::Read(response)); | 148 scoped_ptr<base::Value> value(base::JSONReader::Read(response)); |
145 base::DictionaryValue* dict; | 149 base::DictionaryValue* dict; |
146 if (value && value->GetAsDictionary(&dict)) { | 150 if (value && value->GetAsDictionary(&dict)) { |
147 std::string browser_name; | 151 std::string browser_name; |
148 if (dict->GetString("Browser", &browser_name)) { | 152 if (dict->GetString("Browser", &browser_name)) { |
149 std::vector<std::string> parts; | 153 std::vector<std::string> parts; |
150 Tokenize(browser_name, "/", &parts); | 154 Tokenize(browser_name, "/", &parts); |
151 if (parts.size() == 2) | 155 if (parts.size() == 2) |
152 browser->set_version(parts[1]); | 156 browser->version_ = parts[1]; |
153 else | 157 else |
154 browser->set_version(browser_name); | 158 browser->version_ = browser_name; |
155 } | 159 } |
156 std::string package; | 160 std::string package; |
157 if (dict->GetString("Android-Package", &package)) { | 161 if (dict->GetString("Android-Package", &package)) { |
158 browser->set_display_name( | 162 browser->display_name_ = |
159 AdbDeviceInfoQuery::GetDisplayName(browser->socket(), package)); | 163 AdbDeviceInfoQuery::GetDisplayName(browser->socket(), package); |
160 } | 164 } |
161 } | 165 } |
162 } | 166 } |
163 | 167 |
164 void DiscoveryRequest::ReceivedPages(scoped_refptr<RemoteBrowser> browser, | 168 void DevToolsAndroidBridge::DiscoveryRequest::ReceivedPages( |
165 int result, | 169 scoped_refptr<RemoteBrowser> browser, |
166 const std::string& response) { | 170 int result, |
| 171 const std::string& response) { |
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
168 if (result < 0) | 173 if (result < 0) |
169 return; | 174 return; |
170 scoped_ptr<base::Value> value(base::JSONReader::Read(response)); | 175 scoped_ptr<base::Value> value(base::JSONReader::Read(response)); |
171 base::ListValue* list_value; | 176 base::ListValue* list_value; |
172 if (value && value->GetAsList(&list_value)) | 177 if (value && value->GetAsList(&list_value)) |
173 browser->SetPageDescriptors(*list_value); | 178 browser->page_descriptors_.reset(list_value->DeepCopy()); |
174 } | 179 } |
175 | 180 |
176 // ProtocolCommand ------------------------------------------------------------ | 181 // ProtocolCommand ------------------------------------------------------------ |
177 | 182 |
| 183 namespace { |
| 184 |
178 class ProtocolCommand | 185 class ProtocolCommand |
179 : public DevToolsAndroidBridge::AndroidWebSocket::Delegate { | 186 : public AndroidDeviceManager::AndroidWebSocket::Delegate { |
180 public: | 187 public: |
181 ProtocolCommand( | 188 ProtocolCommand( |
182 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 189 scoped_refptr<AndroidDeviceManager::Device> device, |
| 190 const std::string& socket, |
183 const std::string& debug_url, | 191 const std::string& debug_url, |
184 const std::string& command, | 192 const std::string& command, |
185 const base::Closure callback); | 193 const base::Closure callback); |
186 | 194 |
187 private: | 195 private: |
188 virtual void OnSocketOpened() OVERRIDE; | 196 virtual void OnSocketOpened() OVERRIDE; |
189 virtual void OnFrameRead(const std::string& message) OVERRIDE; | 197 virtual void OnFrameRead(const std::string& message) OVERRIDE; |
190 virtual void OnSocketClosed() OVERRIDE; | 198 virtual void OnSocketClosed() OVERRIDE; |
191 virtual ~ProtocolCommand(); | 199 virtual ~ProtocolCommand(); |
192 | 200 |
193 const std::string command_; | 201 const std::string command_; |
194 const base::Closure callback_; | 202 const base::Closure callback_; |
195 scoped_ptr<DevToolsAndroidBridge::AndroidWebSocket> web_socket_; | 203 scoped_ptr<AndroidDeviceManager::AndroidWebSocket> web_socket_; |
196 | 204 |
197 DISALLOW_COPY_AND_ASSIGN(ProtocolCommand); | 205 DISALLOW_COPY_AND_ASSIGN(ProtocolCommand); |
198 }; | 206 }; |
199 | 207 |
200 ProtocolCommand::ProtocolCommand( | 208 ProtocolCommand::ProtocolCommand( |
201 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 209 scoped_refptr<AndroidDeviceManager::Device> device, |
| 210 const std::string& socket, |
202 const std::string& debug_url, | 211 const std::string& debug_url, |
203 const std::string& command, | 212 const std::string& command, |
204 const base::Closure callback) | 213 const base::Closure callback) |
205 : command_(command), | 214 : command_(command), |
206 callback_(callback), | 215 callback_(callback), |
207 web_socket_(browser->CreateWebSocket(debug_url, this)) { | 216 web_socket_(device->CreateWebSocket(socket, debug_url, this)) { |
208 } | 217 } |
209 | 218 |
210 void ProtocolCommand::OnSocketOpened() { | 219 void ProtocolCommand::OnSocketOpened() { |
211 web_socket_->SendFrame(command_); | 220 web_socket_->SendFrame(command_); |
212 } | 221 } |
213 | 222 |
214 void ProtocolCommand::OnFrameRead(const std::string& message) { | 223 void ProtocolCommand::OnFrameRead(const std::string& message) { |
215 delete this; | 224 delete this; |
216 } | 225 } |
217 | 226 |
218 void ProtocolCommand::OnSocketClosed() { | 227 void ProtocolCommand::OnSocketClosed() { |
219 delete this; | 228 delete this; |
220 } | 229 } |
221 | 230 |
222 ProtocolCommand::~ProtocolCommand() { | 231 ProtocolCommand::~ProtocolCommand() { |
223 if (!callback_.is_null()) | 232 if (!callback_.is_null()) |
224 callback_.Run(); | 233 callback_.Run(); |
225 } | 234 } |
226 | 235 |
227 } // namespace | 236 } // namespace |
228 | 237 |
229 class AgentHostDelegate; | |
230 | |
231 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates; | |
232 | |
233 base::LazyInstance<AgentHostDelegates>::Leaky g_host_delegates = | |
234 LAZY_INSTANCE_INITIALIZER; | |
235 | |
236 DevToolsAndroidBridge::Wrapper::Wrapper(content::BrowserContext* context) { | 238 DevToolsAndroidBridge::Wrapper::Wrapper(content::BrowserContext* context) { |
237 bridge_ = new DevToolsAndroidBridge(Profile::FromBrowserContext(context)); | 239 bridge_ = new DevToolsAndroidBridge(Profile::FromBrowserContext(context)); |
238 } | 240 } |
239 | 241 |
240 DevToolsAndroidBridge::Wrapper::~Wrapper() { | 242 DevToolsAndroidBridge::Wrapper::~Wrapper() { |
241 } | 243 } |
242 | 244 |
243 DevToolsAndroidBridge* DevToolsAndroidBridge::Wrapper::Get() { | 245 DevToolsAndroidBridge* DevToolsAndroidBridge::Wrapper::Get() { |
244 return bridge_.get(); | 246 return bridge_.get(); |
245 } | 247 } |
(...skipping 17 matching lines...) Expand all Loading... |
263 "DevToolsAndroidBridge", | 265 "DevToolsAndroidBridge", |
264 BrowserContextDependencyManager::GetInstance()) {} | 266 BrowserContextDependencyManager::GetInstance()) {} |
265 | 267 |
266 DevToolsAndroidBridge::Factory::~Factory() {} | 268 DevToolsAndroidBridge::Factory::~Factory() {} |
267 | 269 |
268 KeyedService* DevToolsAndroidBridge::Factory::BuildServiceInstanceFor( | 270 KeyedService* DevToolsAndroidBridge::Factory::BuildServiceInstanceFor( |
269 content::BrowserContext* context) const { | 271 content::BrowserContext* context) const { |
270 return new DevToolsAndroidBridge::Wrapper(context); | 272 return new DevToolsAndroidBridge::Wrapper(context); |
271 } | 273 } |
272 | 274 |
273 | |
274 // AgentHostDelegate ---------------------------------------------------------- | 275 // AgentHostDelegate ---------------------------------------------------------- |
275 | 276 |
276 class AgentHostDelegate | 277 class DevToolsAndroidBridge::AgentHostDelegate |
277 : public content::DevToolsExternalAgentProxyDelegate, | 278 : public content::DevToolsExternalAgentProxyDelegate, |
278 public DevToolsAndroidBridge::AndroidWebSocket::Delegate { | 279 public AndroidDeviceManager::AndroidWebSocket::Delegate { |
279 public: | 280 public: |
280 static scoped_refptr<content::DevToolsAgentHost> GetOrCreateAgentHost( | 281 static scoped_refptr<content::DevToolsAgentHost> GetOrCreateAgentHost( |
| 282 scoped_refptr<DevToolsAndroidBridge> bridge, |
281 const std::string& id, | 283 const std::string& id, |
282 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 284 scoped_refptr<RemoteBrowser> browser, |
283 const std::string& debug_url); | 285 const std::string& debug_url); |
284 | 286 |
285 private: | 287 private: |
286 AgentHostDelegate( | 288 AgentHostDelegate( |
| 289 scoped_refptr<DevToolsAndroidBridge> bridge, |
287 const std::string& id, | 290 const std::string& id, |
288 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 291 scoped_refptr<RemoteBrowser> browser, |
289 const std::string& debug_url); | 292 const std::string& debug_url); |
290 virtual ~AgentHostDelegate(); | 293 virtual ~AgentHostDelegate(); |
291 virtual void Attach(content::DevToolsExternalAgentProxy* proxy) OVERRIDE; | 294 virtual void Attach(content::DevToolsExternalAgentProxy* proxy) OVERRIDE; |
292 virtual void Detach() OVERRIDE; | 295 virtual void Detach() OVERRIDE; |
293 virtual void SendMessageToBackend( | 296 virtual void SendMessageToBackend( |
294 const std::string& message) OVERRIDE; | 297 const std::string& message) OVERRIDE; |
295 virtual void OnSocketOpened() OVERRIDE; | 298 virtual void OnSocketOpened() OVERRIDE; |
296 virtual void OnFrameRead(const std::string& message) OVERRIDE; | 299 virtual void OnFrameRead(const std::string& message) OVERRIDE; |
297 virtual void OnSocketClosed() OVERRIDE; | 300 virtual void OnSocketClosed() OVERRIDE; |
298 | 301 |
299 const std::string id_; | 302 std::string id_; |
300 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser_; | 303 scoped_refptr<DevToolsAndroidBridge> bridge_; |
301 const std::string debug_url_; | 304 scoped_refptr<RemoteBrowser> browser_; |
| 305 std::string debug_url_; |
302 bool socket_opened_; | 306 bool socket_opened_; |
303 bool is_web_view_; | 307 bool is_web_view_; |
304 std::vector<std::string> pending_messages_; | 308 std::vector<std::string> pending_messages_; |
305 scoped_ptr<DevToolsAndroidBridge::AndroidWebSocket> web_socket_; | 309 scoped_ptr<AndroidDeviceManager::AndroidWebSocket> web_socket_; |
306 content::DevToolsAgentHost* agent_host_; | 310 content::DevToolsAgentHost* agent_host_; |
307 content::DevToolsExternalAgentProxy* proxy_; | 311 content::DevToolsExternalAgentProxy* proxy_; |
308 DISALLOW_COPY_AND_ASSIGN(AgentHostDelegate); | 312 DISALLOW_COPY_AND_ASSIGN(AgentHostDelegate); |
309 }; | 313 }; |
310 | 314 |
311 // static | 315 // static |
312 scoped_refptr<content::DevToolsAgentHost> | 316 scoped_refptr<content::DevToolsAgentHost> |
313 AgentHostDelegate::GetOrCreateAgentHost( | 317 DevToolsAndroidBridge::AgentHostDelegate::GetOrCreateAgentHost( |
| 318 scoped_refptr<DevToolsAndroidBridge> bridge, |
314 const std::string& id, | 319 const std::string& id, |
315 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 320 scoped_refptr<RemoteBrowser> browser, |
316 const std::string& debug_url) { | 321 const std::string& debug_url) { |
317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
318 AgentHostDelegates::iterator it = g_host_delegates.Get().find(id); | 323 AgentHostDelegates::iterator it = bridge->host_delegates_.find(id); |
319 if (it != g_host_delegates.Get().end()) | 324 if (it != bridge->host_delegates_.end()) |
320 return it->second->agent_host_; | 325 return it->second->agent_host_; |
321 | 326 |
322 AgentHostDelegate* delegate = new AgentHostDelegate(id, browser, debug_url); | 327 AgentHostDelegate* delegate = |
| 328 new AgentHostDelegate(bridge, id, browser, debug_url); |
323 scoped_refptr<content::DevToolsAgentHost> result = | 329 scoped_refptr<content::DevToolsAgentHost> result = |
324 content::DevToolsAgentHost::Create(delegate); | 330 content::DevToolsAgentHost::Create(delegate); |
325 delegate->agent_host_ = result.get(); | 331 delegate->agent_host_ = result.get(); |
326 return result; | 332 return result; |
327 } | 333 } |
328 | 334 |
329 AgentHostDelegate::AgentHostDelegate( | 335 DevToolsAndroidBridge::AgentHostDelegate::AgentHostDelegate( |
| 336 scoped_refptr<DevToolsAndroidBridge> bridge, |
330 const std::string& id, | 337 const std::string& id, |
331 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 338 scoped_refptr<RemoteBrowser> browser, |
332 const std::string& debug_url) | 339 const std::string& debug_url) |
333 : id_(id), | 340 : id_(id), |
| 341 bridge_(bridge), |
334 browser_(browser), | 342 browser_(browser), |
335 debug_url_(debug_url), | 343 debug_url_(debug_url), |
336 socket_opened_(false), | 344 socket_opened_(false), |
337 is_web_view_(browser->IsWebView()), | 345 is_web_view_(browser->IsWebView()), |
338 agent_host_(NULL), | 346 agent_host_(NULL), |
339 proxy_(NULL) { | 347 proxy_(NULL) { |
340 g_host_delegates.Get()[id] = this; | 348 bridge_->host_delegates_[id] = this; |
341 } | 349 } |
342 | 350 |
343 AgentHostDelegate::~AgentHostDelegate() { | 351 DevToolsAndroidBridge::AgentHostDelegate::~AgentHostDelegate() { |
344 g_host_delegates.Get().erase(id_); | 352 bridge_->host_delegates_.erase(id_); |
345 } | 353 } |
346 | 354 |
347 void AgentHostDelegate::Attach(content::DevToolsExternalAgentProxy* proxy) { | 355 void DevToolsAndroidBridge::AgentHostDelegate::Attach( |
| 356 content::DevToolsExternalAgentProxy* proxy) { |
348 proxy_ = proxy; | 357 proxy_ = proxy; |
349 content::RecordAction(base::UserMetricsAction(is_web_view_ ? | 358 content::RecordAction(base::UserMetricsAction(is_web_view_ ? |
350 "DevTools_InspectAndroidWebView" : "DevTools_InspectAndroidPage")); | 359 "DevTools_InspectAndroidWebView" : "DevTools_InspectAndroidPage")); |
351 web_socket_.reset(browser_->CreateWebSocket(debug_url_, this)); | 360 web_socket_.reset( |
| 361 bridge_->CreateWebSocket(browser_, debug_url_, this)); |
352 } | 362 } |
353 | 363 |
354 void AgentHostDelegate::Detach() { | 364 void DevToolsAndroidBridge::AgentHostDelegate::Detach() { |
355 web_socket_.reset(); | 365 web_socket_.reset(); |
356 } | 366 } |
357 | 367 |
358 void AgentHostDelegate::SendMessageToBackend(const std::string& message) { | 368 void DevToolsAndroidBridge::AgentHostDelegate::SendMessageToBackend( |
| 369 const std::string& message) { |
359 if (socket_opened_) | 370 if (socket_opened_) |
360 web_socket_->SendFrame(message); | 371 web_socket_->SendFrame(message); |
361 else | 372 else |
362 pending_messages_.push_back(message); | 373 pending_messages_.push_back(message); |
363 } | 374 } |
364 | 375 |
365 void AgentHostDelegate::OnSocketOpened() { | 376 void DevToolsAndroidBridge::AgentHostDelegate::OnSocketOpened() { |
366 socket_opened_ = true; | 377 socket_opened_ = true; |
367 for (std::vector<std::string>::iterator it = pending_messages_.begin(); | 378 for (std::vector<std::string>::iterator it = pending_messages_.begin(); |
368 it != pending_messages_.end(); ++it) { | 379 it != pending_messages_.end(); ++it) { |
369 SendMessageToBackend(*it); | 380 SendMessageToBackend(*it); |
370 } | 381 } |
371 pending_messages_.clear(); | 382 pending_messages_.clear(); |
372 } | 383 } |
373 | 384 |
374 void AgentHostDelegate::OnFrameRead(const std::string& message) { | 385 void DevToolsAndroidBridge::AgentHostDelegate::OnFrameRead( |
| 386 const std::string& message) { |
375 if (proxy_) | 387 if (proxy_) |
376 proxy_->DispatchOnClientHost(message); | 388 proxy_->DispatchOnClientHost(message); |
377 } | 389 } |
378 | 390 |
379 void AgentHostDelegate::OnSocketClosed() { | 391 void DevToolsAndroidBridge::AgentHostDelegate::OnSocketClosed() { |
380 if (proxy_) | 392 if (proxy_) |
381 proxy_->ConnectionClosed(); | 393 proxy_->ConnectionClosed(); |
382 } | 394 } |
383 | 395 |
384 //// RemotePageTarget ---------------------------------------------- | 396 //// RemotePageTarget ---------------------------------------------- |
385 | 397 |
386 class RemotePageTarget : public DevToolsTargetImpl, | 398 class DevToolsAndroidBridge::RemotePageTarget |
387 public DevToolsAndroidBridge::RemotePage { | 399 : public DevToolsTargetImpl, |
| 400 public DevToolsAndroidBridge::RemotePage { |
388 public: | 401 public: |
389 RemotePageTarget(scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 402 RemotePageTarget(scoped_refptr<DevToolsAndroidBridge> bridge, |
| 403 scoped_refptr<RemoteBrowser> browser, |
390 const base::DictionaryValue& value); | 404 const base::DictionaryValue& value); |
391 virtual ~RemotePageTarget(); | 405 virtual ~RemotePageTarget(); |
392 | 406 |
393 // DevToolsAndroidBridge::RemotePage implementation. | 407 // DevToolsAndroidBridge::RemotePage implementation. |
394 virtual DevToolsTargetImpl* GetTarget() OVERRIDE; | 408 virtual DevToolsTargetImpl* GetTarget() OVERRIDE; |
395 virtual std::string GetFrontendURL() OVERRIDE; | 409 virtual std::string GetFrontendURL() OVERRIDE; |
396 | 410 |
397 // DevToolsTargetImpl overrides. | 411 // DevToolsTargetImpl overrides. |
398 virtual std::string GetId() const OVERRIDE; | 412 virtual std::string GetId() const OVERRIDE; |
399 virtual bool IsAttached() const OVERRIDE; | 413 virtual bool IsAttached() const OVERRIDE; |
400 virtual bool Activate() const OVERRIDE; | 414 virtual bool Activate() const OVERRIDE; |
401 virtual bool Close() const OVERRIDE; | 415 virtual bool Close() const OVERRIDE; |
402 virtual void Inspect(Profile* profile) const OVERRIDE; | 416 virtual void Inspect(Profile* profile) const OVERRIDE; |
403 virtual void Reload() const OVERRIDE; | 417 virtual void Reload() const OVERRIDE; |
404 | 418 |
405 void Navigate(const std::string& url, base::Closure callback) const; | 419 void Navigate(const std::string& url, base::Closure callback) const; |
406 | 420 |
407 private: | 421 private: |
408 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser_; | 422 scoped_refptr<DevToolsAndroidBridge> bridge_; |
| 423 scoped_refptr<RemoteBrowser> browser_; |
409 std::string debug_url_; | 424 std::string debug_url_; |
410 std::string frontend_url_; | 425 std::string frontend_url_; |
411 std::string remote_id_; | 426 std::string remote_id_; |
412 std::string remote_type_; | 427 std::string remote_type_; |
413 std::string local_id_; | 428 std::string local_id_; |
414 DISALLOW_COPY_AND_ASSIGN(RemotePageTarget); | 429 DISALLOW_COPY_AND_ASSIGN(RemotePageTarget); |
415 }; | 430 }; |
416 | 431 |
417 static std::string GetStringProperty(const base::DictionaryValue& value, | 432 static std::string GetStringProperty(const base::DictionaryValue& value, |
418 const std::string& name) { | 433 const std::string& name) { |
419 std::string result; | 434 std::string result; |
420 value.GetString(name, &result); | 435 value.GetString(name, &result); |
421 return result; | 436 return result; |
422 } | 437 } |
423 | 438 |
424 static std::string BuildUniqueTargetId( | 439 static std::string BuildUniqueTargetId( |
425 DevToolsAndroidBridge::RemoteBrowser* browser, | 440 DevToolsAndroidBridge::RemoteBrowser* browser, |
426 const base::DictionaryValue& value) { | 441 const base::DictionaryValue& value) { |
427 return base::StringPrintf("%s:%s:%s", browser->serial().c_str(), | 442 return base::StringPrintf("%s:%s:%s", browser->serial().c_str(), |
428 browser->socket().c_str(), GetStringProperty(value, "id").c_str()); | 443 browser->socket().c_str(), GetStringProperty(value, "id").c_str()); |
429 } | 444 } |
430 | 445 |
431 static std::string GetDebugURL(const base::DictionaryValue& value) { | 446 static std::string GetDebugURL(const base::DictionaryValue& value) { |
432 std::string debug_url = GetStringProperty(value, "webSocketDebuggerUrl"); | 447 std::string debug_url = GetStringProperty(value, "webSocketDebuggerUrl"); |
433 | 448 |
434 if (debug_url.find("ws://") == 0) | 449 if (debug_url.find("ws://") == 0) |
435 debug_url = debug_url.substr(5); | 450 debug_url = debug_url.substr(5); |
436 else | 451 else |
437 debug_url = ""; | 452 debug_url = std::string(); |
438 return debug_url; | 453 return debug_url; |
439 } | 454 } |
440 | 455 |
441 RemotePageTarget::RemotePageTarget( | 456 DevToolsAndroidBridge::RemotePageTarget::RemotePageTarget( |
442 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 457 scoped_refptr<DevToolsAndroidBridge> bridge, |
| 458 scoped_refptr<RemoteBrowser> browser, |
443 const base::DictionaryValue& value) | 459 const base::DictionaryValue& value) |
444 : DevToolsTargetImpl(AgentHostDelegate::GetOrCreateAgentHost( | 460 : DevToolsTargetImpl(AgentHostDelegate::GetOrCreateAgentHost( |
| 461 bridge, |
445 BuildUniqueTargetId(browser.get(), value), | 462 BuildUniqueTargetId(browser.get(), value), |
446 browser, GetDebugURL(value))), | 463 browser, |
| 464 GetDebugURL(value))), |
| 465 bridge_(bridge), |
447 browser_(browser), | 466 browser_(browser), |
448 debug_url_(GetDebugURL(value)), | 467 debug_url_(GetDebugURL(value)), |
449 remote_id_(GetStringProperty(value, "id")), | 468 remote_id_(GetStringProperty(value, "id")), |
450 remote_type_(GetStringProperty(value, "type")), | 469 remote_type_(GetStringProperty(value, "type")), |
451 local_id_(BuildUniqueTargetId(browser.get(), value)) { | 470 local_id_(BuildUniqueTargetId(browser.get(), value)) { |
452 set_type("adb_page"); | 471 set_type("adb_page"); |
453 set_url(GURL(GetStringProperty(value, "url"))); | 472 set_url(GURL(GetStringProperty(value, "url"))); |
454 set_title(base::UTF16ToUTF8(net::UnescapeForHTML(base::UTF8ToUTF16( | 473 set_title(base::UTF16ToUTF8(net::UnescapeForHTML(base::UTF8ToUTF16( |
455 GetStringProperty(value, "title"))))); | 474 GetStringProperty(value, "title"))))); |
456 set_description(GetStringProperty(value, "description")); | 475 set_description(GetStringProperty(value, "description")); |
457 set_favicon_url(GURL(GetStringProperty(value, "faviconUrl"))); | 476 set_favicon_url(GURL(GetStringProperty(value, "faviconUrl"))); |
458 debug_url_ = GetDebugURL(value); | 477 debug_url_ = GetDebugURL(value); |
459 frontend_url_ = GetStringProperty(value, "devtoolsFrontendUrl"); | 478 frontend_url_ = GetStringProperty(value, "devtoolsFrontendUrl"); |
460 | 479 |
461 size_t ws_param = frontend_url_.find("?ws"); | 480 size_t ws_param = frontend_url_.find("?ws"); |
462 if (ws_param != std::string::npos) | 481 if (ws_param != std::string::npos) |
463 frontend_url_ = frontend_url_.substr(0, ws_param); | 482 frontend_url_ = frontend_url_.substr(0, ws_param); |
464 if (frontend_url_.find("http:") == 0) | 483 if (frontend_url_.find("http:") == 0) |
465 frontend_url_ = "https:" + frontend_url_.substr(5); | 484 frontend_url_ = "https:" + frontend_url_.substr(5); |
466 } | 485 } |
467 | 486 |
468 RemotePageTarget::~RemotePageTarget() { | 487 DevToolsAndroidBridge::RemotePageTarget::~RemotePageTarget() { |
469 } | 488 } |
470 | 489 |
471 DevToolsTargetImpl* RemotePageTarget::GetTarget() { | 490 DevToolsTargetImpl* DevToolsAndroidBridge::RemotePageTarget::GetTarget() { |
472 return this; | 491 return this; |
473 } | 492 } |
474 | 493 |
475 std::string RemotePageTarget::GetFrontendURL() { | 494 std::string DevToolsAndroidBridge::RemotePageTarget::GetFrontendURL() { |
476 return frontend_url_; | 495 return frontend_url_; |
477 } | 496 } |
478 | 497 |
479 std::string RemotePageTarget::GetId() const { | 498 std::string DevToolsAndroidBridge::RemotePageTarget::GetId() const { |
480 return local_id_; | 499 return local_id_; |
481 } | 500 } |
482 | 501 |
483 bool RemotePageTarget::IsAttached() const { | 502 bool DevToolsAndroidBridge::RemotePageTarget::IsAttached() const { |
484 return debug_url_.empty(); | 503 return debug_url_.empty(); |
485 } | 504 } |
486 | 505 |
487 static void NoOp(int, const std::string&) {} | 506 static void NoOp(int, const std::string&) {} |
488 | 507 |
489 void RemotePageTarget::Inspect(Profile* profile) const { | 508 void DevToolsAndroidBridge::RemotePageTarget::Inspect(Profile* profile) const { |
490 Activate(); | 509 Activate(); |
491 bool isWorker = remote_type_ == kTargetTypeWorker || | 510 bool isWorker = remote_type_ == kTargetTypeWorker || |
492 remote_type_ == kTargetTypeServiceWorker; | 511 remote_type_ == kTargetTypeServiceWorker; |
493 DevToolsWindow::OpenExternalFrontend(profile, frontend_url_, GetAgentHost(), | 512 DevToolsWindow::OpenExternalFrontend(profile, frontend_url_, GetAgentHost(), |
494 isWorker); | 513 isWorker); |
495 } | 514 } |
496 | 515 |
497 bool RemotePageTarget::Activate() const { | 516 bool DevToolsAndroidBridge::RemotePageTarget::Activate() const { |
498 std::string request = base::StringPrintf(kActivatePageRequest, | 517 std::string request = base::StringPrintf(kActivatePageRequest, |
499 remote_id_.c_str()); | 518 remote_id_.c_str()); |
500 browser_->SendJsonRequest(request, base::Bind(&NoOp)); | 519 bridge_->SendJsonRequest(browser_, request, base::Bind(&NoOp)); |
501 return true; | 520 return true; |
502 } | 521 } |
503 | 522 |
504 bool RemotePageTarget::Close() const { | 523 bool DevToolsAndroidBridge::RemotePageTarget::Close() const { |
505 std::string request = base::StringPrintf(kClosePageRequest, | 524 std::string request = base::StringPrintf(kClosePageRequest, |
506 remote_id_.c_str()); | 525 remote_id_.c_str()); |
507 browser_->SendJsonRequest(request, base::Bind(&NoOp)); | 526 bridge_->SendJsonRequest(browser_, request, base::Bind(&NoOp)); |
508 return true; | 527 return true; |
509 } | 528 } |
510 | 529 |
511 void RemotePageTarget::Reload() const { | 530 void DevToolsAndroidBridge::RemotePageTarget::Reload() const { |
512 browser_->SendProtocolCommand(debug_url_, kPageReloadCommand, NULL, | 531 bridge_->SendProtocolCommand(browser_, debug_url_, kPageReloadCommand, |
513 base::Closure()); | 532 NULL, base::Closure()); |
514 } | 533 } |
515 | 534 |
516 void RemotePageTarget::Navigate(const std::string& url, | 535 void DevToolsAndroidBridge::RemotePageTarget::Navigate( |
517 base::Closure callback) const { | 536 const std::string& url, |
| 537 base::Closure callback) const { |
518 base::DictionaryValue params; | 538 base::DictionaryValue params; |
519 params.SetString(kUrlParam, url); | 539 params.SetString(kUrlParam, url); |
520 browser_->SendProtocolCommand(debug_url_, kPageNavigateCommand, ¶ms, | 540 bridge_->SendProtocolCommand(browser_, debug_url_, kPageNavigateCommand, |
521 callback); | 541 ¶ms, callback); |
522 } | 542 } |
523 | 543 |
524 // DevToolsAndroidBridge::RemoteBrowser --------------------------------------- | 544 // DevToolsAndroidBridge::RemoteBrowser --------------------------------------- |
525 | 545 |
526 DevToolsAndroidBridge::RemoteBrowser::RemoteBrowser( | 546 DevToolsAndroidBridge::RemoteBrowser::RemoteBrowser( |
527 scoped_refptr<Device> device, | 547 const std::string& serial, |
528 const AndroidDeviceManager::BrowserInfo& browser_info) | 548 const AndroidDeviceManager::BrowserInfo& browser_info) |
529 : device_(device), | 549 : serial_(serial), |
530 socket_(browser_info.socket_name), | 550 socket_(browser_info.socket_name), |
531 display_name_(browser_info.display_name), | 551 display_name_(browser_info.display_name), |
532 type_(browser_info.type), | 552 type_(browser_info.type), |
533 page_descriptors_(new base::ListValue()) { | 553 page_descriptors_(new base::ListValue()) { |
534 } | 554 } |
535 | 555 |
536 bool DevToolsAndroidBridge::RemoteBrowser::IsChrome() const { | 556 bool DevToolsAndroidBridge::RemoteBrowser::IsChrome() { |
537 return type_ == AndroidDeviceManager::BrowserInfo::kTypeChrome; | 557 return type_ == AndroidDeviceManager::BrowserInfo::kTypeChrome; |
538 } | 558 } |
539 | 559 |
540 bool DevToolsAndroidBridge::RemoteBrowser::IsWebView() const { | 560 bool DevToolsAndroidBridge::RemoteBrowser::IsWebView() { |
541 return type_ == AndroidDeviceManager::BrowserInfo::kTypeWebView; | 561 return type_ == AndroidDeviceManager::BrowserInfo::kTypeWebView; |
542 } | 562 } |
543 | 563 |
544 DevToolsAndroidBridge::RemoteBrowser::ParsedVersion | 564 DevToolsAndroidBridge::RemoteBrowser::ParsedVersion |
545 DevToolsAndroidBridge::RemoteBrowser::GetParsedVersion() const { | 565 DevToolsAndroidBridge::RemoteBrowser::GetParsedVersion() { |
546 ParsedVersion result; | 566 ParsedVersion result; |
547 std::vector<std::string> parts; | 567 std::vector<std::string> parts; |
548 Tokenize(version_, ".", &parts); | 568 Tokenize(version_, ".", &parts); |
549 for (size_t i = 0; i != parts.size(); ++i) { | 569 for (size_t i = 0; i != parts.size(); ++i) { |
550 int value = 0; | 570 int value = 0; |
551 base::StringToInt(parts[i], &value); | 571 base::StringToInt(parts[i], &value); |
552 result.push_back(value); | 572 result.push_back(value); |
553 } | 573 } |
554 return result; | 574 return result; |
555 } | 575 } |
556 | 576 |
557 std::vector<DevToolsAndroidBridge::RemotePage*> | 577 std::vector<DevToolsAndroidBridge::RemotePage*> |
558 DevToolsAndroidBridge::RemoteBrowser::CreatePages() { | 578 DevToolsAndroidBridge::CreatePages(scoped_refptr<RemoteBrowser> browser) { |
559 std::vector<DevToolsAndroidBridge::RemotePage*> result; | 579 std::vector<RemotePage*> result; |
560 for (size_t i = 0; i < page_descriptors_->GetSize(); ++i) { | 580 for (base::ListValue::const_iterator it = browser->page_descriptors().begin(); |
561 base::Value* item; | 581 it != browser->page_descriptors().end(); ++it) { |
562 page_descriptors_->Get(i, &item); | |
563 if (!item) | |
564 continue; | |
565 base::DictionaryValue* dict; | 582 base::DictionaryValue* dict; |
566 if (!item->GetAsDictionary(&dict)) | 583 if (*it && (*it)->GetAsDictionary(&dict)) |
567 continue; | 584 result.push_back(new RemotePageTarget(this, browser, *dict)); |
568 result.push_back(new RemotePageTarget(this, *dict)); | |
569 } | 585 } |
570 return result; | 586 return result; |
571 } | 587 } |
572 | 588 |
573 void DevToolsAndroidBridge::RemoteBrowser::SetPageDescriptors( | 589 const base::ListValue& |
574 const base::ListValue& list) { | 590 DevToolsAndroidBridge::RemoteBrowser::page_descriptors() { |
575 page_descriptors_.reset(list.DeepCopy()); | 591 return *page_descriptors_; |
576 } | 592 } |
577 | 593 |
578 static void RespondOnUIThread( | 594 void DevToolsAndroidBridge::SendJsonRequest( |
579 const DevToolsAndroidBridge::JsonRequestCallback& callback, | 595 scoped_refptr<RemoteBrowser> browser, |
580 int result, | 596 const std::string& request, |
581 const std::string& response) { | 597 const JsonRequestCallback& callback) { |
582 if (callback.is_null()) | 598 DeviceMap::iterator it = device_map_.find(browser->serial()); |
| 599 if (it == device_map_.end()) { |
| 600 callback.Run(net::ERR_FAILED, std::string()); |
583 return; | 601 return; |
584 BrowserThread::PostTask( | 602 } |
585 BrowserThread::UI, FROM_HERE, base::Bind(callback, result, response)); | 603 it->second->SendJsonRequest(browser->socket(), request, callback); |
586 } | 604 } |
587 | 605 |
588 void DevToolsAndroidBridge::RemoteBrowser::SendJsonRequest( | 606 void DevToolsAndroidBridge::SendProtocolCommand( |
589 const std::string& request, const JsonRequestCallback& callback) { | 607 scoped_refptr<RemoteBrowser> browser, |
590 device_->SendJsonRequest(socket_, request, callback); | |
591 } | |
592 | |
593 void DevToolsAndroidBridge::RemoteBrowser::SendProtocolCommand( | |
594 const std::string& debug_url, | 608 const std::string& debug_url, |
595 const std::string& method, | 609 const std::string& method, |
596 base::DictionaryValue* params, | 610 base::DictionaryValue* params, |
597 const base::Closure callback) { | 611 const base::Closure callback) { |
598 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
599 if (debug_url.empty()) | 613 if (debug_url.empty()) |
600 return; | 614 return; |
| 615 DeviceMap::iterator it = device_map_.find(browser->serial()); |
| 616 if (it == device_map_.end()) { |
| 617 callback.Run(); |
| 618 return; |
| 619 } |
601 DevToolsProtocol::Command command(1, method, params); | 620 DevToolsProtocol::Command command(1, method, params); |
602 new ProtocolCommand(this, debug_url, command.Serialize(), callback); | 621 new ProtocolCommand(it->second, browser->socket(), debug_url, |
603 } | 622 command.Serialize(), callback); |
604 | |
605 void DevToolsAndroidBridge::RemoteBrowser::Open( | |
606 const std::string& url, | |
607 const DevToolsAndroidBridge::RemotePageCallback& callback) { | |
608 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
609 InnerOpen(url, base::Bind(&RemoteBrowser::RespondToOpenOnUIThread, | |
610 this, callback)); | |
611 } | 623 } |
612 | 624 |
613 scoped_refptr<content::DevToolsAgentHost> | 625 scoped_refptr<content::DevToolsAgentHost> |
614 DevToolsAndroidBridge::RemoteBrowser::GetAgentHost() { | 626 DevToolsAndroidBridge::GetBrowserAgentHost( |
| 627 scoped_refptr<RemoteBrowser> browser) { |
615 return AgentHostDelegate::GetOrCreateAgentHost( | 628 return AgentHostDelegate::GetOrCreateAgentHost( |
616 "adb:" + device_->serial() + ":" + socket_, this, kBrowserTargetSocket); | 629 this, |
| 630 "adb:" + browser->serial() + ":" + browser->socket(), |
| 631 browser, |
| 632 kBrowserTargetSocket); |
617 } | 633 } |
618 | 634 |
619 DevToolsAndroidBridge::AndroidWebSocket* | 635 AndroidDeviceManager::AndroidWebSocket* |
620 DevToolsAndroidBridge::RemoteBrowser::CreateWebSocket( | 636 DevToolsAndroidBridge::CreateWebSocket( |
| 637 scoped_refptr<RemoteBrowser> browser, |
621 const std::string& url, | 638 const std::string& url, |
622 DevToolsAndroidBridge::AndroidWebSocket::Delegate* delegate) { | 639 AndroidDeviceManager::AndroidWebSocket::Delegate* delegate) { |
623 return device_->CreateWebSocket(socket_, url, delegate); | 640 DeviceMap::iterator it = device_map_.find(browser->serial()); |
| 641 if (it == device_map_.end()) |
| 642 return NULL; |
| 643 |
| 644 return it->second->CreateWebSocket(browser->socket(), url, delegate); |
624 } | 645 } |
625 | 646 |
626 void DevToolsAndroidBridge::RemoteBrowser::RespondToOpenOnUIThread( | 647 void DevToolsAndroidBridge::RespondToOpenOnUIThread( |
627 const DevToolsAndroidBridge::RemotePageCallback& callback, | 648 scoped_refptr<RemoteBrowser> browser, |
| 649 const RemotePageCallback& callback, |
628 int result, | 650 int result, |
629 const std::string& response) { | 651 const std::string& response) { |
630 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 652 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
631 if (result < 0) { | 653 if (result < 0) { |
632 callback.Run(NULL); | 654 callback.Run(NULL); |
633 return; | 655 return; |
634 } | 656 } |
635 scoped_ptr<base::Value> value(base::JSONReader::Read(response)); | 657 scoped_ptr<base::Value> value(base::JSONReader::Read(response)); |
636 base::DictionaryValue* dict; | 658 base::DictionaryValue* dict; |
637 if (value && value->GetAsDictionary(&dict)) { | 659 if (value && value->GetAsDictionary(&dict)) { |
638 RemotePageTarget* new_page = new RemotePageTarget(this, *dict); | 660 RemotePageTarget* new_page = new RemotePageTarget(this, browser, *dict); |
639 callback.Run(new_page); | 661 callback.Run(new_page); |
640 } | 662 } |
641 } | 663 } |
642 | 664 |
643 void DevToolsAndroidBridge::RemoteBrowser::InnerOpen( | 665 void DevToolsAndroidBridge::Open( |
| 666 scoped_refptr<RemoteBrowser> browser, |
644 const std::string& input_url, | 667 const std::string& input_url, |
645 const JsonRequestCallback& callback) { | 668 const DevToolsAndroidBridge::RemotePageCallback& callback) { |
646 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 669 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
647 GURL gurl(input_url); | 670 GURL gurl(input_url); |
648 if (!gurl.is_valid()) { | 671 if (!gurl.is_valid()) { |
649 gurl = GURL("http://" + input_url); | 672 gurl = GURL("http://" + input_url); |
650 if (!gurl.is_valid()) | 673 if (!gurl.is_valid()) |
651 return; | 674 return; |
652 } | 675 } |
653 std::string url = gurl.spec(); | 676 std::string url = gurl.spec(); |
| 677 RemoteBrowser::ParsedVersion parsed_version = browser->GetParsedVersion(); |
654 | 678 |
655 ParsedVersion parsed_version = GetParsedVersion(); | 679 if (browser->IsChrome() && |
656 if (IsChrome() && | |
657 !parsed_version.empty() && | 680 !parsed_version.empty() && |
658 parsed_version[0] >= kMinVersionNewWithURL) { | 681 parsed_version[0] >= kMinVersionNewWithURL) { |
659 std::string query = net::EscapeQueryParamValue(url, false /* use_plus */); | 682 std::string query = net::EscapeQueryParamValue(url, false /* use_plus */); |
660 std::string request = | 683 std::string request = |
661 base::StringPrintf(kNewPageRequestWithURL, query.c_str()); | 684 base::StringPrintf(kNewPageRequestWithURL, query.c_str()); |
662 SendJsonRequest(request, callback); | 685 SendJsonRequest(browser, request, |
| 686 base::Bind(&DevToolsAndroidBridge::RespondToOpenOnUIThread, |
| 687 this, browser, callback)); |
663 } else { | 688 } else { |
664 SendJsonRequest(kNewPageRequest, | 689 SendJsonRequest(browser, kNewPageRequest, |
665 base::Bind(&RemoteBrowser::PageCreatedOnUIThread, this, | 690 base::Bind(&DevToolsAndroidBridge::PageCreatedOnUIThread, |
666 callback, url)); | 691 this, browser, callback, url)); |
667 } | 692 } |
668 } | 693 } |
669 | 694 |
670 void DevToolsAndroidBridge::RemoteBrowser::PageCreatedOnUIThread( | 695 void DevToolsAndroidBridge::PageCreatedOnUIThread( |
671 const JsonRequestCallback& callback, | 696 scoped_refptr<RemoteBrowser> browser, |
672 const std::string& url, int result, const std::string& response) { | 697 const RemotePageCallback& callback, |
| 698 const std::string& url, |
| 699 int result, |
| 700 const std::string& response) { |
673 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 701 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
674 | 702 |
675 if (result < 0) | 703 if (result < 0) |
676 return; | 704 return; |
677 // Navigating too soon after the page creation breaks navigation history | 705 // Navigating too soon after the page creation breaks navigation history |
678 // (crbug.com/311014). This can be avoided by adding a moderate delay. | 706 // (crbug.com/311014). This can be avoided by adding a moderate delay. |
679 BrowserThread::PostDelayedTask( | 707 BrowserThread::PostDelayedTask( |
680 BrowserThread::UI, FROM_HERE, | 708 BrowserThread::UI, FROM_HERE, |
681 base::Bind(&RemoteBrowser::NavigatePageOnUIThread, | 709 base::Bind(&DevToolsAndroidBridge::NavigatePageOnUIThread, |
682 this, callback, result, response, url), | 710 this, browser, callback, result, response, url), |
683 base::TimeDelta::FromMilliseconds(kNewPageNavigateDelayMs)); | 711 base::TimeDelta::FromMilliseconds(kNewPageNavigateDelayMs)); |
684 } | 712 } |
685 | 713 |
686 void DevToolsAndroidBridge::RemoteBrowser::NavigatePageOnUIThread( | 714 void DevToolsAndroidBridge::NavigatePageOnUIThread( |
687 const JsonRequestCallback& callback, | 715 scoped_refptr<RemoteBrowser> browser, |
688 int result, const std::string& response, const std::string& url) { | 716 const RemotePageCallback& callback, |
| 717 int result, |
| 718 const std::string& response, |
| 719 const std::string& url) { |
689 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 720 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
690 scoped_ptr<base::Value> value(base::JSONReader::Read(response)); | 721 scoped_ptr<base::Value> value(base::JSONReader::Read(response)); |
691 base::DictionaryValue* dict; | 722 base::DictionaryValue* dict; |
692 | 723 |
693 if (value && value->GetAsDictionary(&dict)) { | 724 if (value && value->GetAsDictionary(&dict)) { |
694 RemotePageTarget new_page(this, *dict); | 725 RemotePageTarget new_page(this, browser, *dict); |
695 new_page.Navigate(url, | 726 new_page.Navigate(url, |
696 base::Bind(&RespondOnUIThread, callback, result, response)); | 727 base::Bind(&DevToolsAndroidBridge::RespondToOpenOnUIThread, |
| 728 this, browser, callback, result, response)); |
697 } | 729 } |
698 } | 730 } |
699 | 731 |
700 DevToolsAndroidBridge::RemoteBrowser::~RemoteBrowser() { | 732 DevToolsAndroidBridge::RemoteBrowser::~RemoteBrowser() { |
701 } | 733 } |
702 | 734 |
703 // DevToolsAndroidBridge::RemoteDevice ---------------------------------------- | 735 // DevToolsAndroidBridge::RemoteDevice ---------------------------------------- |
704 | 736 |
705 DevToolsAndroidBridge::RemoteDevice::RemoteDevice( | 737 DevToolsAndroidBridge::RemoteDevice::RemoteDevice( |
706 scoped_refptr<AndroidDeviceManager::Device> device, | 738 const std::string& serial, |
707 const AndroidDeviceManager::DeviceInfo& device_info) | 739 const AndroidDeviceManager::DeviceInfo& device_info) |
708 : device_(device), | 740 : serial_(serial), |
709 model_(device_info.model), | 741 model_(device_info.model), |
710 connected_(device_info.connected), | 742 connected_(device_info.connected), |
711 screen_size_(device_info.screen_size) { | 743 screen_size_(device_info.screen_size) { |
712 for (std::vector<AndroidDeviceManager::BrowserInfo>::const_iterator it = | 744 for (std::vector<AndroidDeviceManager::BrowserInfo>::const_iterator it = |
713 device_info.browser_info.begin(); | 745 device_info.browser_info.begin(); |
714 it != device_info.browser_info.end(); | 746 it != device_info.browser_info.end(); |
715 ++it) { | 747 ++it) { |
716 browsers_.push_back(new DevToolsAndroidBridge::RemoteBrowser(device, *it)); | 748 browsers_.push_back(new RemoteBrowser(serial, *it)); |
717 } | 749 } |
718 } | 750 } |
719 | 751 |
720 void DevToolsAndroidBridge::RemoteDevice::OpenSocket( | |
721 const std::string& socket_name, | |
722 const AndroidDeviceManager::SocketCallback& callback) { | |
723 device_->OpenSocket(socket_name, callback); | |
724 } | |
725 | |
726 DevToolsAndroidBridge::RemoteDevice::~RemoteDevice() { | 752 DevToolsAndroidBridge::RemoteDevice::~RemoteDevice() { |
727 } | 753 } |
728 | 754 |
729 // DevToolsAndroidBridge ------------------------------------------------------ | 755 // DevToolsAndroidBridge ------------------------------------------------------ |
730 | 756 |
731 DevToolsAndroidBridge::DevToolsAndroidBridge(Profile* profile) | 757 DevToolsAndroidBridge::DevToolsAndroidBridge(Profile* profile) |
732 : profile_(profile), | 758 : profile_(profile), |
733 device_manager_(AndroidDeviceManager::Create()), | 759 device_manager_(AndroidDeviceManager::Create()), |
734 task_scheduler_(base::Bind(&DevToolsAndroidBridge::ScheduleTaskDefault)), | 760 task_scheduler_(base::Bind(&DevToolsAndroidBridge::ScheduleTaskDefault)), |
735 port_forwarding_controller_(new PortForwardingController(profile)) { | 761 port_forwarding_controller_(new PortForwardingController(profile)) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
792 PortForwardingListeners::iterator it = std::find( | 818 PortForwardingListeners::iterator it = std::find( |
793 port_forwarding_listeners_.begin(), | 819 port_forwarding_listeners_.begin(), |
794 port_forwarding_listeners_.end(), | 820 port_forwarding_listeners_.end(), |
795 listener); | 821 listener); |
796 DCHECK(it != port_forwarding_listeners_.end()); | 822 DCHECK(it != port_forwarding_listeners_.end()); |
797 port_forwarding_listeners_.erase(it); | 823 port_forwarding_listeners_.erase(it); |
798 if (!NeedsDeviceListPolling()) | 824 if (!NeedsDeviceListPolling()) |
799 StopDeviceListPolling(); | 825 StopDeviceListPolling(); |
800 } | 826 } |
801 | 827 |
802 // static | |
803 bool DevToolsAndroidBridge::HasDevToolsWindow(const std::string& agent_id) { | 828 bool DevToolsAndroidBridge::HasDevToolsWindow(const std::string& agent_id) { |
804 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 829 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
805 return g_host_delegates.Get().find(agent_id) != g_host_delegates.Get().end(); | 830 return host_delegates_.find(agent_id) != host_delegates_.end(); |
806 } | 831 } |
807 | 832 |
808 DevToolsAndroidBridge::~DevToolsAndroidBridge() { | 833 DevToolsAndroidBridge::~DevToolsAndroidBridge() { |
809 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 834 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
810 DCHECK(device_list_listeners_.empty()); | 835 DCHECK(device_list_listeners_.empty()); |
811 DCHECK(device_count_listeners_.empty()); | 836 DCHECK(device_count_listeners_.empty()); |
812 DCHECK(port_forwarding_listeners_.empty()); | 837 DCHECK(port_forwarding_listeners_.empty()); |
813 } | 838 } |
814 | 839 |
815 void DevToolsAndroidBridge::StartDeviceListPolling() { | 840 void DevToolsAndroidBridge::StartDeviceListPolling() { |
816 device_list_callback_.Reset( | 841 device_list_callback_.Reset( |
817 base::Bind(&DevToolsAndroidBridge::ReceivedDeviceList, this)); | 842 base::Bind(&DevToolsAndroidBridge::ReceivedDeviceList, this)); |
818 RequestDeviceList(device_list_callback_.callback()); | 843 RequestDeviceList(device_list_callback_.callback()); |
819 } | 844 } |
820 | 845 |
821 void DevToolsAndroidBridge::StopDeviceListPolling() { | 846 void DevToolsAndroidBridge::StopDeviceListPolling() { |
822 device_list_callback_.Cancel(); | 847 device_list_callback_.Cancel(); |
823 devices_.clear(); | 848 device_map_.clear(); |
824 } | 849 } |
825 | 850 |
826 bool DevToolsAndroidBridge::NeedsDeviceListPolling() { | 851 bool DevToolsAndroidBridge::NeedsDeviceListPolling() { |
827 return !device_list_listeners_.empty() || !port_forwarding_listeners_.empty(); | 852 return !device_list_listeners_.empty() || !port_forwarding_listeners_.empty(); |
828 } | 853 } |
829 | 854 |
830 void DevToolsAndroidBridge::RequestDeviceList( | 855 void DevToolsAndroidBridge::RequestDeviceList( |
831 const base::Callback<void(const RemoteDevices&)>& callback) { | 856 const DeviceListCallback& callback) { |
832 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 857 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
833 | 858 |
834 if (!NeedsDeviceListPolling() || | 859 if (!NeedsDeviceListPolling() || |
835 !callback.Equals(device_list_callback_.callback())) | 860 !callback.Equals(device_list_callback_.callback())) |
836 return; | 861 return; |
837 | 862 |
838 new DiscoveryRequest(device_manager_.get(), callback); | 863 new DiscoveryRequest(device_manager_.get(), callback); |
839 } | 864 } |
840 | 865 |
841 void DevToolsAndroidBridge::ReceivedDeviceList(const RemoteDevices& devices) { | 866 void DevToolsAndroidBridge::ReceivedDeviceList( |
| 867 const AndroidDeviceManager::Devices& devices, |
| 868 const RemoteDevices& remote_devices) { |
842 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 869 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
843 | 870 |
| 871 device_map_.clear(); |
| 872 for (AndroidDeviceManager::Devices::const_iterator it = devices.begin(); |
| 873 it != devices.end(); ++it) { |
| 874 device_map_[(*it)->serial()] = *it; |
| 875 } |
| 876 |
844 DeviceListListeners copy(device_list_listeners_); | 877 DeviceListListeners copy(device_list_listeners_); |
845 for (DeviceListListeners::iterator it = copy.begin(); it != copy.end(); ++it) | 878 for (DeviceListListeners::iterator it = copy.begin(); it != copy.end(); ++it) |
846 (*it)->DeviceListChanged(devices); | 879 (*it)->DeviceListChanged(remote_devices); |
847 | 880 |
848 DevicesStatus status = | 881 DevicesStatus status = |
849 port_forwarding_controller_->DeviceListChanged(devices); | 882 port_forwarding_controller_->DeviceListChanged(device_map_, |
| 883 remote_devices); |
850 PortForwardingListeners forwarding_listeners(port_forwarding_listeners_); | 884 PortForwardingListeners forwarding_listeners(port_forwarding_listeners_); |
851 for (PortForwardingListeners::iterator it = forwarding_listeners.begin(); | 885 for (PortForwardingListeners::iterator it = forwarding_listeners.begin(); |
852 it != forwarding_listeners.end(); ++it) { | 886 it != forwarding_listeners.end(); ++it) { |
853 (*it)->PortStatusChanged(status); | 887 (*it)->PortStatusChanged(status); |
854 } | 888 } |
855 | 889 |
856 if (!NeedsDeviceListPolling()) | 890 if (!NeedsDeviceListPolling()) |
857 return; | 891 return; |
858 | 892 |
859 devices_ = devices; | |
860 | |
861 task_scheduler_.Run( | 893 task_scheduler_.Run( |
862 base::Bind(&DevToolsAndroidBridge::RequestDeviceList, | 894 base::Bind(&DevToolsAndroidBridge::RequestDeviceList, |
863 this, device_list_callback_.callback())); | 895 this, device_list_callback_.callback())); |
864 } | 896 } |
865 | 897 |
866 void DevToolsAndroidBridge::StartDeviceCountPolling() { | 898 void DevToolsAndroidBridge::StartDeviceCountPolling() { |
867 device_count_callback_.Reset( | 899 device_count_callback_.Reset( |
868 base::Bind(&DevToolsAndroidBridge::ReceivedDeviceCount, this)); | 900 base::Bind(&DevToolsAndroidBridge::ReceivedDeviceCount, this)); |
869 RequestDeviceCount(device_count_callback_.callback()); | 901 RequestDeviceCount(device_count_callback_.callback()); |
870 } | 902 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
927 bool enabled; | 959 bool enabled; |
928 if (pref_value->GetAsBoolean(&enabled) && enabled) { | 960 if (pref_value->GetAsBoolean(&enabled) && enabled) { |
929 device_providers.push_back(new UsbDeviceProvider(profile_)); | 961 device_providers.push_back(new UsbDeviceProvider(profile_)); |
930 } | 962 } |
931 device_manager_->SetDeviceProviders(device_providers); | 963 device_manager_->SetDeviceProviders(device_providers); |
932 if (NeedsDeviceListPolling()) { | 964 if (NeedsDeviceListPolling()) { |
933 StopDeviceListPolling(); | 965 StopDeviceListPolling(); |
934 StartDeviceListPolling(); | 966 StartDeviceListPolling(); |
935 } | 967 } |
936 } | 968 } |
OLD | NEW |