[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 5 | #include "content/shell/browser/shell_devtools_delegate.h" |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 6 | |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | 0e60fce | 2014-06-04 22:27:20 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
| 15 | #include "content/public/browser/devtools_agent_host.h" |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 16 | #include "content/public/browser/devtools_http_handler.h" |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 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" |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 21 | #include "content/public/browser/web_contents.h" |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 22 | #include "content/public/common/content_switches.h" |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 23 | #include "content/public/common/url_constants.h" |
[email protected] | efd2c3f | 2014-03-11 09:57:25 | [diff] [blame] | 24 | #include "content/public/common/user_agent.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 25 | #include "content/shell/browser/shell.h" |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 26 | #include "grit/shell_resources.h" |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 27 | #include "net/socket/tcp_server_socket.h" |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 28 | #include "ui/base/resource/resource_bundle.h" |
| 29 | |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 30 | #if defined(OS_ANDROID) |
| 31 | #include "content/public/browser/android/devtools_auth.h" |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 32 | #include "net/socket/unix_domain_server_socket_posix.h" |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 33 | #endif |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 34 | |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 35 | using content::DevToolsAgentHost; |
| 36 | using content::RenderViewHost; |
| 37 | using content::WebContents; |
| 38 | |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 39 | namespace { |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 40 | |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 41 | #if defined(OS_ANDROID) |
| 42 | const char kFrontEndURL[] = |
| 43 | "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/devtools.html"; |
| 44 | #endif |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 45 | const char kTargetTypePage[] = "page"; |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 46 | const char kTargetTypeServiceWorker[] = "service_worker"; |
| 47 | const char kTargetTypeOther[] = "other"; |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 48 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 49 | #if defined(OS_ANDROID) |
| 50 | class UnixDomainServerSocketFactory |
| 51 | : public content::DevToolsHttpHandler::ServerSocketFactory { |
| 52 | public: |
| 53 | explicit UnixDomainServerSocketFactory(const std::string& socket_name) |
| 54 | : content::DevToolsHttpHandler::ServerSocketFactory(socket_name, 0, 1) {} |
| 55 | |
| 56 | private: |
| 57 | // content::DevToolsHttpHandler::ServerSocketFactory. |
anand.ratn | 449f39a4 | 2014-10-06 13:45:57 | [diff] [blame] | 58 | virtual scoped_ptr<net::ServerSocket> Create() const override { |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 59 | return scoped_ptr<net::ServerSocket>( |
| 60 | new net::UnixDomainServerSocket( |
| 61 | base::Bind(&content::CanUserConnectToDevTools), |
| 62 | true /* use_abstract_namespace */)); |
| 63 | } |
| 64 | |
| 65 | DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory); |
| 66 | }; |
| 67 | #else |
| 68 | class TCPServerSocketFactory |
| 69 | : public content::DevToolsHttpHandler::ServerSocketFactory { |
| 70 | public: |
| 71 | TCPServerSocketFactory(const std::string& address, int port, int backlog) |
| 72 | : content::DevToolsHttpHandler::ServerSocketFactory( |
| 73 | address, port, backlog) {} |
| 74 | |
| 75 | private: |
| 76 | // content::DevToolsHttpHandler::ServerSocketFactory. |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame^] | 77 | scoped_ptr<net::ServerSocket> Create() const override { |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 78 | return scoped_ptr<net::ServerSocket>( |
| 79 | new net::TCPServerSocket(NULL, net::NetLog::Source())); |
| 80 | } |
| 81 | |
| 82 | DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); |
| 83 | }; |
| 84 | #endif |
| 85 | |
| 86 | scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory> |
| 87 | CreateSocketFactory() { |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 88 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 89 | #if defined(OS_ANDROID) |
| 90 | std::string socket_name = "content_shell_devtools_remote"; |
| 91 | if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { |
| 92 | socket_name = command_line.GetSwitchValueASCII( |
| 93 | switches::kRemoteDebuggingSocketName); |
| 94 | } |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 95 | return scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>( |
| 96 | new UnixDomainServerSocketFactory(socket_name)); |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 97 | #else |
| 98 | // See if the user specified a port on the command line (useful for |
| 99 | // automation). If not, use an ephemeral port by specifying 0. |
| 100 | int port = 0; |
| 101 | if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) { |
| 102 | int temp_port; |
| 103 | std::string port_str = |
| 104 | command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort); |
| 105 | if (base::StringToInt(port_str, &temp_port) && |
| 106 | temp_port > 0 && temp_port < 65535) { |
| 107 | port = temp_port; |
| 108 | } else { |
| 109 | DLOG(WARNING) << "Invalid http debugger port number " << temp_port; |
| 110 | } |
| 111 | } |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 112 | return scoped_ptr<content::DevToolsHttpHandler::ServerSocketFactory>( |
| 113 | new TCPServerSocketFactory("127.0.0.1", port, 1)); |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 114 | #endif |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 115 | } |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 116 | |
| 117 | class Target : public content::DevToolsTarget { |
| 118 | public: |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 119 | explicit Target(scoped_refptr<DevToolsAgentHost> agent_host); |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 120 | |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame^] | 121 | std::string GetId() const override { return agent_host_->GetId(); } |
| 122 | std::string GetParentId() const override { return std::string(); } |
| 123 | std::string GetType() const override { |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 124 | switch (agent_host_->GetType()) { |
| 125 | case DevToolsAgentHost::TYPE_WEB_CONTENTS: |
| 126 | return kTargetTypePage; |
| 127 | case DevToolsAgentHost::TYPE_SERVICE_WORKER: |
| 128 | return kTargetTypeServiceWorker; |
| 129 | default: |
| 130 | break; |
| 131 | } |
| 132 | return kTargetTypeOther; |
| 133 | } |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame^] | 134 | std::string GetTitle() const override { return agent_host_->GetTitle(); } |
| 135 | std::string GetDescription() const override { return std::string(); } |
| 136 | GURL GetURL() const override { return agent_host_->GetURL(); } |
| 137 | GURL GetFaviconURL() const override { return favicon_url_; } |
| 138 | base::TimeTicks GetLastActivityTime() const override { |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 139 | return last_activity_time_; |
| 140 | } |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame^] | 141 | bool IsAttached() const override { return agent_host_->IsAttached(); } |
| 142 | scoped_refptr<DevToolsAgentHost> GetAgentHost() const override { |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 143 | return agent_host_; |
| 144 | } |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame^] | 145 | bool Activate() const override; |
| 146 | bool Close() const override; |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 147 | |
| 148 | private: |
| 149 | scoped_refptr<DevToolsAgentHost> agent_host_; |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 150 | GURL favicon_url_; |
| 151 | base::TimeTicks last_activity_time_; |
| 152 | }; |
| 153 | |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 154 | Target::Target(scoped_refptr<DevToolsAgentHost> agent_host) |
| 155 | : agent_host_(agent_host) { |
| 156 | if (WebContents* web_contents = agent_host_->GetWebContents()) { |
| 157 | content::NavigationController& controller = web_contents->GetController(); |
| 158 | content::NavigationEntry* entry = controller.GetActiveEntry(); |
| 159 | if (entry != NULL && entry->GetURL().is_valid()) |
| 160 | favicon_url_ = entry->GetFavicon().url; |
| 161 | last_activity_time_ = web_contents->GetLastActiveTime(); |
| 162 | } |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | bool Target::Activate() const { |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 166 | return agent_host_->Activate(); |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | bool Target::Close() const { |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 170 | return agent_host_->Close(); |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 173 | } // namespace |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 174 | |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 175 | namespace content { |
| 176 | |
dgozman | 252e18d | 2014-09-22 12:40:06 | [diff] [blame] | 177 | // ShellDevToolsDelegate ---------------------------------------------------- |
| 178 | |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 179 | ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context) |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 180 | : browser_context_(browser_context) { |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 181 | std::string frontend_url; |
| 182 | #if defined(OS_ANDROID) |
[email protected] | efd2c3f | 2014-03-11 09:57:25 | [diff] [blame] | 183 | frontend_url = base::StringPrintf(kFrontEndURL, GetWebKitRevision().c_str()); |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 184 | #endif |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 185 | devtools_http_handler_ = |
[email protected] | 0e60fce | 2014-06-04 22:27:20 | [diff] [blame] | 186 | DevToolsHttpHandler::Start(CreateSocketFactory(), frontend_url, this, |
| 187 | base::FilePath()); |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | ShellDevToolsDelegate::~ShellDevToolsDelegate() { |
| 191 | } |
| 192 | |
| 193 | void ShellDevToolsDelegate::Stop() { |
| 194 | // The call below destroys this. |
| 195 | devtools_http_handler_->Stop(); |
| 196 | } |
| 197 | |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 198 | std::string ShellDevToolsDelegate::GetDiscoveryPageHTML() { |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 199 | #if defined(OS_ANDROID) |
| 200 | return std::string(); |
| 201 | #else |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 202 | return ResourceBundle::GetSharedInstance().GetRawDataResource( |
[email protected] | 4d8bb1a9 | 2012-11-01 21:12:40 | [diff] [blame] | 203 | IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string(); |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 204 | #endif |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 205 | } |
| 206 | |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 207 | bool ShellDevToolsDelegate::BundlesFrontendResources() { |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 208 | #if defined(OS_ANDROID) |
| 209 | return false; |
| 210 | #else |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 211 | return true; |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 212 | #endif |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 215 | base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() { |
| 216 | return base::FilePath(); |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 217 | } |
| 218 | |
dgozman | 252e18d | 2014-09-22 12:40:06 | [diff] [blame] | 219 | scoped_ptr<net::StreamListenSocket> |
| 220 | ShellDevToolsDelegate::CreateSocketForTethering( |
| 221 | net::StreamListenSocket::Delegate* delegate, |
| 222 | std::string* name) { |
| 223 | return scoped_ptr<net::StreamListenSocket>(); |
| 224 | } |
| 225 | |
| 226 | // ShellDevToolsManagerDelegate ---------------------------------------------- |
| 227 | |
| 228 | ShellDevToolsManagerDelegate::ShellDevToolsManagerDelegate( |
| 229 | BrowserContext* browser_context) |
| 230 | : browser_context_(browser_context) { |
| 231 | } |
| 232 | |
| 233 | ShellDevToolsManagerDelegate::~ShellDevToolsManagerDelegate() { |
| 234 | } |
| 235 | |
| 236 | base::DictionaryValue* ShellDevToolsManagerDelegate::HandleCommand( |
| 237 | DevToolsAgentHost* agent_host, |
| 238 | base::DictionaryValue* command) { |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | std::string ShellDevToolsManagerDelegate::GetPageThumbnailData( |
| 243 | const GURL& url) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 244 | return std::string(); |
[email protected] | e5ea93b | 2012-09-27 05:50:36 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | c78ecec21 | 2013-10-27 19:34:10 | [diff] [blame] | 247 | scoped_ptr<DevToolsTarget> |
dgozman | 252e18d | 2014-09-22 12:40:06 | [diff] [blame] | 248 | ShellDevToolsManagerDelegate::CreateNewTarget(const GURL& url) { |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 249 | Shell* shell = Shell::CreateNewWindow(browser_context_, |
[email protected] | c78ecec21 | 2013-10-27 19:34:10 | [diff] [blame] | 250 | url, |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 251 | NULL, |
| 252 | MSG_ROUTING_NONE, |
[email protected] | cdb80672 | 2013-01-10 14:18:23 | [diff] [blame] | 253 | gfx::Size()); |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 254 | return scoped_ptr<DevToolsTarget>( |
| 255 | new Target(DevToolsAgentHost::GetOrCreateFor(shell->web_contents()))); |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 256 | } |
| 257 | |
dgozman | 252e18d | 2014-09-22 12:40:06 | [diff] [blame] | 258 | void ShellDevToolsManagerDelegate::EnumerateTargets(TargetCallback callback) { |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 259 | TargetList targets; |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 260 | content::DevToolsAgentHost::List agents = |
| 261 | content::DevToolsAgentHost::GetOrCreateAll(); |
| 262 | for (content::DevToolsAgentHost::List::iterator it = agents.begin(); |
| 263 | it != agents.end(); ++it) { |
[email protected] | b3f957e6 | 2014-08-08 10:09:02 | [diff] [blame] | 264 | targets.push_back(new Target(*it)); |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 265 | } |
| 266 | callback.Run(targets); |
[email protected] | 2fb8c1d | 2013-03-11 22:00:17 | [diff] [blame] | 267 | } |
| 268 | |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 269 | } // namespace content |