Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: headless/lib/browser/headless_devtools.cc

Issue 2882393002: Refactored adopting a socket for devtools into cleaner, more idiomatic code. (Closed)
Patch Set: Formatting per reviewer comment Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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)
72 84
85 // Placeholder class to use when a socket_fd is passed in on non-Posix.
86 class DummyTCPServerSocketFactory : public content::DevToolsSocketFactory {
87 public:
88 explicit DummyTCPServerSocketFactory() {}
89
90 private:
91 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
92 return nullptr;
93 }
94
95 std::unique_ptr<net::ServerSocket> CreateForTethering(
96 std::string* out_name) override {
97 return nullptr;
98 }
99
100 DISALLOW_COPY_AND_ASSIGN(DummyTCPServerSocketFactory);
101 };
102 #endif // defined(OS_POSIX)
73 } // namespace 103 } // namespace
74 104
75 void StartLocalDevToolsHttpHandler(HeadlessBrowser::Options* options) { 105 void StartLocalDevToolsHttpHandler(HeadlessBrowser::Options* options) {
76 std::unique_ptr<content::DevToolsSocketFactory> socket_factory; 106 std::unique_ptr<content::DevToolsSocketFactory> socket_factory;
77 if (options->devtools_socket_fd == 0) { 107 if (options->devtools_socket_fd == 0) {
78 const net::IPEndPoint& endpoint = options->devtools_endpoint; 108 const net::IPEndPoint& endpoint = options->devtools_endpoint;
79 socket_factory.reset(new TCPServerSocketFactory(endpoint)); 109 socket_factory.reset(new TCPEndpointServerSocketFactory(endpoint));
80 } else { 110 } else {
111 #if defined(OS_POSIX)
81 const uint16_t socket_fd = options->devtools_socket_fd; 112 const uint16_t socket_fd = options->devtools_socket_fd;
82 socket_factory.reset(new TCPServerSocketFactory(socket_fd)); 113 socket_factory.reset(new TCPAdoptServerSocketFactory(socket_fd));
114 #else
115 LOG(ERROR) << "Can't inherit an open socket on non-Posix systems";
116 socket_factory.reset(new DummyTCPServerSocketFactory());
117 #endif
83 } 118 }
84 content::DevToolsAgentHost::StartRemoteDebuggingServer( 119 content::DevToolsAgentHost::StartRemoteDebuggingServer(
85 std::move(socket_factory), std::string(), 120 std::move(socket_factory), std::string(),
86 options->user_data_dir, // TODO(altimin): Figure a proper value for this. 121 options->user_data_dir, // TODO(altimin): Figure a proper value for this.
87 base::FilePath(), options->product_name_and_version, options->user_agent); 122 base::FilePath(), options->product_name_and_version, options->user_agent);
88 } 123 }
89 124
90 void StopLocalDevToolsHttpHandler() { 125 void StopLocalDevToolsHttpHandler() {
91 content::DevToolsAgentHost::StopRemoteDebuggingServer(); 126 content::DevToolsAgentHost::StopRemoteDebuggingServer();
92 } 127 }
93 128
94 } // namespace headless 129 } // namespace headless
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698