[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. |
| 58 | virtual scoped_ptr<net::ServerSocket> Create() const OVERRIDE { |
| 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. |
| 77 | virtual scoped_ptr<net::ServerSocket> Create() const OVERRIDE { |
| 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 | |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 121 | virtual std::string GetId() const OVERRIDE { return agent_host_->GetId(); } |
[email protected] | 002195f | 2014-06-02 07:33:32 | [diff] [blame] | 122 | virtual std::string GetParentId() const OVERRIDE { return std::string(); } |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 123 | virtual std::string GetType() const OVERRIDE { |
| 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 | } |
| 134 | virtual std::string GetTitle() const OVERRIDE { |
| 135 | return agent_host_->GetTitle(); |
| 136 | } |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 137 | virtual std::string GetDescription() const OVERRIDE { return std::string(); } |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 138 | virtual GURL GetURL() const OVERRIDE { return agent_host_->GetURL(); } |
[email protected] | 657d273 | 2014-05-07 10:09:51 | [diff] [blame] | 139 | virtual GURL GetFaviconURL() const OVERRIDE { return favicon_url_; } |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 140 | virtual base::TimeTicks GetLastActivityTime() const OVERRIDE { |
| 141 | return last_activity_time_; |
| 142 | } |
| 143 | virtual bool IsAttached() const OVERRIDE { |
| 144 | return agent_host_->IsAttached(); |
| 145 | } |
| 146 | virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE { |
| 147 | return agent_host_; |
| 148 | } |
| 149 | virtual bool Activate() const OVERRIDE; |
| 150 | virtual bool Close() const OVERRIDE; |
| 151 | |
| 152 | private: |
| 153 | scoped_refptr<DevToolsAgentHost> agent_host_; |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 154 | GURL favicon_url_; |
| 155 | base::TimeTicks last_activity_time_; |
| 156 | }; |
| 157 | |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 158 | Target::Target(scoped_refptr<DevToolsAgentHost> agent_host) |
| 159 | : agent_host_(agent_host) { |
| 160 | if (WebContents* web_contents = agent_host_->GetWebContents()) { |
| 161 | content::NavigationController& controller = web_contents->GetController(); |
| 162 | content::NavigationEntry* entry = controller.GetActiveEntry(); |
| 163 | if (entry != NULL && entry->GetURL().is_valid()) |
| 164 | favicon_url_ = entry->GetFavicon().url; |
| 165 | last_activity_time_ = web_contents->GetLastActiveTime(); |
| 166 | } |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | bool Target::Activate() const { |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 170 | return agent_host_->Activate(); |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | bool Target::Close() const { |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 174 | return agent_host_->Close(); |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 177 | } // namespace |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 178 | |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 179 | namespace content { |
| 180 | |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 181 | ShellDevToolsDelegate::ShellDevToolsDelegate(BrowserContext* browser_context) |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 182 | : browser_context_(browser_context) { |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 183 | std::string frontend_url; |
| 184 | #if defined(OS_ANDROID) |
[email protected] | efd2c3f | 2014-03-11 09:57:25 | [diff] [blame] | 185 | frontend_url = base::StringPrintf(kFrontEndURL, GetWebKitRevision().c_str()); |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 186 | #endif |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 187 | devtools_http_handler_ = |
[email protected] | 0e60fce | 2014-06-04 22:27:20 | [diff] [blame] | 188 | DevToolsHttpHandler::Start(CreateSocketFactory(), frontend_url, this, |
| 189 | base::FilePath()); |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | ShellDevToolsDelegate::~ShellDevToolsDelegate() { |
| 193 | } |
| 194 | |
| 195 | void ShellDevToolsDelegate::Stop() { |
| 196 | // The call below destroys this. |
| 197 | devtools_http_handler_->Stop(); |
| 198 | } |
| 199 | |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 200 | std::string ShellDevToolsDelegate::GetDiscoveryPageHTML() { |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 201 | #if defined(OS_ANDROID) |
| 202 | return std::string(); |
| 203 | #else |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 204 | return ResourceBundle::GetSharedInstance().GetRawDataResource( |
[email protected] | 4d8bb1a9 | 2012-11-01 21:12:40 | [diff] [blame] | 205 | IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string(); |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 206 | #endif |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 209 | bool ShellDevToolsDelegate::BundlesFrontendResources() { |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 210 | #if defined(OS_ANDROID) |
| 211 | return false; |
| 212 | #else |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 213 | return true; |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 214 | #endif |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 215 | } |
| 216 | |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 217 | base::FilePath ShellDevToolsDelegate::GetDebugFrontendDir() { |
| 218 | return base::FilePath(); |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 219 | } |
| 220 | |
[email protected] | e5ea93b | 2012-09-27 05:50:36 | [diff] [blame] | 221 | std::string ShellDevToolsDelegate::GetPageThumbnailData(const GURL& url) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 222 | return std::string(); |
[email protected] | e5ea93b | 2012-09-27 05:50:36 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | c78ecec21 | 2013-10-27 19:34:10 | [diff] [blame] | 225 | scoped_ptr<DevToolsTarget> |
| 226 | ShellDevToolsDelegate::CreateNewTarget(const GURL& url) { |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 227 | Shell* shell = Shell::CreateNewWindow(browser_context_, |
[email protected] | c78ecec21 | 2013-10-27 19:34:10 | [diff] [blame] | 228 | url, |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 229 | NULL, |
| 230 | MSG_ROUTING_NONE, |
[email protected] | cdb80672 | 2013-01-10 14:18:23 | [diff] [blame] | 231 | gfx::Size()); |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 232 | return scoped_ptr<DevToolsTarget>( |
| 233 | new Target(DevToolsAgentHost::GetOrCreateFor(shell->web_contents()))); |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 236 | void ShellDevToolsDelegate::EnumerateTargets(TargetCallback callback) { |
| 237 | TargetList targets; |
vkuzkokov | e1133eb | 2014-08-25 13:33:16 | [diff] [blame] | 238 | content::DevToolsAgentHost::List agents = |
| 239 | content::DevToolsAgentHost::GetOrCreateAll(); |
| 240 | for (content::DevToolsAgentHost::List::iterator it = agents.begin(); |
| 241 | it != agents.end(); ++it) { |
[email protected] | b3f957e6 | 2014-08-08 10:09:02 | [diff] [blame] | 242 | targets.push_back(new Target(*it)); |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 243 | } |
| 244 | callback.Run(targets); |
[email protected] | 2fb8c1d | 2013-03-11 22:00:17 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | 8b62f8e | 2013-09-03 19:03:06 | [diff] [blame] | 247 | scoped_ptr<net::StreamListenSocket> |
[email protected] | eafc845 | 2013-04-16 04:18:35 | [diff] [blame] | 248 | ShellDevToolsDelegate::CreateSocketForTethering( |
| 249 | net::StreamListenSocket::Delegate* delegate, |
| 250 | std::string* name) { |
[email protected] | 8b62f8e | 2013-09-03 19:03:06 | [diff] [blame] | 251 | return scoped_ptr<net::StreamListenSocket>(); |
[email protected] | eafc845 | 2013-04-16 04:18:35 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 254 | } // namespace content |