WebWorker: Allow the WebWorker process to optionally use Qt networking

This change adds a `--use-lagom-networking` flag to the WebWorker
process. Qt networking is used if this flag isn't passed. The flag is
passed the UI launches the WebWorker process unless the Qt chrome is
being run with the `--enable-qt-networking` flag.

(cherry picked from commit 886714632759c1e2feeb636388975f0e3b515839;
amended Qt/main.cpp to resolve minor conflict due to serenity not having
LadybirdBrowser/ladybird#284 LadybirdBrowser/ladybird#45, and
WebWorker/CMakeLists.txt due to serenity still having
`SERENITY_SOURCE_DIR` instead of `LADYBIRD_SOURCE_DIR` --
LadybirdBrowser/ladybird#17)
This commit is contained in:
Tim Ledbetter 2024-07-06 03:41:07 +01:00 committed by Nico Weber
parent 2a35f9a834
commit 27f724b435
7 changed files with 61 additions and 23 deletions

View file

@ -59,7 +59,7 @@ ErrorOr<NonnullRefPtr<WebView::WebContentClient>> ApplicationBridge::launch_web_
ErrorOr<IPC::File> ApplicationBridge::launch_web_worker()
{
auto web_worker_paths = TRY(get_paths_for_helper_process("WebWorker"sv));
auto worker_client = TRY(launch_web_worker_process(web_worker_paths, *m_impl->request_server_client));
auto worker_client = TRY(launch_web_worker_process(web_worker_paths, m_impl->request_server_client));
return worker_client->dup_socket();
}

View file

@ -145,14 +145,16 @@ ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> launch_image_decoder_process(
return launch_generic_server_process<ImageDecoderClient::Client>("ImageDecoder"sv, candidate_image_decoder_paths, {}, RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
}
ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, NonnullRefPtr<Protocol::RequestClient> request_client)
ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, RefPtr<Protocol::RequestClient> request_client)
{
auto socket = TRY(connect_new_request_server_client(move(request_client)));
Vector<ByteString> arguments {
"--request-server-socket"sv,
ByteString::number(socket.fd()),
};
Vector<ByteString> arguments;
if (request_client) {
auto socket = TRY(connect_new_request_server_client(*request_client));
arguments.append("--request-server-socket"sv);
arguments.append(ByteString::number(socket.fd()));
arguments.append("--use-lagom-networking"sv);
return launch_generic_server_process<Web::HTML::WebWorkerClient>("WebWorker"sv, candidate_web_worker_paths, move(arguments), RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
}
return launch_generic_server_process<Web::HTML::WebWorkerClient>("WebWorker"sv, candidate_web_worker_paths, move(arguments), RegisterWithProcessManager::Yes, Ladybird::EnableCallgrindProfiling::No);
}

View file

@ -25,7 +25,7 @@ ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
Optional<IPC::File> request_server_socket = {});
ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> launch_image_decoder_process(ReadonlySpan<ByteString> candidate_image_decoder_paths);
ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, NonnullRefPtr<Protocol::RequestClient>);
ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, RefPtr<Protocol::RequestClient>);
ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(ReadonlySpan<ByteString> candidate_request_server_paths, StringView serenity_resource_root, Vector<ByteString> const& certificates);
ErrorOr<NonnullRefPtr<SQL::SQLClient>> launch_sql_server_process(ReadonlySpan<ByteString> candidate_sql_server_paths);

View file

@ -131,9 +131,12 @@ WebContentView::WebContentView(QWidget* window, WebContentOptions const& web_con
finish_handling_key_event(event);
};
on_request_worker_agent = []() {
auto& request_server_client = static_cast<Ladybird::Application*>(QApplication::instance())->request_server_client;
auto worker_client = MUST(launch_web_worker_process(MUST(get_paths_for_helper_process("WebWorker"sv)), *request_server_client));
on_request_worker_agent = [&]() {
RefPtr<Protocol::RequestClient> request_server_client {};
if (m_web_content_options.use_lagom_networking == Ladybird::UseLagomNetworking::Yes)
request_server_client = static_cast<Ladybird::Application*>(QApplication::instance())->request_server_client;
auto worker_client = MUST(launch_web_worker_process(MUST(get_paths_for_helper_process("WebWorker"sv)), request_server_client));
return worker_client->dup_socket();
};
}

View file

