Serenity Operating System
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
8#include <AK/Base64.h>
9#include <AK/String.h>
10#include <AK/Utf8View.h>
11#include <AK/Vector.h>
12#include <LibTextCodec/Decoder.h>
13#include <LibWeb/Fetch/FetchMethod.h>
14#include <LibWeb/Forward.h>
15#include <LibWeb/HTML/EventLoop/EventLoop.h>
16#include <LibWeb/HTML/Scripting/Environments.h>
17#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
18#include <LibWeb/HTML/StructuredSerialize.h>
19#include <LibWeb/HTML/Window.h>
20#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
21#include <LibWeb/Infra/Base64.h>
22#include <LibWeb/WebIDL/AbstractOperations.h>
23#include <LibWeb/WebIDL/DOMException.h>
24#include <LibWeb/WebIDL/ExceptionOr.h>
25
26namespace Web::HTML {
27
28WindowOrWorkerGlobalScopeMixin::~WindowOrWorkerGlobalScopeMixin() = default;
29
30// https://html.spec.whatwg.org/multipage/webappapis.html#dom-origin
31WebIDL::ExceptionOr<String> WindowOrWorkerGlobalScopeMixin::origin() const
32{
33 auto& vm = this_impl().vm();
34
35 // The origin getter steps are to return this's relevant settings object's origin, serialized.
36 return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(relevant_settings_object(this_impl()).origin().serialize()));
37}
38
39// https://html.spec.whatwg.org/multipage/webappapis.html#dom-issecurecontext
40bool WindowOrWorkerGlobalScopeMixin::is_secure_context() const
41{
42 // The isSecureContext getter steps are to return true if this's relevant settings object is a secure context, or false otherwise.
43 return HTML::is_secure_context(relevant_settings_object(this_impl()));
44}
45
46// https://html.spec.whatwg.org/multipage/webappapis.html#dom-crossoriginisolated
47bool WindowOrWorkerGlobalScopeMixin::cross_origin_isolated() const
48{
49 // The crossOriginIsolated getter steps are to return this's relevant settings object's cross-origin isolated capability.
50 return relevant_settings_object(this_impl()).cross_origin_isolated_capability() == CanUseCrossOriginIsolatedAPIs::Yes;
51}
52
53// https://html.spec.whatwg.org/multipage/webappapis.html#dom-btoa
54WebIDL::ExceptionOr<String> WindowOrWorkerGlobalScopeMixin::btoa(String const& data) const
55{
56 auto& vm = this_impl().vm();
57 auto& realm = *vm.current_realm();
58
59 // The btoa(data) method must throw an "InvalidCharacterError" DOMException if data contains any character whose code point is greater than U+00FF.
60 Vector<u8> byte_string;
61 byte_string.ensure_capacity(data.bytes().size());
62 for (u32 code_point : Utf8View(data)) {
63 if (code_point > 0xff)
64 return WebIDL::InvalidCharacterError::create(realm, "Data contains characters outside the range U+0000 and U+00FF");
65 byte_string.append(code_point);
66 }
67
68 // 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,
69 // and then must apply forgiving-base64 encode to that byte sequence and return the result.
70 return TRY_OR_THROW_OOM(vm, encode_base64(byte_string.span()));
71}
72
73// https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob
74WebIDL::ExceptionOr<String> WindowOrWorkerGlobalScopeMixin::atob(String const& data) const
75{
76 auto& vm = this_impl().vm();
77 auto& realm = *vm.current_realm();
78
79 // 1. Let decodedData be the result of running forgiving-base64 decode on data.
80 auto decoded_data = Infra::decode_forgiving_base64(data.bytes_as_string_view());
81
82 // 2. If decodedData is failure, then throw an "InvalidCharacterError" DOMException.
83 if (decoded_data.is_error())
84 return WebIDL::InvalidCharacterError::create(realm, "Input string is not valid base64 data");
85
86 // 3. Return decodedData.
87 // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
88 auto decoder = TextCodec::decoder_for("windows-1252"sv);
89 VERIFY(decoder.has_value());
90 return TRY_OR_THROW_OOM(vm, decoder->to_utf8(decoded_data.value()));
91}
92
93// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-queuemicrotask
94void WindowOrWorkerGlobalScopeMixin::queue_microtask(WebIDL::CallbackType& callback)
95{
96 auto& vm = this_impl().vm();
97 auto& realm = *vm.current_realm();
98
99 JS::GCPtr<DOM::Document> document;
100 if (is<Window>(this_impl()))
101 document = &static_cast<Window&>(this_impl()).associated_document();
102
103 // The queueMicrotask(callback) method must queue a microtask to invoke callback, and if callback throws an exception, report the exception.
104 HTML::queue_a_microtask(document, [&callback, &realm] {
105 auto result = WebIDL::invoke_callback(callback, {});
106 if (result.is_error())
107 HTML::report_exception(result, realm);
108 });
109}
110
111// https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone
112WebIDL::ExceptionOr<JS::Value> WindowOrWorkerGlobalScopeMixin::structured_clone(JS::Value value, StructuredSerializeOptions const& options) const
113{
114 auto& vm = this_impl().vm();
115 (void)options;
116
117 // 1. Let serialized be ? StructuredSerializeWithTransfer(value, options["transfer"]).
118 // FIXME: Use WithTransfer variant of the AO
119 auto serialized = TRY(structured_serialize(vm, value));
120
121 // 2. Let deserializeRecord be ? StructuredDeserializeWithTransfer(serialized, this's relevant realm).
122 // FIXME: Use WithTransfer variant of the AO
123 auto deserialized = TRY(structured_deserialize(vm, serialized, relevant_realm(this_impl()), {}));
124
125 // 3. Return deserializeRecord.[[Deserialized]].
126 return deserialized;
127}
128
129JS::NonnullGCPtr<JS::Promise> WindowOrWorkerGlobalScopeMixin::fetch(Fetch::RequestInfo const& input, Fetch::RequestInit const& init) const
130{
131 auto& vm = this_impl().vm();
132 return Fetch::fetch(vm, input, init);
133}
134
135}