Skip to content

Commit 4d039fc

Browse files
committed
LibWeb+WebWorker: Create SharedWorkerGlobalScope for Shared Workers
Also push the onconnect event for the initial connection. This still doesn't properly handle sending an onconnect event to a pre-existing SharedWorkerGlobalScope with the same name for the same origin, but it does give us a lot of WPT passes in the SharedWorker category.
1 parent 978a3b7 commit 4d039fc

File tree

11 files changed

+88
-39
lines changed

11 files changed

+88
-39
lines changed

Libraries/LibWeb/HTML/SharedWorkerGlobalScope.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ class SharedWorkerGlobalScope
2525
public:
2626
virtual ~SharedWorkerGlobalScope() override;
2727

28+
void set_constructor_origin(URL::Origin origin) { m_constructor_origin = move(origin); }
29+
URL::Origin const& constructor_origin() const { return m_constructor_origin; }
30+
31+
void set_constructor_url(URL::URL url) { m_constructor_url = move(url); }
32+
URL::URL const& constructor_url() const { return m_constructor_url; }
33+
34+
Fetch::Infrastructure::Request::CredentialsMode credentials() const { return m_credentials; }
35+
void set_credentials(Fetch::Infrastructure::Request::CredentialsMode credentials) { m_credentials = credentials; }
36+
2837
void close();
2938

3039
#define __ENUMERATE(attribute_name, event_name) \
@@ -38,6 +47,10 @@ class SharedWorkerGlobalScope
3847

3948
virtual void initialize_web_interfaces_impl() override;
4049
virtual void finalize() override;
50+
51+
URL::Origin m_constructor_origin;
52+
URL::URL m_constructor_url;
53+
Fetch::Infrastructure::Request::CredentialsMode m_credentials;
4154
};
4255

4356
HashTable<GC::RawRef<SharedWorkerGlobalScope>>& all_shared_worker_global_scopes();

