diff --git a/include/wx/webrequest.h b/include/wx/webrequest.h index be4cec2a15..ffeabf7036 100644 --- a/include/wx/webrequest.h +++ b/include/wx/webrequest.h @@ -257,7 +257,7 @@ public: const wxWebResponse& response = wxWebResponse(), const wxString& errorDesc = wxString()) : wxEvent(id, type), - m_state(state), m_response(response), m_data(NULL), m_dataSize(0), + m_state(state), m_response(response), m_errorDescription(errorDesc) { } @@ -271,12 +271,11 @@ public: void SetDataFile(const wxString& dataFile) { m_dataFile = dataFile; } - const void* GetDataBuffer() const { return m_data; } + const void* GetDataBuffer() const { return m_dataBuf.GetData(); } - size_t GetDataSize() const { return m_dataSize; } + size_t GetDataSize() const { return m_dataBuf.GetDataLen(); } - void SetDataBuffer(const void* buffer, size_t size) - { m_data = buffer; m_dataSize = size; } + void SetDataBuffer(const wxMemoryBuffer& dataBuf) { m_dataBuf = dataBuf; } wxEvent* Clone() const wxOVERRIDE { return new wxWebRequestEvent(*this); } @@ -284,8 +283,7 @@ private: wxWebRequest::State m_state; const wxWebResponse m_response; // may be invalid wxString m_dataFile; - const void* m_data; - size_t m_dataSize; + wxMemoryBuffer m_dataBuf; wxString m_errorDescription; }; diff --git a/samples/webrequest/webrequest.cpp b/samples/webrequest/webrequest.cpp index 5dca3b3188..3a16eda371 100644 --- a/samples/webrequest/webrequest.cpp +++ b/samples/webrequest/webrequest.cpp @@ -364,22 +364,19 @@ public: void OnRequestData(wxWebRequestEvent& evt) { // Count zero bytes in data buffer - bool notify = false; - const char* p = (const char*) evt.GetDataBuffer(); for ( size_t i = 0; i < evt.GetDataSize(); i++ ) { if ( *p == 0 ) - { m_advCount++; - notify = true; - } p++; } - if ( notify ) - CallAfter(&WebRequestFrame::UpdateAdvCount); + UpdateAdvCount(); + + // Make sure the new text is immediately visible. + m_advCountStaticText->Update(); } void UpdateAdvCount() @@ -425,7 +422,7 @@ public: defaultURL = "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.1/wxWidgets-3.1.1.7z"; break; case Page_Advanced: - defaultURL = "https://httpbin.org/bytes/64000"; + defaultURL = "https://httpbin.org/bytes/100000"; break; } m_urlTextCtrl->SetValue(defaultURL); diff --git a/src/common/webrequest.cpp b/src/common/webrequest.cpp index c3c9b1b4fa..50cae4b05f 100644 --- a/src/common/webrequest.cpp +++ b/src/common/webrequest.cpp @@ -692,10 +692,20 @@ void wxWebResponseImpl::ReportDataReceived(size_t sizeReceived) break; case wxWebRequest::Storage_None: - wxWebRequestEvent evt(wxEVT_WEBREQUEST_DATA, m_request.GetId(), wxWebRequest::State_Active); - evt.SetDataBuffer(m_readBuffer.GetData(), m_readBuffer.GetDataLen()); - m_request.GetHandler()->ProcessEvent(evt); - m_readBuffer.Clear(); + wxWebRequestEvent* const evt = new wxWebRequestEvent + ( + wxEVT_WEBREQUEST_DATA, + m_request.GetId(), + wxWebRequest::State_Active + ); + evt->SetDataBuffer(m_readBuffer); + + m_request.GetHandler()->QueueEvent(evt); + + // Make sure we switch to a different buffer instead of just + // clearing the current one, which will be needed by the event + // handler when it's finally called in the main thread. + m_readBuffer = wxMemoryBuffer(); break; } }