Serenity Operating System
1/*
2 * Copyright (c) 2022, networkException <networkexception@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/URL.h>
10#include <LibWeb/HTML/Scripting/ModuleScript.h>
11
12namespace Web::HTML {
13
14class ModuleLocationTuple {
15public:
16 ModuleLocationTuple(AK::URL url, DeprecatedString type)
17 : m_url(move(url))
18 , m_type(move(type))
19 {
20 }
21
22 AK::URL const& url() const { return m_url; };
23 DeprecatedString const& type() const { return m_type; }
24
25 bool operator==(ModuleLocationTuple const& other) const
26 {
27 return other.url() == m_url && other.type() == m_type;
28 };
29
30private:
31 AK::URL m_url;
32 DeprecatedString m_type;
33};
34
35// https://html.spec.whatwg.org/multipage/webappapis.html#module-map
36class ModuleMap {
37 AK_MAKE_NONCOPYABLE(ModuleMap);
38
39public:
40 ModuleMap() = default;
41 ~ModuleMap() = default;
42
43 enum class EntryType {
44 Fetching,
45 Failed,
46 ModuleScript
47 };
48
49 struct Entry {
50 EntryType type;
51 JavaScriptModuleScript* module_script;
52 };
53
54 bool is_fetching(AK::URL const& url, DeprecatedString const& type) const;
55 bool is_failed(AK::URL const& url, DeprecatedString const& type) const;
56
57 bool is(AK::URL const& url, DeprecatedString const& type, EntryType) const;
58
59 Optional<Entry> get(AK::URL const& url, DeprecatedString const& type) const;
60
61 AK::HashSetResult set(AK::URL const& url, DeprecatedString const& type, Entry);
62
63 void wait_for_change(AK::URL const& url, DeprecatedString const& type, Function<void(Entry)> callback);
64
65private:
66 HashMap<ModuleLocationTuple, Entry> m_values;
67 HashMap<ModuleLocationTuple, Vector<Function<void(Entry)>>> m_callbacks;
68};
69
70}
71
72namespace AK {
73
74template<>
75struct Traits<Web::HTML::ModuleLocationTuple> : public GenericTraits<Web::HTML::ModuleLocationTuple> {
76 static unsigned hash(Web::HTML::ModuleLocationTuple const& tuple)
77 {
78 return pair_int_hash(tuple.url().to_deprecated_string().hash(), tuple.type().hash());
79 }
80};
81
82}