Give names to all utility processes.
[chromium-blink-merge.git] / content / shell / browser / shell_devtools_manager_delegate.cc
blob2b52385ed8a47e3300e76124ed5c181b3254233b
1 // Copyright 2013 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 "content/shell/browser/shell_devtools_manager_delegate.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "content/public/browser/devtools_agent_host.h"
16 #include "content/public/browser/devtools_http_handler.h"
17 #include "content/public/browser/devtools_target.h"
18 #include "content/public/browser/favicon_status.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/render_view_host.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/common/url_constants.h"
24 #include "content/public/common/user_agent.h"
25 #include "content/shell/browser/shell.h"
26 #include "grit/shell_resources.h"
27 #include "net/base/net_errors.h"
28 #include "net/socket/tcp_server_socket.h"
29 #include "ui/base/resource/resource_bundle.h"
31 #if defined(OS_ANDROID)
32 #include "content/public/browser/android/devtools_auth.h"
33 #include "net/socket/unix_domain_server_socket_posix.h"
34 #endif
36 namespace content {
38 namespace {
40 #if defined(OS_ANDROID)
41 const char kFrontEndURL[] =
42 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html";
43 #endif
44 const char kTargetTypePage[] = "page";
45 const char kTargetTypeServiceWorker[] = "service_worker";
46 const char kTargetTypeOther[] = "other";
48 const int kBackLog = 10;
50 #if defined(OS_ANDROID)
51 class UnixDomainServerSocketFactory
52 : public DevToolsHttpHandler::ServerSocketFactory {
53 public:
54 explicit UnixDomainServerSocketFactory(const std::string& socket_name)
55 : socket_name_(socket_name) {}
57 private:
58 // DevToolsHttpHandler::ServerSocketFactory.
59 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
60 scoped_ptr<net::ServerSocket> socket(
61 new net::UnixDomainServerSocket(
62 base::Bind(&CanUserConnectToDevTools),
63 true /* use_abstract_namespace */));
64 if (socket->ListenWithAddressAndPort(socket_name_, 0, kBackLog) != net::OK)
65 return scoped_ptr<net::ServerSocket>();
67 return socket;
70 std::string socket_name_;
72 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
74 #else
75 class TCPServerSocketFactory
76 : public DevToolsHttpHandler::ServerSocketFactory {
77 public:
78 TCPServerSocketFactory(const std::string& address, uint16 port)
79 : address_(address), port_(port) {
82 private:
83 // DevToolsHttpHandler::ServerSocketFactory.
84 scoped_ptr<net::ServerSocket> CreateForHttpServer() override {
85 scoped_ptr<net::ServerSocket> socket(
86 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
87 if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK)
88 return scoped_ptr<net::ServerSocket>();
90 return socket;
93 std::string address_;
94 uint16 port_;
96 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
98 #endif
100 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory>
101 CreateSocketFactory() {
102 const base::CommandLine& command_line =
103 *base::CommandLine::ForCurrentProcess();
104 #if defined(OS_ANDROID)
105 std::string socket_name = "content_shell_devtools_remote";
106 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
107 socket_name = command_line.GetSwitchValueASCII(
108 switches::kRemoteDebuggingSocketName);
110 return scoped_ptr<DevToolsHttpHandler::ServerSocketFactory>(
111 new UnixDomainServerSocketFactory(socket_name));
112 #else
113 // See if the user specified a port on the command line (useful for
114 // automation). If not, use an ephemeral port by specifying 0.
115 uint16 port = 0;
116 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
117 int temp_port;
118 std::string port_str =
119 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
120 if (base::StringToInt(port_str, &temp_port) &&
121 temp_port > 0 && temp_port < 65535) {
122 port = static_cast<uint16>(temp_port);
123 } else {
124 DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
127 return scoped_ptr<DevToolsHttpHandler::ServerSocketFactory>(
128 new TCPServerSocketFactory("127.0.0.1", port));
129 #endif
132 class Target : public DevToolsTarget {
133 public:
134 explicit Target(scoped_refptr<DevToolsAgentHost> agent_host);
136 std::string GetId() const override { return agent_host_->GetId(); }
137 std::string GetParentId() const override { return std::string(); }
138 std::string GetType() const override {
139 switch (agent_host_->GetType()) {
140 case DevToolsAgentHost::TYPE_WEB_CONTENTS:
141 return kTargetTypePage;
142 case DevToolsAgentHost::TYPE_SERVICE_WORKER:
143 return kTargetTypeServiceWorker;
144 default:
145 break;
147 return kTargetTypeOther;
149 std::string GetTitle() const override { return agent_host_->GetTitle(); }
150 std::string GetDescription() const override { return std::string(); }
151 GURL GetURL() const override { return agent_host_->GetURL(); }
152 GURL GetFaviconURL() const override { return favicon_url_; }
153 base::TimeTicks GetLastActivityTime() const override {
154 return last_activity_time_;
156 bool IsAttached() const override { return agent_host_->IsAttached(); }
157 scoped_refptr<DevToolsAgentHost> GetAgentHost() const override {
158 return agent_host_;
160 bool Activate() const override;
161 bool Close() const override;
163 private:
164 scoped_refptr<DevToolsAgentHost> agent_host_;
165 GURL favicon_url_;
166 base::TimeTicks last_activity_time_;
169 Target::Target(scoped_refptr<DevToolsAgentHost> agent_host)
170 : agent_host_(agent_host) {
171 if (WebContents* web_contents = agent_host_->GetWebContents()) {
172 NavigationController& controller = web_contents->GetController();
173 NavigationEntry* entry = controller.GetActiveEntry();
174 if (entry != NULL && entry->GetURL().is_valid())
175 favicon_url_ = entry->GetFavicon().url;
176 last_activity_time_ = web_contents->GetLastActiveTime();
180 bool Target::Activate() const {
181 return agent_host_->Activate();
184 bool Target::Close() const {
185 return agent_host_->Close();
188 // ShellDevToolsDelegate ----------------------------------------------------
190 class ShellDevToolsDelegate : public DevToolsHttpHandlerDelegate {
191 public:
192 explicit ShellDevToolsDelegate(BrowserContext* browser_context);
193 ~ShellDevToolsDelegate() override;
195 // DevToolsHttpHandlerDelegate implementation.
196 std::string GetDiscoveryPageHTML() override;
197 bool BundlesFrontendResources() override;
198 base::FilePath GetDebugFrontendDir() override;
200 private:
201 BrowserContext* browser_context_;
203 DISALLOW_COPY_AND_ASSIGN(ShellDevToolsDelegate);
206 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context)
207 : browser_context_(browser_context) {
210 ShellDevToolsDelegate::~ShellDevToolsDelegate() {
213 std::string ShellDevToolsDelegate::GetDiscoveryPageHTML() {
214 #if defined(OS_ANDROID)
215 return std::string();
216 #else
217 return ResourceBundle::GetSharedInstance().GetRawDataResource(
218 IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
219 #endif
222 bool ShellDevToolsDelegate::BundlesFrontendResources() {
223 #if defined(OS_ANDROID)
224 return false;
225 #else
226 return true;
227 #endif
230 base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() {
231 return base::FilePath();
234 } // namespace
236 // ShellDevToolsManagerDelegate ----------------------------------------------
238 // static
239 DevToolsHttpHandler*
240 ShellDevToolsManagerDelegate::CreateHttpHandler(
241 BrowserContext* browser_context) {
242 std::string frontend_url;
243 #if defined(OS_ANDROID)
244 frontend_url = base::StringPrintf(kFrontEndURL, GetWebKitRevision().c_str());
245 #endif
246 return DevToolsHttpHandler::Start(CreateSocketFactory(),
247 frontend_url,
248 new ShellDevToolsDelegate(browser_context),
249 base::FilePath());
252 ShellDevToolsManagerDelegate::ShellDevToolsManagerDelegate(
253 BrowserContext* browser_context)
254 : browser_context_(browser_context) {
257 ShellDevToolsManagerDelegate::~ShellDevToolsManagerDelegate() {
260 base::DictionaryValue* ShellDevToolsManagerDelegate::HandleCommand(
261 DevToolsAgentHost* agent_host,
262 base::DictionaryValue* command) {
263 return NULL;
266 std::string ShellDevToolsManagerDelegate::GetPageThumbnailData(
267 const GURL& url) {
268 return std::string();
271 scoped_ptr<DevToolsTarget>
272 ShellDevToolsManagerDelegate::CreateNewTarget(const GURL& url) {
273 Shell* shell = Shell::CreateNewWindow(browser_context_,
274 url,
275 NULL,
276 gfx::Size());
277 return scoped_ptr<DevToolsTarget>(
278 new Target(DevToolsAgentHost::GetOrCreateFor(shell->web_contents())));
281 void ShellDevToolsManagerDelegate::EnumerateTargets(TargetCallback callback) {
282 TargetList targets;
283 for (const auto& agent_host : DevToolsAgentHost::GetOrCreateAll()) {
284 targets.push_back(new Target(agent_host));
286 callback.Run(targets);
289 } // namespace content