blob: 13f91e9913dac09428252bb9795a2ff386022e64 [file] [log] [blame]
[email protected]de7d61ff2013-08-20 11:30:411// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]ee75b8992012-01-27 07:53:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]de7d61ff2013-08-20 11:30:415#include "content/shell/browser/shell_devtools_delegate.h"
[email protected]ee75b8992012-01-27 07:53:576
[email protected]90e6c5412013-04-16 22:45:257#include <vector>
8
[email protected]ed6635e2012-10-27 00:44:099#include "base/bind.h"
[email protected]90e6c5412013-04-16 22:45:2510#include "base/command_line.h"
[email protected]0e60fce2014-06-04 22:27:2011#include "base/files/file_path.h"
[email protected]90e6c5412013-04-16 22:45:2512#include "base/strings/string_number_conversions.h"
[email protected]8dffd2c2013-12-02 14:14:3813#include "base/strings/stringprintf.h"
[email protected]852b34ba2013-10-03 16:29:0614#include "base/strings/utf_string_conversions.h"
15#include "content/public/browser/devtools_agent_host.h"
[email protected]ee75b8992012-01-27 07:53:5716#include "content/public/browser/devtools_http_handler.h"
[email protected]852b34ba2013-10-03 16:29:0617#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"
[email protected]eb040992012-10-10 16:56:0021#include "content/public/browser/web_contents.h"
[email protected]90e6c5412013-04-16 22:45:2522#include "content/public/common/content_switches.h"
[email protected]eb040992012-10-10 16:56:0023#include "content/public/common/url_constants.h"
[email protected]efd2c3f2014-03-11 09:57:2524#include "content/public/common/user_agent.h"
[email protected]de7d61ff2013-08-20 11:30:4125#include "content/shell/browser/shell.h"
[email protected]ee75b8992012-01-27 07:53:5726#include "grit/shell_resources.h"
byungchul38c3ae72014-08-25 23:27:4627#include "net/socket/tcp_server_socket.h"
[email protected]ee75b8992012-01-27 07:53:5728#include "ui/base/resource/resource_bundle.h"
29
[email protected]ed6635e2012-10-27 00:44:0930#if defined(OS_ANDROID)
31#include "content/public/browser/android/devtools_auth.h"
byungchul38c3ae72014-08-25 23:27:4632#include "net/socket/unix_domain_server_socket_posix.h"
[email protected]90e6c5412013-04-16 22:45:2533#endif
[email protected]ed6635e2012-10-27 00:44:0934
[email protected]852b34ba2013-10-03 16:29:0635using content::DevToolsAgentHost;
36using content::RenderViewHost;
37using content::WebContents;
38
[email protected]ed6635e2012-10-27 00:44:0939namespace {
[email protected]90e6c5412013-04-16 22:45:2540
[email protected]8dffd2c2013-12-02 14:14:3841#if defined(OS_ANDROID)
42const char kFrontEndURL[] =
43 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
44#endif
[email protected]852b34ba2013-10-03 16:29:0645const char kTargetTypePage[] = "page";
vkuzkokove1133eb2014-08-25 13:33:1646const char kTargetTypeServiceWorker[] = "service_worker";
47const char kTargetTypeOther[] = "other";
[email protected]852b34ba2013-10-03 16:29:0648
byungchul38c3ae72014-08-25 23:27:4649#if defined(OS_ANDROID)
50class UnixDomainServerSocketFactory
51 : public content::DevToolsHttpHandler::ServerSocketFactory {
52 public:
53 explicit UnixDomainServerSocketFactory(const std::string& socket_name)
54 : content::DevToolsHttpHandler::ServerSocketFactory(socket_name, 0, 1) {}
55
56 private:
57 // content::DevToolsHttpHandler::ServerSocketFactory.
58 virtual scoped_ptr<net::ServerSocket> Create() const OVERRIDE {
59 return scoped_ptr<net::ServerSocket>(
60 new net::UnixDomainServerSocket(
61 base::Bind(&content::CanUserConnectToDevTools),
62 true /* use_abstract_namespace */));
63 }
64
65 DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory);
66};
67#else
68class TCPServerSocketFactory
69 : public content::DevToolsHttpHandler::ServerSocketFactory {
70 public:
71 TCPServerSocketFactory(const std::string& address, int port, int backlog)
72 : content::DevToolsHttpHandler::ServerSocketFactory(
73 address, port, backlog) {}
74
75 private:
76 // content::DevToolsHttpHandler::ServerSocketFactory.
77 virtual scoped_ptr<net::ServerSocket> Create() const OVERRIDE {
78 return scoped_ptr<net::ServerSocket>(
79 new net::TCPServerSocket(NULL, net::NetLog::Source()));
80 }
81
82 DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory);
83};
84#endif
85
86scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>
87CreateSocketFactory() {
[email protected]90e6c5412013-04-16 22:45:2588 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
89#if defined(OS_ANDROID)
90 std::string socket_name = "content_shell_devtools_remote";
91 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
92 socket_name = command_line.GetSwitchValueASCII(
93 switches::kRemoteDebuggingSocketName);
94 }
byungchul38c3ae72014-08-25 23:27:4695 return scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>(
96 new UnixDomainServerSocketFactory(socket_name));
[email protected]90e6c5412013-04-16 22:45:2597#else
98 // See if the user specified a port on the command line (useful for
99 // automation). If not, use an ephemeral port by specifying 0.
100 int port = 0;
101 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
102 int temp_port;
103 std::string port_str =
104 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
105 if (base::StringToInt(port_str, &temp_port) &&
106 temp_port > 0 && temp_port < 65535) {
107 port = temp_port;
108 } else {
109 DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
110 }
111 }
byungchul38c3ae72014-08-25 23:27:46112 return scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>(
113 new TCPServerSocketFactory("127.0.0.1", port, 1));
[email protected]ed6635e2012-10-27 00:44:09114#endif
[email protected]90e6c5412013-04-16 22:45:25115}
[email protected]852b34ba2013-10-03 16:29:06116
117class Target : public content::DevToolsTarget {
118 public:
vkuzkokove1133eb2014-08-25 13:33:16119 explicit Target(scoped_refptr<DevToolsAgentHost> agent_host);
[email protected]852b34ba2013-10-03 16:29:06120
vkuzkokove1133eb2014-08-25 13:33:16121 virtual std::string GetId() const OVERRIDE { return agent_host_->GetId(); }
[email protected]002195f2014-06-02 07:33:32122 virtual std::string GetParentId() const OVERRIDE { return std::string(); }
vkuzkokove1133eb2014-08-25 13:33:16123 virtual std::string GetType() const OVERRIDE {
124 switch (agent_host_->GetType()) {
125 case DevToolsAgentHost::TYPE_WEB_CONTENTS:
126 return kTargetTypePage;
127 case DevToolsAgentHost::TYPE_SERVICE_WORKER:
128 return kTargetTypeServiceWorker;
129 default:
130 break;
131 }
132 return kTargetTypeOther;
133 }
134 virtual std::string GetTitle() const OVERRIDE {
135 return agent_host_->GetTitle();
136 }
[email protected]852b34ba2013-10-03 16:29:06137 virtual std::string GetDescription() const OVERRIDE { return std::string(); }
vkuzkokove1133eb2014-08-25 13:33:16138 virtual GURL GetURL() const OVERRIDE { return agent_host_->GetURL(); }
[email protected]657d2732014-05-07 10:09:51139 virtual GURL GetFaviconURL() const OVERRIDE { return favicon_url_; }
[email protected]852b34ba2013-10-03 16:29:06140 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
141 return last_activity_time_;
142 }
143 virtual bool IsAttached() const OVERRIDE {
144 return agent_host_->IsAttached();
145 }
146 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
147 return agent_host_;
148 }
149 virtual bool Activate() const OVERRIDE;
150 virtual bool Close() const OVERRIDE;
151
152 private:
153 scoped_refptr<DevToolsAgentHost> agent_host_;
[email protected]852b34ba2013-10-03 16:29:06154 GURL favicon_url_;
155 base::TimeTicks last_activity_time_;
156};
157
vkuzkokove1133eb2014-08-25 13:33:16158Target::Target(scoped_refptr<DevToolsAgentHost> agent_host)
159 : agent_host_(agent_host) {
160 if (WebContents* web_contents = agent_host_->GetWebContents()) {
161 content::NavigationController& controller = web_contents->GetController();
162 content::NavigationEntry* entry = controller.GetActiveEntry();
163 if (entry != NULL && entry->GetURL().is_valid())
164 favicon_url_ = entry->GetFavicon().url;
165 last_activity_time_ = web_contents->GetLastActiveTime();
166 }
[email protected]852b34ba2013-10-03 16:29:06167}
168
169bool Target::Activate() const {
vkuzkokove1133eb2014-08-25 13:33:16170 return agent_host_->Activate();
[email protected]852b34ba2013-10-03 16:29:06171}
172
173bool Target::Close() const {
vkuzkokove1133eb2014-08-25 13:33:16174 return agent_host_->Close();
[email protected]852b34ba2013-10-03 16:29:06175}
176
[email protected]90e6c5412013-04-16 22:45:25177} // namespace
[email protected]ed6635e2012-10-27 00:44:09178
[email protected]ee75b8992012-01-27 07:53:57179namespace content {
180
[email protected]90e6c5412013-04-16 22:45:25181ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context)
[email protected]eb040992012-10-10 16:56:00182 : browser_context_(browser_context) {
[email protected]8dffd2c2013-12-02 14:14:38183 std::string frontend_url;
184#if defined(OS_ANDROID)
[email protected]efd2c3f2014-03-11 09:57:25185 frontend_url = base::StringPrintf(kFrontEndURL, GetWebKitRevision().c_str());
[email protected]8dffd2c2013-12-02 14:14:38186#endif
[email protected]90e6c5412013-04-16 22:45:25187 devtools_http_handler_ =
[email protected]0e60fce2014-06-04 22:27:20188 DevToolsHttpHandler::Start(CreateSocketFactory(), frontend_url, this,
189 base::FilePath());
[email protected]ee75b8992012-01-27 07:53:57190}
191
192ShellDevToolsDelegate::~ShellDevToolsDelegate() {
193}
194
195void ShellDevToolsDelegate::Stop() {
196 // The call below destroys this.
197 devtools_http_handler_->Stop();
198}
199
[email protected]ee75b8992012-01-27 07:53:57200std::string ShellDevToolsDelegate::GetDiscoveryPageHTML() {
[email protected]8dffd2c2013-12-02 14:14:38201#if defined(OS_ANDROID)
202 return std::string();
203#else
[email protected]ee75b8992012-01-27 07:53:57204 return ResourceBundle::GetSharedInstance().GetRawDataResource(
[email protected]4d8bb1a92012-11-01 21:12:40205 IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
[email protected]8dffd2c2013-12-02 14:14:38206#endif
[email protected]ee75b8992012-01-27 07:53:57207}
208
[email protected]ee75b8992012-01-27 07:53:57209bool ShellDevToolsDelegate::BundlesFrontendResources() {
[email protected]8dffd2c2013-12-02 14:14:38210#if defined(OS_ANDROID)
211 return false;
212#else
[email protected]ee75b8992012-01-27 07:53:57213 return true;
[email protected]8dffd2c2013-12-02 14:14:38214#endif
[email protected]ee75b8992012-01-27 07:53:57215}
216
[email protected]d30a36f2013-02-07 04:16:26217base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() {
218 return base::FilePath();
[email protected]ee75b8992012-01-27 07:53:57219}
220
[email protected]e5ea93b2012-09-27 05:50:36221std::string ShellDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
[email protected]007b3f82013-04-09 08:46:45222 return std::string();
[email protected]e5ea93b2012-09-27 05:50:36223}
224
[email protected]c78ecec212013-10-27 19:34:10225scoped_ptr<DevToolsTarget>
226ShellDevToolsDelegate::CreateNewTarget(const GURL& url) {
[email protected]eb040992012-10-10 16:56:00227 Shell* shell = Shell::CreateNewWindow(browser_context_,
[email protected]c78ecec212013-10-27 19:34:10228 url,
[email protected]eb040992012-10-10 16:56:00229 NULL,
230 MSG_ROUTING_NONE,
[email protected]cdb806722013-01-10 14:18:23231 gfx::Size());
vkuzkokove1133eb2014-08-25 13:33:16232 return scoped_ptr<DevToolsTarget>(
233 new Target(DevToolsAgentHost::GetOrCreateFor(shell->web_contents())));
[email protected]eb040992012-10-10 16:56:00234}
235
[email protected]852b34ba2013-10-03 16:29:06236void ShellDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
237 TargetList targets;
vkuzkokove1133eb2014-08-25 13:33:16238 content::DevToolsAgentHost::List agents =
239 content::DevToolsAgentHost::GetOrCreateAll();
240 for (content::DevToolsAgentHost::List::iterator it = agents.begin();
241 it != agents.end(); ++it) {
[email protected]b3f957e62014-08-08 10:09:02242 targets.push_back(new Target(*it));
[email protected]852b34ba2013-10-03 16:29:06243 }
244 callback.Run(targets);
[email protected]2fb8c1d2013-03-11 22:00:17245}
246
[email protected]8b62f8e2013-09-03 19:03:06247scoped_ptr<net::StreamListenSocket>
[email protected]eafc8452013-04-16 04:18:35248ShellDevToolsDelegate::CreateSocketForTethering(
249 net::StreamListenSocket::Delegate* delegate,
250 std::string* name) {
[email protected]8b62f8e2013-09-03 19:03:06251 return scoped_ptr<net::StreamListenSocket>();
[email protected]eafc8452013-04-16 04:18:35252}
253
[email protected]ee75b8992012-01-27 07:53:57254} // namespace content