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.
This commit is contained in:
Vadim Zeitlin 2021-01-16 14:59:41 +01:00
parent 1bf6d5e188
commit 3241c443c5
2 changed files with 28 additions and 3 deletions

View File

@ -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;

View File

@ -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)