Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / chromecast / browser / devtools / remote_debugging_server.cc
blob29aa7d8980adff5a828b8280a464882ba4c32554
1 // Copyright 2014 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 "chromecast/browser/devtools/remote_debugging_server.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/strings/stringprintf.h"
12 #include "chromecast/browser/cast_browser_process.h"
13 #include "chromecast/browser/devtools/cast_dev_tools_delegate.h"
14 #include "chromecast/common/pref_names.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/devtools_http_handler.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/common/user_agent.h"
20 #include "net/base/net_errors.h"
21 #include "net/socket/tcp_server_socket.h"
23 #if defined(OS_ANDROID)
24 #include "content/public/browser/android/devtools_auth.h"
25 #include "net/socket/unix_domain_server_socket_posix.h"
26 #endif // defined(OS_ANDROID)
28 namespace chromecast {
29 namespace shell {
31 namespace {
33 const char kFrontEndURL[] =
34 "https://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html";
35 const uint16 kDefaultRemoteDebuggingPort = 9222;
37 const int kBackLog = 10;
39 #if defined(OS_ANDROID)
40 class UnixDomainServerSocketFactory
41 : public content::DevToolsHttpHandler::ServerSocketFactory {
42 public:
43 explicit UnixDomainServerSocketFactory(const std::string& socket_name)
44 : socket_name_(socket_name) {}
46 private:
47 // content::DevToolsHttpHandler::ServerSocketFactory.
48 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
49 scoped_ptr<net::ServerSocket> socket(
50 new net::UnixDomainServerSocket(
51 base::Bind(&content::CanUserConnectToDevTools),
52 true /* use_abstract_namespace */));
53 if (socket->ListenWithAddressAndPort(socket_name_, 0, kBackLog) != net::OK)
54 return scoped_ptr<net::ServerSocket>();
56 return socket;
59 std::string socket_name_;
61 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
63 #else
64 class TCPServerSocketFactory
65 : public content::DevToolsHttpHandler::ServerSocketFactory {
66 public:
67 TCPServerSocketFactory(const std::string& address, uint16 port)
68 : address_(address), port_(port) {
71 private:
72 // content::DevToolsHttpHandler::ServerSocketFactory.
73 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
74 scoped_ptr<net::ServerSocket> socket(
75 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
76 if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK)
77 return scoped_ptr<net::ServerSocket>();
79 return socket;
82 std::string address_;
83 uint16 port_;
85 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
87 #endif
89 scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>
90 CreateSocketFactory(uint16 port) {
91 #if defined(OS_ANDROID)
92 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
93 std::string socket_name = "cast_shell_devtools_remote";
94 if (command_line->HasSwitch(switches::kRemoteDebuggingSocketName)) {
95 socket_name = command_line->GetSwitchValueASCII(
96 switches::kRemoteDebuggingSocketName);
98 return scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>(
99 new UnixDomainServerSocketFactory(socket_name));
100 #else
101 return scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>(
102 new TCPServerSocketFactory("0.0.0.0", port));
103 #endif
106 std::string GetFrontendUrl() {
107 return base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str());
110 } // namespace
112 RemoteDebuggingServer::RemoteDebuggingServer() : port_(0) {
113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
114 pref_port_.Init(prefs::kRemoteDebuggingPort,
115 CastBrowserProcess::GetInstance()->pref_service(),
116 base::Bind(&RemoteDebuggingServer::OnPortChanged,
117 base::Unretained(this)));
119 // Starts new dev tools, clearing port number saved in config.
120 // Remote debugging in production must be triggered only by config server.
121 pref_port_.SetValue(ShouldStartImmediately() ?
122 kDefaultRemoteDebuggingPort : 0);
123 OnPortChanged();
126 RemoteDebuggingServer::~RemoteDebuggingServer() {
127 pref_port_.SetValue(0);
128 OnPortChanged();
131 void RemoteDebuggingServer::OnPortChanged() {
132 uint16 new_port = static_cast<uint16>(std::max(*pref_port_, 0));
133 VLOG(1) << "OnPortChanged called: old_port=" << port_
134 << ", new_port=" << new_port;
136 if (new_port == port_) {
137 VLOG(1) << "Port has not been changed. Ignore silently.";
138 return;
141 if (devtools_http_handler_) {
142 LOG(INFO) << "Stop old devtools: port=" << port_;
143 devtools_http_handler_.reset();
146 port_ = new_port;
147 if (port_ > 0) {
148 devtools_http_handler_.reset(content::DevToolsHttpHandler::Start(
149 CreateSocketFactory(port_),
150 GetFrontendUrl(),
151 new CastDevToolsDelegate(),
152 base::FilePath()));
153 LOG(INFO) << "Devtools started: port=" << port_;
157 } // namespace shell
158 } // namespace chromecast