ModuleMap.h 2.0 KB

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