Change wxWebRequest API to use STATE event

This commit is contained in:
Tobias Taschner
2018-10-23 22:27:14 +02:00
parent d756159f5d
commit 701f697fa4
4 changed files with 91 additions and 93 deletions

View File

@@ -32,9 +32,18 @@ public:
enum State
{
State_Idle,
State_Unauthorized,
State_UnauthorizedProxy,
State_Active,
State_Ready,
State_Failed
State_Completed,
State_Failed,
State_Cancelled
};
enum CredentialTarget
{
CredentialTarget_Server,
CredentialTarget_Proxy
};
virtual ~wxWebRequest() { }
@@ -48,6 +57,9 @@ public:
void SetData(wxSharedPtr<wxInputStream> dataStream, const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset);
void SetCredentials(const wxString& user, const wxString& password,
CredentialTarget target);
void SetIgnoreServerErrorStatus(bool ignore) { m_ignoreServerErrorStatus = ignore; }
virtual void Start() = 0;
@@ -79,13 +91,10 @@ protected:
private:
int m_id;
State m_state;
wxString m_failMessage;
bool m_ignoreServerErrorStatus;
wxCharBuffer m_dataText;
void ProcessReadyEvent();
void ProcessFailedEvent();
void ProcessStateEvent(State state, const wxString& failMsg);
wxDECLARE_NO_COPY_CLASS(wxWebRequest);
};
@@ -167,12 +176,14 @@ class WXDLLIMPEXP_NET wxWebRequestEvent : public wxEvent
{
public:
wxWebRequestEvent() {}
wxWebRequestEvent(wxEventType type, int id, wxWebResponse* response = NULL,
const wxString& errorDesc = "")
wxWebRequestEvent(wxEventType type, int id, wxWebRequest::State state,
wxWebResponse* response = NULL, const wxString& errorDesc = "")
: wxEvent(id, type),
m_response(response), m_errorDescription(errorDesc)
m_state(state), m_response(response), m_errorDescription(errorDesc)
{ }
wxWebRequest::State GetState() const { return m_state; }
wxWebResponse* GetResponse() const { return m_response; }
const wxString& GetErrorDescription() const { return m_errorDescription; }
@@ -180,14 +191,15 @@ public:
wxEvent* Clone() const wxOVERRIDE { return new wxWebRequestEvent(*this); }
private:
wxWebRequest::State m_state;
wxWebResponse* m_response;
wxString m_errorDescription;
};
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_READY, wxWebRequestEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_FAILED, wxWebRequestEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_AUTH_REQUIRED, wxWebRequestEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_STATE, wxWebRequestEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_DATA, wxWebRequestEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_DOWNLOAD_PROGRESS, wxWebRequestEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_UPLOAD_PROGRESS, wxWebRequestEvent);
#endif // wxUSE_WEBREQUEST

View File

