OLD | NEW |
| (Empty) |
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_server_socket.h" | |
21 | |
22 #if defined(OS_ANDROID) | |
23 #include "content/public/browser/android/devtools_auth.h" | |
24 #include "net/socket/unix_domain_server_socket_posix.h" | |
25 #endif // defined(OS_ANDROID) | |
26 | |
27 namespace chromecast { | |
28 namespace shell { | |
29 | |
30 namespace { | |
31 | |
32 #if defined(OS_ANDROID) | |
33 const char kFrontEndURL[] = | |
34 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html"; | |
35 #endif // defined(OS_ANDROID) | |
36 const int kDefaultRemoteDebuggingPort = 9222; | |
37 | |
38 #if defined(OS_ANDROID) | |
39 class UnixDomainServerSocketFactory | |
40 : public content::DevToolsHttpHandler::ServerSocketFactory { | |
41 public: | |
42 explicit UnixDomainServerSocketFactory(const std::string& socket_name) | |
43 : content::DevToolsHttpHandler::ServerSocketFactory(socket_name, 0, 1) {} | |
44 | |
45 private: | |
46 // content::DevToolsHttpHandler::ServerSocketFactory. | |
47 virtual scoped_ptr<net::ServerSocket> Create() const override { | |
48 return scoped_ptr<net::ServerSocket>( | |
49 new net::UnixDomainServerSocket( | |
50 base::Bind(&content::CanUserConnectToDevTools), | |
51 true /* use_abstract_namespace */)); | |
52 } | |
53 | |
54 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory); | |
55 }; | |
56 #else | |
57 class TCPServerSocketFactory | |
58 : public content::DevToolsHttpHandler::ServerSocketFactory { | |
59 public: | |
60 TCPServerSocketFactory(const std::string& address, int port, int backlog) | |
61 : content::DevToolsHttpHandler::ServerSocketFactory( | |
62 address, port, backlog) {} | |
63 | |
64 private: | |
65 // content::DevToolsHttpHandler::ServerSocketFactory. | |
66 virtual scoped_ptr<net::ServerSocket> Create() const override { | |
67 return scoped_ptr<net::ServerSocket>( | |
68 new net::TCPServerSocket(NULL, net::NetLog::Source())); | |
69 } | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); | |
72 }; | |
73 #endif | |
74 | |
75 scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory> | |
76 CreateSocketFactory(int port) { | |
77 #if defined(OS_ANDROID) | |
78 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
79 std::string socket_name = "content_shell_devtools_remote"; | |
80 if (command_line->HasSwitch(switches::kRemoteDebuggingSocketName)) { | |
81 socket_name = command_line->GetSwitchValueASCII( | |
82 switches::kRemoteDebuggingSocketName); | |
83 } | |
84 return scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>( | |
85 new UnixDomainServerSocketFactory(socket_name)); | |
86 #else | |
87 return scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>( | |
88 new TCPServerSocketFactory("0.0.0.0", port, 1)); | |
89 #endif | |
90 } | |
91 | |
92 std::string GetFrontendUrl() { | |
93 #if defined(OS_ANDROID) | |
94 return base::StringPrintf(kFrontEndURL, content::GetWebKitRevision().c_str()); | |
95 #else | |
96 return std::string(); | |
97 #endif // defined(OS_ANDROID) | |
98 } | |
99 | |
100 } // namespace | |
101 | |
102 RemoteDebuggingServer::RemoteDebuggingServer() | |
103 : devtools_http_handler_(NULL), | |
104 port_(0) { | |
105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
106 pref_port_.Init(prefs::kRemoteDebuggingPort, | |
107 ChromecastConfig::GetInstance()->pref_service(), | |
108 base::Bind(&RemoteDebuggingServer::OnPortChanged, | |
109 base::Unretained(this))); | |
110 | |
111 // Starts new dev tools, clearing port number saved in config. | |
112 // Remote debugging in production must be triggered only by config server. | |
113 pref_port_.SetValue(ShouldStartImmediately() ? | |
114 kDefaultRemoteDebuggingPort : 0); | |
115 OnPortChanged(); | |
116 } | |
117 | |
118 RemoteDebuggingServer::~RemoteDebuggingServer() { | |
119 pref_port_.SetValue(0); | |
120 OnPortChanged(); | |
121 } | |
122 | |
123 void RemoteDebuggingServer::OnPortChanged() { | |
124 int new_port = *pref_port_; | |
125 if (new_port < 0) { | |
126 new_port = 0; | |
127 } | |
128 VLOG(1) << "OnPortChanged called: old_port=" << port_ | |
129 << ", new_port=" << new_port; | |
130 | |
131 if (new_port == port_) { | |
132 VLOG(1) << "Port has not been changed. Ignore silently."; | |
133 return; | |
134 } | |
135 | |
136 if (devtools_http_handler_) { | |
137 LOG(INFO) << "Stop old devtools: port=" << port_; | |
138 // Note: Stop destroys devtools_http_handler_. | |
139 devtools_http_handler_->Stop(); | |
140 devtools_http_handler_ = NULL; | |
141 } | |
142 | |
143 port_ = new_port; | |
144 if (port_ > 0) { | |
145 devtools_http_handler_ = content::DevToolsHttpHandler::Start( | |
146 CreateSocketFactory(port_), | |
147 GetFrontendUrl(), | |
148 new CastDevToolsDelegate(), | |
149 base::FilePath()); | |
150 LOG(INFO) << "Devtools started: port=" << port_; | |
151 } | |
152 } | |
153 | |
154 } // namespace shell | |
155 } // namespace chromecast | |
OLD | NEW |