Change wxWebRequest and related objects to hide ref counting
Don't force the application code to deal with wxObjectDataPtr<> or, worse, calling {Inc,Dec}Ref() manually by hiding it inside the wx objects themselves and giving the value-like semantics to them. There should be no real changes in the behaviour, but the API does change significantly. Notably, wxWebRequest is not a wxEvtHandler itself any longer, as this would be incompatible with the value semantics, and an event handler needs to be specified when creating it, so that it could be notified about the request state changes.
This commit is contained in:
@@ -10,6 +10,170 @@
|
||||
#ifndef _WX_PRIVATE_WEBREQUEST_H_
|
||||
#define _WX_PRIVATE_WEBREQUEST_H_
|
||||
|
||||
#include "wx/ffile.h"
|
||||
#include "wx/hashmap.h"
|
||||
#include "wx/scopedptr.h"
|
||||
|
||||
WX_DECLARE_STRING_HASH_MAP(wxString, wxWebRequestHeaderMap);
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxWebAuthChallengeImpl
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class wxWebAuthChallengeImpl : public wxRefCounter
|
||||
{
|
||||
public:
|
||||
virtual ~wxWebAuthChallengeImpl() { }
|
||||
|
||||
wxWebAuthChallenge::Source GetSource() const { return m_source; }
|
||||
|
||||
virtual void
|
||||
SetCredentials(const wxString& user, const wxString& password) = 0;
|
||||
|
||||
protected:
|
||||
explicit wxWebAuthChallengeImpl(wxWebAuthChallenge::Source source)
|
||||
: m_source(source) { }
|
||||
|
||||
private:
|
||||
const wxWebAuthChallenge::Source m_source;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebAuthChallengeImpl);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxWebRequestImpl
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class wxWebRequestImpl : public wxRefCounter
|
||||
{
|
||||
public:
|
||||
virtual ~wxWebRequestImpl() { }
|
||||
|
||||
void SetHeader(const wxString& name, const wxString& value)
|
||||
{ m_headers[name] = value; }
|
||||
|
||||
void SetMethod(const wxString& method) { m_method = method; }
|
||||
|
||||
void SetData(const wxString& text, const wxString& contentType, const wxMBConv& conv = wxConvUTF8);
|
||||
|
||||
bool SetData(const wxSharedPtr<wxInputStream>& dataStream, const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset);
|
||||
|
||||
void SetIgnoreServerErrorStatus(bool ignore) { m_ignoreServerErrorStatus = ignore; }
|
||||
|
||||
void SetStorage(wxWebRequest::Storage storage) { m_storage = storage; }
|
||||
|
||||
wxWebRequest::Storage GetStorage() const { return m_storage; }
|
||||
|
||||
virtual void Start() = 0;
|
||||
|
||||
virtual void Cancel() = 0;
|
||||
|
||||
virtual wxWebResponseImplPtr GetResponse() const = 0;
|
||||
|
||||
virtual wxWebAuthChallengeImplPtr GetAuthChallenge() const = 0;
|
||||
|
||||
int GetId() const { return m_id; }
|
||||
|
||||
wxWebSession& GetSession() const { return m_session; }
|
||||
|
||||
wxWebRequest::State GetState() const { return m_state; }
|
||||
|
||||
virtual wxFileOffset GetBytesSent() const = 0;
|
||||
|
||||
virtual wxFileOffset GetBytesExpectedToSend() const = 0;
|
||||
|
||||
virtual wxFileOffset GetBytesReceived() const;
|
||||
|
||||
virtual wxFileOffset GetBytesExpectedToReceive() const;
|
||||
|
||||
void SetState(wxWebRequest::State state, const wxString& failMsg = wxString());
|
||||
|
||||
void ReportDataReceived(size_t sizeReceived);
|
||||
|
||||
wxEvtHandler* GetHandler() const { return m_handler; }
|
||||
|
||||
void ProcessStateEvent(wxWebRequest::State state, const wxString& failMsg);
|
||||
|
||||
protected:
|
||||
wxString m_method;
|
||||
wxWebRequest::Storage m_storage;
|
||||
wxWebRequestHeaderMap m_headers;
|
||||
wxFileOffset m_dataSize;
|
||||
wxSharedPtr<wxInputStream> m_dataStream;
|
||||
|
||||
wxWebRequestImpl(wxWebSession& session, wxEvtHandler* handler, int id);
|
||||
|
||||
bool CheckServerStatus();
|
||||
|
||||
static bool IsActiveState(wxWebRequest::State state);
|
||||
|
||||
private:
|
||||
wxWebSession& m_session;
|
||||
wxEvtHandler* const m_handler;
|
||||
const int m_id;
|
||||
wxWebRequest::State m_state;
|
||||
bool m_ignoreServerErrorStatus;
|
||||
wxFileOffset m_bytesReceived;
|
||||
wxCharBuffer m_dataText;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebRequestImpl);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxWebResponseImpl
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class wxWebResponseImpl : public wxRefCounter
|
||||
{
|
||||
public:
|
||||
virtual ~wxWebResponseImpl();
|
||||
|
||||
virtual wxInt64 GetContentLength() const = 0;
|
||||
|
||||
virtual wxString GetURL() const = 0;
|
||||
|
||||
virtual wxString GetHeader(const wxString& name) const = 0;
|
||||
|
||||
virtual wxString GetMimeType() const;
|
||||
|
||||
virtual int GetStatus() const = 0;
|
||||
|
||||
virtual wxString GetStatusText() const = 0;
|
||||
|
||||
virtual wxInputStream* GetStream() const;
|
||||
|
||||
virtual wxString GetSuggestedFileName() const;
|
||||
|
||||
wxString AsString() const;
|
||||
|
||||
virtual wxString GetFileName() const;
|
||||
|
||||
protected:
|
||||
wxWebRequestImpl& m_request;
|
||||
size_t m_readSize;
|
||||
|
||||
explicit wxWebResponseImpl(wxWebRequestImpl& request);
|
||||
|
||||
// Called from derived class ctor to finish initialization which can't be
|
||||
// performed in ctor itself as it needs to use pure virtual method.
|
||||
void Init();
|
||||
|
||||
void* GetDataBuffer(size_t sizeNeeded);
|
||||
|
||||
void ReportDataReceived(size_t sizeReceived);
|
||||
|
||||
private:
|
||||
// Called by wxWebRequestImpl only.
|
||||
friend class wxWebRequestImpl;
|
||||
void Finalize();
|
||||
|
||||
wxMemoryBuffer m_readBuffer;
|
||||
mutable wxFFile m_file;
|
||||
mutable wxScopedPtr<wxInputStream> m_stream;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebResponseImpl);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxWebSessionFactory
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -17,9 +181,48 @@
|
||||
class wxWebSessionFactory
|
||||
{
|
||||
public:
|
||||
virtual wxWebSession* Create() = 0;
|
||||
virtual wxWebSessionImpl* Create() = 0;
|
||||
|
||||
virtual ~wxWebSessionFactory() { }
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxWebSessionImpl
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class wxWebSessionImpl : public wxRefCounter
|
||||
{
|
||||
public:
|
||||
virtual ~wxWebSessionImpl() { }
|
||||
|
||||
virtual wxWebRequestImplPtr
|
||||
CreateRequest(wxWebSession& session,
|
||||
wxEvtHandler* handler,
|
||||
const wxString& url,
|
||||
int id) = 0;
|
||||
|
||||
virtual wxVersionInfo GetLibraryVersionInfo() = 0;
|
||||
|
||||
void SetHeader(const wxString& name, const wxString& value)
|
||||
{ m_headers[name] = value; }
|
||||
|
||||
void SetTempDir(const wxString& dir) { m_tempDir = dir; }
|
||||
|
||||
wxString GetTempDir() const;
|
||||
|
||||
const wxWebRequestHeaderMap& GetHeaders() const { return m_headers; }
|
||||
|
||||
protected:
|
||||
wxWebSessionImpl();
|
||||
|
||||
private:
|
||||
// Make it a friend to allow accessing our m_headers.
|
||||
friend class wxWebRequest;
|
||||
|
||||
wxWebRequestHeaderMap m_headers;
|
||||
wxString m_tempDir;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebSessionImpl);
|
||||
};
|
||||
|
||||
#endif // _WX_PRIVATE_WEBREQUEST_H_
|
||||
|
Reference in New Issue
Block a user