/src/serenity/Userland/Libraries/LibWeb/HTML/Scripting/ModuleMap.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022-2023, networkException <networkexception@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <LibJS/Heap/Cell.h> |
10 | | #include <LibJS/Heap/HeapFunction.h> |
11 | | #include <LibURL/URL.h> |
12 | | #include <LibWeb/HTML/Scripting/ModuleScript.h> |
13 | | |
14 | | namespace Web::HTML { |
15 | | |
16 | | class ModuleLocationTuple { |
17 | | public: |
18 | | ModuleLocationTuple(URL::URL url, ByteString type) |
19 | 0 | : m_url(move(url)) |
20 | 0 | , m_type(move(type)) |
21 | 0 | { |
22 | 0 | } |
23 | | |
24 | 0 | URL::URL const& url() const { return m_url; } |
25 | 0 | ByteString const& type() const { return m_type; } |
26 | | |
27 | | bool operator==(ModuleLocationTuple const& other) const |
28 | 0 | { |
29 | 0 | return other.url() == m_url && other.type() == m_type; |
30 | 0 | } |
31 | | |
32 | | private: |
33 | | URL::URL m_url; |
34 | | ByteString m_type; |
35 | | }; |
36 | | |
37 | | // https://html.spec.whatwg.org/multipage/webappapis.html#module-map |
38 | | class ModuleMap final : public JS::Cell { |
39 | | JS_CELL(ModuleMap, JS::Cell); |
40 | | JS_DECLARE_ALLOCATOR(ModuleMap); |
41 | | |
42 | | public: |
43 | 0 | ModuleMap() = default; |
44 | 0 | ~ModuleMap() = default; |
45 | | |
46 | | enum class EntryType { |
47 | | Fetching, |
48 | | Failed, |
49 | | ModuleScript |
50 | | }; |
51 | | |
52 | | struct Entry { |
53 | | EntryType type; |
54 | | JS::GCPtr<JavaScriptModuleScript> module_script; |
55 | | }; |
56 | | |
57 | | using CallbackFunction = JS::NonnullGCPtr<JS::HeapFunction<void(Entry)>>; |
58 | | |
59 | | bool is_fetching(URL::URL const& url, ByteString const& type) const; |
60 | | bool is_failed(URL::URL const& url, ByteString const& type) const; |
61 | | |
62 | | bool is(URL::URL const& url, ByteString const& type, EntryType) const; |
63 | | |
64 | | Optional<Entry> get(URL::URL const& url, ByteString const& type) const; |
65 | | |
66 | | AK::HashSetResult set(URL::URL const& url, ByteString const& type, Entry); |
67 | | |
68 | | void wait_for_change(JS::Heap&, URL::URL const& url, ByteString const& type, Function<void(Entry)> callback); |
69 | | |
70 | | private: |
71 | | virtual void visit_edges(JS::Cell::Visitor&) override; |
72 | | |
73 | | HashMap<ModuleLocationTuple, Entry> m_values; |
74 | | HashMap<ModuleLocationTuple, Vector<CallbackFunction>> m_callbacks; |
75 | | |
76 | | bool m_firing_callbacks { false }; |
77 | | }; |
78 | | |
79 | | } |
80 | | |
81 | | namespace AK { |
82 | | |
83 | | template<> |
84 | | struct Traits<Web::HTML::ModuleLocationTuple> : public DefaultTraits<Web::HTML::ModuleLocationTuple> { |
85 | | static unsigned hash(Web::HTML::ModuleLocationTuple const& tuple) |
86 | 0 | { |
87 | 0 | return pair_int_hash(tuple.url().to_byte_string().hash(), tuple.type().hash()); |
88 | 0 | } |
89 | | }; |
90 | | |
91 | | } |