1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:48:10 +00:00

LibWeb: Port fire_progress_event() + request_error_steps() to new String

This ports XHR's fire_progress_event() and request_error_steps() to new
FlyString.

Signature of fire_progress_event() parameter event_name  was changed
from 'String const&' to 'FlyString const&'.
This commit is contained in:
Kenneth Myhra 2023-04-09 10:57:50 +02:00 committed by Linus Groh
parent fdd33d818c
commit bf048da8cb
2 changed files with 22 additions and 22 deletions

View file

@ -92,7 +92,7 @@ void XMLHttpRequest::visit_edges(Cell::Visitor& visitor)
}
// https://xhr.spec.whatwg.org/#concept-event-fire-progress
static void fire_progress_event(XMLHttpRequestEventTarget& target, DeprecatedString const& event_name, u64 transmitted, u64 length)
static void fire_progress_event(XMLHttpRequestEventTarget& target, FlyString const& event_name, u64 transmitted, u64 length)
{
// To fire a progress event named e at target, given transmitted and length, means to fire an event named e at target, using ProgressEvent,
// with the loaded attribute initialized to transmitted, and if length is not 0, with the lengthComputable attribute initialized to true
@ -102,7 +102,7 @@ static void fire_progress_event(XMLHttpRequestEventTarget& target, DeprecatedStr
event_init.loaded = transmitted;
event_init.total = length;
// FIXME: If we're in an async context, this will propagate to a callback context which can't propagate it anywhere else and does not expect this to fail.
target.dispatch_event(*ProgressEvent::create(target.realm(), String::from_deprecated_string(event_name).release_value_but_fixme_should_propagate_errors(), event_init).release_value_but_fixme_should_propagate_errors());
target.dispatch_event(*ProgressEvent::create(target.realm(), event_name, event_init).release_value_but_fixme_should_propagate_errors());
}
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsetext
@ -277,7 +277,7 @@ ErrorOr<MimeSniff::MimeType> XMLHttpRequest::get_response_mime_type() const
ErrorOr<Optional<StringView>> XMLHttpRequest::get_final_encoding() const
{
// 1. Let label be null.
Optional<DeprecatedString> label;
Optional<String> label;
// 2. Let responseMIME be the result of get a response MIME type for xhr.
auto response_mime = TRY(get_response_mime_type());
@ -285,13 +285,13 @@ ErrorOr<Optional<StringView>> XMLHttpRequest::get_final_encoding() const
// 3. If responseMIMEs parameters["charset"] exists, then set label to it.
auto response_mime_charset_it = response_mime.parameters().find("charset"sv);
if (response_mime_charset_it != response_mime.parameters().end())
label = response_mime_charset_it->value.to_deprecated_string();
label = response_mime_charset_it->value;
// 4. If xhrs override MIME types parameters["charset"] exists, then set label to it.
if (m_override_mime_type.has_value()) {
auto override_mime_charset_it = m_override_mime_type->parameters().find("charset"sv);
if (override_mime_charset_it != m_override_mime_type->parameters().end())
label = override_mime_charset_it->value.to_deprecated_string();
label = override_mime_charset_it->value;
}
// 5. If label is null, then return null.
@ -607,7 +607,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<DocumentOrXMLHttpRequest
// 11. If thiss synchronous flag is unset, then:
if (!m_synchronous) {
// 1. Fire a progress event named loadstart at this with 0 and 0.
fire_progress_event(*this, EventNames::loadstart.to_deprecated_fly_string(), 0, 0);
fire_progress_event(*this, EventNames::loadstart, 0, 0);
// 2. Let requestBodyTransmitted be 0.
// NOTE: This is kept on the XHR object itself instead of the stack, as we cannot capture references to stack variables in an async context.
@ -625,7 +625,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<DocumentOrXMLHttpRequest
// 5. If thiss upload complete flag is unset and thiss upload listener flag is set, then fire a progress event named loadstart at thiss upload object with requestBodyTransmitted and requestBodyLength.
if (!m_upload_complete && m_upload_listener)
fire_progress_event(m_upload_object, EventNames::loadstart.to_deprecated_fly_string(), m_request_body_transmitted, request_body_length);
fire_progress_event(m_upload_object, EventNames::loadstart, m_request_body_transmitted, request_body_length);
// 6. If thiss state is not opened or thiss send() flag is unset, then return.
if (m_state != State::Opened || !m_send)
@ -642,7 +642,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<DocumentOrXMLHttpRequest
// 3. If thiss upload listener flag is set, then fire a progress event named progress at thiss upload object with requestBodyTransmitted and requestBodyLength.
if (m_upload_listener)
fire_progress_event(m_upload_object, EventNames::progress.to_deprecated_fly_string(), m_request_body_transmitted, request_body_length);
fire_progress_event(m_upload_object, EventNames::progress, m_request_body_transmitted, request_body_length);
};
// 8. Let processRequestEndOfBody be these steps:
@ -657,13 +657,13 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<DocumentOrXMLHttpRequest
return;
// 3. Fire a progress event named progress at thiss upload object with requestBodyTransmitted and requestBodyLength.
fire_progress_event(m_upload_object, EventNames::progress.to_deprecated_fly_string(), m_request_body_transmitted, request_body_length);
fire_progress_event(m_upload_object, EventNames::progress, m_request_body_transmitted, request_body_length);
// 4. Fire a progress event named load at thiss upload object with requestBodyTransmitted and requestBodyLength.
fire_progress_event(m_upload_object, EventNames::load.to_deprecated_fly_string(), m_request_body_transmitted, request_body_length);
fire_progress_event(m_upload_object, EventNames::load, m_request_body_transmitted, request_body_length);
// 5. Fire a progress event named loadend at thiss upload object with requestBodyTransmitted and requestBodyLength.
fire_progress_event(m_upload_object, EventNames::loadend.to_deprecated_fly_string(), m_request_body_transmitted, request_body_length);
fire_progress_event(m_upload_object, EventNames::loadend, m_request_body_transmitted, request_body_length);
};
// 9. Let processResponse, given a response, be these steps:
@ -1017,7 +1017,7 @@ void XMLHttpRequest::abort()
// NOTE: This cannot throw as we don't pass in an exception. XHR::abort cannot be reached in a synchronous context where the state matches above.
// This is because it pauses inside XHR::send until the request is done or times out and then immediately calls `handle_response_end_of_body`
// which will always set `m_state` to `Done`.
MUST(request_error_steps(EventNames::abort.to_deprecated_fly_string()));
MUST(request_error_steps(EventNames::abort));
}
// 3. If thiss state is done, then set thiss state to unsent and thiss response to a network error.
@ -1078,7 +1078,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::handle_response_end_of_body()
// 6. If xhrs synchronous flag is unset, then fire a progress event named progress at xhr with transmitted and length.
if (!m_synchronous)
fire_progress_event(*this, EventNames::progress.to_deprecated_fly_string(), transmitted, length);
fire_progress_event(*this, EventNames::progress, transmitted, length);
// 7. Set xhrs state to done.
m_state = State::Done;
@ -1091,10 +1091,10 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::handle_response_end_of_body()
dispatch_event(*DOM::Event::create(realm, EventNames::readystatechange).release_value_but_fixme_should_propagate_errors());
// 10. Fire a progress event named load at xhr with transmitted and length.
fire_progress_event(*this, EventNames::load.to_deprecated_fly_string(), transmitted, length);
fire_progress_event(*this, EventNames::load, transmitted, length);
// 11. Fire a progress event named loadend at xhr with transmitted and length.
fire_progress_event(*this, EventNames::loadend.to_deprecated_fly_string(), transmitted, length);
fire_progress_event(*this, EventNames::loadend, transmitted, length);
return {};
}
@ -1108,20 +1108,20 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::handle_errors()
// 2. If xhrs timed out flag is set, then run the request error steps for xhr, timeout, and "TimeoutError" DOMException.
if (m_timed_out)
return TRY(request_error_steps(EventNames::timeout.to_deprecated_fly_string(), WebIDL::TimeoutError::create(realm(), "Timed out"sv)));
return TRY(request_error_steps(EventNames::timeout, WebIDL::TimeoutError::create(realm(), "Timed out"sv)));
// 3. Otherwise, if xhrs responses aborted flag is set, run the request error steps for xhr, abort, and "AbortError" DOMException.
if (m_response->aborted())
return TRY(request_error_steps(EventNames::abort.to_deprecated_fly_string(), WebIDL::AbortError::create(realm(), "Aborted"sv)));
return TRY(request_error_steps(EventNames::abort, WebIDL::AbortError::create(realm(), "Aborted"sv)));
// 4. Otherwise, if xhrs response is a network error, then run the request error steps for xhr, error, and "NetworkError" DOMException.
if (m_response->is_network_error())
return TRY(request_error_steps(EventNames::error.to_deprecated_fly_string(), WebIDL::NetworkError::create(realm(), "Network error"sv)));
return TRY(request_error_steps(EventNames::error, WebIDL::NetworkError::create(realm(), "Network error"sv)));
return {};
}
JS::ThrowCompletionOr<void> XMLHttpRequest::request_error_steps(DeprecatedFlyString const& event_name, JS::GCPtr<WebIDL::DOMException> exception)
JS::ThrowCompletionOr<void> XMLHttpRequest::request_error_steps(FlyString const& event_name, JS::GCPtr<WebIDL::DOMException> exception)
{
// 1. Set xhrs state to done.
m_state = State::Done;
@ -1153,7 +1153,7 @@ JS::ThrowCompletionOr<void> XMLHttpRequest::request_error_steps(DeprecatedFlyStr
fire_progress_event(m_upload_object, event_name, 0, 0);
// 2. Fire a progress event named loadend at xhrs upload object with 0 and 0.
fire_progress_event(m_upload_object, EventNames::loadend.to_deprecated_fly_string(), 0, 0);
fire_progress_event(m_upload_object, EventNames::loadend, 0, 0);
}
}
@ -1161,7 +1161,7 @@ JS::ThrowCompletionOr<void> XMLHttpRequest::request_error_steps(DeprecatedFlyStr
fire_progress_event(*this, event_name, 0, 0);
// 8. Fire a progress event named loadend at xhr with 0 and 0.
fire_progress_event(*this, EventNames::loadend.to_deprecated_fly_string(), 0, 0);
fire_progress_event(*this, EventNames::loadend, 0, 0);
return {};
}

View file

@ -89,7 +89,7 @@ private:
WebIDL::ExceptionOr<void> handle_response_end_of_body();
WebIDL::ExceptionOr<void> handle_errors();
JS::ThrowCompletionOr<void> request_error_steps(DeprecatedFlyString const& event_name, JS::GCPtr<WebIDL::DOMException> exception = nullptr);
JS::ThrowCompletionOr<void> request_error_steps(FlyString const& event_name, JS::GCPtr<WebIDL::DOMException> exception = nullptr);
XMLHttpRequest(JS::Realm&, XMLHttpRequestUpload&, Fetch::Infrastructure::HeaderList&, Fetch::Infrastructure::Response&, Fetch::Infrastructure::FetchController&);