Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[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] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 12 | #include "base/command_line.h" |
[email protected] | 0e60fce | 2014-06-04 22:27:20 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 14 | #include "base/functional/bind.h" |
| 15 | #include "base/functional/callback.h" |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 16 | #include "base/strings/string_number_conversions.h" |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 18 | #include "build/build_config.h" |
yzshen | 6051dfd3 | 2015-10-20 19:23:25 | [diff] [blame] | 19 | #include "content/public/browser/browser_context.h" |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 20 | #include "content/public/browser/devtools_agent_host.h" |
Johannes Henkel | 9d14e4b8 | 2020-01-28 01:29:41 | [diff] [blame] | 21 | #include "content/public/browser/devtools_agent_host_client_channel.h" |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 22 | #include "content/public/browser/devtools_socket_factory.h" |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 23 | #include "content/public/browser/favicon_status.h" |
| 24 | #include "content/public/browser/navigation_entry.h" |
| 25 | #include "content/public/browser/render_view_host.h" |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 26 | #include "content/public/browser/web_contents.h" |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 27 | #include "content/public/common/content_switches.h" |
[email protected] | eb04099 | 2012-10-10 16:56:00 | [diff] [blame] | 28 | #include "content/public/common/url_constants.h" |
[email protected] | efd2c3f | 2014-03-11 09:57:25 | [diff] [blame] | 29 | #include "content/public/common/user_agent.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 30 | #include "content/shell/browser/shell.h" |
dgozman | 102fee9 | 2015-04-20 15:45:46 | [diff] [blame] | 31 | #include "content/shell/common/shell_content_client.h" |
dgozman | 454746093 | 2017-06-22 03:04:36 | [diff] [blame] | 32 | #include "content/shell/common/shell_switches.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 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 39 | #if !BUILDFLAG(IS_ANDROID) |
chenwilliam | e88d836 | 2017-03-21 02:34:30 | [diff] [blame] | 40 | #include "content/public/browser/devtools_frontend_host.h" |
| 41 | #endif |
| 42 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 43 | #if BUILDFLAG(IS_ANDROID) |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 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 | |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 52 | const int kBackLog = 10; |
| 53 | |
pfeldman | f7c18d024 | 2016-09-08 19:54:26 | [diff] [blame] | 54 | base::subtle::Atomic32 g_last_used_port; |
| 55 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 56 | #if BUILDFLAG(IS_ANDROID) |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 57 | class UnixDomainServerSocketFactory : public content::DevToolsSocketFactory { |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 58 | public: |
| 59 | explicit UnixDomainServerSocketFactory(const std::string& socket_name) |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 60 | : socket_name_(socket_name) {} |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 61 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 62 | UnixDomainServerSocketFactory(const UnixDomainServerSocketFactory&) = delete; |
| 63 | UnixDomainServerSocketFactory& operator=( |
| 64 | const UnixDomainServerSocketFactory&) = delete; |
| 65 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 66 | private: |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 67 | // content::DevToolsSocketFactory. |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 68 | std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 69 | std::unique_ptr<net::UnixDomainServerSocket> socket( |
danakj | c988f0be | 2019-05-17 20:48:01 | [diff] [blame] | 70 | new net::UnixDomainServerSocket( |
| 71 | base::BindRepeating(&CanUserConnectToDevTools), |
| 72 | true /* use_abstract_namespace */)); |
tfarina | a7b245d | 2016-02-02 02:03:49 | [diff] [blame] | 73 | if (socket->BindAndListen(socket_name_, kBackLog) != net::OK) |
Lei Zhang | df291f6 | 2021-04-14 17:23:44 | [diff] [blame] | 74 | return nullptr; |
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_; |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 85 | }; |
| 86 | #else |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 87 | class TCPServerSocketFactory : public content::DevToolsSocketFactory { |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 88 | public: |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 89 | TCPServerSocketFactory(const std::string& address, uint16_t port) |
| 90 | : address_(address), port_(port) {} |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 91 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 92 | TCPServerSocketFactory(const TCPServerSocketFactory&) = delete; |
| 93 | TCPServerSocketFactory& operator=(const TCPServerSocketFactory&) = delete; |
| 94 | |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 95 | private: |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 96 | // content::DevToolsSocketFactory. |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 97 | std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 98 | std::unique_ptr<net::ServerSocket> socket( |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 99 | new net::TCPServerSocket(nullptr, net::NetLogSource())); |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 100 | if (socket->ListenWithAddressAndPort(address_, port_, kBackLog) != net::OK) |
Lei Zhang | df291f6 | 2021-04-14 17:23:44 | [diff] [blame] | 101 | return nullptr; |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 102 | |
pfeldman | f7c18d024 | 2016-09-08 19:54:26 | [diff] [blame] | 103 | net::IPEndPoint endpoint; |
| 104 | if (socket->GetLocalAddress(&endpoint) == net::OK) |
| 105 | base::subtle::NoBarrier_Store(&g_last_used_port, endpoint.port()); |
| 106 | |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 107 | return socket; |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 108 | } |
| 109 | |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 110 | std::unique_ptr<net::ServerSocket> CreateForTethering( |
| 111 | std::string* out_name) override { |
| 112 | return nullptr; |
| 113 | } |
| 114 | |
vkuzkokov | 7942676 | 2015-01-13 08:03:00 | [diff] [blame] | 115 | std::string address_; |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 116 | uint16_t port_; |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 117 | }; |
| 118 | #endif |
| 119 | |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 120 | std::unique_ptr<content::DevToolsSocketFactory> CreateSocketFactory() { |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 121 | const base::CommandLine& command_line = |
| 122 | *base::CommandLine::ForCurrentProcess(); |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 123 | #if BUILDFLAG(IS_ANDROID) |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 124 | std::string socket_name = "content_shell_devtools_remote"; |
| 125 | if (command_line.HasSwitch(switches::kRemoteDebuggingSocketName)) { |
| 126 | socket_name = command_line.GetSwitchValueASCII( |
| 127 | switches::kRemoteDebuggingSocketName); |
| 128 | } |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 129 | return std::unique_ptr<content::DevToolsSocketFactory>( |
byungchul | 38c3ae7 | 2014-08-25 23:27:46 | [diff] [blame] | 130 | new UnixDomainServerSocketFactory(socket_name)); |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 131 | #else |
| 132 | // See if the user specified a port on the command line (useful for |
| 133 | // automation). If not, use an ephemeral port by specifying 0. |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 134 | uint16_t port = 0; |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 135 | if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) { |
| 136 | int temp_port; |
| 137 | std::string port_str = |
| 138 | command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort); |
| 139 | if (base::StringToInt(port_str, &temp_port) && |
yzshen | 6051dfd3 | 2015-10-20 19:23:25 | [diff] [blame] | 140 | temp_port >= 0 && temp_port < 65535) { |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 141 | port = static_cast<uint16_t>(temp_port); |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 142 | } else { |
| 143 | DLOG(WARNING) << "Invalid http debugger port number " << temp_port; |
| 144 | } |
| 145 | } |
Tiago Vignatti | a293c2c | 2023-03-28 20:31:04 | [diff] [blame^] | 146 | // By default listen to incoming DevTools connections on localhost. |
| 147 | std::string address_str = net::IPAddress::IPv4Localhost().ToString(); |
| 148 | if (command_line.HasSwitch(switches::kRemoteDebuggingAddress)) { |
| 149 | net::IPAddress address; |
| 150 | address_str = |
| 151 | command_line.GetSwitchValueASCII(switches::kRemoteDebuggingAddress); |
| 152 | if (!address.AssignFromIPLiteral(address_str)) { |
| 153 | DLOG(WARNING) << "Invalid devtools server address: " << address_str; |
| 154 | } |
| 155 | } |
pfeldman | f1a1694 | 2016-09-02 21:28:00 | [diff] [blame] | 156 | return std::unique_ptr<content::DevToolsSocketFactory>( |
Tiago Vignatti | a293c2c | 2023-03-28 20:31:04 | [diff] [blame^] | 157 | new TCPServerSocketFactory(address_str, port)); |
[email protected] | ed6635e | 2012-10-27 00:44:09 | [diff] [blame] | 158 | #endif |
[email protected] | 90e6c541 | 2013-04-16 22:45:25 | [diff] [blame] | 159 | } |
[email protected] | 852b34ba | 2013-10-03 16:29:06 | [diff] [blame] | 160 | |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 161 | } // namespace |
vkuzkokov | cbabd58 | 2014-11-06 13:53:54 | [diff] [blame] | 162 | |
dgozman | 252e18d | 2014-09-22 12:40:06 | [diff] [blame] | 163 | // ShellDevToolsManagerDelegate ---------------------------------------------- |
| 164 | |
vkuzkokov | cbabd58 | 2014-11-06 13:53:54 | [diff] [blame] | 165 | // static |
pfeldman | f7c18d024 | 2016-09-08 19:54:26 | [diff] [blame] | 166 | int ShellDevToolsManagerDelegate::GetHttpHandlerPort() { |
| 167 | return base::subtle::NoBarrier_Load(&g_last_used_port); |
| 168 | } |
| 169 | |
| 170 | // static |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 171 | void ShellDevToolsManagerDelegate::StartHttpHandler( |
vkuzkokov | cbabd58 | 2014-11-06 13:53:54 | [diff] [blame] | 172 | BrowserContext* browser_context) { |
| 173 | std::string frontend_url; |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 174 | DevToolsAgentHost::StartRemoteDebuggingServer( |
Pavel Feldman | c8a484b5 | 2018-02-07 21:07:32 | [diff] [blame] | 175 | CreateSocketFactory(), browser_context->GetPath(), base::FilePath()); |
Pavel Feldman | 2b11e235 | 2017-10-25 05:24:01 | [diff] [blame] | 176 | |
| 177 | const base::CommandLine& command_line = |
| 178 | *base::CommandLine::ForCurrentProcess(); |
| 179 | if (command_line.HasSwitch(switches::kRemoteDebuggingPipe)) |
Dmitry Gozman | 8bbf58d | 2020-11-13 22:48:26 | [diff] [blame] | 180 | DevToolsAgentHost::StartRemoteDebuggingPipeHandler(base::OnceClosure()); |
vkuzkokov | cbabd58 | 2014-11-06 13:53:54 | [diff] [blame] | 181 | } |
| 182 | |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 183 | // static |
| 184 | void ShellDevToolsManagerDelegate::StopHttpHandler() { |
| 185 | DevToolsAgentHost::StopRemoteDebuggingServer(); |
| 186 | } |
| 187 | |
Pavel Feldman | 43f56b7c | 2016-08-30 00:04:35 | [diff] [blame] | 188 | ShellDevToolsManagerDelegate::ShellDevToolsManagerDelegate( |
| 189 | BrowserContext* browser_context) |
| 190 | : browser_context_(browser_context) { |
dgozman | 252e18d | 2014-09-22 12:40:06 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | ShellDevToolsManagerDelegate::~ShellDevToolsManagerDelegate() { |
| 194 | } |
| 195 | |
Andrey Lushnikov | 36299bc | 2018-08-23 22:09:54 | [diff] [blame] | 196 | BrowserContext* ShellDevToolsManagerDelegate::GetDefaultBrowserContext() { |
| 197 | return browser_context_; |
| 198 | } |
| 199 | |
Andrey Lushnikov | df165198 | 2018-09-14 19:00:37 | [diff] [blame] | 200 | void ShellDevToolsManagerDelegate::ClientAttached( |
Johannes Henkel | 9d14e4b8 | 2020-01-28 01:29:41 | [diff] [blame] | 201 | content::DevToolsAgentHostClientChannel* channel) { |
Andrey Lushnikov | df165198 | 2018-09-14 19:00:37 | [diff] [blame] | 202 | // Make sure we don't receive notifications twice for the same client. |
Johannes Henkel | 9d14e4b8 | 2020-01-28 01:29:41 | [diff] [blame] | 203 | CHECK(clients_.find(channel->GetClient()) == clients_.end()); |
| 204 | clients_.insert(channel->GetClient()); |
Andrey Lushnikov | df165198 | 2018-09-14 19:00:37 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void ShellDevToolsManagerDelegate::ClientDetached( |
Johannes Henkel | 9d14e4b8 | 2020-01-28 01:29:41 | [diff] [blame] | 208 | content::DevToolsAgentHostClientChannel* channel) { |
| 209 | clients_.erase(channel->GetClient()); |
Andrey Lushnikov | df165198 | 2018-09-14 19:00:37 | [diff] [blame] | 210 | } |
| 211 | |
Danil Somsikov | d5b3684 | 2022-12-16 11:24:50 | [diff] [blame] | 212 | scoped_refptr<DevToolsAgentHost> ShellDevToolsManagerDelegate::CreateNewTarget( |
| 213 | const GURL& url, |
| 214 | bool for_tab) { |
danakj | 24577b1 | 2020-05-13 22:38:18 | [diff] [blame] | 215 | Shell* shell = Shell::CreateNewWindow(browser_context_, url, nullptr, |
| 216 | Shell::GetShellDefaultSize()); |
Danil Somsikov | d5b3684 | 2022-12-16 11:24:50 | [diff] [blame] | 217 | return for_tab ? DevToolsAgentHost::GetOrCreateForTab(shell->web_contents()) |
| 218 | : DevToolsAgentHost::GetOrCreateFor(shell->web_contents()); |
Pavel Feldman | 43f56b7c | 2016-08-30 00:04:35 | [diff] [blame] | 219 | } |
| 220 | |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 221 | std::string ShellDevToolsManagerDelegate::GetDiscoveryPageHTML() { |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 222 | #if BUILDFLAG(IS_ANDROID) |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 223 | return std::string(); |
| 224 | #else |
Kristian Kirs | b9e27051 | 2020-07-08 15:21:20 | [diff] [blame] | 225 | return ui::ResourceBundle::GetSharedInstance().LoadDataResourceString( |
| 226 | IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE); |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 227 | #endif |
| 228 | } |
| 229 | |
Pavel Feldman | c8a484b5 | 2018-02-07 21:07:32 | [diff] [blame] | 230 | bool ShellDevToolsManagerDelegate::HasBundledFrontendResources() { |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 231 | #if BUILDFLAG(IS_ANDROID) |
Pavel Feldman | c8a484b5 | 2018-02-07 21:07:32 | [diff] [blame] | 232 | return false; |
Nico Weber | 2771b0f | 2021-08-16 17:16:24 | [diff] [blame] | 233 | #else |
Pavel Feldman | c8a484b5 | 2018-02-07 21:07:32 | [diff] [blame] | 234 | return true; |
Nico Weber | 2771b0f | 2021-08-16 17:16:24 | [diff] [blame] | 235 | #endif |
pfeldman | 1062876 | 2016-09-08 07:59:26 | [diff] [blame] | 236 | } |
| 237 | |
[email protected] | ee75b899 | 2012-01-27 07:53:57 | [diff] [blame] | 238 | } // namespace content |