From 3241c443c5d207184b24d488e4e0caeb6d78345a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 16 Jan 2021 14:59:41 +0100 Subject: [PATCH] Process event about the request becoming active synchronously This is required in order to allow doing something with the request when it already have a valid native handle, but hasn't actually started yet. --- interface/wx/webrequest.h | 14 +++++++++++++- src/common/webrequest.cpp | 17 +++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/interface/wx/webrequest.h b/interface/wx/webrequest.h index 66e8ed41cd..79ed1ac312 100644 --- a/interface/wx/webrequest.h +++ b/interface/wx/webrequest.h @@ -137,7 +137,14 @@ public: */ State_Unauthorized, - /// The request has been started and is transferring data + /** + The request is about to start. + + An event notifying about the switch to this state is generated when + Start() is called (unless an error occurs, in which case the state + becomes State_Failed instead). Handling this event allows to do + something right before the asynchronous request actually starts. + */ State_Active, /** @@ -222,6 +229,11 @@ public: - For CURL backend, this is a @c CURL struct pointer. - For macOS backend, this is @c NSURLSessionTask object pointer. + Note that this function returns a valid value only after the request is + started successfully using Start(). Notably, it is guaranteed to return + a valid value when handling wxWebRequestEvent corresponding to the + switch to @c State_Active. + @see wxWebSession::GetNativeHandle() */ wxWebRequestHandle GetNativeHandle() const; diff --git a/src/common/webrequest.cpp b/src/common/webrequest.cpp index f38824f691..0d1ef2b068 100644 --- a/src/common/webrequest.cpp +++ b/src/common/webrequest.cpp @@ -208,8 +208,21 @@ void wxWebRequestImpl::SetState(wxWebRequest::State state, const wxString & fail m_state = state; - // Trigger the event in the main thread - m_handler->CallAfter(StateEventProcessor(*this, state, failMsg)); + // Trigger the event in the main thread except when switching to the active + // state because this always happens in the main thread anyhow and it's + // important to process it synchronously, before the request actually + // starts (this gives the possibility to modify the request using native + // functions, for example, as its GetNativeHandle() is already valid). + if ( state == wxWebRequest::State_Active ) + { + wxASSERT( wxIsMainThread() ); + + ProcessStateEvent(state, failMsg); + } + else + { + m_handler->CallAfter(StateEventProcessor(*this, state, failMsg)); + } } void wxWebRequestImpl::ReportDataReceived(size_t sizeReceived)