Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "headless/lib/browser/headless_devtools.h" | 5 #include "headless/lib/browser/headless_devtools.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "content/public/browser/devtools_agent_host.h" | 12 #include "content/public/browser/devtools_agent_host.h" |
| 13 #include "content/public/browser/devtools_socket_factory.h" | 13 #include "content/public/browser/devtools_socket_factory.h" |
| 14 #include "content/public/browser/navigation_entry.h" | 14 #include "content/public/browser/navigation_entry.h" |
| 15 #include "headless/grit/headless_lib_resources.h" | 15 #include "headless/grit/headless_lib_resources.h" |
| 16 #include "headless/public/headless_browser.h" | 16 #include "headless/public/headless_browser.h" |
| 17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/log/net_log_source.h" | 18 #include "net/log/net_log_source.h" |
| 19 #include "net/socket/tcp_server_socket.h" | 19 #include "net/socket/tcp_server_socket.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 21 | 21 |
| 22 namespace headless { | 22 namespace headless { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const int kBackLog = 10; | 26 const int kBackLog = 10; |
| 27 | 27 |
| 28 class TCPServerSocketFactory : public content::DevToolsSocketFactory { | 28 class TCPEndpointServerSocketFactory : public content::DevToolsSocketFactory { |
| 29 public: | 29 public: |
| 30 explicit TCPServerSocketFactory(const net::IPEndPoint& endpoint) | 30 explicit TCPEndpointServerSocketFactory(const net::IPEndPoint& endpoint) |
| 31 : endpoint_(endpoint), socket_fd_(0) { | 31 : endpoint_(endpoint) { |
| 32 DCHECK(endpoint_.address().IsValid()); | 32 DCHECK(endpoint_.address().IsValid()); |
| 33 } | 33 } |
| 34 | 34 |
| 35 explicit TCPServerSocketFactory(const size_t socket_fd) | 35 private: |
| 36 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { | |
| 37 std::unique_ptr<net::ServerSocket> socket( | |
| 38 new net::TCPServerSocket(nullptr, net::NetLogSource())); | |
| 39 if (socket->Listen(endpoint_, kBackLog) != net::OK) | |
| 40 return std::unique_ptr<net::ServerSocket>(); | |
| 41 return socket; | |
| 42 } | |
| 43 | |
| 44 std::unique_ptr<net::ServerSocket> CreateForTethering( | |
| 45 std::string* out_name) override { | |
| 46 return nullptr; | |
| 47 } | |
| 48 | |
| 49 net::IPEndPoint endpoint_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(TCPEndpointServerSocketFactory); | |
| 52 }; | |
| 53 | |
| 54 #if defined(OS_POSIX) | |
| 55 class TCPAdoptServerSocketFactory : public content::DevToolsSocketFactory { | |
| 56 public: | |
| 57 // Construct a factory to use an already-open, already-listening socket. | |
| 58 explicit TCPAdoptServerSocketFactory(const size_t socket_fd) | |
| 36 : socket_fd_(socket_fd) {} | 59 : socket_fd_(socket_fd) {} |
| 37 | 60 |
| 38 private: | 61 private: |
| 39 // content::DevToolsSocketFactory implementation: | |
| 40 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { | 62 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 41 if (!socket_fd_) { | |
| 42 std::unique_ptr<net::ServerSocket> socket( | |
| 43 new net::TCPServerSocket(nullptr, net::NetLogSource())); | |
| 44 if (socket->Listen(endpoint_, kBackLog) != net::OK) | |
| 45 return std::unique_ptr<net::ServerSocket>(); | |
| 46 return socket; | |
| 47 } | |
| 48 #if defined(OS_POSIX) | |
| 49 std::unique_ptr<net::TCPServerSocket> tsock( | 63 std::unique_ptr<net::TCPServerSocket> tsock( |
| 50 new net::TCPServerSocket(nullptr, net::NetLogSource())); | 64 new net::TCPServerSocket(nullptr, net::NetLogSource())); |
| 51 if (tsock->AdoptSocket(socket_fd_) != net::OK) { | 65 if (tsock->AdoptSocket(socket_fd_) != net::OK) { |
| 52 LOG(ERROR) << "Failed to adopt open socket"; | 66 LOG(ERROR) << "Failed to adopt open socket"; |
| 53 return std::unique_ptr<net::ServerSocket>(); | 67 return std::unique_ptr<net::ServerSocket>(); |
| 54 } | 68 } |
| 55 return std::unique_ptr<net::ServerSocket>(tsock.release()); | 69 // Note that we assume that the socket is already listening, so unlike |
| 56 #else | 70 // TCPEndpointServerSocketFactory, we don't call Listen. |
| 57 LOG(ERROR) << "Can't inherit an open socket on non-Posix systems"; | 71 return std::unique_ptr<net::ServerSocket>(std::move(tsock)); |
| 58 return std::unique_ptr<net::ServerSocket>(); | |
| 59 #endif | |
| 60 } | 72 } |
| 61 | 73 |
| 62 std::unique_ptr<net::ServerSocket> CreateForTethering( | 74 std::unique_ptr<net::ServerSocket> CreateForTethering( |
| 63 std::string* out_name) override { | 75 std::string* out_name) override { |
| 64 return nullptr; | 76 return nullptr; |
| 65 } | 77 } |
| 66 | 78 |
| 67 net::IPEndPoint endpoint_; | |
| 68 size_t socket_fd_; | 79 size_t socket_fd_; |
| 69 | 80 |
| 70 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); | 81 DISALLOW_COPY_AND_ASSIGN(TCPAdoptServerSocketFactory); |
| 71 }; | 82 }; |
| 83 #else // defined(OS_POSIX) | |
| 84 // Placeholder class to use when a socket_fd is passed in on non-Posix. | |
|
mcgreevy
2017/05/24 01:23:19
Can you prepend a blank line here?
Raul Vera
2017/05/24 03:00:06
Done.
| |
| 85 class DummyTCPServerSocketFactory : public content::DevToolsSocketFactory { | |
| 86 public: | |
| 87 explicit DummyTCPServerSocketFactory() {} | |
| 72 | 88 |
| 89 private: | |
| 90 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { | |
| 91 return nullptr; | |
| 92 } | |
| 93 | |
| 94 std::unique_ptr<net::ServerSocket> CreateForTethering( | |
| 95 std::string* out_name) override { | |
| 96 return nullptr; | |
| 97 } | |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(DummyTCPServerSocketFactory); | |
| 100 }; | |
| 101 #endif // defined(OS_POSIX) | |
| 73 } // namespace | 102 } // namespace |
| 74 | 103 |
| 75 void StartLocalDevToolsHttpHandler(HeadlessBrowser::Options* options) { | 104 void StartLocalDevToolsHttpHandler(HeadlessBrowser::Options* options) { |
| 76 std::unique_ptr<content::DevToolsSocketFactory> socket_factory; | 105 std::unique_ptr<content::DevToolsSocketFactory> socket_factory; |
| 77 if (options->devtools_socket_fd == 0) { | 106 if (options->devtools_socket_fd == 0) { |
| 78 const net::IPEndPoint& endpoint = options->devtools_endpoint; | 107 const net::IPEndPoint& endpoint = options->devtools_endpoint; |
| 79 socket_factory.reset(new TCPServerSocketFactory(endpoint)); | 108 socket_factory.reset(new TCPEndpointServerSocketFactory(endpoint)); |
| 80 } else { | 109 } else { |
| 110 #if defined(OS_POSIX) | |
| 81 const uint16_t socket_fd = options->devtools_socket_fd; | 111 const uint16_t socket_fd = options->devtools_socket_fd; |
| 82 socket_factory.reset(new TCPServerSocketFactory(socket_fd)); | 112 socket_factory.reset(new TCPAdoptServerSocketFactory(socket_fd)); |
| 113 #else | |
| 114 LOG(ERROR) << "Can't inherit an open socket on non-Posix systems"; | |
| 115 socket_factory.reset(new DummyTCPServerSocketFactory()); | |
| 116 #endif | |
| 83 } | 117 } |
| 84 content::DevToolsAgentHost::StartRemoteDebuggingServer( | 118 content::DevToolsAgentHost::StartRemoteDebuggingServer( |
| 85 std::move(socket_factory), std::string(), | 119 std::move(socket_factory), std::string(), |
| 86 options->user_data_dir, // TODO(altimin): Figure a proper value for this. | 120 options->user_data_dir, // TODO(altimin): Figure a proper value for this. |
| 87 base::FilePath(), options->product_name_and_version, options->user_agent); | 121 base::FilePath(), options->product_name_and_version, options->user_agent); |
| 88 } | 122 } |
| 89 | 123 |
| 90 void StopLocalDevToolsHttpHandler() { | 124 void StopLocalDevToolsHttpHandler() { |
| 91 content::DevToolsAgentHost::StopRemoteDebuggingServer(); | 125 content::DevToolsAgentHost::StopRemoteDebuggingServer(); |
| 92 } | 126 } |
| 93 | 127 |
| 94 } // namespace headless | 128 } // namespace headless |
| OLD | NEW |