Skip to content

Truly independent web browser

LibWeb+LibWebView+WebContent: Tell a dedicated worker's owner directly

Aliaksandr Kalenik2a1e8822a4b2
diff --git a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
index 9d2f66ece21..de1091428f4 100644
--- a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
+++ b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
@@ -138,6 +138,17 @@ void WorkerAgentParent::start()
m_agent_id = started_agent.agent_id;
m_dedicated_worker_thread_link = move(started_agent.dedicated_worker_thread_link);
+ if (m_agent_id == 0) {
+ dispatch_error_event();
+ forget_agent();
+ return;
+ }
+ if (m_agent_type == AgentType::DedicatedWorker && !m_dedicated_worker_thread_link) {
+ dispatch_error_event();
+ terminate();
+ return;
+ }
+
// NB: This is before any script can use the port, so that it finds out right now whether its peer is in this process.
if (!started_agent.is_hosted_in_this_process)
give_up_on_parked_end();
diff --git a/Libraries/LibWebView/WebWorkerClient.cpp b/Libraries/LibWebView/WebWorkerClient.cpp
index a2d62f45a0a..149baf40392 100644
--- a/Libraries/LibWebView/WebWorkerClient.cpp
+++ b/Libraries/LibWebView/WebWorkerClient.cpp
@@ -92,11 +92,6 @@ void WebWorkerClient::did_fail_loading_worker_script()
WorkerAgentManager::the().worker_did_fail_loading_script(m_agent_id);
}
-void WebWorkerClient::did_report_worker_exception(Utf16String message, Utf16String filename, u32 lineno, u32 colno)
-{
- WorkerAgentManager::the().worker_did_report_exception(m_agent_id, move(message), move(filename), lineno, colno);
-}
-
void WebWorkerClient::did_report_outside_settings_content_security_policy_violation(u64 policy_index, String effective_directive, URL::URL resource)
{
WorkerAgentManager::the().worker_did_report_outside_settings_content_security_policy_violation(m_agent_id, policy_index, move(effective_directive), move(resource));
diff --git a/Libraries/LibWebView/WebWorkerClient.h b/Libraries/LibWebView/WebWorkerClient.h
index c6bf0869d94..379f6a677d8 100644
--- a/Libraries/LibWebView/WebWorkerClient.h
+++ b/Libraries/LibWebView/WebWorkerClient.h
@@ -65,7 +65,6 @@ public:
virtual void did_close_shared_worker() override;
virtual void did_finish_loading_worker_script(bool worker_is_secure_context) override;
virtual void did_fail_loading_worker_script() override;
- virtual void did_report_worker_exception(Utf16String message, Utf16String filename, u32 lineno, u32 colno) override;
virtual void did_report_outside_settings_content_security_policy_violation(u64 policy_index, String effective_directive, URL::URL resource) override;
virtual Messages::WebWorkerClient::DidRequestCookieResponse did_request_cookie(URL::URL, HTTP::Cookie::Source) override;
virtual Messages::WebWorkerClient::DidAddBlobUrlEntryResponse did_add_blob_url_entry(Web::HTML::EnvironmentId environment_id, Utf16String url, Web::FileAPI::SerializedBlobURLEntry entry) override;
diff --git a/Libraries/LibWebView/WorkerAgentManager.cpp b/Libraries/LibWebView/WorkerAgentManager.cpp
index 4941415cc07..d9be69e3e30 100644
--- a/Libraries/LibWebView/WorkerAgentManager.cpp
+++ b/Libraries/LibWebView/WorkerAgentManager.cpp
@@ -65,14 +65,12 @@ WorkerAgentManager::StartedWorkerAgent WorkerAgentManager::start_worker_agent(Ow
// FIXME: The worker takes the rest of the outside settings, such as their top-level origin, policy container and
// cross-origin isolated capability, as the owner's process gave them.
if (!outside_settings.has_value()) {
- notify_worker_script_load_failure(owner);
return {};
}
request.outside_settings.origin = outside_settings->origin();
// Only dedicated and shared workers are started on an owner's request.
if (request.agent_type != Web::HTML::AgentType::DedicatedWorker && request.agent_type != Web::HTML::AgentType::SharedWorker) {
- notify_worker_script_load_failure(owner);
return {};
}
@@ -125,14 +123,12 @@ WorkerAgentManager::StartedWorkerAgent WorkerAgentManager::start_worker_agent(Ow
// 11.5.3. If workerIsSecureContext is not callerIsSecureContext, queue a global task
// on the DOM manipulation task source given worker's relevant global object
// to fire an event named error at worker and abort these steps.
- // AD-HOC: Error firing is routed back over IPC via notify_worker_script_load_failure;
- // the spec's queue-global-task happens inside WorkerAgentParent in the
- // requesting process.
+ // AD-HOC: The owner fires the error itself when it gets no agent back; the spec's
+ // queue-global-task happens inside WorkerAgentParent in the requesting process.
if (agent.worker_type != request.type
|| agent.credentials != request.credentials
|| agent.extended_lifetime != request.extended_lifetime
|| (agent.worker_is_secure_context.has_value() && *agent.worker_is_secure_context != request.caller_is_secure_context)) {
- notify_worker_script_load_failure(owner);
return {};
}
@@ -164,7 +160,6 @@ WorkerAgentManager::StartedWorkerAgent WorkerAgentManager::start_worker_agent(Ow
// whose creator's browsing context has been discarded, leaving it no top-level origin, does not start.
auto top_level_origin = outside_settings->top_level_origin();
if (!top_level_origin.has_value()) {
- notify_worker_script_load_failure(owner);
return {};
}
size_t worker_count_of_top_level_site = 0;
@@ -174,7 +169,6 @@ WorkerAgentManager::StartedWorkerAgent WorkerAgentManager::start_worker_agent(Ow
++worker_count_of_top_level_site;
}
if (worker_count_of_top_level_site >= maximum_worker_count_per_top_level_site) {
- notify_worker_script_load_failure(owner);
return {};
}
@@ -198,7 +192,6 @@ WorkerAgentManager::StartedWorkerAgent WorkerAgentManager::start_worker_agent(Ow
}
// NB: Nothing would close a process started for a dedicated worker, as only shared workers keep a process alive.
if (request.agent_type == Web::HTML::AgentType::DedicatedWorker && !host) {
- notify_worker_script_load_failure(owner);
return {};
}
bool const host_has_only_this_worker = !host;
@@ -250,7 +243,6 @@ WorkerAgentManager::StartedWorkerAgent WorkerAgentManager::start_worker_agent(Ow
dbgln("Unable to start a worker: {}", client_or_error.error());
if (host_has_only_this_worker && host)
host->close_server_if_unused();
- notify_worker_script_load_failure(owner);
return {};
}
auto client = client_or_error.release_value();
@@ -578,62 +570,33 @@ ErrorOr<void> WorkerAgentManager::simulate_request_server_connection_loss_for_te
void WorkerAgentManager::notify_worker_script_load_failure(Owner const& owner)
{
- owner.client.visit(
- [&](WebContentOwner const& web_content_owner) {
- if (web_content_owner.client)
- web_content_owner.client->async_did_worker_agent_fail_loading_script(owner.token);
- },
- [&](WebWorkerOwner const& web_worker_owner) {
- web_worker_owner.client->async_did_worker_agent_fail_loading_script(owner.token);
- });
-}
-
-void WorkerAgentManager::notify_worker_exception(Owner const& owner, Utf16String const& message, Utf16String const& filename, u32 lineno, u32 colno)
-{
- owner.client.visit(
- [&](WebContentOwner const& web_content_owner) {
- if (web_content_owner.client)
- web_content_owner.client->async_did_worker_agent_report_exception(owner.token, message, filename, lineno, colno);
- },
- [&](WebWorkerOwner const& web_worker_owner) {
- web_worker_owner.client->async_did_worker_agent_report_exception(owner.token, message, filename, lineno, colno);
- });
+ if (auto const* web_content_owner = owner.client.get_pointer<WebContentOwner>(); web_content_owner && web_content_owner->client)
+ web_content_owner->client->async_did_worker_agent_fail_loading_script(owner.token);
}
void WorkerAgentManager::notify_worker_content_security_policy_violation(Owner const& owner, u64 policy_index, String const& effective_directive, URL::URL const& resource)
{
- owner.client.visit(
- [&](WebContentOwner const& web_content_owner) {
- if (web_content_owner.client)
- web_content_owner.client->async_did_worker_agent_report_content_security_policy_violation(owner.token, policy_index, effective_directive, resource);
- },
- [&](WebWorkerOwner const& web_worker_owner) {
- web_worker_owner.client->async_did_worker_agent_report_content_security_policy_violation(owner.token, policy_index, effective_directive, resource);
- });
+ if (auto const* web_content_owner = owner.client.get_pointer<WebContentOwner>(); web_content_owner && web_content_owner->client)
+ web_content_owner->client->async_did_worker_agent_report_content_security_policy_violation(owner.token, policy_index, effective_directive, resource);
}
void WorkerAgentManager::notify_worker_close(Owner const& owner)
{
- owner.client.visit(
- [&](WebContentOwner const& web_content_owner) {
- if (web_content_owner.client)
- web_content_owner.client->async_did_worker_agent_close(owner.token);
- },
- [&](WebWorkerOwner const& web_worker_owner) {
- web_worker_owner.client->async_did_worker_agent_close(owner.token);
- });
+ if (auto const* web_content_owner = owner.client.get_pointer<WebContentOwner>(); web_content_owner && web_content_owner->client)
+ web_content_owner->client->async_did_worker_agent_close(owner.token);
}
void WorkerAgentManager::notify_worker_death(Owner const& owner)
{
- owner.client.visit(
- [&](WebContentOwner const& web_content_owner) {
- if (web_content_owner.client)
- web_content_owner.client->async_did_worker_agent_die(owner.token);
- },
- [&](WebWorkerOwner const& web_worker_owner) {
- web_worker_owner.client->async_did_worker_agent_die(owner.token);
- });
+ if (auto const* web_content_owner = owner.client.get_pointer<WebContentOwner>(); web_content_owner && web_content_owner->client)
+ web_content_owner->client->async_did_worker_agent_die(owner.token);
+}
+
+Vector<WorkerAgentManager::Owner> WorkerAgentManager::owners_told_by_the_browser(WorkerAgent const& agent)
+{
+ if (agent.agent_type == Web::HTML::AgentType::DedicatedWorker)
+ return {};
+ return agent.owners;
}
void WorkerAgentManager::worker_did_finish_loading_script(Web::HTML::WorkerAgentId agent_id, bool worker_is_secure_context)
@@ -659,7 +622,7 @@ void WorkerAgentManager::worker_did_fail_loading_script(Web::HTML::WorkerAgentId
agent.closing = true;
agent.outside_settings_owner = {};
- auto owners = agent.owners;
+ auto owners = owners_told_by_the_browser(agent);
for (auto const& owner : owners)
notify_worker_script_load_failure(owner);
@@ -668,16 +631,6 @@ void WorkerAgentManager::worker_did_fail_loading_script(Web::HTML::WorkerAgentId
});
}
-void WorkerAgentManager::worker_did_report_exception(Web::HTML::WorkerAgentId agent_id, Utf16String message, Utf16String filename, u32 lineno, u32 colno)
-{
- auto maybe_agent = m_agents.find(agent_id);
- if (maybe_agent == m_agents.end())
- return;
-
- for (auto const& owner : maybe_agent->value.owners)
- notify_worker_exception(owner, message, filename, lineno, colno);
-}
-
void WorkerAgentManager::worker_did_report_outside_settings_content_security_policy_violation(Web::HTML::WorkerAgentId agent_id, u64 policy_index, String effective_directive, URL::URL resource)
{
auto maybe_agent = m_agents.find(agent_id);
@@ -703,7 +656,7 @@ void WorkerAgentManager::worker_did_close(Web::HTML::WorkerAgentId agent_id)
return;
agent.closing = true;
- auto owners = agent.owners;
+ auto owners = owners_told_by_the_browser(agent);
for (auto const& owner : owners)
notify_worker_close(owner);
@@ -737,7 +690,7 @@ void WorkerAgentManager::worker_did_die(Web::HTML::WorkerAgentId agent_id)
}
agent.closing = true;
- auto owners = agent.owners;
+ auto owners = owners_told_by_the_browser(agent);
for (auto const& owner : owners)
notify_worker_death(owner);
diff --git a/Libraries/LibWebView/WorkerAgentManager.h b/Libraries/LibWebView/WorkerAgentManager.h
index f49f5e0e5a6..227cf14f2c1 100644
--- a/Libraries/LibWebView/WorkerAgentManager.h
+++ b/Libraries/LibWebView/WorkerAgentManager.h
@@ -126,14 +126,12 @@ private:
static bool owner_process_may_host_shared_worker(WebContentOwner const&, CanonicalEnvironmentSettingsObject const& outside_settings);
void notify_worker_script_load_failure(Owner const&);
- void notify_worker_exception(Owner const&, Utf16String const& message, Utf16String const& filename, u32 lineno, u32 colno);
void notify_worker_content_security_policy_violation(Owner const&, u64 policy_index, String const& effective_directive, URL::URL const& resource);
void notify_worker_close(Owner const&);
void notify_worker_death(Owner const&);
void worker_did_finish_loading_script(Web::HTML::WorkerAgentId, bool worker_is_secure_context);
void worker_did_fail_loading_script(Web::HTML::WorkerAgentId);
- void worker_did_report_exception(Web::HTML::WorkerAgentId, Utf16String message, Utf16String filename, u32 lineno, u32 colno);
void worker_did_report_outside_settings_content_security_policy_violation(Web::HTML::WorkerAgentId, u64 policy_index, String effective_directive, URL::URL resource);
void worker_did_close(Web::HTML::WorkerAgentId);
void shared_worker_did_close(Web::HTML::WorkerAgentId);
@@ -166,6 +164,7 @@ private:
};
static bool is_same_owner(Owner const&, Owner const& identity);
+ static Vector<Owner> owners_told_by_the_browser(WorkerAgent const&);
static void forget_removed_outside_settings_owner(WorkerAgent&);
void for_each_client_with_inside_settings(Function<IterationDecision(WebWorkerClient&, CanonicalWorkerEnvironmentSettingsObject const&)> const&);
diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index 2250f44ff1a..d569416a75a 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -3849,11 +3849,6 @@ void ConnectionFromClient::did_worker_agent_fail_loading_script(Web::HTML::Worke
Web::HTML::WorkerAgentParent::did_fail_loading_worker_script(owner_token);
}
-void ConnectionFromClient::did_worker_agent_report_exception(Web::HTML::WorkerAgentOwnerToken owner_token, Utf16String message, Utf16String filename, u32 lineno, u32 colno)
-{
- Web::HTML::WorkerAgentParent::did_report_worker_exception(owner_token, move(message), move(filename), lineno, colno);
-}
-
void ConnectionFromClient::did_worker_agent_report_content_security_policy_violation(Web::HTML::WorkerAgentOwnerToken owner_token, u64 policy_index, String effective_directive, URL::URL resource)
{
Web::HTML::WorkerAgentParent::did_report_content_security_policy_violation(owner_token, policy_index, move(effective_directive), move(resource));
diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.h
index f6b24ba8be9..a5e2d567c75 100644
--- a/Services/WebContent/ConnectionFromClient.h
+++ b/Services/WebContent/ConnectionFromClient.h
@@ -359,7 +359,6 @@ private:
virtual void cookies_changed(Web::PageId page_id, Vector<HTTP::Cookie::Cookie>) override;
virtual void broadcast_channel_message(Web::HTML::BroadcastChannelMessage message) override;
virtual void did_worker_agent_fail_loading_script(Web::HTML::WorkerAgentOwnerToken owner_token) override;
- virtual void did_worker_agent_report_exception(Web::HTML::WorkerAgentOwnerToken owner_token, Utf16String message, Utf16String filename, u32 lineno, u32 colno) override;
virtual void did_worker_agent_report_content_security_policy_violation(Web::HTML::WorkerAgentOwnerToken owner_token, u64 policy_index, String effective_directive, URL::URL resource) override;
virtual void did_worker_agent_close(Web::HTML::WorkerAgentOwnerToken owner_token) override;
virtual void did_worker_agent_die(Web::HTML::WorkerAgentOwnerToken owner_token) override;
diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipc
index ecb59bd987f..531dc216b73 100644
--- a/Services/WebContent/WebContentServer.ipc
+++ b/Services/WebContent/WebContentServer.ipc
@@ -336,7 +336,6 @@ endpoint WebContentServer routed by Web::PageId page_id
broadcast_channel_message(Web::HTML::BroadcastChannelMessage message) =|
did_worker_agent_fail_loading_script(Web::HTML::WorkerAgentOwnerToken owner_token) =|
- did_worker_agent_report_exception(Web::HTML::WorkerAgentOwnerToken owner_token, Utf16String message, Utf16String filename, u32 lineno, u32 colno) =|
did_worker_agent_report_content_security_policy_violation(Web::HTML::WorkerAgentOwnerToken owner_token, u64 policy_index, String effective_directive, URL::URL resource) =|
did_worker_agent_close(Web::HTML::WorkerAgentOwnerToken owner_token) =|
did_worker_agent_die(Web::HTML::WorkerAgentOwnerToken owner_token) =|
diff --git a/Services/WebContent/WebWorkerClient.ipc b/Services/WebContent/WebWorkerClient.ipc
index 3d9f9760743..75af24ca6a0 100644
--- a/Services/WebContent/WebWorkerClient.ipc
+++ b/Services/WebContent/WebWorkerClient.ipc
@@ -18,7 +18,6 @@ endpoint WebWorkerClient {
did_close_shared_worker() => ()
did_finish_loading_worker_script(bool worker_is_secure_context) =|
did_fail_loading_worker_script() =|
- did_report_worker_exception(Utf16String message, Utf16String filename, u32 lineno, u32 colno) =|
did_report_outside_settings_content_security_policy_violation(u64 policy_index, String effective_directive, URL::URL resource) =|
did_request_cookie(URL::URL url, HTTP::Cookie::Source source) => (HTTP::Cookie::VersionedCookie cookie)
did_request_file(ByteString path, i32 request_id) =|
diff --git a/Services/WebContent/WebWorkerServer.ipc b/Services/WebContent/WebWorkerServer.ipc
index 2d4ca136786..35dd9e30617 100644
--- a/Services/WebContent/WebWorkerServer.ipc
+++ b/Services/WebContent/WebWorkerServer.ipc
@@ -36,12 +36,6 @@ endpoint WebWorkerServer {
close_worker() =|
- did_worker_agent_fail_loading_script(Web::HTML::WorkerAgentOwnerToken owner_token) =|
- did_worker_agent_report_exception(Web::HTML::WorkerAgentOwnerToken owner_token, Utf16String message, Utf16String filename, u32 lineno, u32 colno) =|
- did_worker_agent_report_content_security_policy_violation(Web::HTML::WorkerAgentOwnerToken owner_token, u64 policy_index, String effective_directive, URL::URL resource) =|
- did_worker_agent_close(Web::HTML::WorkerAgentOwnerToken owner_token) =|
- did_worker_agent_die(Web::HTML::WorkerAgentOwnerToken owner_token) =|
-
broadcast_channel_message(Web::HTML::BroadcastChannelMessage message) =|
handle_file_return(i32 error, Optional<IPC::File> file, i32 request_id) =|
diff --git a/Services/WebContent/Worker/ConnectionFromClient.cpp b/Services/WebContent/Worker/ConnectionFromClient.cpp
index 4b403b74615..8ad6fdcc623 100644
--- a/Services/WebContent/Worker/ConnectionFromClient.cpp
+++ b/Services/WebContent/Worker/ConnectionFromClient.cpp
@@ -152,6 +152,7 @@ void ConnectionFromClient::set_preferred_languages(Vector<String> preferred_lang
void ConnectionFromClient::close_worker()
{
+ m_was_told_to_close = true;
begin_closing();
}
@@ -170,6 +171,7 @@ void ConnectionFromClient::die()
if (m_shutdown_finished)
return;
+ m_lost_browser_connection = true;
begin_closing();
}
@@ -178,6 +180,7 @@ void ConnectionFromClient::did_lose_peer_during_sync_request()
// NB: Whatever made the request cannot be unwound from here, and a thread cannot take its process with it. So, the
// script stops, and the request fails. That is every caller's to handle, as the connection is shut down from the
// event loop the script returns to.
+ m_lost_browser_connection = true;
m_script_abort_handle->abort_script();
deferred_invoke([this] {
shutdown();
@@ -213,6 +216,13 @@ void ConnectionFromClient::release_agent_state()
if (is_open())
async_did_close_worker();
+ if (m_dedicated_worker_thread_link) {
+ if (m_lost_browser_connection && !m_was_told_to_close)
+ m_dedicated_worker_thread_link->report_death_to_owner();
+ else
+ m_dedicated_worker_thread_link->report_close_to_owner();
+ }
+
if (m_worker_host)
m_worker_host->run_closing_steps();
m_worker_host = nullptr;
@@ -330,31 +340,6 @@ void ConnectionFromClient::handle_file_return(i32 error, Optional<IPC::File> fil
file_request.value().on_file_request_finish(error != 0 ? Error::from_errno(error) : ErrorOr<i32> { file->take_fd() });
}
-void ConnectionFromClient::did_worker_agent_fail_loading_script(Web::HTML::WorkerAgentOwnerToken owner_token)
-{
- Web::HTML::WorkerAgentParent::did_fail_loading_worker_script(owner_token);
-}
-
-void ConnectionFromClient::did_worker_agent_report_exception(Web::HTML::WorkerAgentOwnerToken owner_token, Utf16String message, Utf16String filename, u32 lineno, u32 colno)
-{
- Web::HTML::WorkerAgentParent::did_report_worker_exception(owner_token, move(message), move(filename), lineno, colno);
-}
-
-void ConnectionFromClient::did_worker_agent_report_content_security_policy_violation(Web::HTML::WorkerAgentOwnerToken owner_token, u64 policy_index, String effective_directive, URL::URL resource)
-{
- Web::HTML::WorkerAgentParent::did_report_content_security_policy_violation(owner_token, policy_index, move(effective_directive), move(resource));
-}
-
-void ConnectionFromClient::did_worker_agent_close(Web::HTML::WorkerAgentOwnerToken owner_token)
-{
- Web::HTML::WorkerAgentParent::did_close_worker(owner_token);
-}
-
-void ConnectionFromClient::did_worker_agent_die(Web::HTML::WorkerAgentOwnerToken owner_token)
-{
- Web::HTML::WorkerAgentParent::did_worker_agent_die(owner_token);
-}
-
void ConnectionFromClient::broadcast_channel_message(Web::HTML::BroadcastChannelMessage message)
{
Web::HTML::deliver_broadcast_channel_message_locally(move(message));
diff --git a/Services/WebContent/Worker/ConnectionFromClient.h b/Services/WebContent/Worker/ConnectionFromClient.h
index 33196043d38..ffac4c3bfb2 100644
--- a/Services/WebContent/Worker/ConnectionFromClient.h
+++ b/Services/WebContent/Worker/ConnectionFromClient.h
@@ -47,6 +47,8 @@ public:
void request_file(Web::FileRequest);
+ DedicatedWorkerThreadLink* dedicated_worker_thread_link() { return m_dedicated_worker_thread_link.ptr(); }
+
PageHost& page_host() { return *m_page_host; }
PageHost const& page_host() const { return *m_page_host; }
@@ -78,11 +80,6 @@ private:
virtual void connect_shared_worker(Web::HTML::TransferDataEncoder, Web::HTML::SerializedEnvironmentSettingsObject) override;
virtual void handle_file_return(i32 error, Optional<IPC::File> file, i32 request_id) override;
virtual void blob_url_entry_removed(Utf16String url) override;
- virtual void did_worker_agent_fail_loading_script(Web::HTML::WorkerAgentOwnerToken owner_token) override;
- virtual void did_worker_agent_report_exception(Web::HTML::WorkerAgentOwnerToken owner_token, Utf16String message, Utf16String filename, u32 lineno, u32 colno) override;
- virtual void did_worker_agent_report_content_security_policy_violation(Web::HTML::WorkerAgentOwnerToken owner_token, u64 policy_index, String effective_directive, URL::URL resource) override;
- virtual void did_worker_agent_close(Web::HTML::WorkerAgentOwnerToken owner_token) override;
- virtual void did_worker_agent_die(Web::HTML::WorkerAgentOwnerToken owner_token) override;
virtual void broadcast_channel_message(Web::HTML::BroadcastChannelMessage message) override;
GC::Root<PageHost> m_page_host;
@@ -109,6 +106,8 @@ private:
RefPtr<WorkerHost> m_worker_host;
Function<void()> m_request_server_died_callback_for_testing;
bool m_shutdown_finished { false };
+ bool m_was_told_to_close { false };
+ bool m_lost_browser_connection { false };
NonnullRefPtr<Web::HTML::ScriptAbortHandle> m_script_abort_handle;
};
diff --git a/Services/WebContent/Worker/DedicatedWorkerThreadLink.cpp b/Services/WebContent/Worker/DedicatedWorkerThreadLink.cpp
index d684073f0f7..ba20a142c7d 100644
--- a/Services/WebContent/Worker/DedicatedWorkerThreadLink.cpp
+++ b/Services/WebContent/Worker/DedicatedWorkerThreadLink.cpp
@@ -5,6 +5,7 @@
*/
#include <LibCore/EventLoop.h>
+#include <LibWeb/HTML/WorkerAgentParent.h>
#include <WebContent/Worker/ConnectionFromClient.h>
#include <WebContent/Worker/DedicatedWorkerThreadLink.h>
@@ -64,6 +65,43 @@ void DedicatedWorkerThreadLink::post_to_owner(Function<void()> function)
event_loop->deferred_invoke(move(function));
}
+void DedicatedWorkerThreadLink::report_script_load_failure_to_owner()
+{
+ post_to_owner([owner_token = m_owner_token] {
+ Web::HTML::WorkerAgentParent::did_fail_loading_worker_script(owner_token);
+ });
+}
+
+void DedicatedWorkerThreadLink::report_exception_to_owner(Utf16String message, Utf16String filename, u32 lineno, u32 colno)
+{
+ post_to_owner([owner_token = m_owner_token, message = move(message), filename = move(filename), lineno, colno] mutable {
+ Web::HTML::WorkerAgentParent::did_report_worker_exception(owner_token, move(message), move(filename), lineno, colno);
+ });
+}
+
+void DedicatedWorkerThreadLink::report_content_security_policy_violation_to_owner(u64 policy_index, String effective_directive, URL::URL const& resource)
+{
+ auto resource_without_thread_unsafe_blob_url_entry = resource;
+ resource_without_thread_unsafe_blob_url_entry.set_blob_url_entry({});
+ post_to_owner([owner_token = m_owner_token, policy_index, effective_directive = move(effective_directive), resource = move(resource_without_thread_unsafe_blob_url_entry)] mutable {
+ Web::HTML::WorkerAgentParent::did_report_content_security_policy_violation(owner_token, policy_index, move(effective_directive), move(resource));
+ });
+}
+
+void DedicatedWorkerThreadLink::report_close_to_owner()
+{
+ post_to_owner([owner_token = m_owner_token] {
+ Web::HTML::WorkerAgentParent::did_close_worker(owner_token);
+ });
+}
+
+void DedicatedWorkerThreadLink::report_death_to_owner()
+{
+ post_to_owner([owner_token = m_owner_token] {
+ Web::HTML::WorkerAgentParent::did_worker_agent_die(owner_token);
+ });
+}
+
void DedicatedWorkerThreadLink::rtc_transform_init(u64 transform_id, Web::HTML::SerializedTransferRecord options_record)
{
post_to_worker([transform_id, options_record = move(options_record)](ConnectionFromClient& connection) mutable {
diff --git a/Services/WebContent/Worker/DedicatedWorkerThreadLink.h b/Services/WebContent/Worker/DedicatedWorkerThreadLink.h
index 8aecbf99f2e..ead666c54a1 100644
--- a/Services/WebContent/Worker/DedicatedWorkerThreadLink.h
+++ b/Services/WebContent/Worker/DedicatedWorkerThreadLink.h
@@ -9,8 +9,11 @@
#include <AK/Function.h>
#include <AK/Mutex.h>
#include <AK/NonnullRefPtr.h>
+#include <AK/String.h>
+#include <AK/Utf16String.h>
#include <AK/Vector.h>
#include <LibCore/Forward.h>
+#include <LibURL/URL.h>
#include <LibWeb/HTML/DedicatedWorkerThreadLink.h>
#include <LibWebCommon/HTML/WorkerAgentForward.h>
#include <WebContent/Worker/Forward.h>
@@ -29,6 +32,12 @@ public:
void post_to_owner(Function<void()>);
+ void report_script_load_failure_to_owner();
+ void report_exception_to_owner(Utf16String message, Utf16String filename, u32 lineno, u32 colno);
+ void report_content_security_policy_violation_to_owner(u64 policy_index, String effective_directive, URL::URL const& resource);
+ void report_close_to_owner();
+ void report_death_to_owner();
+
virtual void rtc_transform_init(u64 transform_id, Web::HTML::SerializedTransferRecord options_record) override;
virtual void rtc_transform_teardown(u64 transform_id) override;
virtual void rtc_transform_encoded_audio_frame(u64 transform_id, ByteBuffer payload, u32 ssrc, u8 payload_type, u32 rtp_timestamp, u16 sequence_number) override;
diff --git a/Services/WebContent/Worker/PageHost.cpp b/Services/WebContent/Worker/PageHost.cpp
index 09573759bd5..7abf80d35b5 100644
--- a/Services/WebContent/Worker/PageHost.cpp
+++ b/Services/WebContent/Worker/PageHost.cpp
@@ -7,6 +7,7 @@
#include <LibGC/Heap.h>
#include <LibWebCommon/HTML/WorkerAgentTypes.h>
#include <WebContent/Worker/ConnectionFromClient.h>
+#include <WebContent/Worker/DedicatedWorkerThreadLink.h>
#include <WebContent/Worker/PageHost.h>
#include <WebContent/Worker/WebWorkerCompositorHost.h>
#include <WebContent/Worker/WorkerThread.h>
@@ -116,12 +117,17 @@ bool PageHost::page_did_is_known_hsts_host(String const& domain)
void PageHost::page_did_report_worker_exception(Utf16String const& message, Utf16String const& filename, u32 lineno, u32 colno)
{
- m_client.async_did_report_worker_exception(message, filename, lineno, colno);
+ if (auto* link = m_client.dedicated_worker_thread_link())
+ link->report_exception_to_owner(message, filename, lineno, colno);
}
// NB: The worker's only client another process hosts is outside settings, whose violations its owner reports.
void PageHost::page_did_report_violation_of_remote_client(u64 policy_index, String effective_directive, URL::URL resource)
{
+ if (auto* link = m_client.dedicated_worker_thread_link()) {
+ link->report_content_security_policy_violation_to_owner(policy_index, move(effective_directive), resource);
+ return;
+ }
m_client.async_did_report_outside_settings_content_security_policy_violation(policy_index, effective_directive, resource);
}
@@ -200,6 +206,8 @@ void PageHost::did_finish_loading_worker_script(bool worker_is_secure_context)
void PageHost::did_fail_loading_worker_script()
{
+ if (auto* link = m_client.dedicated_worker_thread_link())
+ link->report_script_load_failure_to_owner();
m_client.async_did_fail_loading_worker_script();
}
diff --git a/Services/WebContent/Worker/WorkerThread.cpp b/Services/WebContent/Worker/WorkerThread.cpp
index 08228fb7caa..e3cc6e4c02a 100644
--- a/Services/WebContent/Worker/WorkerThread.cpp
+++ b/Services/WebContent/Worker/WorkerThread.cpp
@@ -48,6 +48,8 @@ static intptr_t run_worker_thread(IPC::TransportHandle const& worker_handle, Web
auto transport_or_error = worker_handle.create_transport();
if (transport_or_error.is_error()) {
dbgln("Unable to take up the transport of a worker: {}", transport_or_error.error());
+ if (dedicated_worker_thread_link)
+ dedicated_worker_thread_link->report_death_to_owner();
return 1;
}
diff --git a/Tests/LibWeb/Text/expected/Workers/worker-nested-owner-gets-events.txt b/Tests/LibWeb/Text/expected/Workers/worker-nested-owner-gets-events.txt
new file mode 100644
index 00000000000..8c1d0788594
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Workers/worker-nested-owner-gets-events.txt
@@ -0,0 +1,5 @@
+exception: true
+load failure: Event
+ticks long after terminate: 0
+closing
+agents after the owner closed: 0
diff --git a/Tests/LibWeb/Text/input/Workers/worker-nested-owner-gets-events.html b/Tests/LibWeb/Text/input/Workers/worker-nested-owner-gets-events.html
new file mode 100644
index 00000000000..15da51066cb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Workers/worker-nested-owner-gets-events.html
@@ -0,0 +1,66 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ const OWNER = `
+ const blobURL = source => URL.createObjectURL(new Blob([source], { type: "text/javascript" }));
+
+ function throwingWorker() {
+ return new Promise(resolve => {
+ const nested = new Worker(blobURL('throw new Error("thrown in nested worker");'));
+ nested.onerror = event => {
+ event.preventDefault();
+ resolve(\`exception: \${event.message.includes("thrown in nested worker")}\`);
+ };
+ });
+ }
+
+ function workerThatFailsToLoad() {
+ return new Promise(resolve => {
+ const nested = new Worker("data:text/javascript,(");
+ nested.onerror = event => resolve(\`load failure: \${event.constructor.name}\`);
+ });
+ }
+
+ function terminatedWorker() {
+ return new Promise(resolve => {
+ const nested = new Worker(blobURL('setInterval(() => postMessage("tick"), 1);'));
+ nested.onmessage = () => {
+ nested.terminate();
+ let late_ticks = 0;
+ setTimeout(() => {
+ nested.onmessage = () => ++late_ticks;
+ setTimeout(() => resolve(\`ticks long after terminate: \${late_ticks}\`), 50);
+ }, 20);
+ };
+ });
+ }
+
+ (async () => {
+ postMessage(await throwingWorker());
+ postMessage(await workerThatFailsToLoad());
+ postMessage(await terminatedWorker());
+
+ const running = new Worker(blobURL('postMessage("running"); setInterval(() => {}, 1);'));
+ running.onmessage = () => {
+ postMessage("closing");
+ close();
+ };
+ })();
+ `;
+
+ promiseTest(async () => {
+ await waitForCondition(() => internals.workerAgentCount() === 0, 1200);
+
+ const owner = new Worker(URL.createObjectURL(new Blob([OWNER], { type: "text/javascript" })));
+ await new Promise(resolve => {
+ owner.onmessage = ({ data }) => {
+ println(data);
+ if (data === "closing")
+ resolve();
+ };
+ });
+
+ await waitForCondition(() => internals.workerAgentCount() === 0, 1200);
+ println(`agents after the owner closed: ${internals.workerAgentCount()}`);
+ });
+</script>