ModuleMap.h 2.0 KB

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