WindowOrWorkerGlobalScope.cpp 3.7 KB

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