[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / content / shell / browser / shell_devtools_delegate.cc
blob01dca2b4024d81386c1dcfa1168cc39651144eba
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"
7 #include <vector>
9 #include "base/bind.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"
32 #endif
34 using content::DevToolsAgentHost;
35 using content::RenderViewHost;
36 using content::WebContents;
38 namespace {
40 #if defined(OS_ANDROID)
41 const char kFrontEndURL[] =
42 "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html";
43 #endif
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));
56 #else
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.
59 int port = 0;
60 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
61 int temp_port;
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) {
66 port = temp_port;
67 } else {
68 DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
71 return new net::TCPListenSocketFactory("127.0.0.1", port);
72 #endif
75 class Target : public content::DevToolsTarget {
76 public:
77 explicit Target(WebContents* web_contents);
79 virtual std::string GetId() const OVERRIDE { return id_; }
80 virtual std::string GetType() const OVERRIDE { return kTargetTypePage; }
81 virtual std::string GetTitle() const OVERRIDE { return title_; }
82 virtual std::string GetDescription() const OVERRIDE { return std::string(); }
83 virtual GURL GetUrl() const OVERRIDE { return url_; }
84 virtual GURL GetFaviconUrl() const OVERRIDE { return favicon_url_; }
85 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
86 return last_activity_time_;
88 virtual bool IsAttached() const OVERRIDE {
89 return agent_host_->IsAttached();
91 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
92 return agent_host_;
94 virtual bool Activate() const OVERRIDE;
95 virtual bool Close() const OVERRIDE;
97 private:
98 scoped_refptr<DevToolsAgentHost> agent_host_;
99 std::string id_;
100 std::string title_;
101 GURL url_;
102 GURL favicon_url_;
103 base::TimeTicks last_activity_time_;
106 Target::Target(WebContents* web_contents) {
107 agent_host_ =
108 DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost());
109 id_ = agent_host_->GetId();
110 title_ = base::UTF16ToUTF8(web_contents->GetTitle());
111 url_ = web_contents->GetURL();
112 content::NavigationController& controller = web_contents->GetController();
113 content::NavigationEntry* entry = controller.GetActiveEntry();
114 if (entry != NULL && entry->GetURL().is_valid())
115 favicon_url_ = entry->GetFavicon().url;
116 last_activity_time_ = web_contents->GetLastActiveTime();
119 bool Target::Activate() const {
120 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
121 if (!rvh)
122 return false;
123 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
124 if (!web_contents)
125 return false;
126 web_contents->GetDelegate()->ActivateContents(web_contents);
127 return true;
130 bool Target::Close() const {
131 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
132 if (!rvh)
133 return false;
134 rvh->ClosePage();
135 return true;
138 } // namespace
140 namespace content {
142 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context)
143 : browser_context_(browser_context) {
144 std::string frontend_url;
145 #if defined(OS_ANDROID)
146 frontend_url = base::StringPrintf(kFrontEndURL, GetWebKitRevision().c_str());
147 #endif
148 devtools_http_handler_ =
149 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();
163 #else
164 return ResourceBundle::GetSharedInstance().GetRawDataResource(
165 IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string();
166 #endif
169 bool ShellDevToolsDelegate::BundlesFrontendResources() {
170 #if defined(OS_ANDROID)
171 return false;
172 #else
173 return true;
174 #endif
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_,
188 url,
189 NULL,
190 MSG_ROUTING_NONE,
191 gfx::Size());
192 return scoped_ptr<DevToolsTarget>(new Target(shell->web_contents()));
195 void ShellDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
196 TargetList targets;
197 std::vector<RenderViewHost*> rvh_list =
198 content::DevToolsAgentHost::GetValidRenderViewHosts();
199 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
200 it != rvh_list.end(); ++it) {
201 WebContents* web_contents = WebContents::FromRenderViewHost(*it);
202 if (web_contents)
203 targets.push_back(new Target(web_contents));
205 callback.Run(targets);
208 scoped_ptr<net::StreamListenSocket>
209 ShellDevToolsDelegate::CreateSocketForTethering(
210 net::StreamListenSocket::Delegate* delegate,
211 std::string* name) {
212 return scoped_ptr<net::StreamListenSocket>();
215 } // namespace content