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

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: Added comment per reviewer request 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 TCPServerSocketFactory : public content::DevToolsSocketFactory {
29 private:
30 std::unique_ptr<net::ServerSocket> CreateForTethering(
mcgreevy 2017/05/23 04:31:07 I'm not a fan of implementation inheritance, and I
Raul Vera 2017/05/24 00:22:30 What you suggest is what I did first. I added the
mcgreevy 2017/05/24 01:23:19 In general I find it harder to reason about code w
31 std::string* out_name) override {
32 return nullptr;
33 }
34 };
35
36 class TCPEndpointServerSocketFactory : public TCPServerSocketFactory {
29 public: 37 public:
30 explicit TCPServerSocketFactory(const net::IPEndPoint& endpoint) 38 explicit TCPEndpointServerSocketFactory(const net::IPEndPoint& endpoint)
31 : endpoint_(endpoint), socket_fd_(0) { 39 : endpoint_(endpoint) {
32 DCHECK(endpoint_.address().IsValid()); 40 DCHECK(endpoint_.address().IsValid());
mcgreevy 2017/05/23 04:31:07 It's odd that CreateForHttpServer and CreateForTet
Raul Vera 2017/05/24 00:22:30 There was a long thread about this on chromium-dev
mcgreevy 2017/05/24 01:23:19 Fair enough.
33 } 41 }
34 42
35 explicit TCPServerSocketFactory(const size_t socket_fd) 43 private:
44 // content::DevToolsSocketFactory implementation:
mcgreevy 2017/05/23 04:31:07 These comments seem unnecessary.
Raul Vera 2017/05/24 00:22:30 They were there before. I'll remove them. Done.
45 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
46 std::unique_ptr<net::ServerSocket> socket(
47 new net::TCPServerSocket(nullptr, net::NetLogSource()));
48 if (socket->Listen(endpoint_, kBackLog) != net::OK)
49 return std::unique_ptr<net::ServerSocket>();
50 return socket;
51 }
52
53 net::IPEndPoint endpoint_;
54
55 DISALLOW_COPY_AND_ASSIGN(TCPEndpointServerSocketFactory);
56 };
57
58 #if defined(OS_POSIX)
mcgreevy 2017/05/23 04:31:07 Does this really need to be conditionally compiled
Raul Vera 2017/05/24 00:22:30 It won't compile as is on Windows, where a socket
mcgreevy 2017/05/24 01:23:19 OK.
59 class TCPAdoptServerSocketFactory : public TCPServerSocketFactory {
60 public:
61 explicit TCPAdoptServerSocketFactory(const size_t socket_fd)
36 : socket_fd_(socket_fd) {} 62 : socket_fd_(socket_fd) {}
37 63
38 private: 64 private:
39 // content::DevToolsSocketFactory implementation: 65 // content::DevToolsSocketFactory implementation:
40 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { 66 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( 67 std::unique_ptr<net::TCPServerSocket> tsock(
50 new net::TCPServerSocket(nullptr, net::NetLogSource())); 68 new net::TCPServerSocket(nullptr, net::NetLogSource()));
51 if (tsock->AdoptSocket(socket_fd_) != net::OK) { 69 if (tsock->AdoptSocket(socket_fd_) != net::OK) {
52 LOG(ERROR) << "Failed to adopt open socket"; 70 LOG(ERROR) << "Failed to adopt open socket";
53 return std::unique_ptr<net::ServerSocket>(); 71 return std::unique_ptr<net::ServerSocket>();
54 } 72 }
55 return std::unique_ptr<net::ServerSocket>(tsock.release()); 73 // Note that we assume that the socket is already listening, so we don't
mcgreevy 2017/05/23 04:31:07 You should probably document that assumption on th
Raul Vera 2017/05/24 00:22:30 Done. And clarified this comment, too.
56 #else 74 // call Listen.
57 LOG(ERROR) << "Can't inherit an open socket on non-Posix systems"; 75 return std::unique_ptr<net::ServerSocket>(std::move(tsock));
58 return std::unique_ptr<net::ServerSocket>();
59 #endif
60 } 76 }
61 77
62 std::unique_ptr<net::ServerSocket> CreateForTethering(
63 std::string* out_name) override {
64 return nullptr;
65 }
66
67 net::IPEndPoint endpoint_;
68 size_t socket_fd_; 78 size_t socket_fd_;
69 79
70 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); 80 DISALLOW_COPY_AND_ASSIGN(TCPAdoptServerSocketFactory);
71 }; 81 };
72 82 #endif // defined(OS_POSIX)
73 } // namespace 83 } // namespace
74 84
75 void StartLocalDevToolsHttpHandler(HeadlessBrowser::Options* options) { 85 void StartLocalDevToolsHttpHandler(HeadlessBrowser::Options* options) {
76 std::unique_ptr<content::DevToolsSocketFactory> socket_factory; 86 std::unique_ptr<content::DevToolsSocketFactory> socket_factory;
77 if (options->devtools_socket_fd == 0) { 87 if (options->devtools_socket_fd == 0) {
78 const net::IPEndPoint& endpoint = options->devtools_endpoint; 88 const net::IPEndPoint& endpoint = options->devtools_endpoint;
79 socket_factory.reset(new TCPServerSocketFactory(endpoint)); 89 socket_factory.reset(new TCPEndpointServerSocketFactory(endpoint));
80 } else { 90 } else {
91 #if defined(OS_POSIX)
81 const uint16_t socket_fd = options->devtools_socket_fd; 92 const uint16_t socket_fd = options->devtools_socket_fd;
82 socket_factory.reset(new TCPServerSocketFactory(socket_fd)); 93 socket_factory.reset(new TCPAdoptServerSocketFactory(socket_fd));
94 #else
95 LOG(ERROR) << "Can't inherit an open socket on non-Posix systems";
mcgreevy 2017/05/23 04:31:07 In this case, socket_factory will be a null pointe
Raul Vera 2017/05/24 00:22:30 Good point. Done.
96 #endif
83 } 97 }
84 content::DevToolsAgentHost::StartRemoteDebuggingServer( 98 content::DevToolsAgentHost::StartRemoteDebuggingServer(
85 std::move(socket_factory), std::string(), 99 std::move(socket_factory), std::string(),
86 options->user_data_dir, // TODO(altimin): Figure a proper value for this. 100 options->user_data_dir, // TODO(altimin): Figure a proper value for this.
87 base::FilePath(), options->product_name_and_version, options->user_agent); 101 base::FilePath(), options->product_name_and_version, options->user_agent);
88 } 102 }
89 103
90 void StopLocalDevToolsHttpHandler() { 104 void StopLocalDevToolsHttpHandler() {
91 content::DevToolsAgentHost::StopRemoteDebuggingServer(); 105 content::DevToolsAgentHost::StopRemoteDebuggingServer();
92 } 106 }
93 107
94 } // namespace headless 108 } // 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