WorkerGlobalScope.cpp 7.1 KB

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