Remove include for DevToolsUI class which is no longer used in this file
[chromium-blink-merge.git] / chrome / browser / devtools / remote_debugging_server.cc
blobd530eea9db3da40d681ae3787a6eb0f4c4e5bff0
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/devtools/remote_debugging_server.h"
7 #include "base/lazy_instance.h"
8 #include "base/path_service.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/devtools/browser_list_tabcontents_provider.h"
11 #include "chrome/common/chrome_paths.h"
12 #include "content/public/browser/devtools_http_handler.h"
13 #include "net/base/net_errors.h"
14 #include "net/socket/tcp_server_socket.h"
16 namespace {
18 base::LazyInstance<bool>::Leaky g_tethering_enabled = LAZY_INSTANCE_INITIALIZER;
20 const uint16 kMinTetheringPort = 9333;
21 const uint16 kMaxTetheringPort = 9444;
22 const int kBackLog = 10;
24 class TCPServerSocketFactory
25 : public content::DevToolsHttpHandler::ServerSocketFactory {
26 public:
27 TCPServerSocketFactory(const std::string& address, uint16 port)
28 : address_(address),
29 port_(port),
30 last_tethering_port_(kMinTetheringPort) {
33 private:
34 // content::DevToolsHttpHandler::ServerSocketFactory.
35 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
36 scoped_ptr<net::ServerSocket> socket(
37 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
38 if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK)
39 return scoped_ptr<net::ServerSocket>();
41 return socket;
44 scoped_ptr<net::ServerSocket> CreateForTethering(std::string* name) override {
45 if (!g_tethering_enabled.Get())
46 return scoped_ptr<net::ServerSocket>();
48 if (last_tethering_port_ == kMaxTetheringPort)
49 last_tethering_port_ = kMinTetheringPort;
50 uint16 port = ++last_tethering_port_;
51 *name = base::IntToString(port);
52 scoped_ptr<net::TCPServerSocket> socket(
53 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
54 if (socket->ListenWithAddressAndPort("127.0.0.1", port, kBackLog) !=
55 net::OK) {
56 return scoped_ptr<net::ServerSocket>();
58 return socket.Pass();
61 std::string address_;
62 uint16 port_;
63 uint16 last_tethering_port_;
65 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
68 } // namespace
70 // static
71 void RemoteDebuggingServer::EnableTetheringForDebug() {
72 g_tethering_enabled.Get() = true;
75 RemoteDebuggingServer::RemoteDebuggingServer(
76 chrome::HostDesktopType host_desktop_type,
77 const std::string& ip,
78 uint16 port) {
79 base::FilePath output_dir;
80 if (!port) {
81 // The client requested an ephemeral port. Must write the selected
82 // port to a well-known location in the profile directory to
83 // bootstrap the connection process.
84 bool result = PathService::Get(chrome::DIR_USER_DATA, &output_dir);
85 DCHECK(result);
88 devtools_http_handler_.reset(content::DevToolsHttpHandler::Start(
89 make_scoped_ptr(new TCPServerSocketFactory(ip, port)),
90 std::string(),
91 new BrowserListTabContentsProvider(host_desktop_type),
92 output_dir));
95 RemoteDebuggingServer::~RemoteDebuggingServer() {