WindowOrWorkerGlobalScope.cpp 5.7 KB

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