remote_debugging_server.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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.
4
5#include "chromecast/shell/browser/devtools/remote_debugging_server.h"
6
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/common/chromecast_config.h"
13#include "chromecast/common/pref_names.h"
14#include "chromecast/shell/browser/devtools/cast_dev_tools_delegate.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/socket/tcp_listen_socket.h"
21
22#if defined(OS_ANDROID)
23#include "content/public/browser/android/devtools_auth.h"
24#include "net/socket/unix_domain_socket_posix.h"
25#endif  // defined(OS_ANDROID)
26
27namespace chromecast {
28namespace shell {
29
30namespace {
31
32#if defined(OS_ANDROID)
33const char kFrontEndURL[] =
34    "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
35#endif  // defined(OS_ANDROID)
36const int kDefaultRemoteDebuggingPort = 9222;
37
38net::StreamListenSocketFactory* CreateSocketFactory(int port) {
39#if defined(OS_ANDROID)
40  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
41  std::string socket_name = "content_shell_devtools_remote";
42  if (command_line->HasSwitch(switches::kRemoteDebuggingSocketName)) {
43    socket_name = command_line->GetSwitchValueASCII(
44        switches::kRemoteDebuggingSocketName);
45  }
46  return new net::UnixDomainSocketWithAbstractNamespaceFactory(
47      socket_name, "", base::Bind(&content::CanUserConnectToDevTools));
48#else
49  return new net::TCPListenSocketFactory("0.0.0.0", port);
50#endif  // defined(OS_ANDROID)
51}
52
53std::string GetFrontendUrl() {
54#if defined(OS_ANDROID)
55  return base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str());
56#else
57  return std::string();
58#endif  // defined(OS_ANDROID)
59}
60
61}  // namespace
62
63RemoteDebuggingServer::RemoteDebuggingServer()
64    : devtools_http_handler_(NULL),
65      port_(0) {
66  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
67  pref_port_.Init(prefs::kRemoteDebuggingPort,
68                  ChromecastConfig::GetInstance()->pref_service(),
69                  base::Bind(&RemoteDebuggingServer::OnPortChanged,
70                             base::Unretained(this)));
71
72  // Starts new dev tools, clearing port number saved in config.
73  // Remote debugging in production must be triggered only by config server.
74  pref_port_.SetValue(ShouldStartImmediately() ?
75                      kDefaultRemoteDebuggingPort : 0);
76  OnPortChanged();
77}
78
79RemoteDebuggingServer::~RemoteDebuggingServer() {
80  pref_port_.SetValue(0);
81  OnPortChanged();
82}
83
84void RemoteDebuggingServer::OnPortChanged() {
85  int new_port = *pref_port_;
86  if (new_port < 0) {
87    new_port = 0;
88  }
89  VLOG(1) << "OnPortChanged called: old_port=" << port_
90          << ", new_port=" << new_port;
91
92  if (new_port == port_) {
93    VLOG(1) << "Port has not been changed. Ignore silently.";
94    return;
95  }
96
97  if (devtools_http_handler_) {
98    LOG(INFO) << "Stop old devtools: port=" << port_;
99    // Note: Stop destroys devtools_http_handler_.
100    devtools_http_handler_->Stop();
101    devtools_http_handler_ = NULL;
102  }
103
104  port_ = new_port;
105  if (port_ > 0) {
106    devtools_http_handler_ = content::DevToolsHttpHandler::Start(
107        CreateSocketFactory(port_),
108        GetFrontendUrl(),
109        new CastDevToolsDelegate(),
110        base::FilePath());
111    LOG(INFO) << "Devtools started: port=" << port_;
112  }
113}
114
115}  // namespace shell
116}  // namespace chromecast
117