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

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 json/new handler 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/devtools_target.h"
16 #include "content/public/browser/favicon_status.h"
17 #include "content/public/browser/navigation_entry.h"
18 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
14 #include "content/public/common/content_switches.h" 20 #include "content/public/common/content_switches.h"
15 #include "content/public/common/url_constants.h" 21 #include "content/public/common/url_constants.h"
16 #include "content/shell/browser/shell.h" 22 #include "content/shell/browser/shell.h"
17 #include "grit/shell_resources.h" 23 #include "grit/shell_resources.h"
24 #include "net/base/escape.h"
18 #include "net/socket/tcp_listen_socket.h" 25 #include "net/socket/tcp_listen_socket.h"
19 #include "ui/base/resource/resource_bundle.h" 26 #include "ui/base/resource/resource_bundle.h"
20 27
21 #if defined(OS_ANDROID) 28 #if defined(OS_ANDROID)
22 #include "content/public/browser/android/devtools_auth.h" 29 #include "content/public/browser/android/devtools_auth.h"
23 #include "net/socket/unix_domain_socket_posix.h" 30 #include "net/socket/unix_domain_socket_posix.h"
24 #endif 31 #endif
25 32
33 using content::DevToolsAgentHost;
34 using content::RenderViewHost;
35 using content::WebContents;
36
26 namespace { 37 namespace {
27 38
39 const char kTargetTypePage[] = "page";
40
28 net::StreamListenSocketFactory* CreateSocketFactory() { 41 net::StreamListenSocketFactory* CreateSocketFactory() {
29 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 42 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
30 #if defined(OS_ANDROID) 43 #if defined(OS_ANDROID)
31 std::string socket_name = "content_shell_devtools_remote"; 44 std::string socket_name = "content_shell_devtools_remote";
32 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { 45 if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) {
33 socket_name = command_line.GetSwitchValueASCII( 46 socket_name = command_line.GetSwitchValueASCII(
34 switches::kRemoteDebuggingSocketName); 47 switches::kRemoteDebuggingSocketName);
35 } 48 }
36 return new net::UnixDomainSocketWithAbstractNamespaceFactory( 49 return new net::UnixDomainSocketWithAbstractNamespaceFactory(
37 socket_name, "", base::Bind(&content::CanUserConnectToDevTools)); 50 socket_name, "", base::Bind(&content::CanUserConnectToDevTools));
38 #else 51 #else
39 // See if the user specified a port on the command line (useful for 52 // See if the user specified a port on the command line (useful for
40 // automation). If not, use an ephemeral port by specifying 0. 53 // automation). If not, use an ephemeral port by specifying 0.
41 int port = 0; 54 int port = 0;
42 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) { 55 if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) {
43 int temp_port; 56 int temp_port;
44 std::string port_str = 57 std::string port_str =
45 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort); 58 command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort);
46 if (base::StringToInt(port_str, &temp_port) && 59 if (base::StringToInt(port_str, &temp_port) &&
47 temp_port > 0 && temp_port < 65535) { 60 temp_port > 0 && temp_port < 65535) {
48 port = temp_port; 61 port = temp_port;
49 } else { 62 } else {
50 DLOG(WARNING) << "Invalid http debugger port number " << temp_port; 63 DLOG(WARNING) << "Invalid http debugger port number " << temp_port;
51 } 64 }
52 } 65 }
53 return new net::TCPListenSocketFactory("127.0.0.1", port); 66 return new net::TCPListenSocketFactory("127.0.0.1", port);
54 #endif 67 #endif
55 } 68 }
69
70 class Target : public content::DevToolsTarget {
71 public:
72 explicit Target(WebContents* web_contents);
73
74 virtual std::string GetId() const OVERRIDE { return id_; }
75 virtual std::string GetType() const OVERRIDE { return kTargetTypePage; }
76 virtual std::string GetTitle() const OVERRIDE { return title_; }
77 virtual std::string GetDescription() const OVERRIDE { return std::string(); }
78 virtual GURL GetUrl() const OVERRIDE { return url_; }
79 virtual GURL GetFaviconUrl() const OVERRIDE { return favicon_url_; }
80 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
81 return last_activity_time_;
82 }
83 virtual bool IsAttached() const OVERRIDE {
84 return agent_host_->IsAttached();
85 }
86 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
87 return agent_host_;
88 }
89 virtual bool Activate() const OVERRIDE;
90 virtual bool Close() const OVERRIDE;
91
92 private:
93 scoped_refptr<DevToolsAgentHost> agent_host_;
94 std::string id_;
95 std::string title_;
96 GURL url_;
97 GURL favicon_url_;
98 base::TimeTicks last_activity_time_;
99 };
100
101 Target::Target(WebContents* web_contents) {
102 agent_host_ =
103 DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost());
104 id_ = agent_host_->GetId();
105 title_ = UTF16ToUTF8(net::EscapeForHTML(web_contents->GetTitle()));
106 url_ = web_contents->GetURL();
107 content::NavigationController& controller = web_contents->GetController();
108 content::NavigationEntry* entry = controller.GetActiveEntry();
109 if (entry != NULL && entry->GetURL().is_valid())
110 favicon_url_ = entry->GetFavicon().url;
111 last_activity_time_ = web_contents->GetLastSelectedTime();
112 }
113
114 bool Target::Activate() const {
115 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
116 if (!rvh)
117 return false;
118 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
119 if (!web_contents)
120 return false;
121 web_contents->GetDelegate()->ActivateContents(web_contents);
122 return true;
123 }
124
125 bool Target::Close() const {
126 RenderViewHost* rvh = agent_host_->GetRenderViewHost();
127 if (!rvh)
128 return false;
129 rvh->ClosePage();
130 return true;
131 }
132
56 } // namespace 133 } // namespace
57 134
58 namespace content { 135 namespace content {
59 136
60 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context) 137 ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context)
61 : browser_context_(browser_context) { 138 : browser_context_(browser_context) {
62 // Note that Content Shell always used bundled DevTools frontend, 139 // Note that Content Shell always used bundled DevTools frontend,
63 // even on Android, because the shell is used for running layout tests. 140 // even on Android, because the shell is used for running layout tests.
64 devtools_http_handler_ = 141 devtools_http_handler_ =
65 DevToolsHttpHandler::Start(CreateSocketFactory(), std::string(), this); 142 DevToolsHttpHandler::Start(CreateSocketFactory(), std::string(), this);
(...skipping 17 matching lines...) Expand all
83 } 160 }
84 161
85 base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() { 162 base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() {
86 return base::FilePath(); 163 return base::FilePath();
87 } 164 }
88 165
89 std::string ShellDevToolsDelegate::GetPageThumbnailData(const GURL& url) { 166 std::string ShellDevToolsDelegate::GetPageThumbnailData(const GURL& url) {
90 return std::string(); 167 return std::string();
91 } 168 }
92 169
93 RenderViewHost* ShellDevToolsDelegate::CreateNewTarget() { 170 scoped_ptr<DevToolsTarget> ShellDevToolsDelegate::CreateNewTarget() {
94 Shell* shell = Shell::CreateNewWindow(browser_context_, 171 Shell* shell = Shell::CreateNewWindow(browser_context_,
95 GURL(kAboutBlankURL), 172 GURL(kAboutBlankURL),
96 NULL, 173 NULL,
97 MSG_ROUTING_NONE, 174 MSG_ROUTING_NONE,
98 gfx::Size()); 175 gfx::Size());
99 return shell->web_contents()->GetRenderViewHost(); 176 return scoped_ptr<DevToolsTarget>(new Target(shell->web_contents()));
100 } 177 }
101 178
102 DevToolsHttpHandlerDelegate::TargetType 179 void ShellDevToolsDelegate::EnumerateTargets(TargetCallback callback) {
103 ShellDevToolsDelegate::GetTargetType(RenderViewHost*) { 180 TargetList targets;
104 return kTargetTypeTab; 181 std::vector<RenderViewHost*> rvh_list =
105 } 182 content::DevToolsAgentHost::GetValidRenderViewHosts();
106 183 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
107 std::string ShellDevToolsDelegate::GetViewDescription( 184 it != rvh_list.end(); ++it) {
108 content::RenderViewHost*) { 185 WebContents* web_contents = WebContents::FromRenderViewHost(*it);
109 return std::string(); 186 if (web_contents)
187 targets.push_back(new Target(web_contents));
188 }
189 callback.Run(targets);
110 } 190 }
111 191
112 scoped_ptr<net::StreamListenSocket> 192 scoped_ptr<net::StreamListenSocket>
113 ShellDevToolsDelegate::CreateSocketForTethering( 193 ShellDevToolsDelegate::CreateSocketForTethering(
114 net::StreamListenSocket::Delegate* delegate, 194 net::StreamListenSocket::Delegate* delegate,
115 std::string* name) { 195 std::string* name) {
116 return scoped_ptr<net::StreamListenSocket>(); 196 return scoped_ptr<net::StreamListenSocket>();
117 } 197 }
118 198
119 } // namespace content 199 } // namespace content
OLDNEW
« no previous file with comments | « content/shell/browser/shell_devtools_delegate.h ('k') | content/shell/browser/shell_devtools_frontend.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698