WindowOrWorkerGlobalScope.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/Forward.h>
  13. #include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
  14. #include <LibWeb/WebIDL/DOMException.h>
  15. #include <LibWeb/WebIDL/ExceptionOr.h>
  16. namespace Web::HTML {
  17. WindowOrWorkerGlobalScopeMixin::~WindowOrWorkerGlobalScopeMixin() = default;
  18. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-origin
  19. WebIDL::ExceptionOr<String> WindowOrWorkerGlobalScopeMixin::origin() const
  20. {
  21. auto& vm = this_impl().vm();
  22. // The origin getter steps are to return this's relevant settings object's origin, serialized.
  23. return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(relevant_settings_object(this_impl()).origin().serialize()));
  24. }
  25. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-issecurecontext
  26. bool WindowOrWorkerGlobalScopeMixin::is_secure_context() const
  27. {
  28. // The isSecureContext getter steps are to return true if this's relevant settings object is a secure context, or false otherwise.
  29. return HTML::is_secure_context(relevant_settings_object(this_impl()));
  30. }
  31. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-crossoriginisolated
  32. bool WindowOrWorkerGlobalScopeMixin::cross_origin_isolated() const
  33. {
  34. // The crossOriginIsolated getter steps are to return this's relevant settings object's cross-origin isolated capability.
  35. return relevant_settings_object(this_impl()).cross_origin_isolated_capability() == CanUseCrossOriginIsolatedAPIs::Yes;
  36. }
  37. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-btoa
  38. WebIDL::ExceptionOr<String> WindowOrWorkerGlobalScopeMixin::btoa(String const& data) const
  39. {
  40. auto& vm = this_impl().vm();
  41. auto& realm = *vm.current_realm();
  42. // The btoa(data) method must throw an "InvalidCharacterError" DOMException if data contains any character whose code point is greater than U+00FF.
  43. Vector<u8> byte_string;
  44. byte_string.ensure_capacity(data.bytes().size());
  45. for (u32 code_point : Utf8View(data)) {
  46. if (code_point > 0xff)
  47. return WebIDL::InvalidCharacterError::create(realm, "Data contains characters outside the range U+0000 and U+00FF");
  48. byte_string.append(code_point);
  49. }
  50. // 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,
  51. // and then must apply forgiving-base64 encode to that byte sequence and return the result.
  52. return TRY_OR_THROW_OOM(vm, encode_base64(byte_string.span()));
  53. }
  54. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob
  55. WebIDL::ExceptionOr<String> WindowOrWorkerGlobalScopeMixin::atob(String const& data) const
  56. {
  57. auto& vm = this_impl().vm();
  58. auto& realm = *vm.current_realm();
  59. // 1. Let decodedData be the result of running forgiving-base64 decode on data.
  60. auto decoded_data = decode_base64(data.bytes_as_string_view());
  61. // 2. If decodedData is failure, then throw an "InvalidCharacterError" DOMException.
  62. if (decoded_data.is_error())
  63. return WebIDL::InvalidCharacterError::create(realm, "Input string is not valid base64 data");
  64. // 3. Return decodedData.
  65. // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
  66. auto decoder = TextCodec::decoder_for("windows-1252"sv);
  67. VERIFY(decoder.has_value());
  68. return TRY_OR_THROW_OOM(vm, decoder->to_utf8(decoded_data.value()));
  69. }
  70. }