mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-27 23:09:08 +00:00
LibWeb: Implement ModuleMap and expose it on EnvironmentSettingsObject
This patch adds the ModuleMap class used to keep track of the type and url of a module as well as the fetching state associated. Each environment settings object now also has a module map.
This commit is contained in:
parent
83554526f0
commit
992311c0ee
Notes:
sideshowbarker
2024-07-17 08:25:15 +09:00
Author: https://github.com/networkException
Commit: 992311c0ee
Pull-request: https://github.com/SerenityOS/serenity/pull/15275
Reviewed-by: https://github.com/davidot ✅
Reviewed-by: https://github.com/linusg
5 changed files with 143 additions and 1 deletions
81
Userland/Libraries/LibWeb/HTML/Scripting/ModuleMap.h
Normal file
81
Userland/Libraries/LibWeb/HTML/Scripting/ModuleMap.h
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Copyright (c) 2022, networkException <networkexception@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibWeb/HTML/Scripting/ModuleScript.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
class ModuleLocationTuple {
|
||||
public:
|
||||
ModuleLocationTuple(AK::URL url, String type)
|
||||
: m_url(move(url))
|
||||
, m_type(move(type))
|
||||
{
|
||||
}
|
||||
|
||||
AK::URL const& url() const { return m_url; };
|
||||
String const& type() const { return m_type; }
|
||||
|
||||
bool operator==(ModuleLocationTuple const& other) const
|
||||
{
|
||||
return other.url() == m_url && other.type() == m_type;
|
||||
};
|
||||
|
||||
private:
|
||||
AK::URL m_url;
|
||||
String m_type;
|
||||
};
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/webappapis.html#module-map
|
||||
class ModuleMap {
|
||||
AK_MAKE_NONCOPYABLE(ModuleMap);
|
||||
|
||||
public:
|
||||
ModuleMap() = default;
|
||||
~ModuleMap() = default;
|
||||
|
||||
enum class EntryType {
|
||||
Fetching,
|
||||
Failed,
|
||||
ModuleScript
|
||||
};
|
||||
|
||||
struct Entry {
|
||||
EntryType type;
|
||||
JavaScriptModuleScript* module_script;
|
||||
};
|
||||
|
||||
bool is_fetching(AK::URL const& url, String const& type) const;
|
||||
bool is_failed(AK::URL const& url, String const& type) const;
|
||||
|
||||
bool is(AK::URL const& url, String const& type, EntryType) const;
|
||||
|
||||
Optional<Entry> get(AK::URL const& url, String const& type) const;
|
||||
|
||||
AK::HashSetResult set(AK::URL const& url, String const& type, Entry);
|
||||
|
||||
void wait_for_change(AK::URL const& url, String const& type, Function<void(Entry)> callback);
|
||||
|
||||
private:
|
||||
HashMap<ModuleLocationTuple, Entry> m_values;
|
||||
HashMap<ModuleLocationTuple, Vector<Function<void(Entry)>>> m_callbacks;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<>
|
||||
struct Traits<Web::HTML::ModuleLocationTuple> : public GenericTraits<Web::HTML::ModuleLocationTuple> {
|
||||
static unsigned hash(Web::HTML::ModuleLocationTuple const& tuple)
|
||||
{
|
||||
return pair_int_hash(tuple.url().to_string().hash(), tuple.type().hash());
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue