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.
4
5 #include "content/shell/browser/shell_devtools_delegate.h"
6
7 #include <vector>
8
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/socket/tcp_listen_socket.h"
28 #include "ui/base/resource/resource_bundle.h"
29
30 #if defined(OS_ANDROID)
31 #include "content/public/browser/android/devtools_auth.h"
32 #include "net/socket/unix_domain_socket_posix.h"
33 #endif
34
35 using content::DevToolsAgentHost;
36 using content::RenderViewHost;
37 using content::WebContents;
38
39 namespace {
40
41 #if defined(OS_ANDROID)
42 const char kFrontEndURL[] =
43 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
44 #endif
45 const char kTargetTypePage[] = "page";
46
CreateSocketFactory()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);
54 }
55 return new net::UnixDomainSocketWithAbstractNamespaceFactory(
56 socket_name, "", base::Bind(&content::CanUserConnectToDevTools));
57 #else
58 // See if the user specified a port on the command line (useful for
59 // automation). If not, use an ephemeral port by specifying 0.
60 int port = 0;
61 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
62 int temp_port;
63 std::string port_str =
64 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
65 if (base::StringToInt(port_str, &temp_port) &&
66 temp_port > 0 && temp_port < 65535) {
67 port = temp_port;
68 } else {
69 DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
70 }
71 }
72 return new net::TCPListenSocketFactory("127.0.0.1", port);
73 #endif
74 }
75
76 class Target : public content::DevToolsTarget {
77 public:
78 explicit Target(WebContents* web_contents);
79
GetId() const80 virtual std::string GetId() const OVERRIDE { return id_; }
GetParentId() const81 virtual std::string GetParentId() const OVERRIDE { return std::string(); }
GetType() const82 virtual std::string GetType() const OVERRIDE { return kTargetTypePage; }
GetTitle() const83 virtual std::string GetTitle() const OVERRIDE { return title_; }
GetDescription() const84 virtual std::string GetDescription() const OVERRIDE { return std::string(); }
GetURL() const85 virtual GURL GetURL() const OVERRIDE { return url_; }
GetFaviconURL() const86 virtual GURL GetFaviconURL() const OVERRIDE { return favicon_url_; }
GetLastActivityTime() const87 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
88 return last_activity_time_;
89 }
IsAttached() const90 virtual bool IsAttached() const OVERRIDE {
91 return agent_host_->IsAttached();
92 }
GetAgentHost() const93 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
94 return agent_host_;
95 }
96 virtual bool Activate() const OVERRIDE;
97 virtual bool Close() const OVERRIDE;
98
99 private:
100 scoped_refptr<DevToolsAgentHost> agent_host_;
101 std::string id_;
102 std::string title_;
103 GURL url_;
104 GURL favicon_url_;
105 base::TimeTicks last_activity_time_;
106 };
107
Target(WebContents * web_contents)108 Target::Target(WebContents* web_contents) {
109 agent_host_ =
110 DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost());
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();
119 }
120
Activate() const121 bool Target::Activate() const {
122 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
123 if (!rvh)
124 return false;
125 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
126 if (!web_contents)
127 return false;
128 web_contents->GetDelegate()->ActivateContents(web_contents);
129 return true;
130 }
131
Close() const132 bool Target::Close() const {
133 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
134 if (!rvh)
135 return false;
136 rvh->ClosePage();
137 return true;
138 }
139
140 } // namespace
141
142 namespace content {
143
ShellDevToolsDelegate(BrowserContext * browser_context)144 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context)
145 : browser_context_(browser_context) {
146 std::string frontend_url;
147 #if defined(OS_ANDROID)
148 frontend_url = base::StringPrintf(kFrontEndURL, GetWebKitRevision().c_str());
149 #endif
150 devtools_http_handler_ =
151 DevToolsHttpHandler::Start(CreateSocketFactory(), frontend_url, this,
152 base::FilePath());
153 }
154
~ShellDevToolsDelegate()155 ShellDevToolsDelegate::~ShellDevToolsDelegate() {
156 }
157
Stop()158 void ShellDevToolsDelegate::Stop() {
159 // The call below destroys this.
160 devtools_http_handler_->Stop();
161 }
162
GetDiscoveryPageHTML()163 std::string ShellDevToolsDelegate::GetDiscoveryPageHTML() {
164 #if defined(OS_ANDROID)
165 return std::string();
166 #else
167 return ResourceBundle::GetSharedInstance().GetRawDataResource(
168 IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
169 #endif
170 }
171
BundlesFrontendResources()172 bool ShellDevToolsDelegate::BundlesFrontendResources() {
173 #if defined(OS_ANDROID)
174 return false;
175 #else
176 return true;
177 #endif
178 }
179
GetDebugFrontendDir()180 base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() {
181 return base::FilePath();
182 }
183
GetPageThumbnailData(const GURL & url)184 std::string ShellDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
185 return std::string();
186 }
187
188 scoped_ptr<DevToolsTarget>
CreateNewTarget(const GURL & url)189 ShellDevToolsDelegate::CreateNewTarget(const GURL& url) {
190 Shell* shell = Shell::CreateNewWindow(browser_context_,
191 url,
192 NULL,
193 MSG_ROUTING_NONE,
194 gfx::Size());
195 return scoped_ptr<DevToolsTarget>(new Target(shell->web_contents()));
196 }
197
EnumerateTargets(TargetCallback callback)198 void ShellDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
199 TargetList targets;
200 std::vector<RenderViewHost*> rvh_list =
201 content::DevToolsAgentHost::GetValidRenderViewHosts();
202 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
203 it != rvh_list.end(); ++it) {
204 WebContents* web_contents = WebContents::FromRenderViewHost(*it);
205 if (web_contents)
206 targets.push_back(new Target(web_contents));
207 }
208 callback.Run(targets);
209 }
210
211 scoped_ptr<net::StreamListenSocket>
CreateSocketForTethering(net::StreamListenSocket::Delegate * delegate,std::string * name)212 ShellDevToolsDelegate::CreateSocketForTethering(
213 net::StreamListenSocket::Delegate* delegate,
214 std::string* name) {
215 return scoped_ptr<net::StreamListenSocket>();
216 }
217
218 } // namespace content
219