WorkerLocation.cpp 5.2 KB

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