ModuleMap.h 2.1 KB

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