[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 | |
vkuzkokov | cbabd58 | 2014-11-06 13:53:54 | [diff] [blame] | 5 | #include "content/shell/browser/shell_devtools_manager_delegate.h" |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 6 | |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
pfeldman | f7c18d024 | 2016-09-08 19:54:26 | [diff] [blame] | 11 | #include "base/atomicops.h" |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 13 | #include "base/command_line.h" |
[email protected] | 0e60fce | 2014-06-04 22:27:20 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 15 | #include "base/macros.h" |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 16 | #include "base/memory/ptr_util.h" |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 18 | #include "base/strings/stringprintf.h" |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 19 | #include "base/strings/utf_string_conversions.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 20 | #include "build/build_config.h" |
yzshen | 6051dfd3 | 2015-10-20 19:23:25 | [diff] [blame] | 21 | #include "content/public/browser/browser_context.h" |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 22 | #include "content/public/browser/devtools_agent_host.h" |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 23 | #include "content/public/browser/devtools_socket_factory.h" |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 24 | #include "content/public/browser/favicon_status.h" |
| 25 | #include "content/public/browser/navigation_entry.h" |
| 26 | #include "content/public/browser/render_view_host.h" |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 27 | #include "content/public/browser/web_contents.h" |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 28 | #include "content/public/common/content_switches.h" |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 29 | #include "content/public/common/url_constants.h" |
[email protected] | efd2c3f | 2014-03-11 09:57:25 | [diff] [blame] | 30 | #include "content/public/common/user_agent.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 31 | #include "content/shell/browser/shell.h" |
dgozman | 102fee9 | 2015-04-20 15:45:46 | [diff] [blame] | 32 | #include "content/shell/common/shell_content_client.h" |
thakis | da47dac | 2017-02-27 14:35:30 | [diff] [blame] | 33 | #include "content/shell/grit/shell_resources.h" |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 34 | #include "net/base/net_errors.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 35 | #include "net/log/net_log_source.h" |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 36 | #include "net/socket/tcp_server_socket.h" |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 37 | #include "ui/base/resource/resource_bundle.h" |
| 38 | |
chenwilliam | e88d836 | 2017-03-21 02:34:30 | [diff] [blame^] | 39 | #if !defined(OS_ANDROID) |
| 40 | #include "content/public/browser/devtools_frontend_host.h" |
| 41 | #endif |
| 42 | |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 43 | #if defined(OS_ANDROID) |
| 44 | #include "content/public/browser/android/devtools_auth.h" |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 45 | #include "net/socket/unix_domain_server_socket_posix.h" |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 46 | #endif |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 47 | |
vkuzkokov | cbabd58 | 2014-11-06 13:53:54 | [diff] [blame] | 48 | namespace content { |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 49 | |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 50 | namespace { |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 51 | |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 52 | #if defined(OS_ANDROID) |
| 53 | const char kFrontEndURL[] = |
dgozman | a1409c4 | 2014-11-19 16:06:12 | [diff] [blame] | 54 | "http://chrome-devtools-frontend.appspot.com/serve_rev/%s/inspector.html"; |
[email protected] | 8dffd2c | 2013-12-02 14:14:38 | [diff] [blame] | 55 | #endif |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 56 | |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 57 | const int kBackLog = 10; |
| 58 | |
pfeldman | f7c18d024 | 2016-09-08 19:54:26 | [diff] [blame] | 59 | base::subtle::Atomic32 g_last_used_port; |
| 60 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 61 | #if defined(OS_ANDROID) |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 62 | class UnixDomainServerSocketFactory : public content::DevToolsSocketFactory { |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 63 | public: |
| 64 | explicit UnixDomainServerSocketFactory(const std::string& socket_name) |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 65 | : socket_name_(socket_name) {} |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 66 | |
| 67 | private: |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 68 | // content::DevToolsSocketFactory. |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 69 | std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 70 | std::unique_ptr<net::UnixDomainServerSocket> socket( |
tfarina | a7b245d | 2016-02-02 02:03:49 | [diff] [blame] | 71 | new net::UnixDomainServerSocket(base::Bind(&CanUserConnectToDevTools), |
| 72 | true /* use_abstract_namespace */)); |
| 73 | if (socket->BindAndListen(socket_name_, kBackLog) != net::OK) |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 74 | return std::unique_ptr<net::ServerSocket>(); |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 75 | |
tfarina | a7b245d | 2016-02-02 02:03:49 | [diff] [blame] | 76 | return std::move(socket); |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 77 | } |
| 78 | |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 79 | std::unique_ptr<net::ServerSocket> CreateForTethering( |
| 80 | std::string* out_name) override { |
| 81 | return nullptr; |
| 82 | } |
| 83 | |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 84 | std::string socket_name_; |
| 85 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 86 | DISALLOW_COPY_AND_ASSIGN(UnixDomainServerSocketFactory); |
| 87 | }; |
| 88 | #else |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 89 | class TCPServerSocketFactory : public content::DevToolsSocketFactory { |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 90 | public: |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 91 | TCPServerSocketFactory(const std::string& address, uint16_t port) |
| 92 | : address_(address), port_(port) {} |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 93 | |
| 94 | private: |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 95 | // content::DevToolsSocketFactory. |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 96 | std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 97 | std::unique_ptr<net::ServerSocket> socket( |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 98 | new net::TCPServerSocket(nullptr, net::NetLogSource())); |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 99 | if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK) |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 100 | return std::unique_ptr<net::ServerSocket>(); |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 101 | |
pfeldman | f7c18d024 | 2016-09-08 19:54:26 | [diff] [blame] | 102 | net::IPEndPoint endpoint; |
| 103 | if (socket->GetLocalAddress(&endpoint) == net::OK) |
| 104 | base::subtle::NoBarrier_Store(&g_last_used_port, endpoint.port()); |
| 105 | |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 106 | return socket; |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 107 | } |
| 108 | |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 109 | std::unique_ptr<net::ServerSocket> CreateForTethering( |
| 110 | std::string* out_name) override { |
| 111 | return nullptr; |
| 112 | } |
| 113 | |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 114 | std::string address_; |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 115 | uint16_t port_; |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 116 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 117 | DISALLOW_COPY_AND_ASSIGN(TCPServerSocketFactory); |
| 118 | }; |
| 119 | #endif |
| 120 | |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 121 | std::unique_ptr<content::DevToolsSocketFactory> CreateSocketFactory() { |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 122 | const base::CommandLine& command_line = |
| 123 | *base::CommandLine::ForCurrentProcess(); |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 124 | #if defined(OS_ANDROID) |
| 125 | std::string socket_name = "content_shell_devtools_remote"; |
| 126 | if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { |
| 127 | socket_name = command_line.GetSwitchValueASCII( |
| 128 | switches::kRemoteDebuggingSocketName); |
| 129 | } |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 130 | return std::unique_ptr<content::DevToolsSocketFactory>( |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 131 | new UnixDomainServerSocketFactory(socket_name)); |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 132 | #else |
| 133 | // See if the user specified a port on the command line (useful for |
| 134 | // automation). If not, use an ephemeral port by specifying 0. |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 135 | uint16_t port = 0; |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 136 | if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) { |
| 137 | int temp_port; |
| 138 | std::string port_str = |
| 139 | command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort); |
| 140 | if (base::StringToInt(port_str, &temp_port) && |
yzshen | 6051dfd3 | 2015-10-20 19:23:25 | [diff] [blame] | 141 | temp_port >= 0 && temp_port < 65535) { |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 142 | port = static_cast<uint16_t>(temp_port); |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 143 | } else { |
| 144 | DLOG(WARNING) << "Invalid http debugger port number " << temp_port; |
| 145 | } |
| 146 | } |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 147 | return std::unique_ptr<content::DevToolsSocketFactory>( |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 148 | new TCPServerSocketFactory("127.0.0.1", port)); |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 149 | #endif |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 150 | } |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 151 | |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 152 | } // namespace |
vkuzkokov | cbabd58 | 2014-11-06 13:53:54 | [diff] [blame] | 153 | |
dgozman | 252e18d | 2014-09-22 12:40:06 | [diff] [blame] | 154 | // ShellDevToolsManagerDelegate ---------------------------------------------- |
| 155 | |
vkuzkokov | cbabd58 | 2014-11-06 13:53:54 | [diff] [blame] | 156 | // static |
pfeldman | f7c18d024 | 2016-09-08 19:54:26 | [diff] [blame] | 157 | int ShellDevToolsManagerDelegate::GetHttpHandlerPort() { |
| 158 | return base::subtle::NoBarrier_Load(&g_last_used_port); |
| 159 | } |
| 160 | |
| 161 | // static |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 162 | void ShellDevToolsManagerDelegate::StartHttpHandler( |
vkuzkokov | cbabd58 | 2014-11-06 13:53:54 | [diff] [blame] | 163 | BrowserContext* browser_context) { |
| 164 | std::string frontend_url; |
| 165 | #if defined(OS_ANDROID) |
| 166 | frontend_url = base::StringPrintf(kFrontEndURL, GetWebKitRevision().c_str()); |
| 167 | #endif |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 168 | DevToolsAgentHost::StartRemoteDebuggingServer( |
dgozman | 102fee9 | 2015-04-20 15:45:46 | [diff] [blame] | 169 | CreateSocketFactory(), |
| 170 | frontend_url, |
yzshen | 6051dfd3 | 2015-10-20 19:23:25 | [diff] [blame] | 171 | browser_context->GetPath(), |
dgozman | 102fee9 | 2015-04-20 15:45:46 | [diff] [blame] | 172 | base::FilePath(), |
| 173 | std::string(), |
| 174 | GetShellUserAgent()); |
vkuzkokov | cbabd58 | 2014-11-06 13:53:54 | [diff] [blame] | 175 | } |
| 176 | |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 177 | // static |
| 178 | void ShellDevToolsManagerDelegate::StopHttpHandler() { |
| 179 | DevToolsAgentHost::StopRemoteDebuggingServer(); |
| 180 | } |
| 181 | |
Pavel Feldman | 43f56b7c | 2016-08-30 00:04:35 | [diff] [blame] | 182 | ShellDevToolsManagerDelegate::ShellDevToolsManagerDelegate( |
| 183 | BrowserContext* browser_context) |
| 184 | : browser_context_(browser_context) { |
dgozman | 252e18d | 2014-09-22 12:40:06 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | ShellDevToolsManagerDelegate::~ShellDevToolsManagerDelegate() { |
| 188 | } |
| 189 | |
Pavel Feldman | 43f56b7c | 2016-08-30 00:04:35 | [diff] [blame] | 190 | scoped_refptr<DevToolsAgentHost> |
| 191 | ShellDevToolsManagerDelegate::CreateNewTarget(const GURL& url) { |
| 192 | Shell* shell = Shell::CreateNewWindow(browser_context_, |
| 193 | url, |
| 194 | nullptr, |
| 195 | gfx::Size()); |
| 196 | return DevToolsAgentHost::GetOrCreateFor(shell->web_contents()); |
| 197 | } |
| 198 | |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 199 | std::string ShellDevToolsManagerDelegate::GetDiscoveryPageHTML() { |
| 200 | #if defined(OS_ANDROID) |
| 201 | return std::string(); |
| 202 | #else |
| 203 | return ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 204 | IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE).as_string(); |
| 205 | #endif |
| 206 | } |
| 207 | |
| 208 | std::string ShellDevToolsManagerDelegate::GetFrontendResource( |
| 209 | const std::string& path) { |
chenwilliam | e88d836 | 2017-03-21 02:34:30 | [diff] [blame^] | 210 | #if defined(OS_ANDROID) |
| 211 | return std::string(); |
| 212 | #else |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 213 | return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); |
chenwilliam | e88d836 | 2017-03-21 02:34:30 | [diff] [blame^] | 214 | #endif |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 215 | } |
| 216 | |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 217 | } // namespace content |