devtools_manager_delegate.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <vector>
  8. #include "base/bind.h"
  9. #include "base/command_line.h"
  10. #include "base/files/file_path.h"
  11. #include "base/path_service.h"
  12. #include "base/strings/string_number_conversions.h"
  13. #include "base/strings/stringprintf.h"
  14. #include "base/strings/utf_string_conversions.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/atom_paths.h"
  28. #include "ui/base/resource/resource_bundle.h"
  29. namespace electron {
  30. namespace {
  31. class TCPServerSocketFactory : public content::DevToolsSocketFactory {
  32. public:
  33. TCPServerSocketFactory(const std::string& address, int port)
  34. : address_(address), port_(port) {}
  35. private:
  36. // content::ServerSocketFactory.
  37. std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
  38. std::unique_ptr<net::ServerSocket> socket(
  39. new net::TCPServerSocket(nullptr, net::NetLogSource()));
  40. if (socket->ListenWithAddressAndPort(address_, port_, 10) != net::OK)
  41. return std::unique_ptr<net::ServerSocket>();
  42. return socket;
  43. }
  44. std::unique_ptr<net::ServerSocket> CreateForTethering(
  45. std::string* name) override {
  46. return std::unique_ptr<net::ServerSocket>();
  47. }
  48. std::string address_;
  49. uint16_t port_;
  50. DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
  51. };
  52. std::unique_ptr<content::DevToolsSocketFactory> CreateSocketFactory() {
  53. auto& command_line = *base::CommandLine::ForCurrentProcess();
  54. // See if the user specified a port on the command line (useful for
  55. // automation). If not, use an ephemeral port by specifying 0.
  56. int port = 0;
  57. if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
  58. int temp_port;
  59. std::string port_str =
  60. command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
  61. if (base::StringToInt(port_str, &temp_port) && temp_port >= 0 &&
  62. temp_port < 65535) {
  63. port = temp_port;
  64. } else {
  65. DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
  66. }
  67. }
  68. return std::unique_ptr<content::DevToolsSocketFactory>(
  69. new TCPServerSocketFactory("127.0.0.1", port));
  70. }
  71. } // namespace
  72. // DevToolsManagerDelegate ---------------------------------------------------
  73. // static
  74. void DevToolsManagerDelegate::StartHttpHandler() {
  75. base::FilePath user_dir;
  76. base::PathService::Get(DIR_USER_DATA, &user_dir);
  77. content::DevToolsAgentHost::StartRemoteDebuggingServer(
  78. CreateSocketFactory(), user_dir, base::FilePath());
  79. }
  80. DevToolsManagerDelegate::DevToolsManagerDelegate() = default;
  81. DevToolsManagerDelegate::~DevToolsManagerDelegate() = default;
  82. void DevToolsManagerDelegate::Inspect(content::DevToolsAgentHost* agent_host) {}
  83. void DevToolsManagerDelegate::HandleCommand(
  84. content::DevToolsAgentHost* agent_host,
  85. content::DevToolsAgentHostClient* client,
  86. const std::string& method,
  87. base::span<const uint8_t> message,
  88. NotHandledCallback callback) {
  89. std::move(callback).Run(message);
  90. }
  91. scoped_refptr<content::DevToolsAgentHost>
  92. DevToolsManagerDelegate::CreateNewTarget(const GURL& url) {
  93. return nullptr;
  94. }
  95. std::string DevToolsManagerDelegate::GetDiscoveryPageHTML() {
  96. return ui::ResourceBundle::GetSharedInstance()
  97. .GetRawDataResource(IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE)
  98. .as_string();
  99. }
  100. bool DevToolsManagerDelegate::HasBundledFrontendResources() {
  101. return true;
  102. }
  103. } // namespace electron