devtools_manager_delegate.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE-CHROMIUM file.
  4. #include "shell/browser/ui/devtools_manager_delegate.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/command_line.h"
  8. #include "base/files/file_path.h"
  9. #include "base/functional/bind.h"
  10. #include "base/path_service.h"
  11. #include "base/strings/string_number_conversions.h"
  12. #include "base/strings/stringprintf.h"
  13. #include "base/strings/utf_string_conversions.h"
  14. #include "chrome/common/chrome_paths.h"
  15. #include "content/public/browser/devtools_agent_host.h"
  16. #include "content/public/browser/devtools_frontend_host.h"
  17. #include "content/public/browser/devtools_socket_factory.h"
  18. #include "content/public/browser/favicon_status.h"
  19. #include "content/public/browser/navigation_entry.h"
  20. #include "content/public/common/content_switches.h"
  21. #include "content/public/common/url_constants.h"
  22. #include "content/public/common/user_agent.h"
  23. #include "electron/grit/electron_resources.h"
  24. #include "net/base/net_errors.h"
  25. #include "net/socket/stream_socket.h"
  26. #include "net/socket/tcp_server_socket.h"
  27. #include "shell/browser/browser.h"
  28. #include "shell/common/electron_paths.h"
  29. #include "third_party/inspector_protocol/crdtp/dispatch.h"
  30. #include "ui/base/resource/resource_bundle.h"
  31. namespace electron {
  32. namespace {
  33. class TCPServerSocketFactory : public content::DevToolsSocketFactory {
  34. public:
  35. TCPServerSocketFactory(const std::string& address, int port)
  36. : address_(address), port_(port) {}
  37. // disable copy
  38. TCPServerSocketFactory(const TCPServerSocketFactory&) = delete;
  39. TCPServerSocketFactory& operator=(const TCPServerSocketFactory&) = delete;
  40. private:
  41. // content::ServerSocketFactory.
  42. std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
  43. auto socket =
  44. std::make_unique<net::TCPServerSocket>(nullptr, net::NetLogSource());
  45. if (socket->ListenWithAddressAndPort(address_, port_, 10) != net::OK)
  46. return std::unique_ptr<net::ServerSocket>();
  47. return socket;
  48. }
  49. std::unique_ptr<net::ServerSocket> CreateForTethering(
  50. std::string* name) override {
  51. return std::unique_ptr<net::ServerSocket>();
  52. }
  53. std::string address_;
  54. uint16_t port_;
  55. };
  56. std::unique_ptr<content::DevToolsSocketFactory> CreateSocketFactory() {
  57. auto& command_line = *base::CommandLine::ForCurrentProcess();
  58. // See if the user specified a port on the command line (useful for
  59. // automation). If not, use an ephemeral port by specifying 0.
  60. int port = 0;
  61. if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
  62. int temp_port;
  63. std::string port_str =
  64. command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
  65. if (base::StringToInt(port_str, &temp_port) && temp_port >= 0 &&
  66. temp_port < 65535) {
  67. port = temp_port;
  68. } else {
  69. DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
  70. }
  71. }
  72. return std::make_unique<TCPServerSocketFactory>("127.0.0.1", port);
  73. }
  74. const char kBrowserCloseMethod[] = "Browser.close";
  75. } // namespace
  76. // DevToolsManagerDelegate ---------------------------------------------------
  77. // static
  78. void DevToolsManagerDelegate::StartHttpHandler() {
  79. base::FilePath session_data;
  80. base::PathService::Get(DIR_SESSION_DATA, &session_data);
  81. content::DevToolsAgentHost::StartRemoteDebuggingServer(
  82. CreateSocketFactory(), session_data, base::FilePath());
  83. }
  84. DevToolsManagerDelegate::DevToolsManagerDelegate() = default;
  85. DevToolsManagerDelegate::~DevToolsManagerDelegate() = default;
  86. void DevToolsManagerDelegate::Inspect(content::DevToolsAgentHost* agent_host) {}
  87. void DevToolsManagerDelegate::HandleCommand(
  88. content::DevToolsAgentHostClientChannel* channel,
  89. base::span<const uint8_t> message,
  90. NotHandledCallback callback) {
  91. crdtp::Dispatchable dispatchable(crdtp::SpanFrom(message));
  92. DCHECK(dispatchable.ok());
  93. if (crdtp::SpanEquals(crdtp::SpanFrom(kBrowserCloseMethod),
  94. dispatchable.Method())) {
  95. // In theory, we should respond over the protocol saying that the
  96. // Browser.close was handled. But doing so requires instantiating the
  97. // protocol UberDispatcher and generating proper protocol handlers.
  98. // Since we only have one method and it is supposed to close Electron,
  99. // we don't need to add this complexity. Should we decide to support
  100. // methods like Browser.setWindowBounds, we'll need to do it though.
  101. content::GetUIThreadTaskRunner({})->PostTask(
  102. FROM_HERE, base::BindOnce([]() { Browser::Get()->Quit(); }));
  103. return;
  104. }
  105. std::move(callback).Run(message);
  106. }
  107. scoped_refptr<content::DevToolsAgentHost>
  108. DevToolsManagerDelegate::CreateNewTarget(const GURL& url,
  109. TargetType target_type) {
  110. return nullptr;
  111. }
  112. std::string DevToolsManagerDelegate::GetDiscoveryPageHTML() {
  113. return ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
  114. IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE);
  115. }
  116. bool DevToolsManagerDelegate::HasBundledFrontendResources() {
  117. return true;
  118. }
  119. } // namespace electron