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/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/socket/tcp_listen_socket.h"
28 #include "ui/base/resource/resource_bundle.h"
30 #if defined(OS_ANDROID)
31 #include "content/public/browser/android/devtools_auth.h"
32 #include "net/socket/unix_domain_listen_socket_posix.h"
35 using content::DevToolsAgentHost;
36 using content::RenderViewHost;
37 using content::WebContents;
41 #if defined(OS_ANDROID)
42 const char kFrontEndURL[] =
43 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
45 const char kTargetTypePage[] = "page";
47 net::StreamListenSocketFactory* CreateSocketFactory() {
48 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
49 #if defined(OS_ANDROID)
50 std::string socket_name = "content_shell_devtools_remote";
51 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
52 socket_name = command_line.GetSwitchValueASCII(
53 switches::kRemoteDebuggingSocketName);
55 return new net::deprecated::
56 UnixDomainListenSocketWithAbstractNamespaceFactory(
57 socket_name, "", base::Bind(&content::CanUserConnectToDevTools));
59 // See if the user specified a port on the command line (useful for
60 // automation). If not, use an ephemeral port by specifying 0.
62 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
64 std::string port_str =
65 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
66 if (base::StringToInt(port_str, &temp_port) &&
67 temp_port > 0 && temp_port < 65535) {
70 DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
73 return new net::TCPListenSocketFactory("127.0.0.1", port);
77 class Target : public content::DevToolsTarget {
79 explicit Target(WebContents* web_contents);
81 virtual std::string GetId() const OVERRIDE { return id_; }
82 virtual std::string GetParentId() const OVERRIDE { return std::string(); }
83 virtual std::string GetType() const OVERRIDE { return kTargetTypePage; }
84 virtual std::string GetTitle() const OVERRIDE { return title_; }
85 virtual std::string GetDescription() const OVERRIDE { return std::string(); }
86 virtual GURL GetURL() const OVERRIDE { return url_; }
87 virtual GURL GetFaviconURL() const OVERRIDE { return favicon_url_; }
88 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
89 return last_activity_time_;
91 virtual bool IsAttached() const OVERRIDE {
92 return agent_host_->IsAttached();
94 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
97 virtual bool Activate() const OVERRIDE;
98 virtual bool Close() const OVERRIDE;
101 scoped_refptr<DevToolsAgentHost> agent_host_;
106 base::TimeTicks last_activity_time_;
109 Target::Target(WebContents* web_contents) {
110 agent_host_ = DevToolsAgentHost::GetOrCreateFor(web_contents);
111 id_ = agent_host_->GetId();
112 title_ = base::UTF16ToUTF8(web_contents->GetTitle());
113 url_ = web_contents->GetURL();
114 content::NavigationController& controller = web_contents->GetController();
115 content::NavigationEntry* entry = controller.GetActiveEntry();
116 if (entry != NULL && entry->GetURL().is_valid())
117 favicon_url_ = entry->GetFavicon().url;
118 last_activity_time_ = web_contents->GetLastActiveTime();
121 bool Target::Activate() const {
122 WebContents* web_contents = agent_host_->GetWebContents();
125 web_contents->GetDelegate()->ActivateContents(web_contents);
129 bool Target::Close() const {
130 WebContents* web_contents = agent_host_->GetWebContents();
133 web_contents->GetRenderViewHost()->ClosePage();
141 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context)
142 : browser_context_(browser_context) {
143 std::string frontend_url;
144 #if defined(OS_ANDROID)
145 frontend_url = base::StringPrintf(kFrontEndURL, GetWebKitRevision().c_str());
147 devtools_http_handler_ =
148 DevToolsHttpHandler::Start(CreateSocketFactory(), frontend_url, this,
152 ShellDevToolsDelegate::~ShellDevToolsDelegate() {
155 void ShellDevToolsDelegate::Stop() {
156 // The call below destroys this.
157 devtools_http_handler_->Stop();
160 std::string ShellDevToolsDelegate::GetDiscoveryPageHTML() {
161 #if defined(OS_ANDROID)
162 return std::string();
164 return ResourceBundle::GetSharedInstance().GetRawDataResource(
165 IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
169 bool ShellDevToolsDelegate::BundlesFrontendResources() {
170 #if defined(OS_ANDROID)
177 base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() {
178 return base::FilePath();
181 std::string ShellDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
182 return std::string();
185 scoped_ptr<DevToolsTarget>
186 ShellDevToolsDelegate::CreateNewTarget(const GURL& url) {
187 Shell* shell = Shell::CreateNewWindow(browser_context_,
192 return scoped_ptr<DevToolsTarget>(new Target(shell->web_contents()));
195 void ShellDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
197 std::vector<WebContents*> wc_list =
198 content::DevToolsAgentHost::GetInspectableWebContents();
199 for (std::vector<WebContents*>::iterator it = wc_list.begin();
202 targets.push_back(new Target(*it));
204 callback.Run(targets);
207 scoped_ptr<net::StreamListenSocket>
208 ShellDevToolsDelegate::CreateSocketForTethering(
209 net::StreamListenSocket::Delegate* delegate,
211 return scoped_ptr<net::StreamListenSocket>();
214 } // namespace content