OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 file. |
| 4 |
| 5 #include "headless/lib/browser/headless_devtools.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/files/file_path.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 "components/devtools_discovery/basic_target_descriptor.h" |
| 16 #include "components/devtools_discovery/devtools_discovery_manager.h" |
| 17 #include "components/devtools_http_handler/devtools_http_handler.h" |
| 18 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/browser/devtools_agent_host.h" |
| 20 #include "content/public/browser/devtools_frontend_host.h" |
| 21 #include "content/public/browser/favicon_status.h" |
| 22 #include "content/public/browser/navigation_entry.h" |
| 23 #include "content/public/browser/render_view_host.h" |
| 24 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/common/content_switches.h" |
| 26 #include "content/public/common/url_constants.h" |
| 27 #include "content/public/common/user_agent.h" |
| 28 #include "net/base/net_errors.h" |
| 29 #include "net/socket/tcp_server_socket.h" |
| 30 #include "ui/base/resource/resource_bundle.h" |
| 31 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" |
| 32 |
| 33 using devtools_http_handler::DevToolsHttpHandler; |
| 34 |
| 35 namespace headless { |
| 36 |
| 37 namespace { |
| 38 |
| 39 const int kBackLog = 10; |
| 40 |
| 41 class TCPServerSocketFactory |
| 42 : public DevToolsHttpHandler::ServerSocketFactory { |
| 43 public: |
| 44 TCPServerSocketFactory(const std::string& address, uint16 port) |
| 45 : address_(address), port_(port) { |
| 46 } |
| 47 |
| 48 private: |
| 49 // DevToolsHttpHandler::ServerSocketFactory. |
| 50 scoped_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 51 scoped_ptr<net::ServerSocket> socket( |
| 52 new net::TCPServerSocket(nullptr, net::NetLog::Source())); |
| 53 if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK) |
| 54 return scoped_ptr<net::ServerSocket>(); |
| 55 |
| 56 return socket; |
| 57 } |
| 58 |
| 59 std::string address_; |
| 60 uint16 port_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); |
| 63 }; |
| 64 |
| 65 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> |
| 66 CreateSocketFactory() { |
| 67 const base::CommandLine& command_line = |
| 68 *base::CommandLine::ForCurrentProcess(); |
| 69 // See if the user specified a port on the command line (useful for |
| 70 // automation). If not, use an ephemeral port by specifying 0. |
| 71 uint16 port = 0; |
| 72 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) { |
| 73 int temp_port; |
| 74 std::string port_str = |
| 75 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort); |
| 76 if (base::StringToInt(port_str, &temp_port) && |
| 77 temp_port >= 0 && temp_port < 65535) { |
| 78 port = static_cast<uint16>(temp_port); |
| 79 } else { |
| 80 DLOG(WARNING) << "Invalid http debugger port number " << temp_port; |
| 81 } |
| 82 } |
| 83 return scoped_ptr<DevToolsHttpHandler::ServerSocketFactory>( |
| 84 new TCPServerSocketFactory("127.0.0.1", port)); |
| 85 } |
| 86 |
| 87 |
| 88 class HeadlessDevToolsDelegate : |
| 89 public devtools_http_handler::DevToolsHttpHandlerDelegate { |
| 90 public: |
| 91 explicit HeadlessDevToolsDelegate(); |
| 92 ~HeadlessDevToolsDelegate() override; |
| 93 |
| 94 // devtools_http_handler::DevToolsHttpHandlerDelegate implementation. |
| 95 std::string GetDiscoveryPageHTML() override; |
| 96 std::string GetFrontendResource(const std::string& path) override; |
| 97 std::string GetPageThumbnailData(const GURL& url) override; |
| 98 content::DevToolsExternalAgentProxyDelegate* |
| 99 HandleWebSocketConnection(const std::string& path) override; |
| 100 |
| 101 private: |
| 102 DISALLOW_COPY_AND_ASSIGN(HeadlessDevToolsDelegate); |
| 103 }; |
| 104 |
| 105 HeadlessDevToolsDelegate::HeadlessDevToolsDelegate() { } |
| 106 |
| 107 HeadlessDevToolsDelegate::~HeadlessDevToolsDelegate() { } |
| 108 |
| 109 std::string HeadlessDevToolsDelegate::GetDiscoveryPageHTML() { |
| 110 return std::string(); |
| 111 } |
| 112 |
| 113 std::string HeadlessDevToolsDelegate::GetFrontendResource( |
| 114 const std::string& path) { |
| 115 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); |
| 116 } |
| 117 |
| 118 std::string HeadlessDevToolsDelegate::GetPageThumbnailData(const GURL& url) { |
| 119 return std::string(); |
| 120 } |
| 121 |
| 122 content::DevToolsExternalAgentProxyDelegate* |
| 123 HeadlessDevToolsDelegate::HandleWebSocketConnection(const std::string& path) { |
| 124 return nullptr; |
| 125 } |
| 126 |
| 127 } // namespace |
| 128 |
| 129 scoped_ptr<DevToolsHttpHandler> |
| 130 CreateHttpHandler(content::BrowserContext* browser_context) { |
| 131 return make_scoped_ptr(new DevToolsHttpHandler( |
| 132 CreateSocketFactory(), |
| 133 std::string(), |
| 134 new HeadlessDevToolsDelegate(), |
| 135 browser_context->GetPath(), |
| 136 base::FilePath(), |
| 137 std::string(), |
| 138 "UserAgent")); // TODO(altimin): support user agent properly. |
| 139 } |
| 140 |
| 141 } // namespace headless |
OLD | NEW |