LibWeb: Stop leaking entire realms via Blob URLs

This patch implements the File API spec's supplemental steps for
document's "unloading document cleanup steps" so that we now remove blob
URLs associated with the document's relevant settings object when the
document is being unloaded.

Fixes two realm leaks when running our test suite.
This commit is contained in:
Andreas Kling 2024-04-03 20:17:09 +02:00
commit d91d6ee205
Notes: sideshowbarker 2024-07-18 03:35:30 +09:00
3 changed files with 23 additions and 1 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2024, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -7,6 +8,7 @@
#include <AK/StringBuilder.h>
#include <LibURL/URL.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/FileAPI/Blob.h>
#include <LibWeb/FileAPI/BlobURLStore.h>
#include <LibWeb/HTML/Origin.h>
@ -89,4 +91,19 @@ ErrorOr<void> remove_entry_from_blob_url_store(StringView url)
return {};
}
// https://w3c.github.io/FileAPI/#lifeTime
void run_unloading_cleanup_steps(JS::NonnullGCPtr<DOM::Document> document)
{
// 1. Let environment be the Document's relevant settings object.
auto& environment = document->relevant_settings_object();
// 2. Let store be the user agents blob URL store;
auto& store = FileAPI::blob_url_store();
// 3. Remove from store any entries for which the value's environment is equal to environment.
store.remove_all_matching([&](auto&, auto& value) {
return value.environment == &environment;
});
}
}