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" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 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 TCPServerSocketFactory : public content::DevToolsSocketFactory { |
| 29 public: | 29 public: |
| 30 explicit TCPServerSocketFactory(const net::IPEndPoint& endpoint) | 30 explicit TCPServerSocketFactory(const net::IPEndPoint& endpoint) |
| 31 : endpoint_(endpoint) { | 31 : endpoint_(endpoint), socket_fd(0) { |
| 32 DCHECK(endpoint_.address().IsValid()); | 32 DCHECK(endpoint_.address().IsValid()); |
| 33 } | 33 } |
| 34 | 34 |
| 35 explicit TCPServerSocketFactory(const size_t socket_fd) | |
| 36 : socket_fd(socket_fd) {} | |
| 37 | |
| 35 private: | 38 private: |
| 36 // content::DevToolsSocketFactory implementation: | 39 // content::DevToolsSocketFactory implementation: |
| 37 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { | 40 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 38 std::unique_ptr<net::ServerSocket> socket( | 41 if (socket_fd == 0) { |
| 39 new net::TCPServerSocket(nullptr, net::NetLogSource())); | 42 std::unique_ptr<net::ServerSocket> socket( |
| 40 if (socket->Listen(endpoint_, kBackLog) != net::OK) | 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; | |
|
dvallet
2017/04/13 03:44:29
nit: since you are returning here, I would remove
| |
| 47 } else { | |
| 48 #if defined(OS_POSIX) | |
| 49 std::unique_ptr<net::TCPServerSocket> tsock( | |
| 50 new net::TCPServerSocket(nullptr, net::NetLogSource())); | |
| 51 if (tsock->AdopListeningRawSocket(socket_fd) != net::OK) { | |
| 52 LOG(ERROR) << "Failed to adopt open socket"; | |
| 53 return std::unique_ptr<net::ServerSocket>(); | |
| 54 } | |
| 55 return std::unique_ptr<net::ServerSocket>(tsock.release()); | |
| 56 #else | |
| 57 LOG(ERROR) << "Can't inherit an open socket on non-Posix systems"; | |
| 41 return std::unique_ptr<net::ServerSocket>(); | 58 return std::unique_ptr<net::ServerSocket>(); |
| 42 | 59 #endif |
| 43 return socket; | 60 } |
| 44 } | 61 } |
| 45 | 62 |
| 46 std::unique_ptr<net::ServerSocket> CreateForTethering( | 63 std::unique_ptr<net::ServerSocket> CreateForTethering( |
| 47 std::string* out_name) override { | 64 std::string* out_name) override { |
| 48 return nullptr; | 65 return nullptr; |
| 49 } | 66 } |
| 50 | 67 |
| 51 net::IPEndPoint endpoint_; | 68 net::IPEndPoint endpoint_; |
| 69 size_t socket_fd; | |
| 52 | 70 |
| 53 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); | 71 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); |
| 54 }; | 72 }; |
| 55 | 73 |
| 56 } // namespace | 74 } // namespace |
| 57 | 75 |
| 58 void StartLocalDevToolsHttpHandler(HeadlessBrowser::Options* options) { | 76 void StartLocalDevToolsHttpHandler(HeadlessBrowser::Options* options) { |
| 59 const net::IPEndPoint& endpoint = options->devtools_endpoint; | 77 std::unique_ptr<content::DevToolsSocketFactory> socket_factory; |
| 60 std::unique_ptr<content::DevToolsSocketFactory> socket_factory( | 78 if (options->devtools_socket_fd == 0) { |
| 61 new TCPServerSocketFactory(endpoint)); | 79 const net::IPEndPoint& endpoint = options->devtools_endpoint; |
| 80 socket_factory.reset(new TCPServerSocketFactory(endpoint)); | |
| 81 } else { | |
| 82 const uint16_t socket_fd = options->devtools_socket_fd; | |
| 83 socket_factory.reset(new TCPServerSocketFactory(socket_fd)); | |
| 84 } | |
| 62 content::DevToolsAgentHost::StartRemoteDebuggingServer( | 85 content::DevToolsAgentHost::StartRemoteDebuggingServer( |
| 63 std::move(socket_factory), std::string(), | 86 std::move(socket_factory), std::string(), |
| 64 options->user_data_dir, // TODO(altimin): Figure a proper value for this. | 87 options->user_data_dir, // TODO(altimin): Figure a proper value for this. |
| 65 base::FilePath(), options->product_name_and_version, options->user_agent); | 88 base::FilePath(), options->product_name_and_version, options->user_agent); |
| 66 } | 89 } |
| 67 | 90 |
| 68 void StopLocalDevToolsHttpHandler() { | 91 void StopLocalDevToolsHttpHandler() { |
| 69 content::DevToolsAgentHost::StopRemoteDebuggingServer(); | 92 content::DevToolsAgentHost::StopRemoteDebuggingServer(); |
| 70 } | 93 } |
| 71 | 94 |
| 72 } // namespace headless | 95 } // namespace headless |
| OLD | NEW |