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