Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / chromecast / browser / devtools / remote_debugging_server.cc
blob6391b6d63035597090abd92bee27b300f89b25d5
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/cast_content_client.h"
15 #include "chromecast/common/pref_names.h"
16 #include "components/devtools_http_handler/devtools_http_handler.h"
17 #include "content/public/browser/browser_context.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/common/content_switches.h"
20 #include "content/public/common/user_agent.h"
21 #include "net/base/net_errors.h"
22 #include "net/socket/tcp_server_socket.h"
24 #if defined(OS_ANDROID)
25 #include "content/public/browser/android/devtools_auth.h"
26 #include "net/socket/unix_domain_server_socket_posix.h"
27 #endif // defined(OS_ANDROID)
29 using devtools_http_handler::DevToolsHttpHandler;
31 namespace chromecast {
32 namespace shell {
34 namespace {
36 const char kFrontEndURL[] =
37 "https://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html";
38 const uint16 kDefaultRemoteDebuggingPort = 9222;
40 const int kBackLog = 10;
42 #if defined(OS_ANDROID)
43 class UnixDomainServerSocketFactory
44 : public DevToolsHttpHandler::ServerSocketFactory {
45 public:
46 explicit UnixDomainServerSocketFactory(const std::string& socket_name)
47 : socket_name_(socket_name) {}
49 private:
50 // devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory.
51 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
52 scoped_ptr<net::ServerSocket> socket(
53 new net::UnixDomainServerSocket(
54 base::Bind(&content::CanUserConnectToDevTools),
55 true /* use_abstract_namespace */));
56 if (socket->ListenWithAddressAndPort(socket_name_, 0, kBackLog) != net::OK)
57 return scoped_ptr<net::ServerSocket>();
59 return socket;
62 std::string socket_name_;
64 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
66 #else
67 class TCPServerSocketFactory
68 : public DevToolsHttpHandler::ServerSocketFactory {
69 public:
70 TCPServerSocketFactory(const std::string& address, uint16 port)
71 : address_(address), port_(port) {
74 private:
75 // devtools_http_handler::DevToolsHttpHandler::ServerSocketFactory.
76 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
77 scoped_ptr<net::ServerSocket> socket(
78 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
79 if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK)
80 return scoped_ptr<net::ServerSocket>();
82 return socket;
85 std::string address_;
86 uint16 port_;
88 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
90 #endif
92 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory>
93 CreateSocketFactory(uint16 port) {
94 #if defined(OS_ANDROID)
95 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
96 std::string socket_name = "cast_shell_devtools_remote";
97 if (command_line->HasSwitch(switches::kRemoteDebuggingSocketName)) {
98 socket_name = command_line->GetSwitchValueASCII(
99 switches::kRemoteDebuggingSocketName);
101 return scoped_ptr<DevToolsHttpHandler::ServerSocketFactory>(
102 new UnixDomainServerSocketFactory(socket_name));
103 #else
104 return scoped_ptr<DevToolsHttpHandler::ServerSocketFactory>(
105 new TCPServerSocketFactory("0.0.0.0", port));
106 #endif
109 std::string GetFrontendUrl() {
110 return base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str());
113 } // namespace
115 RemoteDebuggingServer::RemoteDebuggingServer() : port_(0) {
116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
117 pref_port_.Init(prefs::kRemoteDebuggingPort,
118 CastBrowserProcess::GetInstance()->pref_service(),
119 base::Bind(&RemoteDebuggingServer::OnPortChanged,
120 base::Unretained(this)));
122 // Starts new dev tools, clearing port number saved in config.
123 // Remote debugging in production must be triggered only by config server.
124 pref_port_.SetValue(ShouldStartImmediately() ?
125 kDefaultRemoteDebuggingPort : 0);
126 OnPortChanged();
129 RemoteDebuggingServer::~RemoteDebuggingServer() {
130 pref_port_.SetValue(0);
131 OnPortChanged();
134 void RemoteDebuggingServer::OnPortChanged() {
135 uint16 new_port = static_cast<uint16>(std::max(*pref_port_, 0));
136 VLOG(1) << "OnPortChanged called: old_port=" << port_
137 << ", new_port=" << new_port;
139 if (new_port == port_) {
140 VLOG(1) << "Port has not been changed. Ignore silently.";
141 return;
144 if (devtools_http_handler_) {
145 LOG(INFO) << "Stop old devtools: port=" << port_;
146 devtools_http_handler_.reset();
149 port_ = new_port;
150 if (port_ > 0) {
151 devtools_http_handler_.reset(new DevToolsHttpHandler(
152 CreateSocketFactory(port_),
153 GetFrontendUrl(),
154 new CastDevToolsDelegate(),
155 base::FilePath(),
156 base::FilePath(),
157 std::string(),
158 GetUserAgent()));
159 LOG(INFO) << "Devtools started: port=" << port_;
163 } // namespace shell
164 } // namespace chromecast