WorkerGlobalScope.cpp 7.8 KB

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