Coverage Report

Created: 2025-09-05 06:52

/src/serenity/Userland/Libraries/LibWeb/HTML/Scripting/ModuleMap.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2022-2023, networkException <networkexception@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <LibWeb/HTML/Scripting/ModuleMap.h>
8
9
namespace Web::HTML {
10
11
JS_DEFINE_ALLOCATOR(ModuleMap);
12
13
void ModuleMap::visit_edges(Visitor& visitor)
14
0
{
15
0
    Base::visit_edges(visitor);
16
0
    for (auto& it : m_values)
17
0
        visitor.visit(it.value.module_script);
18
19
0
    for (auto const& it : m_callbacks)
20
0
        visitor.visit(it.value);
21
0
}
22
23
bool ModuleMap::is_fetching(URL::URL const& url, ByteString const& type) const
24
0
{
25
0
    return is(url, type, EntryType::Fetching);
26
0
}
27
28
bool ModuleMap::is_failed(URL::URL const& url, ByteString const& type) const
29
0
{
30
0
    return is(url, type, EntryType::Failed);
31
0
}
32
33
bool ModuleMap::is(URL::URL const& url, ByteString const& type, EntryType entry_type) const
34
0
{
35
0
    auto value = m_values.get({ url, type });
36
0
    if (!value.has_value())
37
0
        return false;
38
39
0
    return value->type == entry_type;
40
0
}
41
42
Optional<ModuleMap::Entry> ModuleMap::get(URL::URL const& url, ByteString const& type) const
43
0
{
44
0
    return m_values.get({ url, type }).copy();
45
0
}
46
47
AK::HashSetResult ModuleMap::set(URL::URL const& url, ByteString const& type, Entry entry)
48
0
{
49
    // NOTE: Re-entering this function while firing wait_for_change callbacks is not allowed.
50
0
    VERIFY(!m_firing_callbacks);
51
52
0
    auto value = m_values.set({ url, type }, entry);
53
54
0
    auto callbacks = m_callbacks.get({ url, type });
55
0
    if (callbacks.has_value()) {
56
0
        m_firing_callbacks = true;
57
0
        for (auto const& callback : *callbacks)
58
0
            callback->function()(entry);
59
0
        m_firing_callbacks = false;
60
0
    }
61
62
0
    return value;
63
0
}
64
65
void ModuleMap::wait_for_change(JS::Heap& heap, URL::URL const& url, ByteString const& type, Function<void(Entry)> callback)
66
0
{
67
0
    m_callbacks.ensure({ url, type }).append(JS::create_heap_function(heap, move(callback)));
68
0
}
69
70
}