mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:37:35 +00:00
LibWeb+WebContent: Spawn Worker processes from the chrome
Instead of spawning these processes from the WebContent process, we now create them in the Browser chrome. Part 1/N of "all processes are owned by the chrome".
This commit is contained in:
parent
6ea4c248ab
commit
02edd240ae
26 changed files with 152 additions and 99 deletions
|
@ -457,6 +457,7 @@ class Window;
|
|||
class WindowEnvironmentSettingsObject;
|
||||
class WindowProxy;
|
||||
class Worker;
|
||||
class WorkerAgent;
|
||||
class WorkerDebugConsoleClient;
|
||||
class WorkerEnvironmentSettingsObject;
|
||||
class WorkerGlobalScope;
|
||||
|
|
|
@ -4,93 +4,11 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibWeb/Bindings/HostDefined.h>
|
||||
#include <LibWeb/HTML/WorkerAgent.h>
|
||||
#include <LibWeb/Page/Page.h>
|
||||
#include <LibWeb/Worker/WebWorkerClient.h>
|
||||
|
||||
// FIXME: Deduplicate this code with ladybird!!
|
||||
|
||||
#ifndef AK_OS_SERENITY
|
||||
namespace {
|
||||
|
||||
ErrorOr<String> application_directory()
|
||||
{
|
||||
auto current_executable_path = TRY(Core::System::current_executable_path());
|
||||
auto dirname = LexicalPath::dirname(current_executable_path);
|
||||
return String::from_byte_string(dirname);
|
||||
}
|
||||
|
||||
ErrorOr<Vector<String>> get_paths_for_helper_process(StringView process_name)
|
||||
{
|
||||
auto application_path = TRY(application_directory());
|
||||
Vector<String> paths;
|
||||
|
||||
TRY(paths.try_append(TRY(String::formatted("{}/{}", application_path, process_name))));
|
||||
TRY(paths.try_append(TRY(String::formatted("./{}", process_name))));
|
||||
// NOTE: Add platform-specific paths here
|
||||
return paths;
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<String> candidate_web_content_paths)
|
||||
{
|
||||
int socket_fds[2] {};
|
||||
TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
|
||||
|
||||
int ui_fd = socket_fds[0];
|
||||
int wc_fd = socket_fds[1];
|
||||
|
||||
int fd_passing_socket_fds[2] {};
|
||||
TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, fd_passing_socket_fds));
|
||||
|
||||
int ui_fd_passing_fd = fd_passing_socket_fds[0];
|
||||
int wc_fd_passing_fd = fd_passing_socket_fds[1];
|
||||
|
||||
if (auto child_pid = TRY(Core::System::fork()); child_pid == 0) {
|
||||
TRY(Core::System::close(ui_fd_passing_fd));
|
||||
TRY(Core::System::close(ui_fd));
|
||||
|
||||
auto takeover_string = TRY(String::formatted("WebWorker:{}", wc_fd));
|
||||
TRY(Core::System::setenv("SOCKET_TAKEOVER"sv, takeover_string, true));
|
||||
|
||||
auto webcontent_fd_passing_socket_string = TRY(String::number(wc_fd_passing_fd));
|
||||
|
||||
ErrorOr<void> result;
|
||||
for (auto const& path : candidate_web_content_paths) {
|
||||
if (Core::System::access(path, X_OK).is_error())
|
||||
continue;
|
||||
|
||||
auto arguments = Vector {
|
||||
path.bytes_as_string_view(),
|
||||
"--fd-passing-socket"sv,
|
||||
webcontent_fd_passing_socket_string
|
||||
};
|
||||
|
||||
result = Core::System::exec(arguments[0], arguments.span(), Core::System::SearchInPath::Yes);
|
||||
if (!result.is_error())
|
||||
break;
|
||||
}
|
||||
|
||||
if (result.is_error())
|
||||
warnln("Could not launch any of {}: {}", candidate_web_content_paths, result.error());
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
TRY(Core::System::close(wc_fd_passing_fd));
|
||||
TRY(Core::System::close(wc_fd));
|
||||
|
||||
auto socket = TRY(Core::LocalSocket::adopt_fd(ui_fd));
|
||||
TRY(socket->set_blocking(true));
|
||||
|
||||
auto new_client = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Web::HTML::WebWorkerClient(move(socket))));
|
||||
new_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(ui_fd_passing_fd)));
|
||||
|
||||
return new_client;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(WorkerAgent);
|
||||
|
@ -109,16 +27,20 @@ void WorkerAgent::initialize(JS::Realm& realm)
|
|||
m_message_port = MessagePort::create(realm);
|
||||
m_message_port->entangle_with(*m_outside_port);
|
||||
|
||||
#ifndef AK_OS_SERENITY
|
||||
auto paths = MUST(get_paths_for_helper_process("WebWorker"sv));
|
||||
m_worker_ipc = MUST(launch_web_worker_process(paths));
|
||||
#else
|
||||
m_worker_ipc = MUST(Web::HTML::WebWorkerClient::try_create());
|
||||
#endif
|
||||
|
||||
TransferDataHolder data_holder;
|
||||
MUST(m_message_port->transfer_steps(data_holder));
|
||||
|
||||
// NOTE: This blocking IPC call may launch another process.
|
||||
// If spinning the event loop for this can cause other javascript to execute, we're in trouble.
|
||||
auto worker_ipc_sockets = Bindings::host_defined_page(realm).client().request_worker_agent();
|
||||
auto worker_socket = MUST(Core::LocalSocket::adopt_fd(worker_ipc_sockets.socket.take_fd()));
|
||||
MUST(worker_socket->set_blocking(true));
|
||||
|
||||
auto fd_passing_socket = MUST(Core::LocalSocket::adopt_fd(worker_ipc_sockets.fd_passing_socket.take_fd()));
|
||||
|
||||
m_worker_ipc = make_ref_counted<WebWorkerClient>(move(worker_socket));
|
||||
m_worker_ipc->set_fd_passing_socket(move(fd_passing_socket));
|
||||
|
||||
m_worker_ipc->async_start_dedicated_worker(m_url, m_worker_options.type, m_worker_options.credentials, m_worker_options.name, move(data_holder));
|
||||
}
|
||||
|
||||
|
|
|
@ -6,12 +6,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/HTML/MessageEvent.h>
|
||||
#include <LibWeb/HTML/MessagePort.h>
|
||||
#include <LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h>
|
||||
#include <LibWeb/HTML/Window.h>
|
||||
#include <LibWeb/Worker/WebWorkerClient.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
@ -22,14 +18,12 @@ struct WorkerOptions {
|
|||
String name { String {} };
|
||||
};
|
||||
|
||||
struct WorkerAgent : JS::Cell {
|
||||
class WorkerAgent : public JS::Cell {
|
||||
JS_CELL(Agent, JS::Cell);
|
||||
JS_DECLARE_ALLOCATOR(WorkerAgent);
|
||||
|
||||
WorkerAgent(AK::URL url, WorkerOptions const& options, JS::GCPtr<MessagePort> outside_port);
|
||||
|
||||
RefPtr<Web::HTML::WebWorkerClient> m_worker_ipc;
|
||||
|
||||
private:
|
||||
virtual void initialize(JS::Realm&) override;
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
@ -39,6 +33,8 @@ private:
|
|||
|
||||
JS::GCPtr<MessagePort> m_message_port;
|
||||
JS::GCPtr<MessagePort> m_outside_port;
|
||||
|
||||
RefPtr<Web::HTML::WebWorkerClient> m_worker_ipc;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include <LibWeb/HTML/SelectItem.h>
|
||||
#include <LibWeb/Loader/FileRequest.h>
|
||||
#include <LibWeb/PixelUnits.h>
|
||||
#include <LibWebView/SocketPair.h>
|
||||
|
||||
namespace Web {
|
||||
|
||||
|
@ -284,6 +285,8 @@ public:
|
|||
|
||||
virtual void page_did_insert_clipboard_entry([[maybe_unused]] String data, [[maybe_unused]] String presentation_style, [[maybe_unused]] String mime_type) { }
|
||||
|
||||
virtual WebView::SocketPair request_worker_agent() { return { -1, -1 }; }
|
||||
|
||||
virtual void inspector_did_load() { }
|
||||
virtual void inspector_did_select_dom_node([[maybe_unused]] i32 node_id, [[maybe_unused]] Optional<CSS::Selector::PseudoElement::Type> const& pseudo_element) { }
|
||||
virtual void inspector_did_set_dom_node_text([[maybe_unused]] i32 node_id, [[maybe_unused]] String const& text) { }
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibCore/System.h>
|
||||
#include <LibWeb/Worker/WebWorkerClient.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
@ -18,4 +19,12 @@ WebWorkerClient::WebWorkerClient(NonnullOwnPtr<Core::LocalSocket> socket)
|
|||
{
|
||||
}
|
||||
|
||||
WebView::SocketPair WebWorkerClient::dup_sockets()
|
||||
{
|
||||
WebView::SocketPair pair;
|
||||
pair.socket = MUST(Core::System::dup(socket().fd().value()));
|
||||
pair.fd_passing_socket = MUST(Core::System::dup(fd_passing_socket().fd().value()));
|
||||
return pair;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include <LibIPC/ConnectionToServer.h>
|
||||
#include <LibWeb/Worker/WebWorkerClientEndpoint.h>
|
||||
#include <LibWeb/Worker/WebWorkerServerEndpoint.h>
|
||||
#include <LibWebView/SocketPair.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
|
@ -21,6 +22,8 @@ class WebWorkerClient final
|
|||
public:
|
||||
explicit WebWorkerClient(NonnullOwnPtr<Core::LocalSocket>);
|
||||
|
||||
WebView::SocketPair dup_sockets();
|
||||
|
||||
private:
|
||||
virtual void die() override;
|
||||
};
|
||||
|
|
|
@ -8,6 +8,7 @@ set(SOURCES
|
|||
InspectorClient.cpp
|
||||
RequestServerAdapter.cpp
|
||||
SearchEngine.cpp
|
||||
SocketPair.cpp
|
||||
SourceHighlighter.cpp
|
||||
URL.cpp
|
||||
UserAgent.cpp
|
||||
|
|
|
@ -21,6 +21,7 @@ class WebContentClient;
|
|||
struct Attribute;
|
||||
struct CookieStorageKey;
|
||||
struct SearchEngine;
|
||||
struct SocketPair;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#include <LibGfx/Palette.h>
|
||||
#include <LibGfx/SystemTheme.h>
|
||||
#include <LibWeb/Crypto/Crypto.h>
|
||||
#include <LibWeb/Worker/WebWorkerClient.h>
|
||||
|
||||
REGISTER_WIDGET(WebView, OutOfProcessWebView)
|
||||
|
||||
|
@ -71,6 +72,11 @@ OutOfProcessWebView::OutOfProcessWebView()
|
|||
on_finish_handling_input_event = [this](auto event_was_accepted) {
|
||||
did_finish_handling_input_event(event_was_accepted);
|
||||
};
|
||||
|
||||
on_request_worker_agent = []() {
|
||||
auto worker_client = MUST(Web::HTML::WebWorkerClient::try_create());
|
||||
return worker_client->dup_sockets();
|
||||
};
|
||||
}
|
||||
|
||||
OutOfProcessWebView::~OutOfProcessWebView() = default;
|
||||
|
|
26
Userland/Libraries/LibWebView/SocketPair.cpp
Normal file
26
Userland/Libraries/LibWebView/SocketPair.cpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibIPC/Decoder.h>
|
||||
#include <LibIPC/Encoder.h>
|
||||
#include <LibWebView/SocketPair.h>
|
||||
|
||||
template<>
|
||||
ErrorOr<void> IPC::encode(Encoder& encoder, WebView::SocketPair const& pair)
|
||||
{
|
||||
TRY(encoder.encode(pair.socket));
|
||||
TRY(encoder.encode(pair.fd_passing_socket));
|
||||
return {};
|
||||
}
|
||||
|
||||
template<>
|
||||
ErrorOr<WebView::SocketPair> IPC::decode(Decoder& decoder)
|
||||
{
|
||||
auto socket = TRY(decoder.decode<IPC::File>());
|
||||
auto fd_passing_socket = TRY(decoder.decode<IPC::File>());
|
||||
|
||||
return WebView::SocketPair { move(socket), move(fd_passing_socket) };
|
||||
}
|
28
Userland/Libraries/LibWebView/SocketPair.h
Normal file
28
Userland/Libraries/LibWebView/SocketPair.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibIPC/File.h>
|
||||
|
||||
namespace WebView {
|
||||
|
||||
struct SocketPair {
|
||||
IPC::File socket;
|
||||
IPC::File fd_passing_socket;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace IPC {
|
||||
|
||||
template<>
|
||||
ErrorOr<void> encode(Encoder&, WebView::SocketPair const&);
|
||||
|
||||
template<>
|
||||
ErrorOr<WebView::SocketPair> decode(Decoder&);
|
||||
|
||||
}
|
|
@ -177,6 +177,7 @@ public:
|
|||
Function<void(i32, String const&, Vector<Attribute> const&)> on_inspector_replaced_dom_node_attribute;
|
||||
Function<void(i32, Gfx::IntPoint, String const&, Optional<String> const&, Optional<Attribute> const&)> on_inspector_requested_dom_tree_context_menu;
|
||||
Function<void(String const&)> on_inspector_executed_console_script;
|
||||
Function<SocketPair()> on_request_worker_agent;
|
||||
|
||||
virtual Web::DevicePixelRect viewport_rect() const = 0;
|
||||
virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const = 0;
|
||||
|
|
|
@ -478,4 +478,12 @@ void WebContentClient::inspector_did_execute_console_script(String const& script
|
|||
m_view.on_inspector_executed_console_script(script);
|
||||
}
|
||||
|
||||
Messages::WebContentClient::RequestWorkerAgentResponse WebContentClient::request_worker_agent()
|
||||
{
|
||||
if (m_view.on_request_worker_agent)
|
||||
return m_view.on_request_worker_agent();
|
||||
|
||||
return Messages::WebContentClient::RequestWorkerAgentResponse { WebView::SocketPair { -1, -1 } };
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -98,6 +98,7 @@ private:
|
|||
virtual void inspector_did_replace_dom_node_attribute(i32 node_id, String const& name, Vector<Attribute> const& replacement_attributes) override;
|
||||
virtual void inspector_did_request_dom_tree_context_menu(i32 node_id, Gfx::IntPoint position, String const& type, Optional<String> const& tag, Optional<Attribute> const& attribute) override;
|
||||
virtual void inspector_did_execute_console_script(String const& script) override;
|
||||
virtual Messages::WebContentClient::RequestWorkerAgentResponse request_worker_agent() override;
|
||||
|
||||
ViewImplementation& m_view;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue