Skip to content

Commit

Permalink
WebWorker: Allow the WebWorker process to optionally use Qt networking
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
tcl3 authored and nico committed Nov 16, 2024
1 parent 2a35f9a commit 27f724b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Ladybird/AppKit/Application/ApplicationBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
16 changes: 9 additions & 7 deletions Ladybird/HelperProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion Ladybird/HelperProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 6 additions & 3 deletions Ladybird/Qt/WebContentView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
}
Expand Down
9 changes: 5 additions & 4 deletions Ladybird/Qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 19 additions & 5 deletions Ladybird/WebWorker/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
22 changes: 20 additions & 2 deletions Ladybird/WebWorker/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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));

Expand Down

0 comments on commit 27f724b

Please sign in to comment.