dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / file_api / blob_url_store.lucb
#==============================================================================================
#
# blob_url_store - FileAPI's blob URL store and the unloading cleanup steps that prune it
#
# DESCRIPTION:
# Port of Libraries/LibWeb/FileAPI/BlobURLStore.cpp lines 21-25 and 130-143
# (blob_url_store, run_unloading_cleanup_steps): what destroying a document needs. The
# store is a module global in the C heap (DESIGN.md §3.3 rule 7) whose entries keep their
# blob and environment through gc.Roots, released as the entries leave (the C++ Roots'
# destructors).
#
# Adding entries (URL.createObjectURL), resolving and revoking blob URLs and obtaining an
# entry's blob are phase 3 (FileAPI): before then the store stays empty.
#
# Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
# Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## The static store of blob_url_store, in the C heap (see above).
var file_api_blob_url_store_storage: FileApiBlobUrlStore
## Ported from BlobURLStore.cpp:21 Web::FileAPI::blob_url_store.
pub func file_api_blob_url_store() -> FileApiBlobUrlStore*:
return &file_api_blob_url_store_storage
## The captures of run_unloading_cleanup_steps' predicate: `[&]` (the environment).
struct FileApiRunUnloadingCleanupStepsMatch:
var environment: HtmlEnvironmentSettingsObject*
## run_unloading_cleanup_steps' predicate: the entries whose environment is the document's.
func file_api_run_unloading_cleanup_steps_matches(context: void*?, key: const ak.String*, value: const FileApiBlobUrlEntry*) -> bool:
let captures = (FileApiRunUnloadingCleanupStepsMatch*)(context else trap("no captures"))
return gc.root_ptr(&value.environment) == captures.environment
## Ported from BlobURLStore.cpp:131 Web::FileAPI::run_unloading_cleanup_steps.
# https://w3c.github.io/FileAPI/#lifeTime
pub func file_api_run_unloading_cleanup_steps(document: DomDocument*):
# 1. Let environment be the Document's relevant settings object.
let environment = dom_document_relevant_settings_object(document)
# 2. Let store be the user agent’s blob URL store;
let store = file_api_blob_url_store()
# 3. Remove from store any entries for which the value's environment is equal to environment.
var captures = FileApiRunUnloadingCleanupStepsMatch(environment = environment)
var removed: ak.Vector[ak.HashMapEntry[ak.String, FileApiBlobUrlEntry]]
with memory.heap:
removed = ak.hash_map_take_all_matching(store, ak.Function2[const ak.String*, const FileApiBlobUrlEntry*, bool](call = file_api_run_unloading_cleanup_steps_matches, context = &captures))
file_api_release_blob_url_entries(&removed)
## The GC::Roots of entries removed from the store, which end with them in C++.
func file_api_release_blob_url_entries(removed: ak.Vector[ak.HashMapEntry[ak.String, FileApiBlobUrlEntry]]*):
for entry in &ak.vector_span(removed):
gc.root_release(&entry.value.environment)
match entry.value.object:
.blob(root):
var blob_root = root
gc.root_release(&blob_root)
.media_source(root):
var media_source_root = root
gc.root_release(&media_source_root)
## Every entry of the store, for file_api_blob_url_store_forget.
func file_api_blob_url_entry_any(context: void*?, key: const ak.String*, value: const FileApiBlobUrlEntry*) -> bool:
_ = context
_ = key
_ = value
return true
## The store emptied as its VM is destroyed (vm_destroy): its entries keep cells of that VM's heap,
## and in Ladybird the store lives as long as the process's one VM.
func file_api_blob_url_store_forget():
var removed: ak.Vector[ak.HashMapEntry[ak.String, FileApiBlobUrlEntry]]
with memory.heap:
removed = ak.hash_map_take_all_matching(file_api_blob_url_store(), ak.Function2[const ak.String*, const FileApiBlobUrlEntry*, bool](call = file_api_blob_url_entry_any, context = none))
file_api_release_blob_url_entries(&removed)