/src/serenity/Userland/Libraries/LibWeb/HTML/WorkerAgent.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibWeb/Bindings/HostDefined.h> |
8 | | #include <LibWeb/HTML/MessagePort.h> |
9 | | #include <LibWeb/HTML/WorkerAgent.h> |
10 | | #include <LibWeb/Page/Page.h> |
11 | | #include <LibWeb/Worker/WebWorkerClient.h> |
12 | | |
13 | | namespace Web::HTML { |
14 | | |
15 | | JS_DEFINE_ALLOCATOR(WorkerAgent); |
16 | | |
17 | | WorkerAgent::WorkerAgent(URL::URL url, WorkerOptions const& options, JS::GCPtr<MessagePort> outside_port, JS::NonnullGCPtr<EnvironmentSettingsObject> outside_settings) |
18 | 0 | : m_worker_options(options) |
19 | 0 | , m_url(move(url)) |
20 | 0 | , m_outside_port(outside_port) |
21 | 0 | , m_outside_settings(outside_settings) |
22 | 0 | { |
23 | 0 | } |
24 | | |
25 | | void WorkerAgent::initialize(JS::Realm& realm) |
26 | 0 | { |
27 | 0 | Base::initialize(realm); |
28 | |
|
29 | 0 | m_message_port = MessagePort::create(realm); |
30 | 0 | m_message_port->entangle_with(*m_outside_port); |
31 | |
|
32 | 0 | TransferDataHolder data_holder; |
33 | 0 | MUST(m_message_port->transfer_steps(data_holder)); |
34 | | |
35 | | // NOTE: This blocking IPC call may launch another process. |
36 | | // If spinning the event loop for this can cause other javascript to execute, we're in trouble. |
37 | 0 | auto worker_socket_file = Bindings::host_defined_page(realm).client().request_worker_agent(); |
38 | 0 | auto worker_socket = MUST(Core::LocalSocket::adopt_fd(worker_socket_file.take_fd())); |
39 | 0 | MUST(worker_socket->set_blocking(true)); |
40 | |
|
41 | 0 | m_worker_ipc = make_ref_counted<WebWorkerClient>(move(worker_socket)); |
42 | |
|
43 | 0 | m_worker_ipc->async_start_dedicated_worker(m_url, m_worker_options.type, m_worker_options.credentials, m_worker_options.name, move(data_holder), m_outside_settings->serialize()); |
44 | 0 | } |
45 | | |
46 | | void WorkerAgent::visit_edges(Cell::Visitor& visitor) |
47 | 0 | { |
48 | 0 | Base::visit_edges(visitor); |
49 | 0 | visitor.visit(m_message_port); |
50 | 0 | visitor.visit(m_outside_port); |
51 | 0 | visitor.visit(m_outside_settings); |
52 | 0 | } |
53 | | |
54 | | } |