From 70471059cfab6481af42c9e7f000872b73363480 Mon Sep 17 00:00:00 2001 From: Vladimir Kharitonov Date: Wed, 7 Feb 2024 18:42:06 +0000 Subject: [PATCH] alloy: Allow --remote-debugging-port=0 (fixes #3619) --- include/internal/cef_types.h | 14 +++++--- .../devtools/devtools_manager_delegate.cc | 33 ++++++++++--------- libcef/common/alloy/alloy_main_delegate.cc | 5 +-- .../common/chrome/chrome_main_delegate_cef.cc | 5 +-- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/include/internal/cef_types.h b/include/internal/cef_types.h index acc707e9e..0dfaf0e88 100644 --- a/include/internal/cef_types.h +++ b/include/internal/cef_types.h @@ -432,11 +432,15 @@ typedef struct _cef_settings_t { /// /// Set to a value between 1024 and 65535 to enable remote debugging on the - /// specified port. Also configurable using the "remote-debugging-port" - /// command-line switch. Remote debugging can be accessed by loading the - /// chrome://inspect page in Google Chrome. Port numbers 9222 and 9229 are - /// discoverable by default. Other port numbers may need to be configured via - /// "Discover network targets" on the Devices tab. + /// specified port. Setting 0 will result in the selection of an ephemeral + /// port, the port number will be printed as part of WebSocket endpoit URL + /// to stderr. If the cache directory path is provided, the port will be + /// also written into /DevToolsActivePort file. + /// Also configurable using the "remote-debugging-port" command-line switch. + /// Remote debugging can be accessed by loading the chrome://inspect page in + /// Google Chrome. Port numbers 9222 and 9229 are discoverable by default. + /// Other port numbers may need to be configured via "Discover network + /// targets" on the Devices tab. /// int remote_debugging_port; diff --git a/libcef/browser/devtools/devtools_manager_delegate.cc b/libcef/browser/devtools/devtools_manager_delegate.cc index 9e6de98c7..c847032e3 100644 --- a/libcef/browser/devtools/devtools_manager_delegate.cc +++ b/libcef/browser/devtools/devtools_manager_delegate.cc @@ -70,28 +70,29 @@ class TCPServerSocketFactory : public content::DevToolsSocketFactory { std::unique_ptr CreateSocketFactory() { const base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); - // See if the user specified a port on the command line. Specifying 0 would - // result in the selection of an ephemeral port but that doesn't make sense - // for CEF where the URL is otherwise undiscoverable. Also, don't allow - // binding of ports between 0 and 1024 exclusive because they're normally - // restricted to root on Posix-based systems. - uint16_t port = 0; + // See if the user specified a port on the command line. Specifying 0 + // results in the selection of an ephemeral port, the port number will + // be printed as part of WebSocket endpoit URL to stderr. + // If the cache directory path is provided, the port will be also written + // into /DevToolsActivePort file. + // + // It's not allowed to bind ports between 0 and 1024 exclusive because + // they're normally restricted to root on Posix-based systems. if (command_line.HasSwitch(switches::kRemoteDebuggingPort)) { - int temp_port; + int port = 0; std::string port_str = command_line.GetSwitchValueASCII(switches::kRemoteDebuggingPort); - if (base::StringToInt(port_str, &temp_port) && temp_port >= 1024 && - temp_port < 65535) { - port = static_cast(temp_port); + + if (base::StringToInt(port_str, &port) && + (0 == port || (port >= 1024 && port < 65535))) { + return std::unique_ptr( + new TCPServerSocketFactory("127.0.0.1", port)); } else { - DLOG(WARNING) << "Invalid http debugger port number " << temp_port; + DLOG(WARNING) << "Invalid http debugger port number '" << port_str << "'"; } } - if (port == 0) { - return nullptr; - } - return std::unique_ptr( - new TCPServerSocketFactory("127.0.0.1", port)); + + return nullptr; } } // namespace diff --git a/libcef/common/alloy/alloy_main_delegate.cc b/libcef/common/alloy/alloy_main_delegate.cc index bdab512f1..d72c55171 100644 --- a/libcef/common/alloy/alloy_main_delegate.cc +++ b/libcef/common/alloy/alloy_main_delegate.cc @@ -346,8 +346,9 @@ std::optional AlloyMainDelegate::BasicStartupComplete() { } } - if (settings_->remote_debugging_port >= 1024 && - settings_->remote_debugging_port <= 65535) { + if (settings_->remote_debugging_port == 0 || + (settings_->remote_debugging_port >= 1024 && + settings_->remote_debugging_port <= 65535)) { command_line->AppendSwitchASCII( switches::kRemoteDebuggingPort, base::NumberToString(settings_->remote_debugging_port)); diff --git a/libcef/common/chrome/chrome_main_delegate_cef.cc b/libcef/common/chrome/chrome_main_delegate_cef.cc index c624e844a..3274f3860 100644 --- a/libcef/common/chrome/chrome_main_delegate_cef.cc +++ b/libcef/common/chrome/chrome_main_delegate_cef.cc @@ -114,8 +114,9 @@ std::optional ChromeMainDelegateCef::BasicStartupComplete() { CefString(&settings_->javascript_flags).ToString()); } - if (settings_->remote_debugging_port >= 1024 && - settings_->remote_debugging_port <= 65535) { + if (settings_->remote_debugging_port == 0 || + (settings_->remote_debugging_port >= 1024 && + settings_->remote_debugging_port <= 65535)) { command_line->AppendSwitchASCII( switches::kRemoteDebuggingPort, base::NumberToString(settings_->remote_debugging_port));