@@ -136,8 +136,8 @@ public:
wxObjectDataPtr<wxWebRequest> request(wxWebSession::GetDefault().CreateRequest(
m_urlTextCtrl->GetValue()));
// Bind event for failure
request->Bind(wxEVT_WEBREQUEST_FAILED, &WebRequestFrame::OnWebRequestFailed, this);
// Bind event for state change
request->Bind(wxEVT_WEBREQUEST_STATE, &WebRequestFrame::OnWebRequestState, this);
// Prepare request based on selected action
switch (m_notebook->GetSelection())
@@ -145,8 +145,6 @@ public:
case Page_Image:
// Reset static bitmap image
m_imageStaticBitmap->SetBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE));
// Bind completion event to response as image
request->Bind(wxEVT_WEBREQUEST_READY, &WebRequestFrame::OnImageRequestReady, this);
break;
case Page_Text:
// Reset response text control
@@ -158,8 +156,6 @@ public:
request->SetData(m_postRequestTextCtrl->GetValue(),
m_postContentTypeTextCtrl->GetValue());
}
request->Bind(wxEVT_WEBREQUEST_READY, &WebRequestFrame::OnTextRequestReady, this);
break;
case Page_Download:
// TODO: implement
@@ -175,30 +171,37 @@ public:
request->Start();
}
void OnImageRequestReady(wxWebRequestEvent& evt)
void OnWebRequestState(wxWebRequestEvent& evt)
{
m_startButton->Enable(evt.GetState() != wxWebRequest::State_Active);
switch (evt.GetState())
{
case wxWebRequest::State_Completed:
switch (m_notebook->GetSelection())
{
case Page_Image:
{
wxImage img(*evt.GetResponse()->GetStream());
m_imageStaticBitmap->SetBitmap(img);
m_notebook->GetPage(Page_Image)->Layout();
GetStatusBar()->SetStatusText(wxString::Format("Loaded %lld bytes image data", evt.GetResponse()->GetContentLength()));
m_startButton->Enable();
break;
}
void OnTextRequestReady(wxWebRequestEvent& evt)
{
case Page_Text:
m_textResponseTextCtrl->SetValue(evt.GetResponse()->AsString());
GetStatusBar()->SetStatusText(wxString::Format("Loaded %lld bytes text data (Status: %d %s)",
evt.GetResponse()->GetContentLength(),
evt.GetResponse()->GetStatus(),
evt.GetResponse()->GetStatusText()));
m_startButton->Enable();
break;
}
void OnWebRequestFailed(wxWebRequestEvent& evt)
{
break;
case wxWebRequest::State_Failed:
wxLogError("Web Request failed: %s", evt.GetErrorDescription());
GetStatusBar()->SetStatusText("");
m_startButton->Enable();
}
}
void OnPostCheckBox(wxCommandEvent& WXUNUSED(evt))

View File

@@ -36,9 +36,10 @@ extern WXDLLIMPEXP_DATA_NET(const char) wxWebSessionBackendCURL[] = "wxWebSessio
extern WXDLLIMPEXP_DATA_NET(const char) wxWebSessionBackendDefault[] = "wxWebSessionBackendWinHTTP";
#endif
wxDEFINE_EVENT(wxEVT_WEBREQUEST_READY, wxWebRequestEvent);
wxDEFINE_EVENT(wxEVT_WEBREQUEST_FAILED, wxWebRequestEvent);
wxDEFINE_EVENT(wxEVT_WEBREQUEST_AUTH_REQUIRED, wxWebRequestEvent);
wxDEFINE_EVENT(wxEVT_WEBREQUEST_STATE, wxWebRequestEvent);
wxDEFINE_EVENT(wxEVT_WEBREQUEST_DATA, wxWebRequestEvent);
wxDEFINE_EVENT(wxEVT_WEBREQUEST_DOWNLOAD_PROGRESS, wxWebRequestEvent);
wxDEFINE_EVENT(wxEVT_WEBREQUEST_UPLOAD_PROGRESS, wxWebRequestEvent);
//
// wxWebRequest
@@ -82,49 +83,31 @@ void wxWebRequest::SetData(wxSharedPtr<wxInputStream> dataStream, const wxString
SetHeader("Content-Type", contentType);
}
void wxWebRequest::SetCredentials(const wxString & user, const wxString & password, CredentialTarget target)
{
wxFAIL_MSG("not implemented");
}
void wxWebRequest::SetState(State state, const wxString & failMsg)
{
switch (state)
{
case State_Active:
// Add a reference while the request is active
if ( m_state != State_Active )
{
if (state == State_Active && m_state != State_Active)
IncRef();
// Trigger the event in the main thread
CallAfter(&wxWebRequest::ProcessStateEvent, state, failMsg);
}
void wxWebRequest::ProcessStateEvent(State state, const wxString& failMsg)
{
wxWebRequestEvent evt(wxEVT_WEBREQUEST_STATE, GetId(), state,
GetResponse(), failMsg);
ProcessEvent(evt);
// Remove reference after the request is no longer active
if (state == State_Completed || state == State_Failed ||
state == State_Cancelled)
DecRef();
m_state = state;
}
break;
case State_Ready:
// Trigger the ready event in main thread
CallAfter(&wxWebRequest::ProcessReadyEvent);
break;
case State_Failed:
m_failMessage = failMsg;
// Trigger the failed event in main thread
CallAfter(&wxWebRequest::ProcessFailedEvent);
break;
}
}
void wxWebRequest::ProcessReadyEvent()
{
wxWebRequestEvent evt(wxEVT_WEBREQUEST_READY, GetId(), GetResponse());
ProcessEvent(evt);
// Remove reference after the request is no longer active
if ( m_state == State_Active )
DecRef();
m_state = State_Ready;
}
void wxWebRequest::ProcessFailedEvent()
{
wxWebRequestEvent evt(wxEVT_WEBREQUEST_FAILED, GetId(), NULL,
m_failMessage);
ProcessEvent(evt);
// Remove reference after the request is no longer active
if ( m_state == State_Active )
DecRef();
m_state = State_Failed;
}
//

View File

@@ -180,7 +180,7 @@ void wxWebRequestWinHTTP::HandleCallback(DWORD dwInternetStatus,
else
{
m_response->ReportDataComplete();
SetState(State_Ready);
SetState(State_Completed);
}
break;
case WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE:
@@ -319,7 +319,7 @@ void wxWebRequestWinHTTP::Start()
void wxWebRequestWinHTTP::Cancel()
{
// TODO: implement
wxFAIL_MSG("not implemented");
}
wxWebResponse* wxWebRequestWinHTTP::GetResponse()