UniversalGlobalScope.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <LibJS/Heap/HeapFunction.h>
  14. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  15. #include <LibWeb/HTML/StructuredSerialize.h>
  16. #include <LibWeb/HTML/StructuredSerializeOptions.h>
  17. #include <LibWeb/HTML/UniversalGlobalScope.h>
  18. #include <LibWeb/HTML/Window.h>
  19. #include <LibWeb/Infra/Strings.h>
  20. #include <LibWeb/WebIDL/AbstractOperations.h>
  21. #include <LibWeb/WebIDL/DOMException.h>
  22. #include <LibWeb/WebIDL/ExceptionOr.h>
  23. #include <LibWeb/WebIDL/Types.h>
  24. namespace Web::HTML {
  25. UniversalGlobalScopeMixin::~UniversalGlobalScopeMixin() = default;
  26. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-btoa
  27. WebIDL::ExceptionOr<String> UniversalGlobalScopeMixin::btoa(String const& data) const
  28. {
  29. auto& vm = this_impl().vm();
  30. auto& realm = *vm.current_realm();
  31. // The btoa(data) method must throw an "InvalidCharacterError" DOMException if data contains any character whose code point is greater than U+00FF.
  32. Vector<u8> byte_string;
  33. byte_string.ensure_capacity(data.bytes().size());
  34. for (u32 code_point : Utf8View(data)) {
  35. if (code_point > 0xff)
  36. return WebIDL::InvalidCharacterError::create(realm, "Data contains characters outside the range U+0000 and U+00FF"_string);
  37. byte_string.append(code_point);
  38. }
  39. // 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,
  40. // and then must apply forgiving-base64 encode to that byte sequence and return the result.
  41. return TRY_OR_THROW_OOM(vm, encode_base64(byte_string.span()));
  42. }
  43. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob
  44. WebIDL::ExceptionOr<String> UniversalGlobalScopeMixin::atob(String const& data) const
  45. {
  46. auto& vm = this_impl().vm();
  47. auto& realm = *vm.current_realm();
  48. // 1. Let decodedData be the result of running forgiving-base64 decode on data.
  49. auto decoded_data = decode_base64(data);
  50. // 2. If decodedData is failure, then throw an "InvalidCharacterError" DOMException.
  51. if (decoded_data.is_error())
  52. return WebIDL::InvalidCharacterError::create(realm, "Input string is not valid base64 data"_string);
  53. // 3. Return decodedData.
  54. // decode_base64() returns a byte buffer. LibJS uses UTF-8 for strings. Use isomorphic decoding to convert bytes to UTF-8.
  55. return Infra::isomorphic_decode(decoded_data.value());
  56. }
  57. // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-queuemicrotask
  58. void UniversalGlobalScopeMixin::queue_microtask(WebIDL::CallbackType& callback)
  59. {
  60. auto& vm = this_impl().vm();
  61. auto& realm = *vm.current_realm();
  62. JS::GCPtr<DOM::Document> document;
  63. if (is<Window>(this_impl()))
  64. document = &static_cast<Window&>(this_impl()).associated_document();
  65. // The queueMicrotask(callback) method must queue a microtask to invoke callback, and if callback throws an exception, report the exception.
  66. HTML::queue_a_microtask(document, JS::create_heap_function(realm.heap(), [&callback, &realm] {
  67. auto result = WebIDL::invoke_callback(callback, {});
  68. if (result.is_error())
  69. HTML::report_exception(result, realm);
  70. }));
  71. }
  72. // https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone
  73. WebIDL::ExceptionOr<JS::Value> UniversalGlobalScopeMixin::structured_clone(JS::Value value, StructuredSerializeOptions const& options) const
  74. {
  75. auto& vm = this_impl().vm();
  76. (void)options;
  77. // 1. Let serialized be ? StructuredSerializeWithTransfer(value, options["transfer"]).
  78. // FIXME: Use WithTransfer variant of the AO
  79. auto serialized = TRY(structured_serialize(vm, value));
  80. // 2. Let deserializeRecord be ? StructuredDeserializeWithTransfer(serialized, this's relevant realm).
  81. // FIXME: Use WithTransfer variant of the AO
  82. auto deserialized = TRY(structured_deserialize(vm, serialized, relevant_realm(this_impl()), {}));
  83. // 3. Return deserializeRecord.[[Deserialized]].
  84. return deserialized;
  85. }
  86. }