devtools_manager_delegate.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "brightray/browser/devtools_manager_delegate.h"
  5. #include <vector>
  6. #include "base/bind.h"
  7. #include "base/command_line.h"
  8. #include "base/files/file_path.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "base/strings/stringprintf.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "brightray/common/content_client.h"
  13. #include "content/public/browser/devtools_agent_host.h"
  14. #include "content/public/browser/devtools_frontend_host.h"
  15. #include "content/public/browser/devtools_socket_factory.h"
  16. #include "content/public/browser/favicon_status.h"
  17. #include "content/public/browser/navigation_entry.h"
  18. #include "content/public/common/content_switches.h"
  19. #include "content/public/common/url_constants.h"
  20. #include "content/public/common/user_agent.h"
  21. #include "content/shell/grit/shell_resources.h"
  22. #include "net/base/net_errors.h"
  23. #include "net/socket/stream_socket.h"
  24. #include "net/socket/tcp_server_socket.h"
  25. #include "ui/base/resource/resource_bundle.h"
  26. namespace brightray {
  27. namespace {
  28. class TCPServerSocketFactory : public content::DevToolsSocketFactory {
  29. public:
  30. TCPServerSocketFactory(const std::string& address, int port)
  31. : address_(address), port_(port) {}
  32. private:
  33. // content::ServerSocketFactory.
  34. std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
  35. std::unique_ptr<net::ServerSocket> socket(
  36. new net::TCPServerSocket(nullptr, net::NetLogSource()));
  37. if (socket->ListenWithAddressAndPort(address_, port_, 10) != net::OK)
  38. return std::unique_ptr<net::ServerSocket>();
  39. return socket;
  40. }
  41. std::unique_ptr<net::ServerSocket> CreateForTethering(
  42. std::string* name) override {
  43. return std::unique_ptr<net::ServerSocket>();
  44. }
  45. std::string address_;
  46. uint16_t port_;
  47. DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
  48. };
  49. std::unique_ptr<content::DevToolsSocketFactory> CreateSocketFactory() {
  50. auto& command_line = *base::CommandLine::ForCurrentProcess();
  51. // See if the user specified a port on the command line (useful for
  52. // automation). If not, use an ephemeral port by specifying 0.
  53. int port = 0;
  54. if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
  55. int temp_port;
  56. std::string port_str =
  57. command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
  58. if (base::StringToInt(port_str, &temp_port) && temp_port > 0 &&
  59. temp_port < 65535) {
  60. port = temp_port;
  61. } else {
  62. DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
  63. }
  64. }
  65. return std::unique_ptr<content::DevToolsSocketFactory>(
  66. new TCPServerSocketFactory("127.0.0.1", port));
  67. }
  68. } // namespace
  69. // DevToolsManagerDelegate ---------------------------------------------------
  70. // static
  71. void DevToolsManagerDelegate::StartHttpHandler() {
  72. content::DevToolsAgentHost::StartRemoteDebuggingServer(
  73. CreateSocketFactory(), std::string(), base::FilePath(), base::FilePath());
  74. }
  75. DevToolsManagerDelegate::DevToolsManagerDelegate() {}
  76. DevToolsManagerDelegate::~DevToolsManagerDelegate() {}
  77. void DevToolsManagerDelegate::Inspect(content::DevToolsAgentHost* agent_host) {}
  78. bool DevToolsManagerDelegate::HandleCommand(
  79. content::DevToolsAgentHost* agent_host,
  80. int session_id,
  81. base::DictionaryValue* command) {
  82. return false;
  83. }
  84. scoped_refptr<content::DevToolsAgentHost>
  85. DevToolsManagerDelegate::CreateNewTarget(const GURL& url) {
  86. return nullptr;
  87. }
  88. std::string DevToolsManagerDelegate::GetDiscoveryPageHTML() {
  89. return ResourceBundle::GetSharedInstance()
  90. .GetRawDataResource(IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE)
  91. .as_string();
  92. }
  93. std::string DevToolsManagerDelegate::GetFrontendResource(
  94. const std::string& path) {
  95. return content::DevToolsFrontendHost::GetFrontendResource(path).as_string();
  96. }
  97. } // namespace brightray