1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-21 15:25:07 +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

@ -11,7 +11,6 @@
#include <LibWeb/Fetch/Body.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
#include <LibWeb/FileAPI/Blob.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Infra/JSON.h>
#include <LibWeb/MimeSniff/MimeType.h>
#include <LibWeb/Streams/ReadableStream.h>
@ -99,7 +98,6 @@ JS::NonnullGCPtr<JS::Promise> BodyMixin::text() const
WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes, PackageDataType type, Optional<MimeSniff::MimeType> const& mime_type)
{
auto& vm = realm.vm();
auto& window = verify_cast<HTML::Window>(realm.global_object());
switch (type) {
case PackageDataType::ArrayBuffer:
@ -109,7 +107,7 @@ WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes,
// Return a Blob whose contents are bytes and type attribute is mimeType.
// NOTE: If extracting the mime type returns failure, other browsers set it to an empty string - not sure if that's spec'd.
auto mime_type_string = mime_type.has_value() ? mime_type->serialized() : String::empty();
return FileAPI::Blob::create(window, move(bytes), move(mime_type_string));
return FileAPI::Blob::create(realm, move(bytes), move(mime_type_string));
}
case PackageDataType::FormData:
// If mimeTypes essence is "multipart/form-data", then:

View file

@ -17,14 +17,12 @@ namespace Web::Fetch {
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInitOrReadbleBytes const& object, bool keepalive)
{
auto& window = verify_cast<HTML::Window>(realm.global_object());
// 1. Let stream be object if object is a ReadableStream object. Otherwise, let stream be a new ReadableStream, and set up stream.
Streams::ReadableStream* stream;
if (auto const* handle = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
stream = const_cast<Streams::ReadableStream*>(handle->cell());
} else {
stream = realm.heap().allocate<Streams::ReadableStream>(realm, window);
stream = realm.heap().allocate<Streams::ReadableStream>(realm, verify_cast<HTML::Window>(realm.global_object()));
}
// FIXME: 2. Let action be null.
@ -51,19 +49,19 @@ WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm,
},
[&](ReadonlyBytes bytes) -> WebIDL::ExceptionOr<void> {
// Set source to object.
source = TRY_OR_RETURN_OOM(window, ByteBuffer::copy(bytes));
source = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(bytes));
return {};
},
[&](JS::Handle<JS::Object> const& buffer_source) -> WebIDL::ExceptionOr<void> {
// Set source to a copy of the bytes held by object.
source = TRY_OR_RETURN_OOM(window, WebIDL::get_buffer_source_copy(*buffer_source.cell()));
source = TRY_OR_RETURN_OOM(realm, WebIDL::get_buffer_source_copy(*buffer_source.cell()));
return {};
},
[&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> WebIDL::ExceptionOr<void> {
// Set source to the result of running the application/x-www-form-urlencoded serializer with objects list.
source = url_search_params->to_string().to_byte_buffer();
// Set type to `application/x-www-form-urlencoded;charset=UTF-8`.
type = TRY_OR_RETURN_OOM(window, ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
type = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
return {};
},
[&](String const& scalar_value_string) -> WebIDL::ExceptionOr<void> {
@ -71,7 +69,7 @@ WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm,
// Set source to the UTF-8 encoding of object.
source = scalar_value_string.to_byte_buffer();
// Set type to `text/plain;charset=UTF-8`.
type = TRY_OR_RETURN_OOM(window, ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
type = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
return {};
},
[&](JS::Handle<Streams::ReadableStream> const& stream) -> WebIDL::ExceptionOr<void> {

View file

@ -5,16 +5,16 @@
*/
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Fetch/Headers.h>
#include <LibWeb/HTML/Window.h>
namespace Web::Fetch {
// https://fetch.spec.whatwg.org/#dom-headers
WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> Headers::create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init)
WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> Headers::construct_impl(JS::Realm& realm, Optional<HeadersInit> const& init)
{
// The new Headers(init) constructor steps are:
auto* headers = window.heap().allocate<Headers>(window.realm(), window);
auto* headers = realm.heap().allocate<Headers>(realm, realm);
// 1. Set thiss guard to "none".
headers->m_guard = Guard::None;
@ -26,11 +26,11 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> Headers::create_with_global_objec
return JS::NonnullGCPtr(*headers);
}
Headers::Headers(HTML::Window& window)
: PlatformObject(window.realm())
Headers::Headers(JS::Realm& realm)
: PlatformObject(realm)
, m_header_list(make_ref_counted<Infrastructure::HeaderList>())
{
set_prototype(&window.cached_web_prototype("Headers"));
set_prototype(&Bindings::cached_web_prototype(realm, "Headers"));
}
Headers::~Headers() = default;
@ -40,8 +40,8 @@ WebIDL::ExceptionOr<void> Headers::append(String const& name_string, String cons
{
// The append(name, value) method steps are to append (name, value) to this.
auto header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(name_string.bytes())),
.value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(value_string.bytes())),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(name_string.bytes())),
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(value_string.bytes())),
};
TRY(append(move(header)));
return {};
@ -98,7 +98,7 @@ WebIDL::ExceptionOr<String> Headers::get(String const& name_string)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
// 2. Return the result of getting name from thiss header list.
auto byte_buffer = TRY_OR_RETURN_OOM(global_object(), m_header_list->get(name));
auto byte_buffer = TRY_OR_RETURN_OOM(realm(), m_header_list->get(name));
// FIXME: Teach BindingsGenerator about Optional<String>
return byte_buffer.has_value() ? String { byte_buffer->span() } : String {};
}
@ -125,10 +125,10 @@ WebIDL::ExceptionOr<void> Headers::set(String const& name_string, String const&
auto value = value_string.bytes();
// 1. Normalize value.
auto normalized_value = TRY_OR_RETURN_OOM(global_object(), Infrastructure::normalize_header_value(value));
auto normalized_value = TRY_OR_RETURN_OOM(realm(), Infrastructure::normalize_header_value(value));
auto header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(name)),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(name)),
.value = move(normalized_value),
};
@ -155,7 +155,7 @@ WebIDL::ExceptionOr<void> Headers::set(String const& name_string, String const&
return {};
// 7. Set (name, value) in thiss header list.
TRY_OR_RETURN_OOM(global_object(), m_header_list->set(move(header)));
TRY_OR_RETURN_OOM(realm(), m_header_list->set(move(header)));
// 8. If thiss guard is "request-no-cors", then remove privileged no-CORS request headers from this.
if (m_guard == Guard::RequestNoCORS)
@ -208,7 +208,7 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
auto& [name, value] = header;
// 1. Normalize value.
value = TRY_OR_RETURN_OOM(global_object(), Infrastructure::normalize_header_value(value));
value = TRY_OR_RETURN_OOM(realm(), Infrastructure::normalize_header_value(value));
// 2. If name is not a header name or value is not a header value, then throw a TypeError.
if (!Infrastructure::is_header_name(name))
@ -227,21 +227,21 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
// 5. Otherwise, if headerss guard is "request-no-cors":
if (m_guard == Guard::RequestNoCORS) {
// 1. Let temporaryValue be the result of getting name from headerss header list.
auto temporary_value = TRY_OR_RETURN_OOM(global_object(), m_header_list->get(name));
auto temporary_value = TRY_OR_RETURN_OOM(realm(), m_header_list->get(name));
// 2. If temporaryValue is null, then set temporaryValue to value.
if (!temporary_value.has_value()) {
temporary_value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(value));
temporary_value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(value));
}
// 3. Otherwise, set temporaryValue to temporaryValue, followed by 0x2C 0x20, followed by value.
else {
TRY_OR_RETURN_OOM(global_object(), temporary_value->try_append(0x2c));
TRY_OR_RETURN_OOM(global_object(), temporary_value->try_append(0x20));
TRY_OR_RETURN_OOM(global_object(), temporary_value->try_append(value));
TRY_OR_RETURN_OOM(realm(), temporary_value->try_append(0x2c));
TRY_OR_RETURN_OOM(realm(), temporary_value->try_append(0x20));
TRY_OR_RETURN_OOM(realm(), temporary_value->try_append(value));
}
auto temporary_header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(name)),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(name)),
.value = temporary_value.release_value(),
};
@ -254,8 +254,8 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
if (m_guard == Guard::Response && Infrastructure::is_forbidden_response_header_name(name))
return {};
// 7. Append (name, value) to headerss header list
TRY_OR_RETURN_OOM(global_object(), m_header_list->append(move(header)));
// 7. Append (name, value) to headerss header list.
TRY_OR_RETURN_OOM(realm(), m_header_list->append(move(header)));
// 8. If headerss guard is "request-no-cors", then remove privileged no-CORS request headers from headers.
if (m_guard == Guard::RequestNoCORS)
@ -278,8 +278,8 @@ WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
// 2. Append (headers first item, headers second item) to headers.
auto header = Fetch::Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry[0].bytes())),
.value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry[1].bytes())),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry[0].bytes())),
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry[1].bytes())),
};
TRY(append(move(header)));
}
@ -289,8 +289,8 @@ WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
[this](OrderedHashMap<String, String> const& object) -> WebIDL::ExceptionOr<void> {
for (auto const& entry : object) {
auto header = Fetch::Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry.key.bytes())),
.value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry.value.bytes())),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry.key.bytes())),
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry.value.bytes())),
};
TRY(append(move(header)));
}

