WorkerLocation.cpp 5.1 KB

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