Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / devtools / remote_debugging_server.cc
blobca8de35c56b1d6c2e8e800d74787674807573ae3
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/browser/ui/webui/devtools_ui.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "content/public/browser/devtools_http_handler.h"
14 #include "net/base/net_errors.h"
15 #include "net/socket/tcp_server_socket.h"
17 namespace {
19 base::LazyInstance<bool>::Leaky g_tethering_enabled = LAZY_INSTANCE_INITIALIZER;
21 const uint16 kMinTetheringPort = 9333;
22 const uint16 kMaxTetheringPort = 9444;
23 const int kBackLog = 10;
25 class TCPServerSocketFactory
26 : public content::DevToolsHttpHandler::ServerSocketFactory {
27 public:
28 TCPServerSocketFactory(const std::string& address, uint16 port)
29 : address_(address),
30 port_(port),
31 last_tethering_port_(kMinTetheringPort) {
34 private:
35 // content::DevToolsHttpHandler::ServerSocketFactory.
36 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
37 scoped_ptr<net::ServerSocket> socket(
38 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
39 if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK)
40 return scoped_ptr<net::ServerSocket>();
42 return socket;
45 scoped_ptr<net::ServerSocket> CreateForTethering(std::string* name) override {
46 if (!g_tethering_enabled.Get())
47 return scoped_ptr<net::ServerSocket>();
49 if (last_tethering_port_ == kMaxTetheringPort)
50 last_tethering_port_ = kMinTetheringPort;
51 uint16 port = ++last_tethering_port_;
52 *name = base::IntToString(port);
53 scoped_ptr<net::TCPServerSocket> socket(
54 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
55 if (socket->ListenWithAddressAndPort("127.0.0.1", port, kBackLog) !=
56 net::OK) {
57 return scoped_ptr<net::ServerSocket>();
59 return socket.Pass();
62 std::string address_;
63 uint16 port_;
64 uint16 last_tethering_port_;
66 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
69 } // namespace
71 // static
72 void RemoteDebuggingServer::EnableTetheringForDebug() {
73 g_tethering_enabled.Get() = true;
76 RemoteDebuggingServer::RemoteDebuggingServer(
77 chrome::HostDesktopType host_desktop_type,
78 const std::string& ip,
79 uint16 port) {
80 base::FilePath output_dir;
81 if (!port) {
82 // The client requested an ephemeral port. Must write the selected
83 // port to a well-known location in the profile directory to
84 // bootstrap the connection process.
85 bool result = PathService::Get(chrome::DIR_USER_DATA, &output_dir);
86 DCHECK(result);
89 devtools_http_handler_.reset(content::DevToolsHttpHandler::Start(
90 make_scoped_ptr(new TCPServerSocketFactory(ip, port)),
91 std::string(),
92 new BrowserListTabContentsProvider(host_desktop_type),
93 output_dir));
96 RemoteDebuggingServer::~RemoteDebuggingServer() {