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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 public: | 179 public: |
180 ProtocolCommand( | 180 ProtocolCommand( |
181 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 181 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
182 const std::string& debug_url, | 182 const std::string& debug_url, |
183 const std::string& command, | 183 const std::string& command, |
184 const base::Closure callback); | 184 const base::Closure callback); |
185 | 185 |
186 private: | 186 private: |
187 virtual void OnSocketOpened() OVERRIDE; | 187 virtual void OnSocketOpened() OVERRIDE; |
188 virtual void OnFrameRead(const std::string& message) OVERRIDE; | 188 virtual void OnFrameRead(const std::string& message) OVERRIDE; |
189 virtual void OnSocketClosed() OVERRIDE; | 189 virtual void OnSocketClosed(bool closed_by_device) OVERRIDE; |
190 virtual ~ProtocolCommand(); | |
191 | 190 |
192 const std::string command_; | 191 const std::string command_; |
193 const base::Closure callback_; | 192 const base::Closure callback_; |
194 scoped_ptr<DevToolsAndroidBridge::AndroidWebSocket> web_socket_; | 193 scoped_refptr<DevToolsAndroidBridge::AndroidWebSocket> web_socket_; |
195 | 194 |
196 DISALLOW_COPY_AND_ASSIGN(ProtocolCommand); | 195 DISALLOW_COPY_AND_ASSIGN(ProtocolCommand); |
197 }; | 196 }; |
198 | 197 |
199 ProtocolCommand::ProtocolCommand( | 198 ProtocolCommand::ProtocolCommand( |
200 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 199 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
201 const std::string& debug_url, | 200 const std::string& debug_url, |
202 const std::string& command, | 201 const std::string& command, |
203 const base::Closure callback) | 202 const base::Closure callback) |
204 : command_(command), | 203 : command_(command), |
205 callback_(callback), | 204 callback_(callback){ |
206 web_socket_(browser->CreateWebSocket(debug_url, this)) { | 205 web_socket_ = browser->CreateWebSocket(debug_url, this); |
| 206 web_socket_->Connect(); |
207 } | 207 } |
208 | 208 |
209 void ProtocolCommand::OnSocketOpened() { | 209 void ProtocolCommand::OnSocketOpened() { |
210 web_socket_->SendFrame(command_); | 210 web_socket_->SendFrame(command_); |
211 } | 211 } |
212 | 212 |
213 void ProtocolCommand::OnFrameRead(const std::string& message) { | 213 void ProtocolCommand::OnFrameRead(const std::string& message) { |
| 214 web_socket_->Disconnect(); |
| 215 } |
| 216 |
| 217 void ProtocolCommand::OnSocketClosed(bool closed_by_device) { |
| 218 if (!callback_.is_null()) { |
| 219 callback_.Run(); |
| 220 } |
214 delete this; | 221 delete this; |
215 } | 222 } |
216 | 223 |
217 void ProtocolCommand::OnSocketClosed() { | |
218 delete this; | |
219 } | |
220 | |
221 ProtocolCommand::~ProtocolCommand() { | |
222 if (!callback_.is_null()) | |
223 callback_.Run(); | |
224 } | |
225 | |
226 } // namespace | 224 } // namespace |
227 | 225 |
228 class AgentHostDelegate; | 226 class AgentHostDelegate; |
229 | 227 |
230 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates; | 228 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates; |
231 | 229 |
232 base::LazyInstance<AgentHostDelegates>::Leaky g_host_delegates = | 230 base::LazyInstance<AgentHostDelegates>::Leaky g_host_delegates = |
233 LAZY_INSTANCE_INITIALIZER; | 231 LAZY_INSTANCE_INITIALIZER; |
234 | 232 |
235 DevToolsAndroidBridge::Wrapper::Wrapper(content::BrowserContext* context) { | 233 DevToolsAndroidBridge::Wrapper::Wrapper(content::BrowserContext* context) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 const std::string& id, | 284 const std::string& id, |
287 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 285 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
288 const std::string& debug_url); | 286 const std::string& debug_url); |
289 virtual ~AgentHostDelegate(); | 287 virtual ~AgentHostDelegate(); |
290 virtual void Attach(content::DevToolsExternalAgentProxy* proxy) OVERRIDE; | 288 virtual void Attach(content::DevToolsExternalAgentProxy* proxy) OVERRIDE; |
291 virtual void Detach() OVERRIDE; | 289 virtual void Detach() OVERRIDE; |
292 virtual void SendMessageToBackend( | 290 virtual void SendMessageToBackend( |
293 const std::string& message) OVERRIDE; | 291 const std::string& message) OVERRIDE; |
294 virtual void OnSocketOpened() OVERRIDE; | 292 virtual void OnSocketOpened() OVERRIDE; |
295 virtual void OnFrameRead(const std::string& message) OVERRIDE; | 293 virtual void OnFrameRead(const std::string& message) OVERRIDE; |
296 virtual void OnSocketClosed() OVERRIDE; | 294 virtual void OnSocketClosed(bool closed_by_device) OVERRIDE; |
297 | 295 |
298 const std::string id_; | 296 const std::string id_; |
299 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser_; | |
300 const std::string debug_url_; | |
301 bool socket_opened_; | 297 bool socket_opened_; |
| 298 bool detached_; |
302 bool is_web_view_; | 299 bool is_web_view_; |
303 std::vector<std::string> pending_messages_; | 300 std::vector<std::string> pending_messages_; |
304 scoped_ptr<DevToolsAndroidBridge::AndroidWebSocket> web_socket_; | 301 scoped_refptr<DevToolsAndroidBridge::AndroidWebSocket> web_socket_; |
305 content::DevToolsAgentHost* agent_host_; | 302 content::DevToolsAgentHost* agent_host_; |
306 content::DevToolsExternalAgentProxy* proxy_; | 303 content::DevToolsExternalAgentProxy* proxy_; |
307 DISALLOW_COPY_AND_ASSIGN(AgentHostDelegate); | 304 DISALLOW_COPY_AND_ASSIGN(AgentHostDelegate); |
308 }; | 305 }; |
309 | 306 |
310 // static | 307 // static |
311 scoped_refptr<content::DevToolsAgentHost> | 308 scoped_refptr<content::DevToolsAgentHost> |
312 AgentHostDelegate::GetOrCreateAgentHost( | 309 AgentHostDelegate::GetOrCreateAgentHost( |
313 const std::string& id, | 310 const std::string& id, |
314 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 311 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
315 const std::string& debug_url) { | 312 const std::string& debug_url) { |
316 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
317 AgentHostDelegates::iterator it = g_host_delegates.Get().find(id); | 314 AgentHostDelegates::iterator it = g_host_delegates.Get().find(id); |
318 if (it != g_host_delegates.Get().end()) | 315 if (it != g_host_delegates.Get().end()) |
319 return it->second->agent_host_; | 316 return it->second->agent_host_; |
320 | 317 |
321 AgentHostDelegate* delegate = new AgentHostDelegate(id, browser, debug_url); | 318 AgentHostDelegate* delegate = new AgentHostDelegate(id, browser, debug_url); |
322 scoped_refptr<content::DevToolsAgentHost> result = | 319 scoped_refptr<content::DevToolsAgentHost> result = |
323 content::DevToolsAgentHost::Create(delegate); | 320 content::DevToolsAgentHost::Create(delegate); |
324 delegate->agent_host_ = result.get(); | 321 delegate->agent_host_ = result.get(); |
325 return result; | 322 return result; |
326 } | 323 } |
327 | 324 |
328 AgentHostDelegate::AgentHostDelegate( | 325 AgentHostDelegate::AgentHostDelegate( |
329 const std::string& id, | 326 const std::string& id, |
330 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 327 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
331 const std::string& debug_url) | 328 const std::string& debug_url) |
332 : id_(id), | 329 : id_(id), |
333 browser_(browser), | |
334 debug_url_(debug_url), | |
335 socket_opened_(false), | 330 socket_opened_(false), |
| 331 detached_(false), |
336 is_web_view_(browser->IsWebView()), | 332 is_web_view_(browser->IsWebView()), |
| 333 web_socket_(browser->CreateWebSocket(debug_url, this)), |
337 agent_host_(NULL), | 334 agent_host_(NULL), |
338 proxy_(NULL) { | 335 proxy_(NULL) { |
339 g_host_delegates.Get()[id] = this; | 336 g_host_delegates.Get()[id] = this; |
340 } | 337 } |
341 | 338 |
342 AgentHostDelegate::~AgentHostDelegate() { | 339 AgentHostDelegate::~AgentHostDelegate() { |
343 g_host_delegates.Get().erase(id_); | 340 g_host_delegates.Get().erase(id_); |
| 341 web_socket_->ClearDelegate(); |
344 } | 342 } |
345 | 343 |
346 void AgentHostDelegate::Attach(content::DevToolsExternalAgentProxy* proxy) { | 344 void AgentHostDelegate::Attach(content::DevToolsExternalAgentProxy* proxy) { |
347 proxy_ = proxy; | 345 proxy_ = proxy; |
348 content::RecordAction(base::UserMetricsAction(is_web_view_ ? | 346 content::RecordAction(base::UserMetricsAction(is_web_view_ ? |
349 "DevTools_InspectAndroidWebView" : "DevTools_InspectAndroidPage")); | 347 "DevTools_InspectAndroidWebView" : "DevTools_InspectAndroidPage")); |
350 web_socket_.reset(browser_->CreateWebSocket(debug_url_, this)); | 348 web_socket_->Connect(); |
351 } | 349 } |
352 | 350 |
353 void AgentHostDelegate::Detach() { | 351 void AgentHostDelegate::Detach() { |
354 web_socket_.reset(); | 352 detached_ = true; |
| 353 if (socket_opened_) |
| 354 web_socket_->Disconnect(); |
355 } | 355 } |
356 | 356 |
357 void AgentHostDelegate::SendMessageToBackend(const std::string& message) { | 357 void AgentHostDelegate::SendMessageToBackend(const std::string& message) { |
358 if (socket_opened_) | 358 if (socket_opened_) |
359 web_socket_->SendFrame(message); | 359 web_socket_->SendFrame(message); |
360 else | 360 else |
361 pending_messages_.push_back(message); | 361 pending_messages_.push_back(message); |
362 } | 362 } |
363 | 363 |
364 void AgentHostDelegate::OnSocketOpened() { | 364 void AgentHostDelegate::OnSocketOpened() { |
| 365 if (detached_) { |
| 366 web_socket_->Disconnect(); |
| 367 return; |
| 368 } |
| 369 |
365 socket_opened_ = true; | 370 socket_opened_ = true; |
366 for (std::vector<std::string>::iterator it = pending_messages_.begin(); | 371 for (std::vector<std::string>::iterator it = pending_messages_.begin(); |
367 it != pending_messages_.end(); ++it) { | 372 it != pending_messages_.end(); ++it) { |
368 SendMessageToBackend(*it); | 373 SendMessageToBackend(*it); |
369 } | 374 } |
370 pending_messages_.clear(); | 375 pending_messages_.clear(); |
371 } | 376 } |
372 | 377 |
373 void AgentHostDelegate::OnFrameRead(const std::string& message) { | 378 void AgentHostDelegate::OnFrameRead(const std::string& message) { |
374 if (proxy_) | 379 if (proxy_) |
375 proxy_->DispatchOnClientHost(message); | 380 proxy_->DispatchOnClientHost(message); |
376 } | 381 } |
377 | 382 |
378 void AgentHostDelegate::OnSocketClosed() { | 383 void AgentHostDelegate::OnSocketClosed(bool closed_by_device) { |
379 if (proxy_) | 384 if (proxy_ && closed_by_device) |
380 proxy_->ConnectionClosed(); | 385 proxy_->ConnectionClosed(); |
381 } | 386 } |
382 | 387 |
383 //// RemotePageTarget ---------------------------------------------- | 388 //// RemotePageTarget ---------------------------------------------- |
384 | 389 |
385 class RemotePageTarget : public DevToolsTargetImpl, | 390 class RemotePageTarget : public DevToolsTargetImpl, |
386 public DevToolsAndroidBridge::RemotePage { | 391 public DevToolsAndroidBridge::RemotePage { |
387 public: | 392 public: |
388 RemotePageTarget(scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 393 RemotePageTarget(scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
389 const base::DictionaryValue& value); | 394 const base::DictionaryValue& value); |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 InnerOpen(url, base::Bind(&RemoteBrowser::RespondToOpenOnUIThread, | 607 InnerOpen(url, base::Bind(&RemoteBrowser::RespondToOpenOnUIThread, |
603 this, callback)); | 608 this, callback)); |
604 } | 609 } |
605 | 610 |
606 scoped_refptr<content::DevToolsAgentHost> | 611 scoped_refptr<content::DevToolsAgentHost> |
607 DevToolsAndroidBridge::RemoteBrowser::GetAgentHost() { | 612 DevToolsAndroidBridge::RemoteBrowser::GetAgentHost() { |
608 return AgentHostDelegate::GetOrCreateAgentHost( | 613 return AgentHostDelegate::GetOrCreateAgentHost( |
609 "adb:" + device_->serial() + ":" + socket_, this, kBrowserTargetSocket); | 614 "adb:" + device_->serial() + ":" + socket_, this, kBrowserTargetSocket); |
610 } | 615 } |
611 | 616 |
612 DevToolsAndroidBridge::AndroidWebSocket* | 617 scoped_refptr<DevToolsAndroidBridge::AndroidWebSocket> |
613 DevToolsAndroidBridge::RemoteBrowser::CreateWebSocket( | 618 DevToolsAndroidBridge::RemoteBrowser::CreateWebSocket( |
614 const std::string& url, | 619 const std::string& url, |
615 DevToolsAndroidBridge::AndroidWebSocket::Delegate* delegate) { | 620 DevToolsAndroidBridge::AndroidWebSocket::Delegate* delegate) { |
616 return device_->CreateWebSocket(socket_, url, delegate); | 621 return device_->CreateWebSocket(socket_, url, delegate); |
617 } | 622 } |
618 | 623 |
619 void DevToolsAndroidBridge::RemoteBrowser::RespondToOpenOnUIThread( | 624 void DevToolsAndroidBridge::RemoteBrowser::RespondToOpenOnUIThread( |
620 const DevToolsAndroidBridge::RemotePageCallback& callback, | 625 const DevToolsAndroidBridge::RemotePageCallback& callback, |
621 int result, | 626 int result, |
622 const std::string& response) { | 627 const std::string& response) { |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
885 bool enabled; | 890 bool enabled; |
886 if (pref_value->GetAsBoolean(&enabled) && enabled) { | 891 if (pref_value->GetAsBoolean(&enabled) && enabled) { |
887 device_providers.push_back(new UsbDeviceProvider(profile_)); | 892 device_providers.push_back(new UsbDeviceProvider(profile_)); |
888 } | 893 } |
889 device_manager_->SetDeviceProviders(device_providers); | 894 device_manager_->SetDeviceProviders(device_providers); |
890 if (!device_list_listeners_.empty()) { | 895 if (!device_list_listeners_.empty()) { |
891 StopDeviceListPolling(); | 896 StopDeviceListPolling(); |
892 StartDeviceListPolling(); | 897 StartDeviceListPolling(); |
893 } | 898 } |
894 } | 899 } |
OLD | NEW |