1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 15:07:45 +00:00

LibWeb+WebWorker: Implement a first cut of post_message for Workers

This implementation completely ignores MessagePorts, and manually plumbs
data through LocalSockets.
This commit is contained in:
Andrew Kaster 2023-11-22 09:57:22 -07:00 committed by Andreas Kling
parent 05ec93e276
commit 1602663b9e
15 changed files with 225 additions and 29 deletions

View file

@ -52,9 +52,9 @@ Web::Page const& ConnectionFromClient::page() const
return m_page_host->page();
}
void ConnectionFromClient::start_dedicated_worker(AK::URL const& url, String const& type, String const&, String const&, IPC::File const&)
void ConnectionFromClient::start_dedicated_worker(AK::URL const& url, String const& type, String const&, String const&, IPC::File const& implicit_port)
{
m_worker_host = make_ref_counted<DedicatedWorkerHost>(page(), url, type);
m_worker_host = make_ref_counted<DedicatedWorkerHost>(page(), url, type, implicit_port.take_fd());
m_worker_host->run();
}

View file

@ -17,14 +17,18 @@
namespace WebWorker {
DedicatedWorkerHost::DedicatedWorkerHost(Web::Page& page, AK::URL url, String type)
DedicatedWorkerHost::DedicatedWorkerHost(Web::Page& page, AK::URL url, String type, int outside_port)
: m_page(page)
, m_url(move(url))
, m_type(move(type))
, m_outside_port(outside_port)
{
}
DedicatedWorkerHost::~DedicatedWorkerHost() = default;
DedicatedWorkerHost::~DedicatedWorkerHost()
{
::close(m_outside_port);
}
// https://html.spec.whatwg.org/multipage/workers.html#run-a-worker
// FIXME: Extract out into a helper for both shared and dedicated workers
@ -128,7 +132,7 @@ void DedicatedWorkerHost::run()
};
auto perform_fetch = Web::HTML::create_perform_the_fetch_hook(inner_settings->heap(), move(perform_fetch_function));
auto on_complete_function = [inner_settings, worker_global_scope](JS::GCPtr<Web::HTML::Script> script) {
auto on_complete_function = [inner_settings, worker_global_scope, outside_port = m_outside_port](JS::GCPtr<Web::HTML::Script> script) {
auto& realm = inner_settings->realm();
// 1. If script is null or if script's error to rethrow is non-null, then:
if (!script || !script->error_to_rethrow().is_null()) {
@ -147,6 +151,8 @@ void DedicatedWorkerHost::run()
// FIXME: 3. Let inside port be a new MessagePort object in inside settings's Realm.
// FIXME: 4. Associate inside port with worker global scope.
// FIXME: 5. Entangle outside port and inside port.
// This is a hack, move to a real MessagePort object per above FIXMEs.
worker_global_scope->set_outside_port(MUST(Core::BufferedLocalSocket::create(MUST(Core::LocalSocket::adopt_fd(outside_port)))));
// 6. Create a new WorkerLocation object and associate it with worker global scope.
worker_global_scope->set_location(realm.heap().allocate<Web::HTML::WorkerLocation>(realm, *worker_global_scope));

View file

@ -15,7 +15,7 @@ namespace WebWorker {
class DedicatedWorkerHost : public RefCounted<DedicatedWorkerHost> {
public:
explicit DedicatedWorkerHost(Web::Page&, AK::URL url, String type);
explicit DedicatedWorkerHost(Web::Page&, AK::URL url, String type, int outside_port);
~DedicatedWorkerHost();
void run();
@ -26,6 +26,8 @@ private:
AK::URL m_url;
String m_type;
int m_outside_port { -1 };
};
}