Truly independent web browser
LibWeb: Update active XHR timers when timeout changes
Andreas Klingd8e878fe785d
diff --git a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Libraries/LibWeb/XHR/XMLHttpRequest.cppindex 05e063bf86b..e97f1069cba 100644--- a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp+++ b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp@@ -164,6 +164,32 @@ void XMLHttpRequest::visit_edges(Cell::Visitor& visitor)visitor.visit(cached_response_object.object);}+void XMLHttpRequest::restart_timeout_timer()+{+ stop_timeout_timer();+ if (!m_send || m_synchronous || !m_async_request || m_async_request->done() || m_timeout == 0)+ return;++ // NB: Timeout changes keep the deadline relative to send(), rather than starting a fresh timeout period.+ auto elapsed = MonotonicTime::now() - m_async_send_start_time;+ auto remaining = max(AK::Duration::zero(), AK::Duration::from_milliseconds(m_timeout) - elapsed);+ m_timeout_timer = Platform::Timer::create_single_shot(heap(), remaining, nullptr);+ auto request = NonnullRefPtr { *m_async_request };+ m_timeout_timer->on_timeout = GC::create_function(heap(), [this, request, fetch_controller = m_fetch_controller] {+ if (m_async_request.ptr() != request.ptr() || m_fetch_controller != fetch_controller)+ return;++ stop_timeout_timer();+ m_async_request = nullptr;+ if (!request->done()) {+ m_timed_out = true;+ fetch_controller->terminate();+ MUST(handle_errors());+ }+ });+ m_timeout_timer->start();+}+void XMLHttpRequest::stop_timeout_timer(){if (!m_timeout_timer)@@ -638,6 +664,8 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::open(String const& method, Utf16String// 10. Terminate this’s fetch controller.// Spec Note: A fetch can be ongoing at this point.m_fetch_controller->terminate();+ stop_timeout_timer();+ m_async_request = nullptr;m_activity_root.release();// 11. Set variables associated with the object as follows:@@ -1037,22 +1065,13 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(NullableDocumentOrXMLHttpRequestB}));// 11. Let now be the present time.+ m_async_send_start_time = MonotonicTime::now();+ m_async_request = request;+// 12. Run these steps in parallel:// 1. Wait until either req’s done flag is set or this’s timeout is not 0 and this’s timeout milliseconds have passed since now.// 2. If req’s done flag is unset, then set this’s timed out flag and terminate this’s fetch controller.- if (m_timeout != 0) {- stop_timeout_timer();- m_timeout_timer = Platform::Timer::create_single_shot(heap(), AK::Duration::from_milliseconds(m_timeout), nullptr);- m_timeout_timer->on_timeout = GC::create_function(GC::Heap::the(), [this, request]() {- if (!request->done()) {- m_timed_out = true;- m_fetch_controller->terminate();- MUST(handle_errors());- }- stop_timeout_timer();- });- m_timeout_timer->start();- }+ restart_timeout_timer();}// 12. Otherwise, if this’s synchronous flag is set:else {@@ -1223,6 +1242,10 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::set_timeout(u32 timeout)// 2. Set this’s timeout to the given value.m_timeout = timeout;+ // NB: The asynchronous send() timeout wait observes changes to this value.+ if (m_send && !m_synchronous)+ restart_timeout_timer();+return {};}@@ -1257,6 +1280,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::set_with_credentials(bool with_credentvoid XMLHttpRequest::abort(){stop_timeout_timer();+ m_async_request = nullptr;auto& associated_realm = HTML::relevant_realm(relevant_global_object());// 1. Abort this’s fetch controller.@@ -1304,6 +1328,7 @@ WebIDL::ExceptionOr<String> XMLHttpRequest::status_text() constWebIDL::ExceptionOr<void> XMLHttpRequest::handle_response_end_of_body(){stop_timeout_timer();+ m_async_request = nullptr;// 1. Handle errors for xhr.TRY(handle_errors());@@ -1375,6 +1400,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::handle_errors()WebIDL::ExceptionOr<void> XMLHttpRequest::request_error_steps(Utf16FlyString const& event_name, GC::Ptr<WebIDL::DOMException> exception){stop_timeout_timer();+ m_async_request = nullptr;// 1. Set xhr’s state to done.m_state = State::Done;diff --git a/Libraries/LibWeb/XHR/XMLHttpRequest.h b/Libraries/LibWeb/XHR/XMLHttpRequest.hindex e9ca8562b1b..3dd7742e1cf 100644--- a/Libraries/LibWeb/XHR/XMLHttpRequest.h+++ b/Libraries/LibWeb/XHR/XMLHttpRequest.h@@ -10,6 +10,7 @@#include <AK/ByteBuffer.h>#include <AK/RefCounted.h>+#include <AK/RefPtr.h>#include <AK/Time.h>#include <AK/Vector.h>#include <AK/Weakable.h>@@ -129,6 +130,7 @@ private:WebIDL::ExceptionOr<void> handle_errors();WebIDL::ExceptionOr<void> request_error_steps(Utf16FlyString const& event_name, GC::Ptr<WebIDL::DOMException> exception = nullptr);+ void restart_timeout_timer();void stop_timeout_timer();bool should_be_kept_alive() const;void update_activity_root();@@ -247,6 +249,8 @@ private:Optional<MonotonicTime> m_last_upload_progress_timestamp;Optional<MonotonicTime> m_last_download_progress_timestamp;+ RefPtr<Fetch::Infrastructure::Request> m_async_request;+ MonotonicTime m_async_send_start_time;GC::Ptr<Platform::Timer> m_timeout_timer;GC::ActivityRoot m_activity_root;};diff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-overrides@timeout overrides load after a delay.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-overrides@timeout overrides load after a delay.txtindex 3b82df1a3f5..68246566b58 100644--- a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-overrides@timeout overrides load after a delay.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-overrides@timeout overrides load after a delay.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail Timeout test: timeout overrides load after a delay, original timeout at 5000, reset at 1000 to 2000\ No newline at end of file+1 Pass+Pass Timeout test: timeout overrides load after a delay, original timeout at 5000, reset at 1000 to 2000\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-overridesexpires@timeout set to expired value before load fires.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-overridesexpires@timeout set to expired value before load fires.txtindex 93cecc4b34d..1842f5f7402 100644--- a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-overridesexpires@timeout set to expired value before load fires.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-overridesexpires@timeout set to expired value before load fires.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail Timeout test: timeout set to expired value before load fires, original timeout at 5000, reset at 2000 to 1100\ No newline at end of file+1 Pass+Pass Timeout test: timeout set to expired value before load fires, original timeout at 5000, reset at 2000 to 1100\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overrides@timeout overrides load after a delay.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overrides@timeout overrides load after a delay.txtindex 3b82df1a3f5..68246566b58 100644--- a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overrides@timeout overrides load after a delay.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overrides@timeout overrides load after a delay.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail Timeout test: timeout overrides load after a delay, original timeout at 5000, reset at 1000 to 2000\ No newline at end of file+1 Pass+Pass Timeout test: timeout overrides load after a delay, original timeout at 5000, reset at 1000 to 2000\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to expired value before load fires.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to expired value before load fires.txtindex 0c9217e6c44..eb92d5e0800 100644--- a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to expired value before load fires.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to expired value before load fires.txt@@ -2,8 +2,7 @@ Harness status: OKFound 3 tests-2 Pass-1 Fail+3 PassPass Timeout test: timeout set to expiring value after load fires, original timeout at 5000, reset at 4000 to 1000-Fail Timeout test: timeout set to expired value before load fires, original timeout at 5000, reset at 2000 to 1100+Pass Timeout test: timeout set to expired value before load fires, original timeout at 5000, reset at 2000 to 1100Pass Timeout test: timeout set to non-expiring value after timeout fires, original timeout at 1000, reset at 2000 to 500\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to expiring value after load fires.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to expiring value after load fires.txtindex 0c9217e6c44..eb92d5e0800 100644--- a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to expiring value after load fires.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to expiring value after load fires.txt@@ -2,8 +2,7 @@ Harness status: OKFound 3 tests-2 Pass-1 Fail+3 PassPass Timeout test: timeout set to expiring value after load fires, original timeout at 5000, reset at 4000 to 1000-Fail Timeout test: timeout set to expired value before load fires, original timeout at 5000, reset at 2000 to 1100+Pass Timeout test: timeout set to expired value before load fires, original timeout at 5000, reset at 2000 to 1100Pass Timeout test: timeout set to non-expiring value after timeout fires, original timeout at 1000, reset at 2000 to 500\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to non-expiring value after timeout fires.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to non-expiring value after timeout fires.txtindex 0c9217e6c44..eb92d5e0800 100644--- a/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to non-expiring value after timeout fires.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires@timeout set to non-expiring value after timeout fires.txt@@ -2,8 +2,7 @@ Harness status: OKFound 3 tests-2 Pass-1 Fail+3 PassPass Timeout test: timeout set to expiring value after load fires, original timeout at 5000, reset at 4000 to 1000-Fail Timeout test: timeout set to expired value before load fires, original timeout at 5000, reset at 2000 to 1100+Pass Timeout test: timeout set to expired value before load fires, original timeout at 5000, reset at 2000 to 1100Pass Timeout test: timeout set to non-expiring value after timeout fires, original timeout at 1000, reset at 2000 to 500\ No newline at end of file