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 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 void OnFrameRead(const std::string& message) override; | 301 void OnFrameRead(const std::string& message) override; |
302 void OnSocketClosed() override; | 302 void OnSocketClosed() override; |
303 | 303 |
304 std::string id_; | 304 std::string id_; |
305 scoped_refptr<DevToolsAndroidBridge> bridge_; | 305 scoped_refptr<DevToolsAndroidBridge> bridge_; |
306 BrowserId browser_id_; | 306 BrowserId browser_id_; |
307 std::string debug_url_; | 307 std::string debug_url_; |
308 bool socket_opened_; | 308 bool socket_opened_; |
309 bool is_web_view_; | 309 bool is_web_view_; |
310 std::vector<std::string> pending_messages_; | 310 std::vector<std::string> pending_messages_; |
| 311 scoped_refptr<AndroidDeviceManager::Device> device_; |
311 scoped_ptr<AndroidDeviceManager::AndroidWebSocket> web_socket_; | 312 scoped_ptr<AndroidDeviceManager::AndroidWebSocket> web_socket_; |
312 content::DevToolsAgentHost* agent_host_; | 313 content::DevToolsAgentHost* agent_host_; |
313 content::DevToolsExternalAgentProxy* proxy_; | 314 content::DevToolsExternalAgentProxy* proxy_; |
314 DISALLOW_COPY_AND_ASSIGN(AgentHostDelegate); | 315 DISALLOW_COPY_AND_ASSIGN(AgentHostDelegate); |
315 }; | 316 }; |
316 | 317 |
317 // static | 318 // static |
318 scoped_refptr<content::DevToolsAgentHost> | 319 scoped_refptr<content::DevToolsAgentHost> |
319 DevToolsAndroidBridge::AgentHostDelegate::GetOrCreateAgentHost( | 320 DevToolsAndroidBridge::AgentHostDelegate::GetOrCreateAgentHost( |
320 scoped_refptr<DevToolsAndroidBridge> bridge, | 321 scoped_refptr<DevToolsAndroidBridge> bridge, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 | 355 |
355 DevToolsAndroidBridge::AgentHostDelegate::~AgentHostDelegate() { | 356 DevToolsAndroidBridge::AgentHostDelegate::~AgentHostDelegate() { |
356 bridge_->host_delegates_.erase(id_); | 357 bridge_->host_delegates_.erase(id_); |
357 } | 358 } |
358 | 359 |
359 void DevToolsAndroidBridge::AgentHostDelegate::Attach( | 360 void DevToolsAndroidBridge::AgentHostDelegate::Attach( |
360 content::DevToolsExternalAgentProxy* proxy) { | 361 content::DevToolsExternalAgentProxy* proxy) { |
361 proxy_ = proxy; | 362 proxy_ = proxy; |
362 content::RecordAction(base::UserMetricsAction(is_web_view_ ? | 363 content::RecordAction(base::UserMetricsAction(is_web_view_ ? |
363 "DevTools_InspectAndroidWebView" : "DevTools_InspectAndroidPage")); | 364 "DevTools_InspectAndroidWebView" : "DevTools_InspectAndroidPage")); |
| 365 |
| 366 // Retain the device so it's not released until AgentHost is detached. |
| 367 device_ = bridge_->FindDevice(browser_id_.first); |
| 368 if (!device_.get()) |
| 369 return; |
| 370 |
364 web_socket_.reset( | 371 web_socket_.reset( |
365 bridge_->CreateWebSocket(browser_id_, debug_url_, this)); | 372 device_->CreateWebSocket(browser_id_.second, debug_url_, this)); |
366 } | 373 } |
367 | 374 |
368 void DevToolsAndroidBridge::AgentHostDelegate::Detach() { | 375 void DevToolsAndroidBridge::AgentHostDelegate::Detach() { |
369 web_socket_.reset(); | 376 web_socket_.reset(); |
| 377 device_ = nullptr; |
370 } | 378 } |
371 | 379 |
372 void DevToolsAndroidBridge::AgentHostDelegate::SendMessageToBackend( | 380 void DevToolsAndroidBridge::AgentHostDelegate::SendMessageToBackend( |
373 const std::string& message) { | 381 const std::string& message) { |
374 if (socket_opened_) | 382 if (socket_opened_) |
375 web_socket_->SendFrame(message); | 383 web_socket_->SendFrame(message); |
376 else | 384 else |
377 pending_messages_.push_back(message); | 385 pending_messages_.push_back(message); |
378 } | 386 } |
379 | 387 |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 DevToolsTargetImpl* | 595 DevToolsTargetImpl* |
588 DevToolsAndroidBridge::CreatePageTarget(scoped_refptr<RemotePage> page) { | 596 DevToolsAndroidBridge::CreatePageTarget(scoped_refptr<RemotePage> page) { |
589 return new RemotePageTarget(this, page->browser_id_, *page->dict_, | 597 return new RemotePageTarget(this, page->browser_id_, *page->dict_, |
590 page->is_web_view_); | 598 page->is_web_view_); |
591 } | 599 } |
592 | 600 |
593 void DevToolsAndroidBridge::SendJsonRequest( | 601 void DevToolsAndroidBridge::SendJsonRequest( |
594 const BrowserId& browser_id, | 602 const BrowserId& browser_id, |
595 const std::string& request, | 603 const std::string& request, |
596 const JsonRequestCallback& callback) { | 604 const JsonRequestCallback& callback) { |
597 DeviceMap::iterator it = device_map_.find(browser_id.first); | 605 scoped_refptr<AndroidDeviceManager::Device> device( |
598 if (it == device_map_.end()) { | 606 FindDevice(browser_id.first)); |
| 607 if (!device.get()) { |
599 callback.Run(net::ERR_FAILED, std::string()); | 608 callback.Run(net::ERR_FAILED, std::string()); |
600 return; | 609 return; |
601 } | 610 } |
602 it->second->SendJsonRequest(browser_id.second, request, callback); | 611 device->SendJsonRequest(browser_id.second, request, callback); |
603 } | 612 } |
604 | 613 |
605 void DevToolsAndroidBridge::SendProtocolCommand( | 614 void DevToolsAndroidBridge::SendProtocolCommand( |
606 const BrowserId& browser_id, | 615 const BrowserId& browser_id, |
607 const std::string& debug_url, | 616 const std::string& debug_url, |
608 const std::string& method, | 617 const std::string& method, |
609 base::DictionaryValue* params, | 618 base::DictionaryValue* params, |
610 const base::Closure callback) { | 619 const base::Closure callback) { |
611 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 620 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
612 if (debug_url.empty()) | 621 if (debug_url.empty()) |
613 return; | 622 return; |
614 DeviceMap::iterator it = device_map_.find(browser_id.first); | 623 scoped_refptr<AndroidDeviceManager::Device> device( |
615 if (it == device_map_.end()) { | 624 FindDevice(browser_id.first)); |
| 625 if (!device.get()) { |
616 callback.Run(); | 626 callback.Run(); |
617 return; | 627 return; |
618 } | 628 } |
619 DevToolsProtocol::Command command(1, method, params); | 629 DevToolsProtocol::Command command(1, method, params); |
620 new ProtocolCommand(it->second, browser_id.second, debug_url, | 630 new ProtocolCommand(device, browser_id.second, debug_url, |
621 command.Serialize(), callback); | 631 command.Serialize(), callback); |
622 } | 632 } |
623 | 633 |
624 scoped_refptr<content::DevToolsAgentHost> | 634 scoped_refptr<content::DevToolsAgentHost> |
625 DevToolsAndroidBridge::GetBrowserAgentHost( | 635 DevToolsAndroidBridge::GetBrowserAgentHost( |
626 scoped_refptr<RemoteBrowser> browser) { | 636 scoped_refptr<RemoteBrowser> browser) { |
627 return AgentHostDelegate::GetOrCreateAgentHost( | 637 return AgentHostDelegate::GetOrCreateAgentHost( |
628 this, | 638 this, |
629 "adb:" + browser->serial() + ":" + browser->socket(), | 639 "adb:" + browser->serial() + ":" + browser->socket(), |
630 browser->browser_id_, | 640 browser->browser_id_, |
631 kBrowserTargetSocket, | 641 kBrowserTargetSocket, |
632 browser->IsWebView()); | 642 browser->IsWebView()); |
633 } | 643 } |
634 | 644 |
635 AndroidDeviceManager::AndroidWebSocket* | 645 scoped_refptr<AndroidDeviceManager::Device> DevToolsAndroidBridge::FindDevice( |
636 DevToolsAndroidBridge::CreateWebSocket( | 646 const std::string& serial) { |
637 const BrowserId& browser_id, | 647 DeviceMap::iterator it = device_map_.find(serial); |
638 const std::string& url, | 648 return it == device_map_.end() ? nullptr : it->second; |
639 AndroidDeviceManager::AndroidWebSocket::Delegate* delegate) { | |
640 DeviceMap::iterator it = device_map_.find(browser_id.first); | |
641 if (it == device_map_.end()) | |
642 return NULL; | |
643 | |
644 return it->second->CreateWebSocket(browser_id.second, url, delegate); | |
645 } | 649 } |
646 | 650 |
647 void DevToolsAndroidBridge::RespondToOpenOnUIThread( | 651 void DevToolsAndroidBridge::RespondToOpenOnUIThread( |
648 scoped_refptr<RemoteBrowser> browser, | 652 scoped_refptr<RemoteBrowser> browser, |
649 const RemotePageCallback& callback, | 653 const RemotePageCallback& callback, |
650 int result, | 654 int result, |
651 const std::string& response) { | 655 const std::string& response) { |
652 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 656 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
653 if (result < 0) { | 657 if (result < 0) { |
654 callback.Run(NULL); | 658 callback.Run(NULL); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 | 757 |
754 DevToolsAndroidBridge::RemoteDevice::~RemoteDevice() { | 758 DevToolsAndroidBridge::RemoteDevice::~RemoteDevice() { |
755 } | 759 } |
756 | 760 |
757 // DevToolsAndroidBridge ------------------------------------------------------ | 761 // DevToolsAndroidBridge ------------------------------------------------------ |
758 | 762 |
759 DevToolsAndroidBridge::DevToolsAndroidBridge(Profile* profile) | 763 DevToolsAndroidBridge::DevToolsAndroidBridge(Profile* profile) |
760 : profile_(profile), | 764 : profile_(profile), |
761 device_manager_(AndroidDeviceManager::Create()), | 765 device_manager_(AndroidDeviceManager::Create()), |
762 task_scheduler_(base::Bind(&DevToolsAndroidBridge::ScheduleTaskDefault)), | 766 task_scheduler_(base::Bind(&DevToolsAndroidBridge::ScheduleTaskDefault)), |
763 port_forwarding_controller_(new PortForwardingController(profile)) { | 767 port_forwarding_controller_(new PortForwardingController(profile, this)) { |
764 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 768 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
765 pref_change_registrar_.Init(profile_->GetPrefs()); | 769 pref_change_registrar_.Init(profile_->GetPrefs()); |
766 pref_change_registrar_.Add(prefs::kDevToolsDiscoverUsbDevicesEnabled, | 770 pref_change_registrar_.Add(prefs::kDevToolsDiscoverUsbDevicesEnabled, |
767 base::Bind(&DevToolsAndroidBridge::CreateDeviceProviders, | 771 base::Bind(&DevToolsAndroidBridge::CreateDeviceProviders, |
768 base::Unretained(this))); | 772 base::Unretained(this))); |
769 CreateDeviceProviders(); | 773 CreateDeviceProviders(); |
770 } | 774 } |
771 | 775 |
772 void DevToolsAndroidBridge::AddDeviceListListener( | 776 void DevToolsAndroidBridge::AddDeviceListListener( |
773 DeviceListListener* listener) { | 777 DeviceListListener* listener) { |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 for (const auto& pair : complete_devices) { | 878 for (const auto& pair : complete_devices) { |
875 device_map_[pair.first->serial()] = pair.first; | 879 device_map_[pair.first->serial()] = pair.first; |
876 remote_devices.push_back(pair.second); | 880 remote_devices.push_back(pair.second); |
877 } | 881 } |
878 | 882 |
879 DeviceListListeners copy(device_list_listeners_); | 883 DeviceListListeners copy(device_list_listeners_); |
880 for (DeviceListListeners::iterator it = copy.begin(); it != copy.end(); ++it) | 884 for (DeviceListListeners::iterator it = copy.begin(); it != copy.end(); ++it) |
881 (*it)->DeviceListChanged(remote_devices); | 885 (*it)->DeviceListChanged(remote_devices); |
882 | 886 |
883 ForwardingStatus status = | 887 ForwardingStatus status = |
884 port_forwarding_controller_->DeviceListChanged(complete_devices); | 888 port_forwarding_controller_->DeviceListChanged(remote_devices); |
885 PortForwardingListeners forwarding_listeners(port_forwarding_listeners_); | 889 PortForwardingListeners forwarding_listeners(port_forwarding_listeners_); |
886 for (PortForwardingListeners::iterator it = forwarding_listeners.begin(); | 890 for (PortForwardingListeners::iterator it = forwarding_listeners.begin(); |
887 it != forwarding_listeners.end(); ++it) { | 891 it != forwarding_listeners.end(); ++it) { |
888 (*it)->PortStatusChanged(status); | 892 (*it)->PortStatusChanged(status); |
889 } | 893 } |
890 | 894 |
891 if (!NeedsDeviceListPolling()) | 895 if (!NeedsDeviceListPolling()) |
892 return; | 896 return; |
893 | 897 |
894 task_scheduler_.Run( | 898 task_scheduler_.Run( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
960 bool enabled; | 964 bool enabled; |
961 if (pref_value->GetAsBoolean(&enabled) && enabled) { | 965 if (pref_value->GetAsBoolean(&enabled) && enabled) { |
962 device_providers.push_back(new UsbDeviceProvider(profile_)); | 966 device_providers.push_back(new UsbDeviceProvider(profile_)); |
963 } | 967 } |
964 device_manager_->SetDeviceProviders(device_providers); | 968 device_manager_->SetDeviceProviders(device_providers); |
965 if (NeedsDeviceListPolling()) { | 969 if (NeedsDeviceListPolling()) { |
966 StopDeviceListPolling(); | 970 StopDeviceListPolling(); |
967 StartDeviceListPolling(); | 971 StartDeviceListPolling(); |
968 } | 972 } |
969 } | 973 } |
OLD | NEW |