ModuleMap.h 2.2 KB

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