Truly independent web browser
LibWeb+LibWebView+WebContent: Do not reuse a closed shared worker
Andreas Kling9b2d130efd4d
diff --git a/Libraries/LibWeb/HTML/SharedWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/SharedWorkerGlobalScope.cppindex 7648990463c..ecfe3c848ee 100644--- a/Libraries/LibWeb/HTML/SharedWorkerGlobalScope.cpp+++ b/Libraries/LibWeb/HTML/SharedWorkerGlobalScope.cpp@@ -44,6 +44,10 @@ void SharedWorkerGlobalScope::close(){// The close() method steps are to close a worker given this.close_a_worker();++ // NB: This script can run on for as long as it wants to. The UI process has to know by the time it returns, as a+ // constructor that runs after that may not pick a global whose closing flag is true.+ page()->client().page_did_close_shared_worker();}#define __ENUMERATE(attribute_name, event_name) \diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.hindex 9e751a41527..87a1baf0b9a 100644--- a/Libraries/LibWeb/Page/Page.h+++ b/Libraries/LibWeb/Page/Page.h@@ -1014,6 +1014,7 @@ public:virtual void page_did_report_navigation_violation_of_remote_client([[maybe_unused]] HTML::RemoteClientNavigation const& navigation, [[maybe_unused]] u64 policy_index, [[maybe_unused]] ContentSecurityPolicy::RemoteViolationDirective directive, [[maybe_unused]] bool url_is_https) { }virtual void page_did_register_javascript_source([[maybe_unused]] DOM::Document&, [[maybe_unused]] HTML::ScriptRegistry::Description const&) { }virtual void page_did_post_broadcast_channel_message([[maybe_unused]] HTML::PostedBroadcastChannelMessage const& message) { }+ virtual void page_did_close_shared_worker() { }struct StartedWorkerAgent {HTML::WorkerAgentId agent_id { 0 };diff --git a/Libraries/LibWebView/WebWorkerClient.cpp b/Libraries/LibWebView/WebWorkerClient.cppindex a3f8e2442e7..92c0a38ba6d 100644--- a/Libraries/LibWebView/WebWorkerClient.cpp+++ b/Libraries/LibWebView/WebWorkerClient.cpp@@ -75,6 +75,11 @@ void WebWorkerClient::did_close_worker()WorkerAgentManager::the().worker_did_close(m_agent_id);}+void WebWorkerClient::did_close_shared_worker()+{+ WorkerAgentManager::the().shared_worker_did_close(m_agent_id);+}+void WebWorkerClient::did_finish_loading_worker_script(bool worker_is_secure_context){WorkerAgentManager::the().worker_did_finish_loading_script(m_agent_id, worker_is_secure_context);diff --git a/Libraries/LibWebView/WebWorkerClient.h b/Libraries/LibWebView/WebWorkerClient.hindex 0c71c627282..ef90bc5e32c 100644--- a/Libraries/LibWebView/WebWorkerClient.h+++ b/Libraries/LibWebView/WebWorkerClient.h@@ -53,6 +53,7 @@ public:Optional<CanonicalEnvironmentSettingsObject const&> hosted_environment(Web::HTML::EnvironmentId const& environment_id) const;virtual void did_close_worker() override;+ 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;diff --git a/Libraries/LibWebView/WorkerAgentManager.cpp b/Libraries/LibWebView/WorkerAgentManager.cppindex 047c086ab3a..61a85b953f9 100644--- a/Libraries/LibWebView/WorkerAgentManager.cpp+++ b/Libraries/LibWebView/WorkerAgentManager.cpp@@ -774,6 +774,17 @@ void WorkerAgentManager::worker_did_close(Web::HTML::WorkerAgentId agent_id)});}+void WorkerAgentManager::shared_worker_did_close(Web::HTML::WorkerAgentId agent_id)+{+ // NB: The script can run on, and its thread with it, but no constructor may connect to this worker any more.+ auto maybe_agent = m_agents.find(agent_id);+ if (maybe_agent == m_agents.end() || !maybe_agent->value.shared_worker_key.has_value())+ return;+ auto const& key = *maybe_agent->value.shared_worker_key;+ if (m_shared_workers.get(key) == agent_id)+ m_shared_workers.remove(key);+}+void WorkerAgentManager::worker_did_die(Web::HTML::WorkerAgentId agent_id){auto maybe_agent = m_agents.find(agent_id);@@ -850,7 +861,8 @@ void WorkerAgentManager::remove_agent(Web::HTML::WorkerAgentId agent_id, AgentReif (agent.extended_lifetime_timer)Core::deferred_invoke([timer = move(agent.extended_lifetime_timer)] { });- if (agent.shared_worker_key.has_value())+ // NB: A new worker may have taken this one's place by now.+ if (agent.shared_worker_key.has_value() && m_shared_workers.get(*agent.shared_worker_key) == agent_id)m_shared_workers.remove(*agent.shared_worker_key);// NB: The owner of a page can be removing the agent from the destructor of the host, which nothing can take adiff --git a/Libraries/LibWebView/WorkerAgentManager.h b/Libraries/LibWebView/WorkerAgentManager.hindex 41e930bd3d3..f38322497f5 100644--- a/Libraries/LibWebView/WorkerAgentManager.h+++ b/Libraries/LibWebView/WorkerAgentManager.h@@ -141,6 +141,7 @@ private: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);void worker_did_die(Web::HTML::WorkerAgentId);void worker_thread_did_exit(WebWorkerClient&);void worker_did_request_file(Web::HTML::WorkerAgentId, ByteString path, i32 request_id);diff --git a/Services/WebContent/WebWorkerClient.ipc b/Services/WebContent/WebWorkerClient.ipcindex 5077afe772f..28f0abf3cba 100644--- a/Services/WebContent/WebWorkerClient.ipc+++ b/Services/WebContent/WebWorkerClient.ipc@@ -15,6 +15,7 @@ endpoint WebWorkerClient {did_request_blob_url_entry(Utf16String url, Optional<URL::BlobURLEntry::Token> token) => (Optional<Web::FileAPI::SerializedBlobURLEntry> entry)did_close_worker() =|+ 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) =|diff --git a/Services/WebContent/Worker/PageHost.cpp b/Services/WebContent/Worker/PageHost.cppindex 586284915ac..47dd2b255bd 100644--- a/Services/WebContent/Worker/PageHost.cpp+++ b/Services/WebContent/Worker/PageHost.cpp@@ -130,6 +130,11 @@ void PageHost::page_did_post_broadcast_channel_message(Web::HTML::PostedBroadcasm_client.async_did_post_broadcast_channel_message(message);}+void PageHost::page_did_close_shared_worker()+{+ (void)m_client.send_sync_but_allow_failure<Messages::WebWorkerClient::DidCloseSharedWorker>();+}+void PageHost::request_file(Web::FileRequest request){m_client.request_file(move(request));diff --git a/Services/WebContent/Worker/PageHost.h b/Services/WebContent/Worker/PageHost.hindex df95d24fa60..7a9d4404d0f 100644--- a/Services/WebContent/Worker/PageHost.h+++ b/Services/WebContent/Worker/PageHost.h@@ -45,6 +45,7 @@ public:virtual void page_did_report_worker_exception(Utf16String const& message, Utf16String const& filename, u32 lineno, u32 colno) override;virtual void page_did_report_violation_of_remote_client(u64 policy_index, String effective_directive, URL::URL resource) override;virtual void page_did_post_broadcast_channel_message(Web::HTML::PostedBroadcastChannelMessage const& message) override;+ virtual void page_did_close_shared_worker() override;virtual void request_file(Web::FileRequest) override;virtual URL::BlobURLEntry::Token page_did_add_blob_url_entry(Web::HTML::EnvironmentSettingsObject const&, Utf16String const& url, Web::FileAPI::SerializedBlobURLEntry const&) override;virtual void page_did_remove_blob_url_entries(Web::HTML::EnvironmentSettingsObject const&, Vector<Utf16String> const& urls) override;diff --git a/Tests/LibWeb/Text/expected/Workers/shared-worker-new-after-close.txt b/Tests/LibWeb/Text/expected/Workers/shared-worker-new-after-close.txtnew file mode 100644index 00000000000..83926b5b989--- /dev/null+++ b/Tests/LibWeb/Text/expected/Workers/shared-worker-new-after-close.txt@@ -0,0 +1,4 @@+first worker, connection 1+closed+fresh worker: true, connection 1+same worker: true, connection 2diff --git a/Tests/LibWeb/Text/input/Workers/shared-worker-new-after-close.html b/Tests/LibWeb/Text/input/Workers/shared-worker-new-after-close.htmlnew file mode 100644index 00000000000..c8a90595464--- /dev/null+++ b/Tests/LibWeb/Text/input/Workers/shared-worker-new-after-close.html@@ -0,0 +1,47 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ const workerScript = `+ let connections = 0;+ const workerId = Math.random();+ onconnect = event => {+ const port = event.ports[0];+ port.onmessage = () => {+ close();+ port.postMessage("closed");+ // The closing flag is set, and this script and its thread go on.+ const end = Date.now() + 200;+ while (Date.now() < end);+ };+ port.postMessage({ workerId, connection: ++connections });+ };+ `;+ const workerURL = URL.createObjectURL(new Blob([workerScript], { type: "text/javascript" }));++ function connect() {+ const worker = new SharedWorker(workerURL, "new-after-close");+ return new Promise(resolve => {+ worker.port.onmessage = event => resolve({ worker, ...event.data });+ });+ }++ const first = await connect();+ println(`first worker, connection ${first.connection}`);++ const closed = new Promise(resolve => {+ first.worker.port.onmessage = event => resolve(event.data);+ });+ first.worker.port.postMessage("close");+ println(await closed);++ const second = await connect();+ println(`fresh worker: ${second.workerId !== first.workerId}, connection ${second.connection}`);++ // The closed worker goes away, and has to leave the one that took its place alone.+ while (internals.workerAgentCount() !== 1) await new Promise(resolve => setTimeout(resolve, 10));++ const third = await connect();+ println(`same worker: ${third.workerId === second.workerId}, connection ${third.connection}`);+ });+</script>