Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: content/shell/browser/shell_devtools_delegate.cc

Issue 24995003: DevTools: Extract target discovery and manipulation from DevToolsHttpHandlerImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed clang compile and a minor bug Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/shell/browser/shell_devtools_delegate.h" 5 #include "content/shell/browser/shell_devtools_delegate.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/public/browser/devtools_agent_host.h"
12 #include "content/public/browser/devtools_http_handler.h" 14 #include "content/public/browser/devtools_http_handler.h"
15 #include "content/public/browser/favicon_status.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
14 #include "content/public/common/content_switches.h" 19 #include "content/public/common/content_switches.h"
15 #include "content/public/common/url_constants.h" 20 #include "content/public/common/url_constants.h"
16 #include "content/shell/browser/shell.h" 21 #include "content/shell/browser/shell.h"
17 #include "grit/shell_resources.h" 22 #include "grit/shell_resources.h"
23 #include "net/base/escape.h"
18 #include "net/socket/tcp_listen_socket.h" 24 #include "net/socket/tcp_listen_socket.h"
19 #include "ui/base/resource/resource_bundle.h" 25 #include "ui/base/resource/resource_bundle.h"
20 26
21 #if defined(OS_ANDROID) 27 #if defined(OS_ANDROID)
22 #include "content/public/browser/android/devtools_auth.h" 28 #include "content/public/browser/android/devtools_auth.h"
23 #include "net/socket/unix_domain_socket_posix.h" 29 #include "net/socket/unix_domain_socket_posix.h"
24 #endif 30 #endif
25 31
32 using content::DevToolsAgentHost;
33 using content::RenderViewHost;
34 using content::WebContents;
35
26 namespace { 36 namespace {
27 37
38 const char kTargetTypePage[] = "page";
39
28 net::StreamListenSocketFactory* CreateSocketFactory() { 40 net::StreamListenSocketFactory* CreateSocketFactory() {
29 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 41 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
30 #if defined(OS_ANDROID) 42 #if defined(OS_ANDROID)
31 std::string socket_name = "content_shell_devtools_remote"; 43 std::string socket_name = "content_shell_devtools_remote";
32 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { 44 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
33 socket_name = command_line.GetSwitchValueASCII( 45 socket_name = command_line.GetSwitchValueASCII(
34 switches::kRemoteDebuggingSocketName); 46 switches::kRemoteDebuggingSocketName);
35 } 47 }
36 return new net::UnixDomainSocketWithAbstractNamespaceFactory( 48 return new net::UnixDomainSocketWithAbstractNamespaceFactory(
37 socket_name, "", base::Bind(&content::CanUserConnectToDevTools)); 49 socket_name, "", base::Bind(&content::CanUserConnectToDevTools));
38 #else 50 #else
39 // See if the user specified a port on the command line (useful for 51 // See if the user specified a port on the command line (useful for
40 // automation). If not, use an ephemeral port by specifying 0. 52 // automation). If not, use an ephemeral port by specifying 0.
41 int port = 0; 53 int port = 0;
42 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) { 54 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
43 int temp_port; 55 int temp_port;
44 std::string port_str = 56 std::string port_str =
45 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort); 57 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
46 if (base::StringToInt(port_str, &temp_port) && 58 if (base::StringToInt(port_str, &temp_port) &&
47 temp_port > 0 && temp_port < 65535) { 59 temp_port > 0 && temp_port < 65535) {
48 port = temp_port; 60 port = temp_port;
49 } else { 61 } else {
50 DLOG(WARNING) << "Invalid http debugger port number " << temp_port; 62 DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
51 } 63 }
52 } 64 }
53 return new net::TCPListenSocketFactory("127.0.0.1", port); 65 return new net::TCPListenSocketFactory("127.0.0.1", port);
54 #endif 66 #endif
55 } 67 }
68
69 class 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 virtual ~Target() {}
93
94 scoped_refptr<DevToolsAgentHost> agent_host_;
95 std::string id_;
96 std::string title_;
97 GURL url_;
98 GURL favicon_url_;
99 base::TimeTicks last_activity_time_;
100 };
101
102 Target::Target(WebContents* web_contents) {
103 agent_host_ =
104 DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost());
105 id_ = agent_host_->GetId();
106 title_ = UTF16ToUTF8(net::EscapeForHTML(web_contents->GetTitle()));
107 url_ = web_contents->GetURL();
108 content::NavigationController& controller = web_contents->GetController();
109 content::NavigationEntry* entry = controller.GetActiveEntry();
110 if (entry != NULL && entry->GetURL().is_valid())
111 favicon_url_ = entry->GetFavicon().url;
112 last_activity_time_ = web_contents->GetLastSelectedTime();
113 }
114
115 bool Target::Activate() const {
116 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
117 if (!rvh)
118 return false;
119 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
120 if (!web_contents)
121 return false;
122 web_contents->GetDelegate()->ActivateContents(web_contents);
123 return true;
124 }
125
126 bool Target::Close() const {
127 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
128 if (!rvh)
129 return false;
130 rvh->ClosePage();
131 return true;
132 }
133
56 } // namespace 134 } // namespace
57 135
58 namespace content { 136 namespace content {
59 137
60 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context) 138 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context)
61 : browser_context_(browser_context) { 139 : browser_context_(browser_context) {
62 // Note that Content Shell always used bundled DevTools frontend, 140 // Note that Content Shell always used bundled DevTools frontend,
63 // even on Android, because the shell is used for running layout tests. 141 // even on Android, because the shell is used for running layout tests.
64 devtools_http_handler_ = 142 devtools_http_handler_ =
65 DevToolsHttpHandler::Start(CreateSocketFactory(), std::string(), this); 143 DevToolsHttpHandler::Start(CreateSocketFactory(), std::string(), this);
(...skipping 17 matching lines...) Expand all
83 } 161 }
84 162
85 base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() { 163 base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() {
86 return base::FilePath(); 164 return base::FilePath();
87 } 165 }
88 166
89 std::string ShellDevToolsDelegate::GetPageThumbnailData(const GURL& url) { 167 std::string ShellDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
90 return std::string(); 168 return std::string();
91 } 169 }
92 170
93 RenderViewHost* ShellDevToolsDelegate::CreateNewTarget() { 171 scoped_refptr<DevToolsTarget> ShellDevToolsDelegate::CreateNewTarget() {
94 Shell* shell = Shell::CreateNewWindow(browser_context_, 172 Shell* shell = Shell::CreateNewWindow(browser_context_,
95 GURL(kAboutBlankURL), 173 GURL(kAboutBlankURL),
96 NULL, 174 NULL,
97 MSG_ROUTING_NONE, 175 MSG_ROUTING_NONE,
98 gfx::Size()); 176 gfx::Size());
99 return shell->web_contents()->GetRenderViewHost(); 177 return new Target(shell->web_contents());
100 } 178 }
101 179
102 DevToolsHttpHandlerDelegate::TargetType 180 void ShellDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
103 ShellDevToolsDelegate::GetTargetType(RenderViewHost*) { 181 TargetList targets;
104 return kTargetTypeTab; 182 std::vector<RenderViewHost*> rvh_list =
105 } 183 content::DevToolsAgentHost::GetValidRenderViewHosts();
106 184 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
107 std::string ShellDevToolsDelegate::GetViewDescription( 185 it != rvh_list.end(); ++it) {
108 content::RenderViewHost*) { 186 WebContents* web_contents = WebContents::FromRenderViewHost(*it);
109 return std::string(); 187 if (web_contents)
188 targets.push_back(new Target(web_contents));
189 }
190 callback.Run(targets);
110 } 191 }
111 192
112 scoped_ptr<net::StreamListenSocket> 193 scoped_ptr<net::StreamListenSocket>
113 ShellDevToolsDelegate::CreateSocketForTethering( 194 ShellDevToolsDelegate::CreateSocketForTethering(
114 net::StreamListenSocket::Delegate* delegate, 195 net::StreamListenSocket::Delegate* delegate,
115 std::string* name) { 196 std::string* name) {
116 return scoped_ptr<net::StreamListenSocket>(); 197 return scoped_ptr<net::StreamListenSocket>();
117 } 198 }
118 199
119 } // namespace content 200 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698