UniversalGlobalScope.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  5. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/Base64.h>
  10. #include <AK/String.h>
  11. #include <AK/Utf8View.h>
  12. #include <AK/Vector.h>
  13. #include <LibGC/Function.h>
  14. #include <LibJS/Runtime/NativeFunction.h>
  15. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  16. #include <LibWeb/HTML/StructuredSerialize.h>
  17. #include <LibWeb/HTML/StructuredSerializeOptions.h>
  18. #include <LibWeb/HTML/UniversalGlobalScope.h>
  19. #include <LibWeb/HTML/Window.h>
  20. #include <LibWeb/Infra/Strings.h>
  21. #include <LibWeb/WebIDL/AbstractOperations.h>
  22. #include <LibWeb/WebIDL/DOMException.h>
  23. #include <LibWeb/WebIDL/ExceptionOr.h>
  24. #include <LibWeb/WebIDL/Types.h>
  25. namespace Web::HTML {
  26. UniversalGlobalScopeMixin::~UniversalGlobalScopeMixin() = default;
  27. void UniversalGlobalScopeMixin::visit_edges(GC::Cell::Visitor& visitor)
  28. {
  29. visitor.visit(m_count_queuing_strategy_size_function);
  30. visitor.visit(m_byte_length_queuing_strategy_size_function);
  31. }
  32. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-btoa
  33. WebIDL::ExceptionOr<String> UniversalGlobalScopeMixin::btoa(String const& data) const
  34. {
  35. auto& vm = this_impl().vm();
  36. auto& realm = *vm.current_realm();
  37. // The btoa(data) method must throw an "InvalidCharacterError" DOMException if data contains any character whose code point is greater than U+00FF.
  38. Vector<u8> byte_string;
  39. byte_string.ensure_capacity(data.bytes().size());
  40. for (u32 code_point : Utf8View(data)) {
  41. if (code_point > 0xff)
  42. return WebIDL::InvalidCharacterError::create(realm, "Data contains characters outside the range U+0000 and U+00FF"_string);
  43. byte_string.append(code_point);
  44. }
  45. // 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,
  46. // and then must apply forgiving-base64 encode to that byte sequence and return the result.
  47. return TRY_OR_THROW_OOM(vm, encode_base64(byte_string.span()));
  48. }
  49. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob
  50. WebIDL::ExceptionOr<String> UniversalGlobalScopeMixin::atob(String const& data) const
  51. {
  52. auto& vm = this_impl().vm();
  53. auto& realm = *vm.current_realm();
  54. // 1. Let decodedData be the result of running forgiving-base64 decode on data.
  55. auto decoded_data = decode_base64(data);
  56. // 2. If decodedData is failure, then throw an "InvalidCharacterError" DOMException.
  57. if (decoded_data.is_error())
  58. return WebIDL::InvalidCharacterError::create(realm, "Input string is not valid base64 data"_string);
  59. // 3. Return decodedData.
  60. // decode_base64() returns a byte buffer. LibJS uses UTF-8 for strings. Use isomorphic decoding to convert bytes to UTF-8.
  61. return Infra::isomorphic_decode(decoded_data.value());
  62. }
  63. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-queuemicrotask
  64. void UniversalGlobalScopeMixin::queue_microtask(WebIDL::CallbackType& callback)
  65. {
  66. auto& vm = this_impl().vm();
  67. auto& realm = *vm.current_realm();
  68. GC::Ptr<DOM::Document> document;
  69. if (is<Window>(this_impl()))
  70. document = &static_cast<Window&>(this_impl()).associated_document();
  71. // The queueMicrotask(callback) method must queue a microtask to invoke callback, and if callback throws an exception, report the exception.
  72. HTML::queue_a_microtask(document, GC::create_function(realm.heap(), [&callback, &realm] {
  73. auto result = WebIDL::invoke_callback(callback, {});
  74. if (result.is_error())
  75. HTML::report_exception(result, realm);
  76. }));
  77. }
  78. // https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone
  79. WebIDL::ExceptionOr<JS::Value> UniversalGlobalScopeMixin::structured_clone(JS::Value value, StructuredSerializeOptions const& options) const
  80. {
  81. auto& vm = this_impl().vm();
  82. (void)options;
  83. // 1. Let serialized be ? StructuredSerializeWithTransfer(value, options["transfer"]).
  84. // FIXME: Use WithTransfer variant of the AO
  85. auto serialized = TRY(structured_serialize(vm, value));
  86. // 2. Let deserializeRecord be ? StructuredDeserializeWithTransfer(serialized, this's relevant realm).
  87. // FIXME: Use WithTransfer variant of the AO
  88. auto deserialized = TRY(structured_deserialize(vm, serialized, relevant_realm(this_impl()), {}));
  89. // 3. Return deserializeRecord.[[Deserialized]].
  90. return deserialized;
  91. }
  92. // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
  93. GC::Ref<WebIDL::CallbackType> UniversalGlobalScopeMixin::count_queuing_strategy_size_function()
  94. {
  95. auto& realm = HTML::relevant_realm(this_impl());
  96. if (!m_count_queuing_strategy_size_function) {
  97. // 1. Let steps be the following steps:
  98. auto steps = [](auto const&) {
  99. // 1. Return 1.
  100. return 1.0;
  101. };
  102. // 2. Let F be ! CreateBuiltinFunction(steps, 0, "size", « », globalObject’s relevant Realm).
  103. auto function = JS::NativeFunction::create(realm, move(steps), 0, "size", &realm);
  104. // 3. Set globalObject’s count queuing strategy size function to a Function that represents a reference to F, with callback context equal to globalObject’s relevant settings object.
  105. // FIXME: Update spec comment to pass globalObject's relevant realm once Streams spec is updated for ShadowRealm spec
  106. m_count_queuing_strategy_size_function = realm.create<WebIDL::CallbackType>(*function, realm);
  107. }
  108. return GC::Ref { *m_count_queuing_strategy_size_function };
  109. }
  110. // https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function
  111. GC::Ref<WebIDL::CallbackType> UniversalGlobalScopeMixin::byte_length_queuing_strategy_size_function()
  112. {
  113. auto& realm = HTML::relevant_realm(this_impl());
  114. if (!m_byte_length_queuing_strategy_size_function) {
  115. // 1. Let steps be the following steps, given chunk:
  116. auto steps = [](JS::VM& vm) {
  117. auto chunk = vm.argument(0);
  118. // 1. Return ? GetV(chunk, "byteLength").
  119. return chunk.get(vm, vm.names.byteLength);
  120. };
  121. // 2. Let F be ! CreateBuiltinFunction(steps, 1, "size", « », globalObject’s relevant Realm).
  122. auto function = JS::NativeFunction::create(realm, move(steps), 1, "size", &realm);
  123. // 3. Set globalObject’s byte length queuing strategy size function to a Function that represents a reference to F, with callback context equal to globalObject’s relevant settings object.
  124. // FIXME: Update spec comment to pass globalObject's relevant realm once Streams spec is updated for ShadowRealm spec
  125. m_byte_length_queuing_strategy_size_function = realm.create<WebIDL::CallbackType>(*function, realm);
  126. }
  127. return GC::Ref { *m_byte_length_queuing_strategy_size_function };
  128. }
  129. }