WorkerLocation.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibURL/Parser.h>
  7. #include <LibWeb/Bindings/WorkerLocationPrototype.h>
  8. #include <LibWeb/HTML/WorkerGlobalScope.h>
  9. #include <LibWeb/HTML/WorkerLocation.h>
  10. namespace Web::HTML {
  11. GC_DEFINE_ALLOCATOR(WorkerLocation);
  12. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-href
  13. String WorkerLocation::href() const
  14. {
  15. // The href getter steps are to return this's WorkerGlobalScope object's url, serialized.
  16. return MUST(String::from_byte_string(m_global_scope->url().serialize()));
  17. }
  18. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-origin
  19. String WorkerLocation::origin() const
  20. {
  21. // The origin getter steps are to return the serialization of this's WorkerGlobalScope object's url's origin.
  22. return m_global_scope->url().origin().serialize();
  23. }
  24. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-protocol
  25. String WorkerLocation::protocol() const
  26. {
  27. // The protocol getter steps are to return this's WorkerGlobalScope object's url's scheme, followed by ":".
  28. return MUST(String::formatted("{}:", m_global_scope->url().scheme()));
  29. }
  30. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-host
  31. String WorkerLocation::host() const
  32. {
  33. // The host getter steps are:
  34. // 1. Let url be this's WorkerGlobalScope object's url.
  35. auto const& url = m_global_scope->url();
  36. // 2. If url's host is null, return the empty string.
  37. if (!url.host().has_value())
  38. return String {};
  39. // 3. If url's port is null, return url's host, serialized.
  40. if (!url.port().has_value())
  41. return url.serialized_host();
  42. // 4. Return url's host, serialized, followed by ":" and url's port, serialized.
  43. return MUST(String::formatted("{}:{}", url.serialized_host(), url.port().value()));
  44. }
  45. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hostname
  46. String WorkerLocation::hostname() const
  47. {
  48. // The hostname getter steps are:
  49. // 1. Let host be this's WorkerGlobalScope object's url's host.
  50. auto const& host = m_global_scope->url().host();
  51. // 2. If host is null, return the empty string.
  52. if (!host.has_value())
  53. return String {};
  54. // 3. Return host, serialized.
  55. return host->serialize();
  56. }
  57. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-port
  58. String WorkerLocation::port() const
  59. {
  60. // The port getter steps are:
  61. // 1. Let port be this's WorkerGlobalScope object's url's port.
  62. auto const& port = m_global_scope->url().port();
  63. // 2. If port is null, return the empty string.
  64. if (!port.has_value())
  65. return String {};
  66. // 3. Return port, serialized.
  67. return String::number(port.value());
  68. }
  69. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-pathname
  70. String WorkerLocation::pathname() const
  71. {
  72. // The pathname getter steps are to return the result of URL path serializing this's WorkerGlobalScope object's url.
  73. return m_global_scope->url().serialize_path();
  74. }
  75. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-search
  76. String WorkerLocation::search() const
  77. {
  78. // The search getter steps are:
  79. // 1. Let query be this's WorkerGlobalScope object's url's query.
  80. auto const& query = m_global_scope->url().query();
  81. // 2. If query is either null or the empty string, return the empty string.
  82. if (!query.has_value() || query->is_empty())
  83. return String {};
  84. // 3. Return "?", followed by query.
  85. return MUST(String::formatted("?{}", *query));
  86. }
  87. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hash
  88. String WorkerLocation::hash() const
  89. {
  90. // The hash getter steps are:
  91. // 1. Let fragment be this's WorkerGlobalScope object's url's fragment.
  92. auto const& fragment = m_global_scope->url().fragment();
  93. // 2. If fragment is either null or the empty string, return the empty string.
  94. if (!fragment.has_value() || fragment->is_empty())
  95. return String {};
  96. // 3. Return "#", followed by fragment.
  97. return MUST(String::formatted("#{}", *fragment));
  98. }
  99. WorkerLocation::WorkerLocation(WorkerGlobalScope& global_scope)
  100. : PlatformObject(global_scope.realm())
  101. , m_global_scope(global_scope)
  102. {
  103. // FIXME: Set prototype once we can get to worker scope prototypes.
  104. }
  105. WorkerLocation::~WorkerLocation() = default;
  106. void WorkerLocation::initialize(JS::Realm& realm)
  107. {
  108. Base::initialize(realm);
  109. WEB_SET_PROTOTYPE_FOR_INTERFACE(WorkerLocation);
  110. }
  111. void WorkerLocation::visit_edges(Cell::Visitor& visitor)
  112. {
  113. Base::visit_edges(visitor);
  114. visitor.visit(m_global_scope);
  115. }
  116. }