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_delegate.h"
10 #include "base/command_line.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "content/public/browser/devtools_agent_host.h"
15 #include "content/public/browser/devtools_http_handler.h"
16 #include "content/public/browser/devtools_target.h"
17 #include "content/public/browser/favicon_status.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/content_switches.h"
22 #include "content/public/common/url_constants.h"
23 #include "content/public/common/user_agent.h"
24 #include "content/shell/browser/shell.h"
25 #include "grit/shell_resources.h"
26 #include "net/socket/tcp_listen_socket.h"
27 #include "ui/base/resource/resource_bundle.h"
29 #if defined(OS_ANDROID)
30 #include "content/public/browser/android/devtools_auth.h"
31 #include "net/socket/unix_domain_socket_posix.h"
34 using content::DevToolsAgentHost
;
35 using content::RenderViewHost
;
36 using content::WebContents
;
40 #if defined(OS_ANDROID)
41 const char kFrontEndURL
[] =
42 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
44 const char kTargetTypePage
[] = "page";
46 net::StreamListenSocketFactory
* CreateSocketFactory() {
47 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
48 #if defined(OS_ANDROID)
49 std::string socket_name
= "content_shell_devtools_remote";
50 if (command_line
.HasSwitch(switches::kRemoteDebuggingSocketName
)) {
51 socket_name
= command_line
.GetSwitchValueASCII(
52 switches::kRemoteDebuggingSocketName
);
54 return new net::UnixDomainSocketWithAbstractNamespaceFactory(
55 socket_name
, "", base::Bind(&content::CanUserConnectToDevTools
));
57 // See if the user specified a port on the command line (useful for
58 // automation). If not, use an ephemeral port by specifying 0.
60 if (command_line
.HasSwitch(switches::kRemoteDebuggingPort
)) {
62 std::string port_str
=
63 command_line
.GetSwitchValueASCII(switches::kRemoteDebuggingPort
);
64 if (base::StringToInt(port_str
, &temp_port
) &&
65 temp_port
> 0 && temp_port
< 65535) {
68 DLOG(WARNING
) << "Invalid http debugger port number " << temp_port
;
71 return new net::TCPListenSocketFactory("127.0.0.1", port
);
75 class Target
: public content::DevToolsTarget
{
77 explicit Target(WebContents
* web_contents
);
79 virtual std::string
GetId() const OVERRIDE
{ return id_
; }
80 virtual std::string
GetParentId() const OVERRIDE
{ return std::string(); }
81 virtual std::string
GetType() const OVERRIDE
{ return kTargetTypePage
; }
82 virtual std::string
GetTitle() const OVERRIDE
{ return title_
; }
83 virtual std::string
GetDescription() const OVERRIDE
{ return std::string(); }
84 virtual GURL
GetURL() const OVERRIDE
{ return url_
; }
85 virtual GURL
GetFaviconURL() const OVERRIDE
{ return favicon_url_
; }
86 virtual base::TimeTicks
GetLastActivityTime() const OVERRIDE
{
87 return last_activity_time_
;
89 virtual bool IsAttached() const OVERRIDE
{
90 return agent_host_
->IsAttached();
92 virtual scoped_refptr
<DevToolsAgentHost
> GetAgentHost() const OVERRIDE
{
95 virtual bool Activate() const OVERRIDE
;
96 virtual bool Close() const OVERRIDE
;
99 scoped_refptr
<DevToolsAgentHost
> agent_host_
;
104 base::TimeTicks last_activity_time_
;
107 Target::Target(WebContents
* web_contents
) {
109 DevToolsAgentHost::GetOrCreateFor(web_contents
->GetRenderViewHost());
110 id_
= agent_host_
->GetId();
111 title_
= base::UTF16ToUTF8(web_contents
->GetTitle());
112 url_
= web_contents
->GetURL();
113 content::NavigationController
& controller
= web_contents
->GetController();
114 content::NavigationEntry
* entry
= controller
.GetActiveEntry();
115 if (entry
!= NULL
&& entry
->GetURL().is_valid())
116 favicon_url_
= entry
->GetFavicon().url
;
117 last_activity_time_
= web_contents
->GetLastActiveTime();
120 bool Target::Activate() const {
121 RenderViewHost
* rvh
= agent_host_
->GetRenderViewHost();
124 WebContents
* web_contents
= WebContents::FromRenderViewHost(rvh
);
127 web_contents
->GetDelegate()->ActivateContents(web_contents
);
131 bool Target::Close() const {
132 RenderViewHost
* rvh
= agent_host_
->GetRenderViewHost();
143 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext
* browser_context
)
144 : browser_context_(browser_context
) {
145 std::string frontend_url
;
146 #if defined(OS_ANDROID)
147 frontend_url
= base::StringPrintf(kFrontEndURL
, GetWebKitRevision().c_str());
149 devtools_http_handler_
=
150 DevToolsHttpHandler::Start(CreateSocketFactory(), frontend_url
, this);
153 ShellDevToolsDelegate::~ShellDevToolsDelegate() {
156 void ShellDevToolsDelegate::Stop() {
157 // The call below destroys this.
158 devtools_http_handler_
->Stop();
161 std::string
ShellDevToolsDelegate::GetDiscoveryPageHTML() {
162 #if defined(OS_ANDROID)
163 return std::string();
165 return ResourceBundle::GetSharedInstance().GetRawDataResource(
166 IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE
).as_string();
170 bool ShellDevToolsDelegate::BundlesFrontendResources() {
171 #if defined(OS_ANDROID)
178 base::FilePath
ShellDevToolsDelegate::GetDebugFrontendDir() {
179 return base::FilePath();
182 std::string
ShellDevToolsDelegate::GetPageThumbnailData(const GURL
& url
) {
183 return std::string();
186 scoped_ptr
<DevToolsTarget
>
187 ShellDevToolsDelegate::CreateNewTarget(const GURL
& url
) {
188 Shell
* shell
= Shell::CreateNewWindow(browser_context_
,
193 return scoped_ptr
<DevToolsTarget
>(new Target(shell
->web_contents()));
196 void ShellDevToolsDelegate::EnumerateTargets(TargetCallback callback
) {
198 std::vector
<RenderViewHost
*> rvh_list
=
199 content::DevToolsAgentHost::GetValidRenderViewHosts();
200 for (std::vector
<RenderViewHost
*>::iterator it
= rvh_list
.begin();
201 it
!= rvh_list
.end(); ++it
) {
202 WebContents
* web_contents
= WebContents::FromRenderViewHost(*it
);
204 targets
.push_back(new Target(web_contents
));
206 callback
.Run(targets
);
209 scoped_ptr
<net::StreamListenSocket
>
210 ShellDevToolsDelegate::CreateSocketForTethering(
211 net::StreamListenSocket::Delegate
* delegate
,
213 return scoped_ptr
<net::StreamListenSocket
>();
216 } // namespace content