@ -167,11 +167,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto cookie_jar = database ? TRY(WebView::CookieJar::create(*database)) : WebView::CookieJar::create();
// NOTE: WebWorker *always* needs a request server connection, even if WebContent uses Qt Networking
// FIXME: Create an abstraction to re-spawn the RequestServer and re-hook up its client hooks to each tab on crash
auto request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
auto protocol_client = TRY(launch_request_server_process(request_server_paths, s_serenity_resource_root, certificates));
app.request_server_client = protocol_client;
if (!enable_qt_networking) {
auto request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
auto protocol_client = TRY(launch_request_server_process(request_server_paths, s_serenity_resource_root, certificates));
app.request_server_client = move(protocol_client);
}
StringBuilder command_line_builder;
command_line_builder.join(' ', arguments.strings);

View file

@ -1,9 +1,5 @@
set(WEBWORKER_SOURCE_DIR ${SERENITY_SOURCE_DIR}/Userland/Services/WebWorker)
set(CMAKE_AUTOMOC OFF)
set(CMAKE_AUTORCC OFF)
set(CMAKE_AUTOUIC OFF)
set(WEBWORKER_SOURCES
"${WEBWORKER_SOURCE_DIR}/ConnectionFromClient.cpp"
"${WEBWORKER_SOURCE_DIR}/DedicatedWorkerHost.cpp"
@ -13,12 +9,30 @@ set(WEBWORKER_SOURCES
)
add_library(webworker STATIC ${WEBWORKER_SOURCES})
set_target_properties(webworker PROPERTIES AUTOMOC OFF AUTORCC OFF AUTOUIC OFF)
target_include_directories(webworker PRIVATE ${SERENITY_SOURCE_DIR}/Userland/Services/)
target_include_directories(webworker PRIVATE ${SERENITY_SOURCE_DIR}/Userland/)
target_include_directories(webworker PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/..)
target_link_libraries(webworker PUBLIC LibCore LibFileSystem LibGfx LibIPC LibJS LibProtocol LibWeb LibWebView LibLocale LibImageDecoderClient LibMain LibSQL LibURL)
add_executable(WebWorker main.cpp)
if (ENABLE_QT)
qt_add_executable(WebWorker
../Qt/EventLoopImplementationQt.cpp
../Qt/EventLoopImplementationQtEventTarget.cpp
../Qt/RequestManagerQt.cpp
../Qt/StringUtils.cpp
../Qt/WebSocketQt.cpp
../Qt/WebSocketImplQt.cpp
main.cpp
)
target_link_libraries(WebWorker PRIVATE Qt::Core Qt::Network)
target_link_libraries(WebWorker PRIVATE webworker LibWebSocket)
target_compile_definitions(WebWorker PRIVATE HAVE_QT=1)
else()
add_executable(WebWorker main.cpp)
endif()
target_include_directories(WebWorker PRIVATE ${SERENITY_SOURCE_DIR}/Userland/)
target_link_libraries(WebWorker PRIVATE webworker)

View file

@ -26,6 +26,12 @@
#include <LibWebView/WebSocketClientAdapter.h>
#include <WebWorker/ConnectionFromClient.h>
#if defined(HAVE_QT)
# include <Ladybird/Qt/EventLoopImplementationQt.h>
# include <Ladybird/Qt/RequestManagerQt.h>
# include <QCoreApplication>
#endif
static ErrorOr<void> initialize_lagom_networking(int request_server_socket);
ErrorOr<int> serenity_main(Main::Arguments arguments)
@ -34,20 +40,32 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
int request_server_socket { -1 };
StringView serenity_resource_root;
bool use_lagom_networking { false };
Core::ArgsParser args_parser;
args_parser.add_option(request_server_socket, "File descriptor of the request server socket", "request-server-socket", 's', "request-server-socket");
args_parser.add_option(serenity_resource_root, "Absolute path to directory for serenity resources", "serenity-resource-root", 'r', "serenity-resource-root");
args_parser.add_option(use_lagom_networking, "Enable Lagom servers for networking", "use-lagom-networking");
args_parser.parse(arguments);
#if defined(HAVE_QT)
QCoreApplication app(arguments.argc, arguments.argv);
Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
#endif
Core::EventLoop event_loop;
platform_init();
Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity);
Core::EventLoop event_loop;
Web::Platform::FontPlugin::install(*new Web::Platform::FontPluginSerenity);
TRY(initialize_lagom_networking(request_server_socket));
#if defined(HAVE_QT)
if (!use_lagom_networking)
Web::ResourceLoader::initialize(Ladybird::RequestManagerQt::create());
else
#endif
TRY(initialize_lagom_networking(request_server_socket));
TRY(Web::Bindings::initialize_main_thread_vm(Web::HTML::EventLoop::Type::Worker));