WorkerGlobalScope.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Base64.h>
  7. #include <AK/DeprecatedString.h>
  8. #include <AK/Utf8View.h>
  9. #include <AK/Vector.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibTextCodec/Decoder.h>
  12. #include <LibWeb/Bindings/WorkerGlobalScopePrototype.h>
  13. #include <LibWeb/Forward.h>
  14. #include <LibWeb/HTML/EventHandler.h>
  15. #include <LibWeb/HTML/EventNames.h>
  16. #include <LibWeb/HTML/Window.h>
  17. #include <LibWeb/HTML/WorkerGlobalScope.h>
  18. #include <LibWeb/HTML/WorkerLocation.h>
  19. #include <LibWeb/HTML/WorkerNavigator.h>
  20. #include <LibWeb/WebIDL/DOMException.h>
  21. namespace Web::HTML {
  22. WorkerGlobalScope::WorkerGlobalScope(JS::Realm& realm)
  23. : DOM::EventTarget(realm)
  24. {
  25. }
  26. WorkerGlobalScope::~WorkerGlobalScope() = default;
  27. JS::ThrowCompletionOr<void> WorkerGlobalScope::initialize(JS::Realm& realm)
  28. {
  29. MUST_OR_THROW_OOM(Base::initialize(realm));
  30. m_navigator = WorkerNavigator::create(*this);
  31. return {};
  32. }
  33. void WorkerGlobalScope::visit_edges(Cell::Visitor& visitor)
  34. {
  35. Base::visit_edges(visitor);
  36. visitor.visit(m_location.ptr());
  37. visitor.visit(m_navigator.ptr());
  38. }
  39. // https://html.spec.whatwg.org/multipage/workers.html#importing-scripts-and-libraries
  40. WebIDL::ExceptionOr<void> WorkerGlobalScope::import_scripts(Vector<DeprecatedString> urls)
  41. {
  42. // The algorithm may optionally be customized by supplying custom perform the fetch hooks,
  43. // which if provided will be used when invoking fetch a classic worker-imported script.
  44. // NOTE: Service Workers is an example of a specification that runs this algorithm with its own options for the perform the fetch hook.
  45. // FIXME: 1. If worker global scope's type is "module", throw a TypeError exception.
  46. // FIXME: 2. Let settings object be the current settings object.
  47. // 3. If urls is empty, return.
  48. if (urls.is_empty())
  49. return {};
  50. // FIXME: 4. Parse each value in urls relative to settings object. If any fail, throw a "SyntaxError" DOMException.
  51. // FIXME: 5. For each url in the resulting URL records, run these substeps:
  52. // 1. Fetch a classic worker-imported script given url and settings object, passing along any custom perform the fetch steps provided.
  53. // If this succeeds, let script be the result. Otherwise, rethrow the exception.
  54. // 2. Run the classic script script, with the rethrow errors argument set to true.
  55. // NOTE: script will run until it either returns, fails to parse, fails to catch an exception,
  56. // or gets prematurely aborted by the terminate a worker algorithm defined above.
  57. // If an exception was thrown or if the script was prematurely aborted, then abort all these steps,
  58. // letting the exception or aborting continue to be processed by the calling script.
  59. return {};
  60. }
  61. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-location
  62. JS::NonnullGCPtr<WorkerLocation> WorkerGlobalScope::location() const
  63. {
  64. // The location attribute must return the WorkerLocation object whose associated WorkerGlobalScope object is the WorkerGlobalScope object.
  65. return *m_location;
  66. }
  67. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-navigator
  68. JS::NonnullGCPtr<WorkerNavigator> WorkerGlobalScope::navigator() const
  69. {
  70. // The navigator attribute of the WorkerGlobalScope interface must return an instance of the WorkerNavigator interface,
  71. // which represents the identity and state of the user agent (the client).
  72. return *m_navigator;
  73. }
  74. #undef __ENUMERATE
  75. #define __ENUMERATE(attribute_name, event_name) \
  76. void WorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \
  77. { \
  78. set_event_handler_attribute(event_name, move(value)); \
  79. } \
  80. WebIDL::CallbackType* WorkerGlobalScope::attribute_name() \
  81. { \
  82. return event_handler_attribute(event_name); \
  83. }
  84. ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
  85. #undef __ENUMERATE
  86. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-origin
  87. DeprecatedString WorkerGlobalScope::origin() const
  88. {
  89. // FIXME: The origin getter steps are to return this's relevant settings object's origin, serialized.
  90. return {};
  91. }
  92. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-issecurecontext
  93. bool WorkerGlobalScope::is_secure_context() const
  94. {
  95. // FIXME: The isSecureContext getter steps are to return true if this's relevant settings object is a secure context, or false otherwise.
  96. return false;
  97. }
  98. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-crossoriginisolated
  99. bool WorkerGlobalScope::cross_origin_isolated() const
  100. {
  101. // The crossOriginIsolated getter steps are to return this's relevant settings object's cross-origin isolated capability.
  102. // FIXME: Is this the same thing as https://html.spec.whatwg.org/multipage/workers.html#concept-workerglobalscope-cross-origin-isolated-capability?
  103. // "A WorkerGlobalScope object has an associated cross-origin isolated capability boolean. It is initially false."
  104. return m_cross_origin_isolated_capability;
  105. }
  106. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-btoa
  107. WebIDL::ExceptionOr<DeprecatedString> WorkerGlobalScope::btoa(DeprecatedString const& data) const
  108. {
  109. // FIXME: This is the same as the implementation in Bindings/WindowObject.cpp
  110. // Find a way to share this implementation, since they come from the same mixin.
  111. // The btoa(data) method must throw an "InvalidCharacterError" DOMException if data contains any character whose code point is greater than U+00FF.
  112. Vector<u8> byte_string;
  113. byte_string.ensure_capacity(data.length());
  114. for (u32 code_point : Utf8View(data)) {
  115. if (code_point > 0xff)
  116. return WebIDL::InvalidCharacterError::create(realm(), "Data contains characters outside the range U+0000 and U+00FF");
  117. byte_string.append(code_point);
  118. }
  119. // Otherwise, the user agent must convert data to a byte sequence whose nth byte is the eight-bit representation of the nth code point of data,
  120. // and then must apply forgiving-base64 encode to that byte sequence and return the result.
  121. return TRY_OR_THROW_OOM(vm(), encode_base64(byte_string.span())).to_deprecated_string();
  122. }
  123. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob
  124. WebIDL::ExceptionOr<DeprecatedString> WorkerGlobalScope::atob(DeprecatedString const& data) const
  125. {
  126. // FIXME: This is the same as the implementation in Bindings/WindowObject.cpp
  127. // Find a way to share this implementation, since they come from the same mixin.
  128. // 1. Let decodedData be the result of running forgiving-base64 decode on data.
  129. auto decoded_data = decode_base64(data.view());
  130. // 2. If decodedData is failure, then throw an "InvalidCharacterError" DOMException.
  131. if (decoded_data.is_error())
  132. return WebIDL::InvalidCharacterError::create(realm(), "Input string is not valid base64 data");
  133. // 3. Return decodedData.
  134. // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
  135. auto* decoder = TextCodec::decoder_for("windows-1252"sv);
  136. VERIFY(decoder);
  137. return decoder->to_utf8(decoded_data.value());
  138. }
  139. }