Libraries/LibWeb/HTML/Worker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ WebIDL::ExceptionOr<GC::Ref<Worker>> Worker::create(String const& script_url, Wo
9393
void run_a_worker(Variant<GC::Ref<Worker>, GC::Ref<SharedWorker>> worker, URL::URL& url, EnvironmentSettingsObject& outside_settings, GC::Ptr<MessagePort> port, WorkerOptions const& options)
9494
{
9595
// 1. Let is shared be true if worker is a SharedWorker object, and false otherwise.
96-
// FIXME: SharedWorker support
96+
Bindings::AgentType agent_type = worker.has<GC::Ref<SharedWorker>>() ? Bindings::AgentType::SharedWorker : Bindings::AgentType::DedicatedWorker;
9797

9898
// 2. Let owner be the relevant owner to add given outside settings.
9999
// FIXME: Support WorkerGlobalScope options
@@ -111,7 +111,7 @@ void run_a_worker(Variant<GC::Ref<Worker>, GC::Ref<SharedWorker>> worker, URL::U
111111
// and is shared. Run the rest of these steps in that agent.
112112

113113
// Note: This spawns a new process to act as the 'agent' for the worker.
114-
auto agent = outside_settings.realm().create<WorkerAgentParent>(url, options, port, outside_settings);
114+
auto agent = outside_settings.realm().create<WorkerAgentParent>(url, options, port, outside_settings, agent_type);
115115
worker.visit([&](auto worker) { worker->set_agent(agent); });
116116
}
117117

Libraries/LibWeb/HTML/WorkerAgentParent.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ namespace Web::HTML {
1414

1515
GC_DEFINE_ALLOCATOR(WorkerAgentParent);
1616

17-
WorkerAgentParent::WorkerAgentParent(URL::URL url, WorkerOptions const& options, GC::Ptr<MessagePort> outside_port, GC::Ref<EnvironmentSettingsObject> outside_settings)
17+
WorkerAgentParent::WorkerAgentParent(URL::URL url, WorkerOptions const& options, GC::Ptr<MessagePort> outside_port, GC::Ref<EnvironmentSettingsObject> outside_settings, Bindings::AgentType agent_type)
1818
: m_worker_options(options)
19+
, m_agent_type(agent_type)
1920
, m_url(move(url))
2021
, m_outside_port(outside_port)
2122
, m_outside_settings(outside_settings)
@@ -34,7 +35,7 @@ void WorkerAgentParent::initialize(JS::Realm& realm)
3435

3536
// NOTE: This blocking IPC call may launch another process.
3637
// If spinning the event loop for this can cause other javascript to execute, we're in trouble.
37-
auto worker_socket_file = Bindings::principal_host_defined_page(realm).client().request_worker_agent(Bindings::AgentType::DedicatedWorker);
38+
auto worker_socket_file = Bindings::principal_host_defined_page(realm).client().request_worker_agent(m_agent_type);
3839

3940
auto worker_socket = MUST(Core::LocalSocket::adopt_fd(worker_socket_file.take_fd()));
4041
MUST(worker_socket->set_blocking(true));
@@ -44,7 +45,7 @@ void WorkerAgentParent::initialize(JS::Realm& realm)
4445

4546
m_worker_ipc = make_ref_counted<WebWorkerClient>(move(transport));
4647

47-
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());
48+
m_worker_ipc->async_start_worker(m_url, m_worker_options.type, m_worker_options.credentials, m_worker_options.name, move(data_holder), m_outside_settings->serialize(), m_agent_type);
4849
}
4950

5051
void WorkerAgentParent::visit_edges(Cell::Visitor& visitor)

Libraries/LibWeb/HTML/WorkerAgentParent.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#pragma once
88

9+
#include <LibWeb/Bindings/AgentType.h>
910
#include <LibWeb/Bindings/RequestPrototype.h>
1011
#include <LibWeb/Bindings/WorkerPrototype.h>
1112
#include <LibWeb/Forward.h>
@@ -25,12 +26,13 @@ class WorkerAgentParent : public JS::Cell {
2526
GC_DECLARE_ALLOCATOR(WorkerAgentParent);
2627

2728
protected:
28-
WorkerAgentParent(URL::URL url, WorkerOptions const& options, GC::Ptr<MessagePort> outside_port, GC::Ref<EnvironmentSettingsObject> outside_settings);
29+
WorkerAgentParent(URL::URL url, WorkerOptions const& options, GC::Ptr<MessagePort> outside_port, GC::Ref<EnvironmentSettingsObject> outside_settings, Bindings::AgentType);
2930
virtual void initialize(JS::Realm&) override;
3031
virtual void visit_edges(Cell::Visitor&) override;
3132

3233
private:
3334
WorkerOptions m_worker_options;
35+
Bindings::AgentType m_agent_type { Bindings::AgentType::DedicatedWorker };
3436
URL::URL m_url;
3537

3638
GC::Ptr<MessagePort> m_message_port;

Libraries/LibWeb/Worker/WebWorkerServer.ipc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#include <LibURL/URL.h>
22
#include <LibIPC/File.h>
3+
#include <LibWeb/Bindings/AgentType.h>
4+
#include <LibWeb/Bindings/WorkerPrototype.h>
35
#include <LibWeb/HTML/StructuredSerialize.h>
46
#include <LibWeb/HTML/Scripting/SerializedEnvironmentSettingsObject.h>
5-
#include <LibWeb/Bindings/WorkerPrototype.h>
67

78
endpoint WebWorkerServer {
89

9-
start_dedicated_worker(URL::URL url, Web::Bindings::WorkerType type, Web::Bindings::RequestCredentials credentials, String name, Web::HTML::TransferDataHolder message_port, Web::HTML::SerializedEnvironmentSettingsObject outside_settings) =|
10+
start_worker(URL::URL url,
11+
Web::Bindings::WorkerType type,
12+
Web::Bindings::RequestCredentials credentials,
13+
String name,
14+
Web::HTML::TransferDataHolder message_port,
15+
Web::HTML::SerializedEnvironmentSettingsObject outside_settings,
16+
Web::Bindings::AgentType agent_type) =|
1017

1118
close_worker() =|
1219

Services/WebWorker/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set(WEBWORKER_SOURCES
22
ConnectionFromClient.cpp
3-
DedicatedWorkerHost.cpp
43
PageHost.cpp
4+
WorkerHost.cpp
55
)
66

77
# FIXME: Add Android service

Services/WebWorker/ConnectionFromClient.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#include <LibCore/EventLoop.h>
88
#include <WebWorker/ConnectionFromClient.h>
9-
#include <WebWorker/DedicatedWorkerHost.h>
109
#include <WebWorker/PageHost.h>
10+
#include <WebWorker/WorkerHost.h>
1111

1212
namespace WebWorker {
1313

@@ -63,10 +63,16 @@ Web::Page const& ConnectionFromClient::page() const
6363
return m_page_host->page();
6464
}
6565

66-
void ConnectionFromClient::start_dedicated_worker(URL::URL url, Web::Bindings::WorkerType type, Web::Bindings::RequestCredentials, String name, Web::HTML::TransferDataHolder implicit_port, Web::HTML::SerializedEnvironmentSettingsObject outside_settings)
66+
void ConnectionFromClient::start_worker(URL::URL url, Web::Bindings::WorkerType type, Web::Bindings::RequestCredentials credentials, String name, Web::HTML::TransferDataHolder implicit_port, Web::HTML::SerializedEnvironmentSettingsObject outside_settings, Web::Bindings::AgentType agent_type)
6767
{
68-
m_worker_host = make_ref_counted<DedicatedWorkerHost>(move(url), type, move(name));
69-
m_worker_host->run(page(), move(implicit_port), outside_settings);
68+
m_worker_host = make_ref_counted<WorkerHost>(move(url), type, move(name));
69+
70+
bool const is_shared = agent_type == Web::Bindings::AgentType::SharedWorker;
71+
VERIFY(is_shared || agent_type == Web::Bindings::AgentType::DedicatedWorker);
72+
73+
// FIXME: Add an assertion that the agent_type passed here is the same that was passed at process creation to initialize_main_thread_vm()
74+
75+
m_worker_host->run(page(), move(implicit_port), outside_settings, credentials, is_shared);
7076
}
7177

7278
void ConnectionFromClient::handle_file_return(i32 error, Optional<IPC::File> file, i32 request_id)

Services/WebWorker/ConnectionFromClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ConnectionFromClient final
4141
Web::Page& page();
4242
Web::Page const& page() const;
4343

44-
virtual void start_dedicated_worker(URL::URL url, Web::Bindings::WorkerType type, Web::Bindings::RequestCredentials credentials, String name, Web::HTML::TransferDataHolder, Web::HTML::SerializedEnvironmentSettingsObject) override;
44+
virtual void start_worker(URL::URL url, Web::Bindings::WorkerType type, Web::Bindings::RequestCredentials credentials, String name, Web::HTML::TransferDataHolder, Web::HTML::SerializedEnvironmentSettingsObject, Web::Bindings::AgentType) override;
4545
virtual void handle_file_return(i32 error, Optional<IPC::File> file, i32 request_id) override;
4646

4747
GC::Root<PageHost> m_page_host;
@@ -51,7 +51,7 @@ class ConnectionFromClient final
5151
HashMap<int, Web::FileRequest> m_requested_files {};
5252
int last_id { 0 };
5353

54-
RefPtr<DedicatedWorkerHost> m_worker_host;
54+
RefPtr<WorkerHost> m_worker_host;
5555
};
5656

5757
}

Services/WebWorker/Forward.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace WebWorker {
1010

1111
class ConnectionFromClient;
12-
class DedicatedWorkerHost;
1312
class PageHost;
13+
class WorkerHost;
1414

1515
}

Services/WebWorker/DedicatedWorkerHost.cpp renamed to Services/WebWorker/WorkerHost.cpp

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,49 @@
66

77
#include <LibIPC/File.h>
88
#include <LibJS/Runtime/ConsoleObject.h>
9+
#include <LibWeb/Fetch/Enums.h>
910
#include <LibWeb/Fetch/Fetching/Fetching.h>
1011
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
1112
#include <LibWeb/HTML/DedicatedWorkerGlobalScope.h>
13+
#include <LibWeb/HTML/MessageEvent.h>
1214
#include <LibWeb/HTML/MessagePort.h>
1315
#include <LibWeb/HTML/Scripting/ClassicScript.h>
1416
#include <LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h>
1517
#include <LibWeb/HTML/Scripting/Fetching.h>
18+
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
1619
#include <LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h>
20+
#include <LibWeb/HTML/SharedWorkerGlobalScope.h>
1721
#include <LibWeb/HTML/WorkerDebugConsoleClient.h>
1822
#include <LibWeb/HTML/WorkerGlobalScope.h>
1923
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
2024
#include <LibWeb/Loader/ResourceLoader.h>
21-
#include <WebWorker/DedicatedWorkerHost.h>
25+
#include <WebWorker/WorkerHost.h>
2226

2327
namespace WebWorker {
2428

25-
DedicatedWorkerHost::DedicatedWorkerHost(URL::URL url, Web::Bindings::WorkerType type, String name)
29+
WorkerHost::WorkerHost(URL::URL url, Web::Bindings::WorkerType type, String name)
2630
: m_url(move(url))
2731
, m_type(type)
2832
, m_name(move(name))
2933
{
3034
}
3135

32-
DedicatedWorkerHost::~DedicatedWorkerHost() = default;
36+
WorkerHost::~WorkerHost() = default;
3337

3438
// https://html.spec.whatwg.org/multipage/workers.html#run-a-worker
35-
// FIXME: Extract out into a helper for both shared and dedicated workers
36-
void DedicatedWorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataHolder message_port_data, Web::HTML::SerializedEnvironmentSettingsObject const& outside_settings_snapshot)
39+
void WorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataHolder message_port_data, Web::HTML::SerializedEnvironmentSettingsObject const& outside_settings_snapshot, Web::Bindings::RequestCredentials credentials, bool is_shared)
3740
{
38-
bool const is_shared = false;
39-
4041
// 3. Let unsafeWorkerCreationTime be the unsafe shared current time.
4142
auto unsafe_worker_creation_time = Web::HighResolutionTime::unsafe_shared_current_time();
4243

4344
// 7. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
4445
auto realm_execution_context = Web::Bindings::create_a_new_javascript_realm(
4546
Web::Bindings::main_thread_vm(),
46-
[page](JS::Realm& realm) -> JS::Object* {
47+
[page, is_shared](JS::Realm& realm) -> JS::Object* {
4748
// 7a. For the global object, if is shared is true, create a new SharedWorkerGlobalScope object.
4849
// 7b. Otherwise, create a new DedicatedWorkerGlobalScope object.
49-
// FIXME: Proper support for both SharedWorkerGlobalScope and DedicatedWorkerGlobalScope
5050
if (is_shared)
51-
TODO();
51+
return Web::Bindings::main_thread_vm().heap().allocate<Web::HTML::SharedWorkerGlobalScope>(realm, page);
5252
return Web::Bindings::main_thread_vm().heap().allocate<Web::HTML::DedicatedWorkerGlobalScope>(realm, page);
5353
},
5454
nullptr);
@@ -67,10 +67,7 @@ void DedicatedWorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataHo
6767
console_object.console().set_client(*m_console);
6868

6969
// 10. Set worker global scope's name to the value of options's name member.
70-
if (is_shared)
71-
TODO();
72-
else
73-
static_cast<Web::HTML::DedicatedWorkerGlobalScope&>(*worker_global_scope).set_name(m_name);
70+
worker_global_scope->set_name(m_name);
7471

7572
// 11. Append owner to worker global scope's owner set.
7673
// FIXME: support for 'owner' set on WorkerGlobalScope
@@ -80,19 +77,26 @@ void DedicatedWorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataHo
8077

8178
// 12. If is shared is true, then:
8279
if (is_shared) {
83-
// FIXME: Shared worker support
80+
auto& shared_global_scope = static_cast<Web::HTML::SharedWorkerGlobalScope&>(*worker_global_scope);
8481
// 1. Set worker global scope's constructor origin to outside settings's origin.
82+
shared_global_scope.set_constructor_origin(outside_settings->origin());
83+
8584
// 2. Set worker global scope's constructor url to url.
85+
shared_global_scope.set_constructor_url(m_url);
86+
8687
// 3. Set worker global scope's type to the value of options's type member.
88+
shared_global_scope.set_type(m_type);
89+
8790
// 4. Set worker global scope's credentials to the value of options's credentials member.
91+
shared_global_scope.set_credentials(Web::Fetch::from_bindings_enum(credentials));
8892
}
8993

9094
// 13. Let destination be "sharedworker" if is shared is true, and "worker" otherwise.
9195
auto destination = is_shared ? Web::Fetch::Infrastructure::Request::Destination::SharedWorker
9296
: Web::Fetch::Infrastructure::Request::Destination::Worker;
9397

9498
// In both cases, let performFetch be the following perform the fetch hook given request, isTopLevel and processCustomFetchResponse:
95-
auto perform_fetch_function = [inside_settings, worker_global_scope](GC::Ref<Web::Fetch::Infrastructure::Request> request, Web::HTML::TopLevelModule is_top_level, Web::Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_custom_fetch_response) -> Web::WebIDL::ExceptionOr<void> {
99+
auto perform_fetch_function = [inside_settings, worker_global_scope, is_shared](GC::Ref<Web::Fetch::Infrastructure::Request> request, Web::HTML::TopLevelModule is_top_level, Web::Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_custom_fetch_response) -> Web::WebIDL::ExceptionOr<void> {
96100
auto& realm = inside_settings->realm();
97101
auto& vm = realm.vm();
98102

@@ -112,7 +116,7 @@ void DedicatedWorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataHo
112116
auto process_custom_fetch_response_function = GC::create_function(vm.heap(), move(process_custom_fetch_response));
113117

114118
// 3. Fetch request with processResponseConsumeBody set to the following steps given response response and null, failure, or a byte sequence bodyBytes:
115-
fetch_algorithms_input.process_response_consume_body = [worker_global_scope, process_custom_fetch_response_function, inside_settings](auto response, auto body_bytes) {
119+
fetch_algorithms_input.process_response_consume_body = [worker_global_scope, process_custom_fetch_response_function, inside_settings, is_shared](auto response, auto body_bytes) {
116120
auto& vm = inside_settings->vm();
117121

118122
// 1. Set worker global scope's url to response's url.
@@ -127,6 +131,7 @@ void DedicatedWorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataHo
127131
response = Web::Fetch::Infrastructure::Response::network_error(vm, "Blocked by Content Security Policy"_string);
128132
}
129133

134+
// FIXME: Use worker global scope's policy container's embedder policy
130135
// FIXME: 4. If worker global scope's embedder policy's value is compatible with cross-origin isolation and is shared is true,
131136
// then set agent's agent cluster's cross-origin isolation mode to "logical" or "concrete".
132137
// The one chosen is implementation-defined.
@@ -150,7 +155,7 @@ void DedicatedWorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataHo
150155
};
151156
auto perform_fetch = Web::HTML::create_perform_the_fetch_hook(inside_settings->heap(), move(perform_fetch_function));
152157

153-
auto on_complete_function = [inside_settings, worker_global_scope, message_port_data = move(message_port_data), url = m_url](GC::Ptr<Web::HTML::Script> script) mutable {
158+
auto on_complete_function = [inside_settings, worker_global_scope, message_port_data = move(message_port_data), url = m_url, is_shared](GC::Ptr<Web::HTML::Script> script) mutable {
154159
auto& realm = inside_settings->realm();
155160
// 1. If script is null or if script's error to rethrow is non-null, then:
156161
if (!script || !script->error_to_rethrow().is_null()) {
@@ -206,10 +211,25 @@ void DedicatedWorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataHo
206211
inside_port->start();
207212
}
208213

209-
// FIXME: 13. If is shared is true, then queue a global task on DOM manipulation task source given worker
214+
// 13. If is shared is true, then queue a global task on DOM manipulation task source given worker
210215
// global scope to fire an event named connect at worker global scope, using MessageEvent,
211216
// with the data attribute initialized to the empty string, the ports attribute initialized
212217
// to a new frozen array containing inside port, and the source attribute initialized to inside port.
218+
if (is_shared) {
219+
Web::HTML::queue_global_task(Web::HTML::Task::Source::DOMManipulation, *worker_global_scope, GC::create_function(realm.heap(), [worker_global_scope, inside_port] {
220+
auto& realm = worker_global_scope->realm();
221+
auto& vm = realm.vm();
222+
Web::HTML::TemporaryExecutionContext const context(realm);
223+
224+
Web::HTML::MessageEventInit event_init {};
225+
event_init.data = GC::Ref { vm.empty_string() };
226+
event_init.ports = { inside_port };
227+
event_init.source = inside_port;
228+
229+
auto message_event = Web::HTML::MessageEvent::create(realm, Web::HTML::EventNames::connect, event_init);
230+
worker_global_scope->dispatch_event(message_event);
231+
}));
232+
}
213233

214234
// FIXME: 14. Enable the client message queue of the ServiceWorkerContainer object whose associated service
215235
// worker client is worker global scope's relevant settings object.

Services/WebWorker/DedicatedWorkerHost.h renamed to Services/WebWorker/WorkerHost.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
namespace WebWorker {
1818

19-
class DedicatedWorkerHost : public RefCounted<DedicatedWorkerHost> {
19+
class WorkerHost : public RefCounted<WorkerHost> {
2020
public:
21-
explicit DedicatedWorkerHost(URL::URL url, Web::Bindings::WorkerType type, String name);
22-
~DedicatedWorkerHost();
21+
explicit WorkerHost(URL::URL url, Web::Bindings::WorkerType type, String name);
22+
~WorkerHost();
2323

24-
void run(GC::Ref<Web::Page>, Web::HTML::TransferDataHolder message_port_data, Web::HTML::SerializedEnvironmentSettingsObject const&);
24+
void run(GC::Ref<Web::Page>, Web::HTML::TransferDataHolder message_port_data, Web::HTML::SerializedEnvironmentSettingsObject const&, Web::Bindings::RequestCredentials, bool is_shared);
2525

2626
private:
2727
GC::Root<Web::HTML::WorkerDebugConsoleClient> m_console;

0 commit comments

Comments
 (0)