View file

@ -31,7 +31,7 @@ public:
None,
};
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> construct_impl(JS::Realm& realm, Optional<HeadersInit> const& init);
virtual ~Headers() override;
@ -58,7 +58,7 @@ public:
private:
friend class HeadersIterator;
explicit Headers(HTML::Window&);
explicit Headers(JS::Realm&);
void remove_privileged_no_cors_headers();

View file

@ -7,8 +7,8 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibWeb/Bindings/HeadersIteratorPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Fetch/HeadersIterator.h>
#include <LibWeb/HTML/Window.h>
namespace Web::Fetch {
@ -22,7 +22,7 @@ HeadersIterator::HeadersIterator(Headers const& headers, JS::Object::PropertyKin
, m_headers(headers)
, m_iteration_kind(iteration_kind)
{
set_prototype(&headers.global_object().ensure_web_prototype<Bindings::HeadersIteratorPrototype>("HeadersIterator"));
set_prototype(&Bindings::ensure_web_prototype<Bindings::HeadersIteratorPrototype>(headers.realm(), "HeadersIterator"));
}
HeadersIterator::~HeadersIterator() = default;

View file

@ -6,6 +6,7 @@
#include <AK/ScopeGuard.h>
#include <AK/URLParser.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/DOM/AbortSignal.h>
#include <LibWeb/Fetch/Enums.h>
#include <LibWeb/Fetch/Headers.h>
@ -15,7 +16,6 @@
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Request.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
namespace Web::Fetch {
@ -24,8 +24,7 @@ Request::Request(JS::Realm& realm, NonnullOwnPtr<Infrastructure::Request> reques
: PlatformObject(realm)
, m_request(move(request))
{
auto& window = verify_cast<HTML::Window>(realm.global_object());
set_prototype(&window.cached_web_prototype("Request"));
set_prototype(&Bindings::cached_web_prototype(realm, "Request"));
}
Request::~Request() = default;
@ -75,8 +74,6 @@ Optional<Infrastructure::Body&> Request::body_impl()
// https://fetch.spec.whatwg.org/#request-create
JS::NonnullGCPtr<Request> Request::create(NonnullOwnPtr<Infrastructure::Request> request, Headers::Guard guard, JS::Realm& realm)
{
auto& window = verify_cast<HTML::Window>(realm.global_object());
// Copy a NonnullRefPtr to the request's header list before request is being move()'d.
auto request_reader_list = request->header_list();
@ -85,27 +82,22 @@ JS::NonnullGCPtr<Request> Request::create(NonnullOwnPtr<Infrastructure::Request>
auto* request_object = realm.heap().allocate<Request>(realm, realm, move(request));
// 3. Set requestObjects headers to a new Headers object with realm, whose headers list is requests headers list and guard is guard.
request_object->m_headers = realm.heap().allocate<Headers>(realm, window);
request_object->m_headers = realm.heap().allocate<Headers>(realm, realm);
request_object->m_headers->set_header_list(move(request_reader_list));
request_object->m_headers->set_guard(guard);
// 4. Set requestObjects signal to a new AbortSignal object with realm.
request_object->m_signal = realm.heap().allocate<DOM::AbortSignal>(realm, window);
request_object->m_signal = realm.heap().allocate<DOM::AbortSignal>(realm, realm);
// 5. Return requestObject.
return JS::NonnullGCPtr { *request_object };
}
// https://fetch.spec.whatwg.org/#dom-request
WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::create_with_global_object(HTML::Window& html_window, RequestInfo const& input, RequestInit const& init)
WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::construct_impl(JS::Realm& realm, RequestInfo const& input, RequestInit const& init)
{
auto& realm = html_window.realm();
// Referred to as 'this' in the spec.
auto request_object = [&] {
auto request = adopt_own(*new Infrastructure::Request());
return JS::NonnullGCPtr { *realm.heap().allocate<Request>(realm, realm, move(request)) };
}();
auto request_object = JS::NonnullGCPtr { *realm.heap().allocate<Request>(realm, realm, make<Infrastructure::Request>()) };
// 1. Let request be null.
Infrastructure::Request const* input_request = nullptr;
@ -191,13 +183,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::create_with_global_objec
// method
// requests method.
request.set_method(TRY_OR_RETURN_OOM(html_window, ByteBuffer::copy(request.method())));
request.set_method(TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(request.method())));
// header list
// A copy of requests header list.
auto header_list_copy = make_ref_counted<Infrastructure::HeaderList>();
for (auto& header : *request.header_list())
TRY_OR_RETURN_OOM(html_window, header_list_copy->append(header));
TRY_OR_RETURN_OOM(realm, header_list_copy->append(header));
request.set_header_list(move(header_list_copy));
// unsafe-request flag
@ -382,7 +374,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::create_with_global_objec
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method must not be one of CONNECT, TRACE, or TRACK"sv };
// 3. Normalize method.
method = TRY_OR_RETURN_OOM(html_window, Infrastructure::normalize_method(method.bytes()));
method = TRY_OR_RETURN_OOM(realm, Infrastructure::normalize_method(method.bytes()));
// 4. Set requests method to method.
request.set_method(MUST(ByteBuffer::copy(method.bytes())));
@ -397,7 +389,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::create_with_global_objec
// cannot exist with a null Infrastructure::Request.
// 28. Set thiss signal to a new AbortSignal object with thiss relevant Realm.
request_object->m_signal = realm.heap().allocate<DOM::AbortSignal>(HTML::relevant_realm(*request_object), html_window);
auto& this_relevant_realm = HTML::relevant_realm(*request_object);
request_object->m_signal = realm.heap().allocate<DOM::AbortSignal>(this_relevant_realm, this_relevant_realm);
// 29. If signal is not null, then make thiss signal follow signal.
if (input_signal != nullptr) {
@ -405,7 +398,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::create_with_global_objec
}
// 30. Set thiss headers to a new Headers object with thiss relevant Realm, whose header list is requests header list and guard is "request".
request_object->m_headers = realm.heap().allocate<Headers>(realm, html_window);
request_object->m_headers = realm.heap().allocate<Headers>(realm, realm);
request_object->m_headers->set_header_list(request.header_list());
request_object->m_headers->set_guard(Headers::Guard::Request);

View file

@ -64,7 +64,7 @@ class Request final
public:
static JS::NonnullGCPtr<Request> create(NonnullOwnPtr<Infrastructure::Request>, Headers::Guard, JS::Realm&);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> create_with_global_object(HTML::Window&, RequestInfo const& input, RequestInit const& init = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> construct_impl(JS::Realm&, RequestInfo const& input, RequestInit const& init = {});
virtual ~Request() override;

View file

@ -5,6 +5,7 @@
*/
#include <AK/URLParser.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Fetch/Enums.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
@ -12,7 +13,6 @@
#include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
#include <LibWeb/Fetch/Response.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Infra/JSON.h>
namespace Web::Fetch {
@ -21,8 +21,7 @@ Response::Response(JS::Realm& realm, NonnullOwnPtr<Infrastructure::Response> res
: PlatformObject(realm)
, m_response(move(response))
{
auto& window = verify_cast<HTML::Window>(realm.global_object());
set_prototype(&window.cached_web_prototype("Response"));
set_prototype(&Bindings::cached_web_prototype(realm, "Response"));
}
Response::~Response() = default;
@ -67,8 +66,6 @@ Optional<Infrastructure::Body&> Response::body_impl()
// https://fetch.spec.whatwg.org/#response-create
JS::NonnullGCPtr<Response> Response::create(NonnullOwnPtr<Infrastructure::Response> response, Headers::Guard guard, JS::Realm& realm)
{
auto& window = verify_cast<HTML::Window>(realm.global_object());
// Copy a NonnullRefPtr to the response's header list before response is being move()'d.
auto response_reader_list = response->header_list();
@ -77,7 +74,7 @@ JS::NonnullGCPtr<Response> Response::create(NonnullOwnPtr<Infrastructure::Respon
auto* response_object = realm.heap().allocate<Response>(realm, realm, move(response));
// 3. Set responseObjects headers to a new Headers object with realm, whose headers list is responses headers list and guard is guard.
response_object->m_headers = realm.heap().allocate<Headers>(realm, window);
response_object->m_headers = realm.heap().allocate<Headers>(realm, realm);
response_object->m_headers->set_header_list(move(response_reader_list));
response_object->m_headers->set_guard(guard);
@ -88,10 +85,6 @@ JS::NonnullGCPtr<Response> Response::create(NonnullOwnPtr<Infrastructure::Respon
// https://fetch.spec.whatwg.org/#initialize-a-response
WebIDL::ExceptionOr<void> Response::initialize_response(ResponseInit const& init, Optional<Infrastructure::BodyWithType> const& body)
{
auto& vm = Bindings::main_thread_vm();
auto& realm = *vm.current_realm();
auto& window = verify_cast<HTML::Window>(realm.global_object());
// 1. If init["status"] is not in the range 200 to 599, inclusive, then throw a RangeError.
if (init.status < 200 || init.status > 599)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Status must be in range 200-599"sv };
@ -102,7 +95,7 @@ WebIDL::ExceptionOr<void> Response::initialize_response(ResponseInit const& init
m_response->set_status(init.status);
// 4. Set responses responses status message to init["statusText"].
m_response->set_status_message(TRY_OR_RETURN_OOM(window, ByteBuffer::copy(init.status_text.bytes())));
m_response->set_status_message(TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(init.status_text.bytes())));
// 5. If init["headers"] exists, then fill responses headers with init["headers"].
if (init.headers.has_value())
@ -120,10 +113,10 @@ WebIDL::ExceptionOr<void> Response::initialize_response(ResponseInit const& init
// 3. If bodys type is non-null and responses header list does not contain `Content-Type`, then append (`Content-Type`, bodys type) to responses header list.
if (body->type.has_value() && m_response->header_list()->contains("Content-Type"sv.bytes())) {
auto header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy("Content-Type"sv.bytes())),
.value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(body->type->span())),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy("Content-Type"sv.bytes())),
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(body->type->span())),
};
TRY_OR_RETURN_OOM(global_object(), m_response->header_list()->append(move(header)));
TRY_OR_RETURN_OOM(realm(), m_response->header_list()->append(move(header)));
}
}
@ -131,22 +124,17 @@ WebIDL::ExceptionOr<void> Response::initialize_response(ResponseInit const& init
}
// https://fetch.spec.whatwg.org/#dom-response
WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::create_with_global_object(HTML::Window& window, Optional<BodyInit> const& body, ResponseInit const& init)
WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::construct_impl(JS::Realm& realm, Optional<BodyInit> const& body, ResponseInit const& init)
{
auto& realm = window.realm();
// Referred to as 'this' in the spec.
auto response_object = [&] {
auto response = adopt_own(*new Infrastructure::Response());
return JS::NonnullGCPtr { *realm.heap().allocate<Response>(realm, realm, move(response)) };
}();
auto response_object = JS::NonnullGCPtr { *realm.heap().allocate<Response>(realm, realm, make<Infrastructure::Response>()) };
// 1. Set thiss response to a new response.
// NOTE: This is done at the beginning as the 'this' value Response object
// cannot exist with a null Infrastructure::Response.
// 2. Set thiss headers to a new Headers object with thiss relevant Realm, whose header list is thiss responses header list and guard is "response".
response_object->m_headers = realm.heap().allocate<Headers>(realm, window);
response_object->m_headers = realm.heap().allocate<Headers>(realm, realm);
response_object->m_headers->set_header_list(response_object->response().header_list());
response_object->m_headers->set_guard(Headers::Guard::Response);
@ -176,9 +164,7 @@ JS::NonnullGCPtr<Response> Response::error()
// https://fetch.spec.whatwg.org/#dom-response-redirect
WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(String const& url, u16 status)
{
auto& vm = Bindings::main_thread_vm();
auto& realm = *vm.current_realm();
auto& window = verify_cast<HTML::Window>(realm.global_object());
auto& realm = HTML::current_settings_object().realm();
// 1. Let parsedURL be the result of parsing url with current settings objects API base URL.
auto api_base_url = HTML::current_settings_object().api_base_url();
@ -194,7 +180,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(String const&
// 4. Let responseObject be the result of creating a Response object, given a new response, "immutable", and thiss relevant Realm.
// FIXME: How can we reliably get 'this', i.e. the object the function was called on, in IDL-defined functions?
auto response_object = Response::create(make<Infrastructure::Response>(), Headers::Guard::Immutable, *vm.current_realm());
auto response_object = Response::create(make<Infrastructure::Response>(), Headers::Guard::Immutable, realm);
// 5. Set responseObjects responses status to status.
response_object->response().set_status(status);
@ -204,10 +190,10 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(String const&
// 7. Append (`Location`, value) to responseObjects responses header list.
auto header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(window, ByteBuffer::copy("Location"sv.bytes())),
.value = TRY_OR_RETURN_OOM(window, ByteBuffer::copy(value.bytes())),
.name = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("Location"sv.bytes())),
.value = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(value.bytes())),
};
TRY_OR_RETURN_OOM(window, response_object->response().header_list()->append(move(header)));
TRY_OR_RETURN_OOM(realm, response_object->response().header_list()->append(move(header)));
// 8. Return responseObject.
return response_object;
@ -218,7 +204,6 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::json(JS::Value data, R
{
auto& vm = Bindings::main_thread_vm();
auto& realm = *vm.current_realm();
auto& window = verify_cast<HTML::Window>(realm.global_object());
// 1. Let bytes the result of running serialize a JavaScript value to JSON bytes on data.
auto bytes = TRY(Infra::serialize_javascript_value_to_json_bytes(vm, data));
@ -228,12 +213,12 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::json(JS::Value data, R
// 3. Let responseObject be the result of creating a Response object, given a new response, "response", and thiss relevant Realm.
// FIXME: How can we reliably get 'this', i.e. the object the function was called on, in IDL-defined functions?
auto response_object = Response::create(make<Infrastructure::Response>(), Headers::Guard::Response, *vm.current_realm());
auto response_object = Response::create(make<Infrastructure::Response>(), Headers::Guard::Response, realm);
// 4. Perform initialize a response given responseObject, init, and (body, "application/json").
auto body_with_type = Infrastructure::BodyWithType {
.body = move(body),
.type = TRY_OR_RETURN_OOM(window, ByteBuffer::copy("application/json"sv.bytes()))
.type = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("application/json"sv.bytes()))
};
TRY(response_object->initialize_response(init, move(body_with_type)));

View file

@ -31,7 +31,7 @@ class Response final
public:
static JS::NonnullGCPtr<Response> create(NonnullOwnPtr<Infrastructure::Response>, Headers::Guard, JS::Realm&);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> create_with_global_object(HTML::Window&, Optional<BodyInit> const& body = {}, ResponseInit const& init = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> construct_impl(JS::Realm&, Optional<BodyInit> const& body = {}, ResponseInit const& init = {});
virtual ~Response() override;

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

View file

@ -33,9 +33,9 @@ class Blob : public Bindings::PlatformObject {
public:
virtual ~Blob() override;
static JS::NonnullGCPtr<Blob> create(HTML::Window&, ByteBuffer, String type);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create(HTML::Window&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create_with_global_object(HTML::Window&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
static JS::NonnullGCPtr<Blob> create(JS::Realm&, ByteBuffer, String type);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> create(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Blob>> construct_impl(JS::Realm&, Optional<Vector<BlobPart>> const& blob_parts = {}, Optional<BlobPropertyBag> const& options = {});
// https://w3c.github.io/FileAPI/#dfn-size
u64 size() const { return m_byte_buffer.size(); }
@ -50,11 +50,11 @@ public:
ReadonlyBytes bytes() const { return m_byte_buffer.bytes(); }
protected:
Blob(HTML::Window&, ByteBuffer, String type);
Blob(HTML::Window&, ByteBuffer);
Blob(JS::Realm&, ByteBuffer, String type);
Blob(JS::Realm&, ByteBuffer);
private:
explicit Blob(HTML::Window&);
explicit Blob(JS::Realm&);
ByteBuffer m_byte_buffer {};
String m_type {};

View file

@ -4,26 +4,27 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Time.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/FileAPI/File.h>
#include <LibWeb/HTML/Window.h>
namespace Web::FileAPI {
File::File(HTML::Window& window, ByteBuffer byte_buffer, String file_name, String type, i64 last_modified)
: Blob(window, move(byte_buffer), move(type))
File::File(JS::Realm& realm, ByteBuffer byte_buffer, String file_name, String type, i64 last_modified)
: Blob(realm, move(byte_buffer), move(type))
, m_name(move(file_name))
, m_last_modified(last_modified)
{
set_prototype(&window.cached_web_prototype("File"));
set_prototype(&Bindings::cached_web_prototype(realm, "File"));
}
File::~File() = default;
// https://w3c.github.io/FileAPI/#ref-for-dom-file-file
WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(HTML::Window& window, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(JS::Realm& realm, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
{
// 1. Let bytes be the result of processing blob parts given fileBits and options.
auto bytes = TRY_OR_RETURN_OOM(window, process_blob_parts(file_bits, static_cast<Optional<BlobPropertyBag> const&>(*options)));
auto bytes = TRY_OR_RETURN_OOM(realm, process_blob_parts(file_bits, static_cast<Optional<BlobPropertyBag> const&>(*options)));
// 2. Let n be the fileName argument to the constructor.
// NOTE: Underlying OS filesystems use differing conventions for file name; with constructed files, mandating UTF-16 lessens ambiquity when file names are converted to byte sequences.
@ -57,12 +58,12 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create(HTML::Window& window, V
// 4. F.name is set to n.
// 5. F.type is set to t.
// 6. F.lastModified is set to d.
return JS::NonnullGCPtr(*window.heap().allocate<File>(window.realm(), window, move(bytes), move(name), move(type), last_modified));
return JS::NonnullGCPtr(*realm.heap().allocate<File>(realm, realm, move(bytes), move(name), move(type), last_modified));
}
WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::create_with_global_object(HTML::Window& window, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> File::construct_impl(JS::Realm& realm, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options)
{
return create(window, file_bits, file_name, options);
return create(realm, file_bits, file_name, options);
}
}

View file

@ -6,7 +6,6 @@
#pragma once
#include <AK/Time.h>
#include <LibWeb/FileAPI/Blob.h>
namespace Web::FileAPI {
@ -19,8 +18,8 @@ class File : public Blob {
WEB_PLATFORM_OBJECT(File, Blob);
public:
static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> create(HTML::Window&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> create_with_global_object(HTML::Window&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> create(JS::Realm&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<File>> construct_impl(JS::Realm&, Vector<BlobPart> const& file_bits, String const& file_name, Optional<FilePropertyBag> const& options = {});
virtual ~File() override;
@ -30,7 +29,7 @@ public:
i64 last_modified() const { return m_last_modified; }
private:
File(HTML::Window&, ByteBuffer, String file_name, String type, i64 last_modified);
File(JS::Realm&, ByteBuffer, String file_name, String type, i64 last_modified);
String m_name;
i64 m_last_modified { 0 };

View file

@ -4,28 +4,28 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/XHR/ProgressEvent.h>
namespace Web::XHR {
ProgressEvent* ProgressEvent::create(HTML::Window& window_object, FlyString const& event_name, ProgressEventInit const& event_init)
ProgressEvent* ProgressEvent::create(JS::Realm& realm, FlyString const& event_name, ProgressEventInit const& event_init)
{
return window_object.heap().allocate<ProgressEvent>(window_object.realm(), window_object, event_name, event_init);
return realm.heap().allocate<ProgressEvent>(realm, realm, event_name, event_init);
}
ProgressEvent* ProgressEvent::create_with_global_object(HTML::Window& window_object, FlyString const& event_name, ProgressEventInit const& event_init)
ProgressEvent* ProgressEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, ProgressEventInit const& event_init)
{
return create(window_object, event_name, event_init);
return create(realm, event_name, event_init);
}
ProgressEvent::ProgressEvent(HTML::Window& window_object, FlyString const& event_name, ProgressEventInit const& event_init)
: Event(window_object, event_name, event_init)
ProgressEvent::ProgressEvent(JS::Realm& realm, FlyString const& event_name, ProgressEventInit const& event_init)
: Event(realm, event_name, event_init)
, m_length_computable(event_init.length_computable)
, m_loaded(event_init.loaded)
, m_total(event_init.total)
{
set_prototype(&window_object.cached_web_prototype("ProgressEvent"));
set_prototype(&Bindings::cached_web_prototype(realm, "ProgressEvent"));
}
ProgressEvent::~ProgressEvent() = default;

View file

@ -23,10 +23,8 @@ class ProgressEvent final : public DOM::Event {
WEB_PLATFORM_OBJECT(ProgressEvent, DOM::Event);
public:
static ProgressEvent* create(HTML::Window&, FlyString const& event_name, ProgressEventInit const& event_init);
static ProgressEvent* create_with_global_object(HTML::Window&, FlyString const& event_name, ProgressEventInit const& event_init);
ProgressEvent(HTML::Window&, FlyString const& event_name, ProgressEventInit const& event_init);
static ProgressEvent* create(JS::Realm&, FlyString const& event_name, ProgressEventInit const& event_init);
static ProgressEvent* construct_impl(JS::Realm&, FlyString const& event_name, ProgressEventInit const& event_init);
virtual ~ProgressEvent() override;
@ -35,6 +33,8 @@ public:
u64 total() const { return m_total; }
private:
ProgressEvent(JS::Realm&, FlyString const& event_name, ProgressEventInit const& event_init);
bool m_length_computable { false };
u64 m_loaded { 0 };
u64 m_total { 0 };

View file

@ -40,9 +40,10 @@
namespace Web::XHR {
JS::NonnullGCPtr<XMLHttpRequest> XMLHttpRequest::create_with_global_object(HTML::Window& window)
JS::NonnullGCPtr<XMLHttpRequest> XMLHttpRequest::construct_impl(JS::Realm& realm)
{
return *window.heap().allocate<XMLHttpRequest>(window.realm(), window);
auto& window = verify_cast<HTML::Window>(realm.global_object());
return *realm.heap().allocate<XMLHttpRequest>(realm, window);
}
XMLHttpRequest::XMLHttpRequest(HTML::Window& window)
@ -50,7 +51,7 @@ XMLHttpRequest::XMLHttpRequest(HTML::Window& window)
, m_window(window)
, m_response_type(Bindings::XMLHttpRequestResponseType::Empty)
{
set_prototype(&window.cached_web_prototype("XMLHttpRequest"));
set_prototype(&Bindings::cached_web_prototype(window.realm(), "XMLHttpRequest"));
}
XMLHttpRequest::~XMLHttpRequest() = default;
@ -67,7 +68,7 @@ void XMLHttpRequest::visit_edges(Cell::Visitor& visitor)
void XMLHttpRequest::set_ready_state(ReadyState ready_state)
{
m_ready_state = ready_state;
dispatch_event(*DOM::Event::create(global_object(), EventNames::readystatechange));
dispatch_event(*DOM::Event::create(realm(), EventNames::readystatechange));
}
void XMLHttpRequest::fire_progress_event(String const& event_name, u64 transmitted, u64 length)
@ -76,7 +77,7 @@ void XMLHttpRequest::fire_progress_event(String const& event_name, u64 transmitt
event_init.length_computable = true;
event_init.loaded = transmitted;
event_init.total = length;
dispatch_event(*ProgressEvent::create(global_object(), event_name, event_init));
dispatch_event(*ProgressEvent::create(realm(), event_name, event_init));
}
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsetext
@ -84,7 +85,7 @@ WebIDL::ExceptionOr<String> XMLHttpRequest::response_text() const
{
// 1. If thiss response type is not the empty string or "text", then throw an "InvalidStateError" DOMException.
if (m_response_type != Bindings::XMLHttpRequestResponseType::Empty && m_response_type != Bindings::XMLHttpRequestResponseType::Text)
return WebIDL::InvalidStateError::create(global_object(), "XHR responseText can only be used for responseType \"\" or \"text\"");
return WebIDL::InvalidStateError::create(realm(), "XHR responseText can only be used for responseType \"\" or \"text\"");
// 2. If thiss state is not loading or done, then return the empty string.
if (m_ready_state != ReadyState::Loading && m_ready_state != ReadyState::Done)
@ -132,8 +133,8 @@ WebIDL::ExceptionOr<JS::Value> XMLHttpRequest::response()
}
// 6. Otherwise, if thiss response type is "blob", set thiss response object to a new Blob object representing thiss received bytes with type set to the result of get a final MIME type for this.
else if (m_response_type == Bindings::XMLHttpRequestResponseType::Blob) {
auto blob_part = FileAPI::Blob::create(global_object(), m_received_bytes, get_final_mime_type().type());
auto blob = TRY(FileAPI::Blob::create(global_object(), Vector<FileAPI::BlobPart> { JS::make_handle(*blob_part) }));
auto blob_part = FileAPI::Blob::create(realm(), m_received_bytes, get_final_mime_type().type());
auto blob = TRY(FileAPI::Blob::create(realm(), Vector<FileAPI::BlobPart> { JS::make_handle(*blob_part) }));
m_response_object = JS::Value(blob.ptr());
}
// 7. Otherwise, if thiss response type is "document", set a document response for this.
@ -276,20 +277,20 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::set_request_header(String const& name_
// 1. If thiss state is not opened, then throw an "InvalidStateError" DOMException.
if (m_ready_state != ReadyState::Opened)
return WebIDL::InvalidStateError::create(global_object(), "XHR readyState is not OPENED");
return WebIDL::InvalidStateError::create(realm(), "XHR readyState is not OPENED");
// 2. If thiss send() flag is set, then throw an "InvalidStateError" DOMException.
if (m_send)
return WebIDL::InvalidStateError::create(global_object(), "XHR send() flag is already set");
return WebIDL::InvalidStateError::create(realm(), "XHR send() flag is already set");
// 3. Normalize value.
value = MUST(Fetch::Infrastructure::normalize_header_value(value));
// 4. If name is not a header name or value is not a header value, then throw a "SyntaxError" DOMException.
if (!Fetch::Infrastructure::is_header_name(name))
return WebIDL::SyntaxError::create(global_object(), "Header name contains invalid characters.");
return WebIDL::SyntaxError::create(realm(), "Header name contains invalid characters.");
if (!Fetch::Infrastructure::is_header_value(value))
return WebIDL::SyntaxError::create(global_object(), "Header value contains invalid characters.");
return WebIDL::SyntaxError::create(realm(), "Header value contains invalid characters.");
// 5. If name is a forbidden header name, then return.
if (Fetch::Infrastructure::is_forbidden_header_name(name))
@ -327,15 +328,15 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::open(String const& method_string, Stri
// 2. If settingsObject has a responsible document and it is not fully active, then throw an "InvalidStateError" DOMException.
if (settings_object.responsible_document() && !settings_object.responsible_document()->is_active())
return WebIDL::InvalidStateError::create(global_object(), "Invalid state: Responsible document is not fully active.");
return WebIDL::InvalidStateError::create(realm(), "Invalid state: Responsible document is not fully active.");
// 3. If method is not a method, then throw a "SyntaxError" DOMException.
if (!Fetch::Infrastructure::is_method(method))
return WebIDL::SyntaxError::create(global_object(), "An invalid or illegal string was specified.");
return WebIDL::SyntaxError::create(realm(), "An invalid or illegal string was specified.");
// 4. If method is a forbidden method, then throw a "SecurityError" DOMException.
if (Fetch::Infrastructure::is_forbidden_method(method))
return WebIDL::SecurityError::create(global_object(), "Forbidden method, must not be 'CONNECT', 'TRACE', or 'TRACK'");
return WebIDL::SecurityError::create(realm(), "Forbidden method, must not be 'CONNECT', 'TRACE', or 'TRACK'");
// 5. Normalize method.
method = MUST(Fetch::Infrastructure::normalize_method(method));
@ -345,7 +346,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::open(String const& method_string, Stri
// 7. If parsedURL is failure, then throw a "SyntaxError" DOMException.
if (!parsed_url.is_valid())
return WebIDL::SyntaxError::create(global_object(), "Invalid URL");
return WebIDL::SyntaxError::create(realm(), "Invalid URL");
// 8. If the async argument is omitted, set async to true, and set username and password to null.
// NOTE: This is handled in the overload lacking the async argument.
@ -401,10 +402,10 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<Fetch::XMLHttpRequestBod
auto& realm = *vm.current_realm();
if (m_ready_state != ReadyState::Opened)
return WebIDL::InvalidStateError::create(global_object(), "XHR readyState is not OPENED");
return WebIDL::InvalidStateError::create(realm, "XHR readyState is not OPENED");
if (m_send)
return WebIDL::InvalidStateError::create(global_object(), "XHR send() flag is already set");
return WebIDL::InvalidStateError::create(realm, "XHR send() flag is already set");
// If thiss request method is `GET` or `HEAD`, then set body to null.
if (m_method.is_one_of("GET"sv, "HEAD"sv))
@ -425,14 +426,14 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<Fetch::XMLHttpRequestBod
if (should_enforce_same_origin_policy && !m_window->associated_document().origin().is_same_origin(request_url_origin)) {
dbgln("XHR failed to load: Same-Origin Policy violation: {} may not load {}", m_window->associated_document().url(), request_url);
set_ready_state(ReadyState::Done);
dispatch_event(*DOM::Event::create(global_object(), HTML::EventNames::error));
dispatch_event(*DOM::Event::create(realm, HTML::EventNames::error));
return {};
}
auto request = LoadRequest::create_for_url_on_page(request_url, m_window->page());
request.set_method(m_method);
if (body_with_type.has_value()) {
TRY_OR_RETURN_OOM(global_object(), body_with_type->body.source().visit([&](ByteBuffer const& buffer) -> ErrorOr<void> {
TRY_OR_RETURN_OOM(realm, body_with_type->body.source().visit([&](ByteBuffer const& buffer) -> ErrorOr<void> {
request.set_body(buffer);
return {}; }, [&](JS::Handle<FileAPI::Blob> const& blob) -> ErrorOr<void> {
auto byte_buffer = TRY(ByteBuffer::copy(blob->bytes()));
@ -491,7 +492,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<Fetch::XMLHttpRequestBod
xhr.m_status = status_code.value_or(0);
xhr.m_response_headers = move(response_headers);
xhr.m_send = false;
xhr.dispatch_event(*DOM::Event::create(xhr.global_object(), EventNames::readystatechange));
xhr.dispatch_event(*DOM::Event::create(xhr.realm(), EventNames::readystatechange));
xhr.fire_progress_event(EventNames::load, transmitted, length);
xhr.fire_progress_event(EventNames::loadend, transmitted, length);
},
@ -503,7 +504,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<Fetch::XMLHttpRequestBod
auto& xhr = const_cast<XMLHttpRequest&>(*strong_this);
xhr.set_ready_state(ReadyState::Done);
xhr.set_status(status_code.value_or(0));
xhr.dispatch_event(*DOM::Event::create(xhr.global_object(), HTML::EventNames::error));
xhr.dispatch_event(*DOM::Event::create(xhr.realm(), HTML::EventNames::error));
},
m_timeout,
[weak_this = make_weak_ptr<XMLHttpRequest>()] {
@ -511,7 +512,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<Fetch::XMLHttpRequestBod
if (!strong_this)
return;
auto& xhr = const_cast<XMLHttpRequest&>(*strong_this);
xhr.dispatch_event(*DOM::Event::create(xhr.global_object(), EventNames::timeout));
xhr.dispatch_event(*DOM::Event::create(xhr.realm(), EventNames::timeout));
});
} else {
TODO();
@ -552,7 +553,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::override_mime_type(String const& mime)
{
// 1. If thiss state is loading or done, then throw an "InvalidStateError" DOMException.
if (m_ready_state == ReadyState::Loading || m_ready_state == ReadyState::Done)
return WebIDL::InvalidStateError::create(global_object(), "Cannot override MIME type when state is Loading or Done.");
return WebIDL::InvalidStateError::create(realm(), "Cannot override MIME type when state is Loading or Done.");
// 2. Set thiss override MIME type to the result of parsing mime.
m_override_mime_type = MimeSniff::MimeType::from_string(mime);
@ -570,7 +571,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::set_timeout(u32 timeout)
// 1. If the current global object is a Window object and thiss synchronous flag is set,
// then throw an "InvalidAccessError" DOMException.
if (is<HTML::Window>(HTML::current_global_object()) && m_synchronous)
return WebIDL::InvalidAccessError::create(global_object(), "Use of XMLHttpRequest's timeout attribute is not supported in the synchronous mode in window context.");
return WebIDL::InvalidAccessError::create(realm(), "Use of XMLHttpRequest's timeout attribute is not supported in the synchronous mode in window context.");
// 2. Set thiss timeout to the given value.
m_timeout = timeout;

View file

@ -35,7 +35,7 @@ public:
Done = 4,
};
static JS::NonnullGCPtr<XMLHttpRequest> create_with_global_object(HTML::Window&);
static JS::NonnullGCPtr<XMLHttpRequest> construct_impl(JS::Realm&);
virtual ~XMLHttpRequest() override;