Skip to content

Truly independent web browser

LibWeb: Keep service worker registrations alive for their JS objects

Tim Ledbetter0eb5f96267a7
diff --git a/Libraries/LibWeb/HTML/Scripting/Environments.cpp b/Libraries/LibWeb/HTML/Scripting/Environments.cpp
index f9b122e4657..5443f134d31 100644
--- a/Libraries/LibWeb/HTML/Scripting/Environments.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Environments.cpp
@@ -793,11 +793,8 @@ GC::Ref<ServiceWorker::ServiceWorkerRegistration> EnvironmentSettingsObject::get
// 1. Let objectMap be environment’s service worker registration object map.
auto& object_map = this->m_service_worker_registration_object_map;
- // FIXME: File spec issue asking if this should be keyed on the registration's scope url only or on the url and the storage key
- auto const key = ServiceWorker::RegistrationKey { registration.storage_key(), registration.scope_url().serialize(URL::ExcludeFragment::Yes).to_byte_string() };
-
// 2. If objectMap[registration] does not exist, then:
- if (!object_map.contains(key)) {
+ if (!object_map.contains(&registration)) {
// 1. Let registrationObject be a new ServiceWorkerRegistration in environment’s Realm.
// 2. Set registrationObject’s service worker registration to registration.
// 3. Set registrationObject’s installing attribute to null.
@@ -818,11 +815,11 @@ GC::Ref<ServiceWorker::ServiceWorkerRegistration> EnvironmentSettingsObject::get
registration_object->set_active(get_service_worker_object(registration.active_worker()));
// 9. Set objectMap[registration] to registrationObject.
- object_map.set(key, registration_object);
+ object_map.set(&registration, registration_object);
}
// 3. Return objectMap[registration].
- return *object_map.get(key);
+ return *object_map.get(&registration);
}
GC::Ref<ServiceWorker::ServiceWorker> EnvironmentSettingsObject::get_service_worker_object(ServiceWorker::ServiceWorkerRecord* service_worker)
diff --git a/Libraries/LibWeb/HTML/Scripting/Environments.h b/Libraries/LibWeb/HTML/Scripting/Environments.h
index ef49e6d4147..508d9d15284 100644
--- a/Libraries/LibWeb/HTML/Scripting/Environments.h
+++ b/Libraries/LibWeb/HTML/Scripting/Environments.h
@@ -206,7 +206,7 @@ private:
// https://w3c.github.io/ServiceWorker/#environment-settings-object-service-worker-registration-object-map
// An environment settings object has a service worker registration object map,
// a map where the keys are service worker registrations and the values are ServiceWorkerRegistration objects.
- HashMap<ServiceWorker::RegistrationKey, GC::Ref<ServiceWorker::ServiceWorkerRegistration>> m_service_worker_registration_object_map;
+ HashMap<ServiceWorker::Registration const*, GC::Ref<ServiceWorker::ServiceWorkerRegistration>> m_service_worker_registration_object_map;
// https://w3c.github.io/ServiceWorker/#environment-settings-object-service-worker-object-map
// An environment settings object has a service worker object map,
diff --git a/Libraries/LibWeb/ServiceWorker/Job.cpp b/Libraries/LibWeb/ServiceWorker/Job.cpp
index 31fa92b6e56..f3e5303cac4 100644
--- a/Libraries/LibWeb/ServiceWorker/Job.cpp
+++ b/Libraries/LibWeb/ServiceWorker/Job.cpp
@@ -167,7 +167,7 @@ public:
return GC::Heap::the().allocate<UpdateAlgorithmState>(registration, newest_worker);
}
- Registration& registration() const { return m_registration; }
+ Registration& registration() const { return *m_registration; }
ServiceWorkerRecord* newest_worker() const { return m_newest_worker; }
OrderedHashMap<URL::URL, GC::Ref<Fetch::Infrastructure::ResponseView>>& updated_resource_map() { return m_map; }
@@ -189,7 +189,7 @@ private:
// NB: The registration and the newest worker are not GC-allocated. The fetch callbacks of the Update algorithm
// reach them through this state.
- Registration& m_registration;
+ NonnullRefPtr<Registration> m_registration;
ServiceWorkerRecord* m_newest_worker { nullptr };
OrderedHashMap<URL::URL, GC::Ref<Fetch::Infrastructure::ResponseView>> m_map;
bool m_has_updated_resources { false };
diff --git a/Libraries/LibWeb/ServiceWorker/Registration.cpp b/Libraries/LibWeb/ServiceWorker/Registration.cpp
index a04e4bcb713..d973d535d84 100644
--- a/Libraries/LibWeb/ServiceWorker/Registration.cpp
+++ b/Libraries/LibWeb/ServiceWorker/Registration.cpp
@@ -6,7 +6,7 @@
#include <AK/HashMap.h>
#include <AK/NeverDestroyed.h>
-#include <AK/NonnullOwnPtr.h>
+#include <AK/NonnullRefPtr.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/ServiceWorker/Registration.h>
@@ -15,9 +15,9 @@ namespace Web::ServiceWorker {
// FIXME: Surely this needs hooks to be cleared and manipulated at the UA level
// Does this need to be serialized to disk as well?
-static OrderedHashMap<RegistrationKey, NonnullOwnPtr<Registration>>& registration_map()
+static OrderedHashMap<RegistrationKey, NonnullRefPtr<Registration>>& registration_map()
{
- static thread_local NeverDestroyed<OrderedHashMap<RegistrationKey, NonnullOwnPtr<Registration>>> registrations;
+ static thread_local NeverDestroyed<OrderedHashMap<RegistrationKey, NonnullRefPtr<Registration>>> registrations;
return *registrations;
}
@@ -80,7 +80,7 @@ Registration& Registration::set(StorageAPI::StorageKey const& storage_key, URL::
// FIXME: Is there a way to "ensure but always replace?"
auto key = RegistrationKey { storage_key, scope.serialize(URL::ExcludeFragment::Yes).to_byte_string() };
- (void)registration_map().set(key, adopt_own(*new Registration(storage_key, scope, update_via_cache)));
+ (void)registration_map().set(key, adopt_ref(*new Registration(storage_key, scope, update_via_cache)));
return *registration_map().get(key).value();
}
diff --git a/Libraries/LibWeb/ServiceWorker/Registration.h b/Libraries/LibWeb/ServiceWorker/Registration.h
index 73b18f41e53..fe3a46d13a2 100644
--- a/Libraries/LibWeb/ServiceWorker/Registration.h
+++ b/Libraries/LibWeb/ServiceWorker/Registration.h
@@ -7,10 +7,10 @@
#pragma once
#include <AK/Optional.h>
+#include <AK/RefCounted.h>
#include <AK/Time.h>
#include <AK/Traits.h>
#include <AK/Vector.h>
-#include <AK/kmalloc.h>
#include <LibURL/URL.h>
#include <LibWeb/Bindings/ServiceWorkerRegistration.h>
#include <LibWeb/Forward.h>
@@ -23,11 +23,7 @@ namespace Web::ServiceWorker {
// This class corresponds to "service worker registration", not "ServiceWorkerRegistration"
// FIXME: This object needs to live at the user-agent level, in LibWebView, not in LibWeb
// .. And it will need some way to synchronize updates to each 'client' (aka process aka ESO)
-class Registration {
- AK_MAKE_NONCOPYABLE(Registration);
- AK_MAKE_DEFAULT_MOVABLE(Registration);
- AK_ALLOC_WITH_KMALLOC;
-
+class Registration : public RefCounted<Registration> {
public:
static void forget_registrations_of_this_agent();
diff --git a/Libraries/LibWeb/ServiceWorker/ServiceWorkerRegistration.h b/Libraries/LibWeb/ServiceWorker/ServiceWorkerRegistration.h
index 93427560cd0..c4e67dfb92c 100644
--- a/Libraries/LibWeb/ServiceWorker/ServiceWorkerRegistration.h
+++ b/Libraries/LibWeb/ServiceWorker/ServiceWorkerRegistration.h
@@ -6,8 +6,10 @@
#pragma once
+#include <AK/NonnullRefPtr.h>
#include <LibWeb/Bindings/ServiceWorkerRegistration.h>
#include <LibWeb/DOM/EventTarget.h>
+#include <LibWeb/ServiceWorker/Registration.h>
namespace Web::ServiceWorker {
@@ -19,7 +21,7 @@ class ServiceWorkerRegistration : public DOM::EventTarget {
public:
[[nodiscard]] static GC::Ref<ServiceWorkerRegistration> create(Registration const& registration);
- Registration const& registration() { return m_registration; }
+ Registration const& registration() { return *m_registration; }
GC::Ptr<ServiceWorker> installing() const { return m_installing; }
void set_installing(GC::Ptr<ServiceWorker> installing) { m_installing = installing; }
@@ -36,12 +38,12 @@ public:
// https://w3c.github.io/ServiceWorker/#dom-serviceworkerregistration-scope
Utf16String scope() const
{
- auto serialized_url = m_registration.scope_url().serialize();
+ auto serialized_url = m_registration->scope_url().serialize();
return Utf16String::from_ascii_without_validation(serialized_url.bytes());
}
// https://w3c.github.io/ServiceWorker/#dom-serviceworkerregistration-updateviacache
- ServiceWorkerUpdateViaCache update_via_cache() const { return m_registration.update_via_cache(); }
+ ServiceWorkerUpdateViaCache update_via_cache() const { return m_registration->update_via_cache(); }
explicit ServiceWorkerRegistration(Registration const&);
virtual ~ServiceWorkerRegistration() override = default;
@@ -49,7 +51,7 @@ public:
private:
virtual void visit_edges(JS::Cell::Visitor&) override;
- Registration const& m_registration;
+ NonnullRefPtr<Registration const> m_registration;
GC::Ptr<ServiceWorker> m_installing;
GC::Ptr<ServiceWorker> m_waiting;
GC::Ptr<ServiceWorker> m_active;
diff --git a/Tests/LibWeb/Text/expected/ServiceWorker/service-worker-registration-removed-by-failed-update.txt b/Tests/LibWeb/Text/expected/ServiceWorker/service-worker-registration-removed-by-failed-update.txt
new file mode 100644
index 00000000000..afd8ad38b07
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/ServiceWorker/service-worker-registration-removed-by-failed-update.txt
@@ -0,0 +1,6 @@
+register() with a missing script: rejected with TypeError
+getRegistration() after the failed update: undefined
+Object of the removed registration keeps its scope: true
+Object of the removed registration keeps its updateViaCache: imports
+register() with a missing script: rejected with TypeError
+Registering the scope again gives a new registration object: true
diff --git a/Tests/LibWeb/Text/input/ServiceWorker/service-worker-registration-removed-by-failed-update.html b/Tests/LibWeb/Text/input/ServiceWorker/service-worker-registration-removed-by-failed-update.html
new file mode 100644
index 00000000000..274175f9fb7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/ServiceWorker/service-worker-registration-removed-by-failed-update.html
@@ -0,0 +1,37 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const scope = server.echoURL("/scope/");
+
+ async function getRegistrationWhileScriptIsMissing() {
+ const token = uniqueID();
+ const scriptURL = await server.createEcho("GET", `/missing-${token}.js`, {
+ status: 404,
+ headers: { "Content-Type": "text/javascript" },
+ body: "",
+ wait_for_unblock: token,
+ });
+ const registered = navigator.serviceWorker.register(scriptURL, { scope });
+ await server.waitForRequest(scriptURL);
+ const registration = await navigator.serviceWorker.getRegistration(scope);
+ await fetch(`${server.baseURL}/unblock/${token}`);
+ try {
+ await registered;
+ println("register() with a missing script: resolved");
+ } catch (error) {
+ println(`register() with a missing script: rejected with ${error.name}`);
+ }
+ return registration;
+ }
+
+ const first = await getRegistrationWhileScriptIsMissing();
+ println(`getRegistration() after the failed update: ${await navigator.serviceWorker.getRegistration(scope)}`);
+ println(`Object of the removed registration keeps its scope: ${first.scope === scope}`);
+ println(`Object of the removed registration keeps its updateViaCache: ${first.updateViaCache}`);
+
+ const second = await getRegistrationWhileScriptIsMissing();
+ println(`Registering the scope again gives a new registration object: ${second !== first}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/ServiceWorker/service-worker-registration-removed-by-failed-update.html.headers b/Tests/LibWeb/Text/input/ServiceWorker/service-worker-registration-removed-by-failed-update.html.headers
new file mode 100644
index 00000000000..156209f9c81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/ServiceWorker/service-worker-registration-removed-by-failed-update.html.headers
@@ -0,0 +1 @@
+Content-Type: text/html