OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/devtools_adb_bridge.h" | 5 #include "chrome/browser/devtools/devtools_adb_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" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/singleton.h" | 17 #include "base/memory/singleton.h" |
18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
22 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
23 #include "base/values.h" | 23 #include "base/values.h" |
24 #include "chrome/browser/devtools/adb/android_rsa.h" | 24 #include "chrome/browser/devtools/adb/android_rsa.h" |
25 #include "chrome/browser/devtools/adb_client_socket.h" | 25 #include "chrome/browser/devtools/adb_client_socket.h" |
26 #include "chrome/browser/devtools/adb_web_socket.h" | 26 #include "chrome/browser/devtools/adb_web_socket.h" |
27 #include "chrome/browser/devtools/devtools_protocol.h" | |
27 #include "chrome/browser/devtools/devtools_window.h" | 28 #include "chrome/browser/devtools/devtools_window.h" |
28 #include "chrome/browser/devtools/tethering_adb_filter.h" | 29 #include "chrome/browser/devtools/tethering_adb_filter.h" |
29 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
30 #include "chrome/common/chrome_switches.h" | 31 #include "chrome/common/chrome_switches.h" |
31 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" | 32 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" |
32 #include "content/public/browser/devtools_agent_host.h" | 33 #include "content/public/browser/devtools_agent_host.h" |
33 #include "content/public/browser/devtools_client_host.h" | 34 #include "content/public/browser/devtools_client_host.h" |
34 #include "content/public/browser/devtools_external_agent_proxy.h" | 35 #include "content/public/browser/devtools_external_agent_proxy.h" |
35 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" | 36 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" |
36 #include "content/public/browser/devtools_manager.h" | 37 #include "content/public/browser/devtools_manager.h" |
37 #include "crypto/rsa_private_key.h" | 38 #include "crypto/rsa_private_key.h" |
38 #include "net/base/net_errors.h" | 39 #include "net/base/net_errors.h" |
39 | 40 |
40 using content::BrowserThread; | 41 using content::BrowserThread; |
42 using chrome::DevToolsProtocol; | |
41 | 43 |
42 namespace { | 44 namespace { |
43 | 45 |
44 static const char kDevToolsAdbBridgeThreadName[] = "Chrome_DevToolsADBThread"; | 46 static const char kDevToolsAdbBridgeThreadName[] = "Chrome_DevToolsADBThread"; |
45 static const char kHostDevicesCommand[] = "host:devices"; | 47 static const char kHostDevicesCommand[] = "host:devices"; |
46 static const char kHostTransportCommand[] = "host:transport:%s|%s"; | 48 static const char kHostTransportCommand[] = "host:transport:%s|%s"; |
47 static const char kLocalAbstractCommand[] = "localabstract:%s"; | 49 static const char kLocalAbstractCommand[] = "localabstract:%s"; |
48 static const char kDeviceModelCommand[] = "shell:getprop ro.product.model"; | 50 static const char kDeviceModelCommand[] = "shell:getprop ro.product.model"; |
49 static const char kUnknownModel[] = "Unknown"; | 51 static const char kUnknownModel[] = "Unknown"; |
50 static const char kOpenedUnixSocketsCommand[] = "shell:cat /proc/net/unix"; | 52 static const char kOpenedUnixSocketsCommand[] = "shell:cat /proc/net/unix"; |
51 | 53 |
52 static const char kPageListRequest[] = "GET /json HTTP/1.1\r\n\r\n"; | 54 static const char kPageListRequest[] = "GET /json HTTP/1.1\r\n\r\n"; |
53 static const char kVersionRequest[] = "GET /json/version HTTP/1.1\r\n\r\n"; | 55 static const char kVersionRequest[] = "GET /json/version HTTP/1.1\r\n\r\n"; |
56 static const char kClosePageRequest[] = "GET /json/close/%s HTTP/1.1\r\n\r\n"; | |
57 static const char kNewPageRequest[] = "GET /json/new HTTP/1.1\r\n\r\n"; | |
54 const int kAdbPort = 5037; | 58 const int kAdbPort = 5037; |
55 const int kBufferSize = 16 * 1024; | 59 const int kBufferSize = 16 * 1024; |
56 const int kAdbPollingIntervalMs = 1000; | 60 const int kAdbPollingIntervalMs = 1000; |
57 | 61 |
58 typedef DevToolsAdbBridge::Callback Callback; | 62 typedef DevToolsAdbBridge::Callback Callback; |
59 typedef std::vector<scoped_refptr<DevToolsAdbBridge::AndroidDevice> > | 63 typedef std::vector<scoped_refptr<DevToolsAdbBridge::AndroidDevice> > |
60 AndroidDevices; | 64 AndroidDevices; |
61 typedef base::Callback<void(const AndroidDevices&)> AndroidDevicesCallback; | 65 typedef base::Callback<void(const AndroidDevices&)> AndroidDevicesCallback; |
62 | 66 |
63 class AdbDeviceImpl : public DevToolsAdbBridge::AndroidDevice { | 67 class AdbDeviceImpl : public DevToolsAdbBridge::AndroidDevice { |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
375 } | 379 } |
376 | 380 |
377 scoped_refptr<DevToolsAdbBridge> bridge_; | 381 scoped_refptr<DevToolsAdbBridge> bridge_; |
378 Callback callback_; | 382 Callback callback_; |
379 AndroidDevices devices_; | 383 AndroidDevices devices_; |
380 std::vector<std::string> sockets_; | 384 std::vector<std::string> sockets_; |
381 std::map<std::string, std::string> socket_to_package_; | 385 std::map<std::string, std::string> socket_to_package_; |
382 scoped_ptr<DevToolsAdbBridge::RemoteDevices> remote_devices_; | 386 scoped_ptr<DevToolsAdbBridge::RemoteDevices> remote_devices_; |
383 }; | 387 }; |
384 | 388 |
389 // AdbProtocolCommand --------------------------------------------------------- | |
390 | |
391 class AdbProtocolCommand : public AdbWebSocket::Delegate { | |
392 public: | |
393 AdbProtocolCommand( | |
394 scoped_refptr<DevToolsAdbBridge> bridge_, | |
395 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, | |
396 const std::string& socket_name, | |
397 const std::string& debug_url, | |
398 const std::string& command); | |
399 | |
400 private: | |
401 virtual void OnSocketOpened() OVERRIDE; | |
402 virtual void OnFrameRead(const std::string& message) OVERRIDE; | |
403 virtual void OnSocketClosed(bool closed_by_device) OVERRIDE; | |
404 virtual bool ProcessIncomingMessage(const std::string& message) OVERRIDE; | |
405 | |
406 scoped_refptr<DevToolsAdbBridge> bridge_; | |
407 const std::string command_; | |
408 scoped_refptr<AdbWebSocket> web_socket_; | |
409 | |
410 DISALLOW_COPY_AND_ASSIGN(AdbProtocolCommand); | |
411 }; | |
412 | |
413 AdbProtocolCommand::AdbProtocolCommand( | |
414 scoped_refptr<DevToolsAdbBridge> bridge, | |
415 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, | |
416 const std::string& socket_name, | |
417 const std::string& debug_url, | |
418 const std::string& command) | |
419 : bridge_(bridge), | |
420 command_(command) { | |
421 web_socket_ = new AdbWebSocket( | |
422 device, socket_name, debug_url, bridge_->GetAdbMessageLoop(), this); | |
423 } | |
424 | |
425 void AdbProtocolCommand::OnSocketOpened() { | |
426 web_socket_->SendFrame(command_); | |
427 web_socket_->Disconnect(); | |
428 } | |
429 | |
430 void AdbProtocolCommand::OnFrameRead(const std::string& message) {} | |
431 | |
432 void AdbProtocolCommand::OnSocketClosed(bool closed_by_device) { | |
433 delete this; | |
434 } | |
435 | |
436 bool AdbProtocolCommand::ProcessIncomingMessage(const std::string& message) { | |
437 return false; | |
438 } | |
439 | |
385 } // namespace | 440 } // namespace |
386 | 441 |
387 const char kDevToolsChannelNameFormat[] = "%s_devtools_remote"; | 442 const char kDevToolsChannelNameFormat[] = "%s_devtools_remote"; |
388 | 443 |
389 class AgentHostDelegate; | 444 class AgentHostDelegate; |
390 | 445 |
391 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates; | 446 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates; |
392 | 447 |
393 base::LazyInstance<AgentHostDelegates>::Leaky g_host_delegates = | 448 base::LazyInstance<AgentHostDelegates>::Leaky g_host_delegates = |
394 LAZY_INSTANCE_INITIALIZER; | 449 LAZY_INSTANCE_INITIALIZER; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
436 } | 491 } |
437 | 492 |
438 void DevToolsAdbBridge::AndroidDevice::HttpQuery( | 493 void DevToolsAdbBridge::AndroidDevice::HttpQuery( |
439 const std::string& la_name, | 494 const std::string& la_name, |
440 const std::string& request, | 495 const std::string& request, |
441 const CommandCallback& callback) { | 496 const CommandCallback& callback) { |
442 OpenSocket(la_name, base::Bind(&AndroidDevice::OnHttpSocketOpened, this, | 497 OpenSocket(la_name, base::Bind(&AndroidDevice::OnHttpSocketOpened, this, |
443 request, callback)); | 498 request, callback)); |
444 } | 499 } |
445 | 500 |
446 void DevToolsAdbBridge::AndroidDevice::HttpQuery( | 501 void DevToolsAdbBridge::AndroidDevice::HttpConnect( |
447 const std::string& la_name, | 502 const std::string& la_name, |
448 const std::string& request, | 503 const std::string& request, |
449 const SocketCallback& callback) { | 504 const SocketCallback& callback) { |
450 OpenSocket(la_name, base::Bind(&AndroidDevice::OnHttpSocketOpened2, this, | 505 OpenSocket(la_name, base::Bind(&AndroidDevice::OnHttpSocketOpened2, this, |
451 request, callback)); | 506 request, callback)); |
452 } | 507 } |
453 | 508 |
454 DevToolsAdbBridge::AndroidDevice::~AndroidDevice() { | 509 DevToolsAdbBridge::AndroidDevice::~AndroidDevice() { |
455 } | 510 } |
456 | 511 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
576 else | 631 else |
577 debug_url_ = ""; | 632 debug_url_ = ""; |
578 | 633 |
579 size_t ws_param = frontend_url_.find("?ws"); | 634 size_t ws_param = frontend_url_.find("?ws"); |
580 if (ws_param != std::string::npos) | 635 if (ws_param != std::string::npos) |
581 frontend_url_ = frontend_url_.substr(0, ws_param); | 636 frontend_url_ = frontend_url_.substr(0, ws_param); |
582 if (frontend_url_.find("http:") == 0) | 637 if (frontend_url_.find("http:") == 0) |
583 frontend_url_ = "https:" + frontend_url_.substr(5); | 638 frontend_url_ = "https:" + frontend_url_.substr(5); |
584 | 639 |
585 global_id_ = base::StringPrintf( | 640 global_id_ = base::StringPrintf( |
586 "%s:%s:%s", device->serial().c_str(), socket_.c_str(), id_.c_str()); | 641 "page:%s:%s:%s", device->serial().c_str(), socket_.c_str(), id_.c_str()); |
587 } | 642 } |
588 | 643 |
589 void DevToolsAdbBridge::RemotePage::Inspect(Profile* profile) { | 644 void DevToolsAdbBridge::RemotePage::Inspect(Profile* profile) { |
590 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 645 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
591 AgentHostDelegates::iterator it = | 646 AgentHostDelegates::iterator it = |
592 g_host_delegates.Get().find(global_id()); | 647 g_host_delegates.Get().find(global_id()); |
593 if (it != g_host_delegates.Get().end()) | 648 if (it != g_host_delegates.Get().end()) |
594 it->second->OpenFrontend(); | 649 it->second->OpenFrontend(); |
595 else if (!debug_url_.empty()) | 650 else if (!debug_url_.empty()) |
596 new AgentHostDelegate( | 651 new AgentHostDelegate( |
597 global_id_, device_, socket_, debug_url_, | 652 global_id_, device_, socket_, debug_url_, |
598 frontend_url_, bridge_->GetAdbMessageLoop(), profile); | 653 frontend_url_, bridge_->GetAdbMessageLoop(), profile); |
599 } | 654 } |
600 | 655 |
656 static void Noop(int, const std::string&) {} | |
657 | |
658 void DevToolsAdbBridge::RemotePage::Close() { | |
659 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
660 if (attached()) | |
661 return; | |
662 std::string request = base::StringPrintf(kClosePageRequest, id_.c_str()); | |
663 bridge_->GetAdbMessageLoop()->PostTask(FROM_HERE, | |
664 base::Bind(&AndroidDevice::HttpQuery, | |
665 device_, socket_, request, base::Bind(&Noop))); | |
666 } | |
667 | |
668 void DevToolsAdbBridge::RemotePage::Reload() { | |
669 SendProtocolCommand("Page.reload", NULL); | |
pfeldman
2013/08/07 09:48:12
kPageReloadCommand
Vladislav Kaznacheev
2013/08/07 12:11:05
Done. Also kPageNavigateCommand
| |
670 } | |
671 | |
672 void DevToolsAdbBridge::RemotePage::SendProtocolCommand( | |
673 const std::string& method, | |
674 base::DictionaryValue* params) { | |
675 if (attached()) | |
676 return; | |
677 DevToolsProtocol::Command command(1, method, params); | |
678 new AdbProtocolCommand( | |
679 bridge_, device_, socket_, debug_url_, command.Serialize()); | |
680 } | |
681 | |
601 DevToolsAdbBridge::RemotePage::~RemotePage() { | 682 DevToolsAdbBridge::RemotePage::~RemotePage() { |
602 } | 683 } |
603 | 684 |
604 DevToolsAdbBridge::RemoteBrowser::RemoteBrowser( | 685 DevToolsAdbBridge::RemoteBrowser::RemoteBrowser( |
605 scoped_refptr<DevToolsAdbBridge> bridge, | 686 scoped_refptr<DevToolsAdbBridge> bridge, |
606 scoped_refptr<AndroidDevice> device, | 687 scoped_refptr<AndroidDevice> device, |
607 const std::string& socket, | 688 const std::string& socket, |
608 const std::string& name) | 689 const std::string& name) |
609 : bridge_(bridge), | 690 : bridge_(bridge), |
610 device_(device), | 691 device_(device), |
611 socket_(socket), | 692 socket_(socket), |
612 name_(name) { | 693 name_(name) { |
694 global_id_ = base::StringPrintf( | |
695 "browser:%s:%s", device_->serial().c_str(), socket_.c_str()); | |
696 } | |
697 | |
698 void DevToolsAdbBridge::RemoteBrowser::Open(const std::string& url) { | |
699 bridge_->GetAdbMessageLoop()->PostTask(FROM_HERE, | |
700 base::Bind(&AndroidDevice::HttpQuery, | |
701 device_, socket_, kNewPageRequest, | |
702 base::Bind(&RemoteBrowser::PageCreatedOnHandlerThread, this, url))); | |
703 } | |
704 | |
705 void DevToolsAdbBridge::RemoteBrowser::PageCreatedOnHandlerThread( | |
706 const std::string& url, int result, const std::string& response) { | |
707 if (result < 0) | |
708 return; | |
709 BrowserThread::PostTask( | |
710 BrowserThread::UI, FROM_HERE, | |
711 base::Bind(&RemoteBrowser::PageCreatedOnUIThread, this, response, url)); | |
712 } | |
713 | |
714 void DevToolsAdbBridge::RemoteBrowser::PageCreatedOnUIThread( | |
715 const std::string& response, const std::string& url) { | |
716 scoped_ptr<base::Value> value(base::JSONReader::Read(response)); | |
717 base::DictionaryValue* dict; | |
718 if (value && value->GetAsDictionary(&dict)) { | |
719 scoped_refptr<RemotePage> new_page = | |
720 new RemotePage(bridge_, device_, socket_, *dict); | |
721 base::DictionaryValue params; | |
722 params.SetString("url", url); | |
pfeldman
2013/08/07 09:48:12
kUrl
Vladislav Kaznacheev
2013/08/07 12:11:05
Done.
| |
723 new_page->SendProtocolCommand("Page.navigate", ¶ms); | |
724 } | |
613 } | 725 } |
614 | 726 |
615 DevToolsAdbBridge::RemoteBrowser::~RemoteBrowser() { | 727 DevToolsAdbBridge::RemoteBrowser::~RemoteBrowser() { |
616 } | 728 } |
617 | 729 |
618 DevToolsAdbBridge::RemoteDevice::RemoteDevice( | 730 DevToolsAdbBridge::RemoteDevice::RemoteDevice( |
619 scoped_refptr<DevToolsAdbBridge> bridge, | 731 scoped_refptr<DevToolsAdbBridge> bridge, |
620 scoped_refptr<AndroidDevice> device) | 732 scoped_refptr<AndroidDevice> device) |
621 : bridge_(bridge), | 733 : bridge_(bridge), |
622 device_(device) { | 734 device_(device) { |
735 global_id_ = base::StringPrintf("device:%s", device_->serial().c_str()); | |
623 } | 736 } |
624 | 737 |
625 DevToolsAdbBridge::RemoteDevice::~RemoteDevice() { | 738 DevToolsAdbBridge::RemoteDevice::~RemoteDevice() { |
626 } | 739 } |
627 | 740 |
628 | 741 |
629 DevToolsAdbBridge::RefCountedAdbThread* | 742 DevToolsAdbBridge::RefCountedAdbThread* |
630 DevToolsAdbBridge::RefCountedAdbThread::instance_ = NULL; | 743 DevToolsAdbBridge::RefCountedAdbThread::instance_ = NULL; |
631 | 744 |
632 // static | 745 // static |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
781 | 894 |
782 if (listeners_.empty()) | 895 if (listeners_.empty()) |
783 return; | 896 return; |
784 | 897 |
785 BrowserThread::PostDelayedTask( | 898 BrowserThread::PostDelayedTask( |
786 BrowserThread::UI, | 899 BrowserThread::UI, |
787 FROM_HERE, | 900 FROM_HERE, |
788 base::Bind(&DevToolsAdbBridge::RequestRemoteDevices, this), | 901 base::Bind(&DevToolsAdbBridge::RequestRemoteDevices, this), |
789 base::TimeDelta::FromMilliseconds(kAdbPollingIntervalMs)); | 902 base::TimeDelta::FromMilliseconds(kAdbPollingIntervalMs)); |
790 } | 903 } |
OLD | NEW |