1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 17:35:08 +00:00

LibWeb: Remove unecessary dependence on Window from Fetch, XHR, FileAPI

These classes only needed Window to get at its realm. Pass a realm
directly to construct Fetch, XMLHttpRequest and FileAPI classes.
This commit is contained in:
Andrew Kaster 2022-09-25 18:08:29 -06:00 committed by Linus Groh
parent 6a10352712
commit 4878a18ee7
17 changed files with 144 additions and 169 deletions

View file

@ -8,15 +8,15 @@
#include <AK/StdLibExtras.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibWeb/Bindings/BlobPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/FileAPI/Blob.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
namespace Web::FileAPI {
JS::NonnullGCPtr<Blob> Blob::create(HTML::Window& window, ByteBuffer byte_buffer, String type)
JS::NonnullGCPtr<Blob> Blob::create(JS::Realm& realm, ByteBuffer byte_buffer, String type)
{
return JS::NonnullGCPtr(*window.heap().allocate<Blob>(window.realm(), window, move(byte_buffer), move(type)));
return JS::NonnullGCPtr(*realm.heap().allocate<Blob>(realm, realm, move(byte_buffer), move(type)));
}
// https://w3c.github.io/FileAPI/#convert-line-endings-to-native
@ -111,40 +111,40 @@ bool is_basic_latin(StringView view)
return true;
}
Blob::Blob(HTML::Window& window)
: PlatformObject(window.realm())
Blob::Blob(JS::Realm& realm)
: PlatformObject(realm)
{
set_prototype(&window.cached_web_prototype("Blob"));
set_prototype(&Bindings::cached_web_prototype(realm, "Blob"));
}
Blob::Blob(HTML::Window& window, ByteBuffer byte_buffer, String type)
: PlatformObject(window.realm())
Blob::Blob(JS::Realm& realm, ByteBuffer byte_buffer, String type)
: PlatformObject(realm)
, m_byte_buffer(move(byte_buffer))
, m_type(move(type))
{
set_prototype(&window.cached_web_prototype("Blob"));
set_prototype(&Bindings::cached_web_prototype(realm, "Blob"));
}
Blob::Blob(HTML::Window& window, ByteBuffer byte_buffer)
: PlatformObject(window.realm())
Blob::Blob(JS::Realm& realm, ByteBuffer byte_buffer)
: PlatformObject(realm)
, m_byte_buffer(move(byte_buffer))
{
set_prototype(&window.cached_web_prototype("Blob"));
set_prototype(&Bindings::cached_web_prototype(realm, "Blob"));
}
Blob::~Blob() = default;
// https://w3c.github.io/FileAPI/#ref-for-dom-blob-blob
WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(HTML::Window& window, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
{
// 1. If invoked with zero parameters, return a new Blob object consisting of 0 bytes, with size set to 0, and with type set to the empty string.
if (!blob_parts.has_value() && !options.has_value())
return JS::NonnullGCPtr(*window.heap().allocate<Blob>(window.realm(), window));
return JS::NonnullGCPtr(*realm.heap().allocate<Blob>(realm, realm));
ByteBuffer byte_buffer {};
// 2. Let bytes be the result of processing blob parts given blobParts and options.
if (blob_parts.has_value()) {
byte_buffer = TRY_OR_RETURN_OOM(window, process_blob_parts(blob_parts.value(), options));
byte_buffer = TRY_OR_RETURN_OOM(realm, process_blob_parts(blob_parts.value(), options));
}
auto type = String::empty();
@ -164,12 +164,12 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create(HTML::Window& window, O
}
// 4. Return a Blob object referring to bytes as its associated byte sequence, with its size set to the length of bytes, and its type set to the value of t from the substeps above.
return JS::NonnullGCPtr(*window.heap().allocate<Blob>(window.realm(), window, move(byte_buffer), move(type)));
return JS::NonnullGCPtr(*realm.heap().allocate<Blob>(realm, realm, move(byte_buffer), move(type)));
}
WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::create_with_global_object(HTML::Window& window, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::construct_impl(JS::Realm& realm, Optional<Vector<BlobPart>> const& blob_parts, Optional<BlobPropertyBag> const& options)
{
return Blob::create(window, blob_parts, options);
return Blob::create(realm, blob_parts, options);
}
// https://w3c.github.io/FileAPI/#dfn-slice
@ -232,8 +232,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> Blob::slice(Optional<i64> start, Opt
// a. S refers to span consecutive bytes from this, beginning with the byte at byte-order position relativeStart.
// b. S.size = span.
// c. S.type = relativeContentType.
auto byte_buffer = TRY_OR_RETURN_OOM(global_object(), m_byte_buffer.slice(relative_start, span));
return JS::NonnullGCPtr(*heap().allocate<Blob>(realm(), global_object(), move(byte_buffer), move(relative_content_type)));
auto byte_buffer = TRY_OR_RETURN_OOM(realm(), m_byte_buffer.slice(relative_start, span));
return JS::NonnullGCPtr(*heap().allocate<Blob>(realm(), realm(), move(byte_buffer), move(relative_content_type)));
}
// https://w3c.github.io/FileAPI/#dom-blob-text