diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index 7a145ee90a..f307692e63 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -693,4 +693,23 @@ DOM::ExceptionOr XMLHttpRequest::override_mime_type(String const& mime) return {}; } + +// https://xhr.spec.whatwg.org/#ref-for-dom-xmlhttprequest-timeout%E2%91%A2 +DOM::ExceptionOr XMLHttpRequest::set_timeout(u32 timeout) +{ + // 1. If the current global object is a Window object and this’s synchronous flag is set, + // then throw an "InvalidAccessError" DOMException. + auto& global_object = wrapper()->global_object(); + if (global_object.class_name() == "WindowObject" && m_synchronous) + return DOM::InvalidAccessError::create("Use of XMLHttpRequest's timeout attribute is not supported in the synchronous mode in window context."); + + // 2. Set this’s timeout to the given value. + m_timeout = timeout; + + return {}; +} + +// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-timeout +u32 XMLHttpRequest::timeout() const { return m_timeout; } + } diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h index b556aed0b3..678e83ce8a 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.h @@ -71,6 +71,9 @@ public: DOM::ExceptionOr override_mime_type(String const& mime); + DOM::ExceptionOr set_timeout(u32 timeout); + u32 timeout() const; + private: virtual void ref_event_target() override { ref(); } virtual void unref_event_target() override { unref(); } @@ -96,6 +99,7 @@ private: ReadyState m_ready_state { ReadyState::Unsent }; unsigned m_status { 0 }; bool m_send { false }; + u32 m_timeout { 0 }; String m_method; AK::URL m_url; diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl index 797cc4546f..56d1c9d1f7 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.idl @@ -25,6 +25,7 @@ interface XMLHttpRequest : XMLHttpRequestEventTarget { readonly attribute DOMString responseText; readonly attribute any response; attribute XMLHttpRequestResponseType responseType; + attribute unsigned long timeout; undefined open(DOMString method, DOMString url); undefined open(ByteString method, USVString url, boolean async, optional USVString? username = {}, optional USVString? password = {});