WorkerLocation.cpp 5.2 KB

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