blob: d8fcfb5b6bf1e6cd402ccd72bea3362cd24e535e [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"
11#include "base/strings/string_number_conversions.h"
[email protected]852b34ba2013-10-03 16:29:0612#include "base/strings/utf_string_conversions.h"
13#include "content/public/browser/devtools_agent_host.h"
[email protected]ee75b8992012-01-27 07:53:5714#include "content/public/browser/devtools_http_handler.h"
[email protected]852b34ba2013-10-03 16:29:0615#include "content/public/browser/devtools_target.h"
16#include "content/public/browser/favicon_status.h"
17#include "content/public/browser/navigation_entry.h"
18#include "content/public/browser/render_view_host.h"
[email protected]eb040992012-10-10 16:56:0019#include "content/public/browser/web_contents.h"
[email protected]90e6c5412013-04-16 22:45:2520#include "content/public/common/content_switches.h"
[email protected]eb040992012-10-10 16:56:0021#include "content/public/common/url_constants.h"
[email protected]de7d61ff2013-08-20 11:30:4122#include "content/shell/browser/shell.h"
[email protected]ee75b8992012-01-27 07:53:5723#include "grit/shell_resources.h"
[email protected]1946d9e2013-04-09 01:43:3724#include "net/socket/tcp_listen_socket.h"
[email protected]ee75b8992012-01-27 07:53:5725#include "ui/base/resource/resource_bundle.h"
26
[email protected]ed6635e2012-10-27 00:44:0927#if defined(OS_ANDROID)
28#include "content/public/browser/android/devtools_auth.h"
[email protected]1946d9e2013-04-09 01:43:3729#include "net/socket/unix_domain_socket_posix.h"
[email protected]90e6c5412013-04-16 22:45:2530#endif
[email protected]ed6635e2012-10-27 00:44:0931
[email protected]852b34ba2013-10-03 16:29:0632using content::DevToolsAgentHost;
33using content::RenderViewHost;
34using content::WebContents;
35
[email protected]ed6635e2012-10-27 00:44:0936namespace {
[email protected]90e6c5412013-04-16 22:45:2537
[email protected]852b34ba2013-10-03 16:29:0638const char kTargetTypePage[] = "page";
39
[email protected]90e6c5412013-04-16 22:45:2540net::StreamListenSocketFactory* CreateSocketFactory() {
41 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
42#if defined(OS_ANDROID)
43 std::string socket_name = "content_shell_devtools_remote";
44 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
45 socket_name = command_line.GetSwitchValueASCII(
46 switches::kRemoteDebuggingSocketName);
47 }
48 return new net::UnixDomainSocketWithAbstractNamespaceFactory(
[email protected]a0dea2e2013-05-30 12:38:3949 socket_name, "", base::Bind(&content::CanUserConnectToDevTools));
[email protected]90e6c5412013-04-16 22:45:2550#else
51 // See if the user specified a port on the command line (useful for
52 // automation). If not, use an ephemeral port by specifying 0.
53 int port = 0;
54 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
55 int temp_port;
56 std::string port_str =
57 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
58 if (base::StringToInt(port_str, &temp_port) &&
59 temp_port > 0 && temp_port < 65535) {
60 port = temp_port;
61 } else {
62 DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
63 }
64 }
65 return new net::TCPListenSocketFactory("127.0.0.1", port);
[email protected]ed6635e2012-10-27 00:44:0966#endif
[email protected]90e6c5412013-04-16 22:45:2567}
[email protected]852b34ba2013-10-03 16:29:0668
69class Target : public content::DevToolsTarget {
70 public:
71 explicit Target(WebContents* web_contents);
72
73 virtual std::string GetId() const OVERRIDE { return id_; }
74 virtual std::string GetType() const OVERRIDE { return kTargetTypePage; }
75 virtual std::string GetTitle() const OVERRIDE { return title_; }
76 virtual std::string GetDescription() const OVERRIDE { return std::string(); }
77 virtual GURL GetUrl() const OVERRIDE { return url_; }
78 virtual GURL GetFaviconUrl() const OVERRIDE { return favicon_url_; }
79 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
80 return last_activity_time_;
81 }
82 virtual bool IsAttached() const OVERRIDE {
83 return agent_host_->IsAttached();
84 }
85 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
86 return agent_host_;
87 }
88 virtual bool Activate() const OVERRIDE;
89 virtual bool Close() const OVERRIDE;
90
91 private:
92 scoped_refptr<DevToolsAgentHost> agent_host_;
93 std::string id_;
94 std::string title_;
95 GURL url_;
96 GURL favicon_url_;
97 base::TimeTicks last_activity_time_;
98};
99
100Target::Target(WebContents* web_contents) {
101 agent_host_ =
102 DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost());
103 id_ = agent_host_->GetId();
[email protected]fa471b92013-10-23 14:01:14104 title_ = UTF16ToUTF8(web_contents->GetTitle());
[email protected]852b34ba2013-10-03 16:29:06105 url_ = web_contents->GetURL();
106 content::NavigationController& controller = web_contents->GetController();
107 content::NavigationEntry* entry = controller.GetActiveEntry();
108 if (entry != NULL && entry->GetURL().is_valid())
109 favicon_url_ = entry->GetFavicon().url;
110 last_activity_time_ = web_contents->GetLastSelectedTime();
111}
112
113bool Target::Activate() const {
114 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
115 if (!rvh)
116 return false;
117 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
118 if (!web_contents)
119 return false;
120 web_contents->GetDelegate()->ActivateContents(web_contents);
121 return true;
122}
123
124bool Target::Close() const {
125 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
126 if (!rvh)
127 return false;
128 rvh->ClosePage();
129 return true;
130}
131
[email protected]90e6c5412013-04-16 22:45:25132} // namespace
[email protected]ed6635e2012-10-27 00:44:09133
[email protected]ee75b8992012-01-27 07:53:57134namespace content {
135
[email protected]90e6c5412013-04-16 22:45:25136ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context)
[email protected]eb040992012-10-10 16:56:00137 : browser_context_(browser_context) {
[email protected]616d4942013-06-27 14:28:35138 // Note that Content Shell always used bundled DevTools frontend,
139 // even on Android, because the shell is used for running layout tests.
[email protected]90e6c5412013-04-16 22:45:25140 devtools_http_handler_ =
141 DevToolsHttpHandler::Start(CreateSocketFactory(), std::string(), this);
[email protected]ee75b8992012-01-27 07:53:57142}
143
144ShellDevToolsDelegate::~ShellDevToolsDelegate() {
145}
146
147void ShellDevToolsDelegate::Stop() {
148 // The call below destroys this.
149 devtools_http_handler_->Stop();
150}
151
[email protected]ee75b8992012-01-27 07:53:57152std::string ShellDevToolsDelegate::GetDiscoveryPageHTML() {
153 return ResourceBundle::GetSharedInstance().GetRawDataResource(
[email protected]4d8bb1a92012-11-01 21:12:40154 IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
[email protected]ee75b8992012-01-27 07:53:57155}
156
[email protected]ee75b8992012-01-27 07:53:57157bool ShellDevToolsDelegate::BundlesFrontendResources() {
158 return true;
159}
160
[email protected]d30a36f2013-02-07 04:16:26161base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() {
162 return base::FilePath();
[email protected]ee75b8992012-01-27 07:53:57163}
164
[email protected]e5ea93b2012-09-27 05:50:36165std::string ShellDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
[email protected]007b3f82013-04-09 08:46:45166 return std::string();
[email protected]e5ea93b2012-09-27 05:50:36167}
168
[email protected]c78ecec212013-10-27 19:34:10169scoped_ptr<DevToolsTarget>
170ShellDevToolsDelegate::CreateNewTarget(const GURL& url) {
[email protected]eb040992012-10-10 16:56:00171 Shell* shell = Shell::CreateNewWindow(browser_context_,
[email protected]c78ecec212013-10-27 19:34:10172 url,
[email protected]eb040992012-10-10 16:56:00173 NULL,
174 MSG_ROUTING_NONE,
[email protected]cdb806722013-01-10 14:18:23175 gfx::Size());
[email protected]852b34ba2013-10-03 16:29:06176 return scoped_ptr<DevToolsTarget>(new Target(shell->web_contents()));
[email protected]eb040992012-10-10 16:56:00177}
178
[email protected]852b34ba2013-10-03 16:29:06179void ShellDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
180 TargetList targets;
181 std::vector<RenderViewHost*> rvh_list =
182 content::DevToolsAgentHost::GetValidRenderViewHosts();
183 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
184 it != rvh_list.end(); ++it) {
185 WebContents* web_contents = WebContents::FromRenderViewHost(*it);
186 if (web_contents)
187 targets.push_back(new Target(web_contents));
188 }
189 callback.Run(targets);
[email protected]2fb8c1d2013-03-11 22:00:17190}
191
[email protected]8b62f8e2013-09-03 19:03:06192scoped_ptr<net::StreamListenSocket>
[email protected]eafc8452013-04-16 04:18:35193ShellDevToolsDelegate::CreateSocketForTethering(
194 net::StreamListenSocket::Delegate* delegate,
195 std::string* name) {
[email protected]8b62f8e2013-09-03 19:03:06196 return scoped_ptr<net::StreamListenSocket>();
[email protected]eafc8452013-04-16 04:18:35197}
198
[email protected]ee75b8992012-01-27 07:53:57199} // namespace content