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; |
41 | 42 |
42 namespace { | 43 namespace { |
43 | 44 |
44 static const char kDevToolsAdbBridgeThreadName[] = "Chrome_DevToolsADBThread"; | 45 static const char kDevToolsAdbBridgeThreadName[] = "Chrome_DevToolsADBThread"; |
45 static const char kHostDevicesCommand[] = "host:devices"; | 46 static const char kHostDevicesCommand[] = "host:devices"; |
46 static const char kHostTransportCommand[] = "host:transport:%s|%s"; | 47 static const char kHostTransportCommand[] = "host:transport:%s|%s"; |
47 static const char kLocalAbstractCommand[] = "localabstract:%s"; | 48 static const char kLocalAbstractCommand[] = "localabstract:%s"; |
48 static const char kDeviceModelCommand[] = "shell:getprop ro.product.model"; | 49 static const char kDeviceModelCommand[] = "shell:getprop ro.product.model"; |
49 static const char kUnknownModel[] = "Unknown"; | 50 static const char kUnknownModel[] = "Unknown"; |
50 static const char kOpenedUnixSocketsCommand[] = "shell:cat /proc/net/unix"; | 51 static const char kOpenedUnixSocketsCommand[] = "shell:cat /proc/net/unix"; |
51 | 52 |
52 static const char kPageListRequest[] = "GET /json HTTP/1.1\r\n\r\n"; | 53 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"; | 54 static const char kVersionRequest[] = "GET /json/version HTTP/1.1\r\n\r\n"; |
| 55 static const char kClosePageRequest[] = "GET /json/close/%s HTTP/1.1\r\n\r\n"; |
| 56 static const char kNewPageRequest[] = "GET /json/new HTTP/1.1\r\n\r\n"; |
54 const int kAdbPort = 5037; | 57 const int kAdbPort = 5037; |
55 const int kBufferSize = 16 * 1024; | 58 const int kBufferSize = 16 * 1024; |
56 const int kAdbPollingIntervalMs = 1000; | 59 const int kAdbPollingIntervalMs = 1000; |
57 | 60 |
| 61 static const char kUrlParam[] = "url"; |
| 62 static const char kPageReloadCommand[] = "Page.reload"; |
| 63 static const char kPageNavigateCommand[] = "Page.navigate"; |
| 64 |
58 typedef DevToolsAdbBridge::Callback Callback; | 65 typedef DevToolsAdbBridge::Callback Callback; |
59 typedef std::vector<scoped_refptr<DevToolsAdbBridge::AndroidDevice> > | 66 typedef std::vector<scoped_refptr<DevToolsAdbBridge::AndroidDevice> > |
60 AndroidDevices; | 67 AndroidDevices; |
61 typedef base::Callback<void(const AndroidDevices&)> AndroidDevicesCallback; | 68 typedef base::Callback<void(const AndroidDevices&)> AndroidDevicesCallback; |
62 | 69 |
63 class AdbDeviceImpl : public DevToolsAdbBridge::AndroidDevice { | 70 class AdbDeviceImpl : public DevToolsAdbBridge::AndroidDevice { |
64 public: | 71 public: |
65 explicit AdbDeviceImpl(const std::string& serial) | 72 explicit AdbDeviceImpl(const std::string& serial) |
66 : AndroidDevice(serial) { | 73 : AndroidDevice(serial) { |
67 } | 74 } |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 } | 382 } |
376 | 383 |
377 scoped_refptr<DevToolsAdbBridge> bridge_; | 384 scoped_refptr<DevToolsAdbBridge> bridge_; |
378 Callback callback_; | 385 Callback callback_; |
379 AndroidDevices devices_; | 386 AndroidDevices devices_; |
380 std::vector<std::string> sockets_; | 387 std::vector<std::string> sockets_; |
381 std::map<std::string, std::string> socket_to_package_; | 388 std::map<std::string, std::string> socket_to_package_; |
382 scoped_ptr<DevToolsAdbBridge::RemoteDevices> remote_devices_; | 389 scoped_ptr<DevToolsAdbBridge::RemoteDevices> remote_devices_; |
383 }; | 390 }; |
384 | 391 |
| 392 // AdbProtocolCommand --------------------------------------------------------- |
| 393 |
| 394 class AdbProtocolCommand : public AdbWebSocket::Delegate { |
| 395 public: |
| 396 AdbProtocolCommand( |
| 397 scoped_refptr<DevToolsAdbBridge> bridge_, |
| 398 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, |
| 399 const std::string& socket_name, |
| 400 const std::string& debug_url, |
| 401 const std::string& command); |
| 402 |
| 403 private: |
| 404 virtual void OnSocketOpened() OVERRIDE; |
| 405 virtual void OnFrameRead(const std::string& message) OVERRIDE; |
| 406 virtual void OnSocketClosed(bool closed_by_device) OVERRIDE; |
| 407 virtual bool ProcessIncomingMessage(const std::string& message) OVERRIDE; |
| 408 |
| 409 scoped_refptr<DevToolsAdbBridge> bridge_; |
| 410 const std::string command_; |
| 411 scoped_refptr<AdbWebSocket> web_socket_; |
| 412 |
| 413 DISALLOW_COPY_AND_ASSIGN(AdbProtocolCommand); |
| 414 }; |
| 415 |
| 416 AdbProtocolCommand::AdbProtocolCommand( |
| 417 scoped_refptr<DevToolsAdbBridge> bridge, |
| 418 scoped_refptr<DevToolsAdbBridge::AndroidDevice> device, |
| 419 const std::string& socket_name, |
| 420 const std::string& debug_url, |
| 421 const std::string& command) |
| 422 : bridge_(bridge), |
| 423 command_(command) { |
| 424 web_socket_ = new AdbWebSocket( |
| 425 device, socket_name, debug_url, bridge_->GetAdbMessageLoop(), this); |
| 426 } |
| 427 |
| 428 void AdbProtocolCommand::OnSocketOpened() { |
| 429 web_socket_->SendFrame(command_); |
| 430 web_socket_->Disconnect(); |
| 431 } |
| 432 |
| 433 void AdbProtocolCommand::OnFrameRead(const std::string& message) {} |
| 434 |
| 435 void AdbProtocolCommand::OnSocketClosed(bool closed_by_device) { |
| 436 delete this; |
| 437 } |
| 438 |
| 439 bool AdbProtocolCommand::ProcessIncomingMessage(const std::string& message) { |
| 440 return false; |
| 441 } |
| 442 |
385 } // namespace | 443 } // namespace |
386 | 444 |
387 const char kDevToolsChannelNameFormat[] = "%s_devtools_remote"; | 445 const char kDevToolsChannelNameFormat[] = "%s_devtools_remote"; |
388 | 446 |
389 class AgentHostDelegate; | 447 class AgentHostDelegate; |
390 | 448 |
391 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates; | 449 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates; |
392 | 450 |
393 base::LazyInstance<AgentHostDelegates>::Leaky g_host_delegates = | 451 base::LazyInstance<AgentHostDelegates>::Leaky g_host_delegates = |
394 LAZY_INSTANCE_INITIALIZER; | 452 LAZY_INSTANCE_INITIALIZER; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 } | 494 } |
437 | 495 |
438 void DevToolsAdbBridge::AndroidDevice::HttpQuery( | 496 void DevToolsAdbBridge::AndroidDevice::HttpQuery( |
439 const std::string& la_name, | 497 const std::string& la_name, |
440 const std::string& request, | 498 const std::string& request, |
441 const CommandCallback& callback) { | 499 const CommandCallback& callback) { |
442 OpenSocket(la_name, base::Bind(&AndroidDevice::OnHttpSocketOpened, this, | 500 OpenSocket(la_name, base::Bind(&AndroidDevice::OnHttpSocketOpened, this, |
443 request, callback)); | 501 request, callback)); |
444 } | 502 } |
445 | 503 |
446 void DevToolsAdbBridge::AndroidDevice::HttpQuery( | 504 void DevToolsAdbBridge::AndroidDevice::HttpUpgrade( |
447 const std::string& la_name, | 505 const std::string& la_name, |
448 const std::string& request, | 506 const std::string& request, |
449 const SocketCallback& callback) { | 507 const SocketCallback& callback) { |
450 OpenSocket(la_name, base::Bind(&AndroidDevice::OnHttpSocketOpened2, this, | 508 OpenSocket(la_name, base::Bind(&AndroidDevice::OnHttpSocketOpened2, this, |
451 request, callback)); | 509 request, callback)); |
452 } | 510 } |
453 | 511 |
454 DevToolsAdbBridge::AndroidDevice::~AndroidDevice() { | 512 DevToolsAdbBridge::AndroidDevice::~AndroidDevice() { |
455 } | 513 } |
456 | 514 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 if (debug_url_.find("ws://") == 0) | 632 if (debug_url_.find("ws://") == 0) |
575 debug_url_ = debug_url_.substr(5); | 633 debug_url_ = debug_url_.substr(5); |
576 else | 634 else |
577 debug_url_ = ""; | 635 debug_url_ = ""; |
578 | 636 |
579 size_t ws_param = frontend_url_.find("?ws"); | 637 size_t ws_param = frontend_url_.find("?ws"); |
580 if (ws_param != std::string::npos) | 638 if (ws_param != std::string::npos) |
581 frontend_url_ = frontend_url_.substr(0, ws_param); | 639 frontend_url_ = frontend_url_.substr(0, ws_param); |
582 if (frontend_url_.find("http:") == 0) | 640 if (frontend_url_.find("http:") == 0) |
583 frontend_url_ = "https:" + frontend_url_.substr(5); | 641 frontend_url_ = "https:" + frontend_url_.substr(5); |
584 | |
585 global_id_ = base::StringPrintf( | |
586 "%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)); |
| 646 std::string agent_id = base::StringPrintf("%s:%s:%s", |
| 647 device_->serial().c_str(), socket_.c_str(), id_.c_str()); |
591 AgentHostDelegates::iterator it = | 648 AgentHostDelegates::iterator it = |
592 g_host_delegates.Get().find(global_id()); | 649 g_host_delegates.Get().find(agent_id); |
593 if (it != g_host_delegates.Get().end()) | 650 if (it != g_host_delegates.Get().end()) |
594 it->second->OpenFrontend(); | 651 it->second->OpenFrontend(); |
595 else if (!debug_url_.empty()) | 652 else if (!debug_url_.empty()) |
596 new AgentHostDelegate( | 653 new AgentHostDelegate( |
597 global_id_, device_, socket_, debug_url_, | 654 agent_id, device_, socket_, debug_url_, |
598 frontend_url_, bridge_->GetAdbMessageLoop(), profile); | 655 frontend_url_, bridge_->GetAdbMessageLoop(), profile); |
599 } | 656 } |
600 | 657 |
| 658 static void Noop(int, const std::string&) {} |
| 659 |
| 660 void DevToolsAdbBridge::RemotePage::Close() { |
| 661 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 662 if (attached()) |
| 663 return; |
| 664 std::string request = base::StringPrintf(kClosePageRequest, id_.c_str()); |
| 665 bridge_->GetAdbMessageLoop()->PostTask(FROM_HERE, |
| 666 base::Bind(&AndroidDevice::HttpQuery, |
| 667 device_, socket_, request, base::Bind(&Noop))); |
| 668 } |
| 669 |
| 670 void DevToolsAdbBridge::RemotePage::Reload() { |
| 671 SendProtocolCommand(kPageReloadCommand, NULL); |
| 672 } |
| 673 |
| 674 void DevToolsAdbBridge::RemotePage::SendProtocolCommand( |
| 675 const std::string& method, |
| 676 base::DictionaryValue* params) { |
| 677 if (attached()) |
| 678 return; |
| 679 DevToolsProtocol::Command command(1, method, params); |
| 680 new AdbProtocolCommand( |
| 681 bridge_, device_, socket_, debug_url_, command.Serialize()); |
| 682 } |
| 683 |
601 DevToolsAdbBridge::RemotePage::~RemotePage() { | 684 DevToolsAdbBridge::RemotePage::~RemotePage() { |
602 } | 685 } |
603 | 686 |
604 DevToolsAdbBridge::RemoteBrowser::RemoteBrowser( | 687 DevToolsAdbBridge::RemoteBrowser::RemoteBrowser( |
605 scoped_refptr<DevToolsAdbBridge> bridge, | 688 scoped_refptr<DevToolsAdbBridge> bridge, |
606 scoped_refptr<AndroidDevice> device, | 689 scoped_refptr<AndroidDevice> device, |
607 const std::string& socket, | 690 const std::string& socket, |
608 const std::string& name) | 691 const std::string& name) |
609 : bridge_(bridge), | 692 : bridge_(bridge), |
610 device_(device), | 693 device_(device), |
611 socket_(socket), | 694 socket_(socket), |
612 name_(name) { | 695 name_(name) { |
613 } | 696 } |
614 | 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(kUrlParam, url); |
| 723 new_page->SendProtocolCommand(kPageNavigateCommand, ¶ms); |
| 724 } |
| 725 } |
| 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) { |
623 } | 735 } |
624 | 736 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
781 | 893 |
782 if (listeners_.empty()) | 894 if (listeners_.empty()) |
783 return; | 895 return; |
784 | 896 |
785 BrowserThread::PostDelayedTask( | 897 BrowserThread::PostDelayedTask( |
786 BrowserThread::UI, | 898 BrowserThread::UI, |
787 FROM_HERE, | 899 FROM_HERE, |
788 base::Bind(&DevToolsAdbBridge::RequestRemoteDevices, this), | 900 base::Bind(&DevToolsAdbBridge::RequestRemoteDevices, this), |
789 base::TimeDelta::FromMilliseconds(kAdbPollingIntervalMs)); | 901 base::TimeDelta::FromMilliseconds(kAdbPollingIntervalMs)); |
790 } | 902 } |
OLD | NEW |