Make wxWebResponse::Init() and Finalize() non-public

The former can be called from the derived class ctors while the latter
only needs to be called from wxWebRequest itself, so just make it a
friend: this is not ideal, but still better than leaving this public and
simpler than any alternatives.
This commit is contained in:
Vadim Zeitlin
2020-12-13 01:33:01 +01:00
parent 1c61fe6baf
commit 8ea4f38689
5 changed files with 21 additions and 11 deletions

View File

@@ -154,10 +154,6 @@ public:
wxString AsString() const;
bool Init();
void Finalize();
virtual wxString GetFileName() const;
protected:
@@ -166,15 +162,24 @@ protected:
wxWebResponse(wxWebRequest& 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 wxWebRequest only.
void Finalize();
wxMemoryBuffer m_readBuffer;
mutable wxFFile m_file;
mutable wxScopedPtr<wxInputStream> m_stream;
friend class wxWebRequest;
wxDECLARE_NO_COPY_CLASS(wxWebResponse);
};

View File

@@ -260,7 +260,7 @@ wxWebResponse::~wxWebResponse()
wxRemoveFile(m_file.GetName());
}
bool wxWebResponse::Init()
void wxWebResponse::Init()
{
if ( m_request.GetStorage() == wxWebRequest::Storage_File )
{
@@ -274,15 +274,13 @@ bool wxWebResponse::Init()
GetContentLength() > freeSpace )
{
m_request.SetState(wxWebRequest::State_Failed, _("Not enough free disk space for download."));
return false;
return;
}
}
tmpPrefix.SetName("wxd");
wxFileName::CreateTempFileName(tmpPrefix.GetFullPath(), &m_file);
}
return true;
}
wxString wxWebResponse::GetMimeType() const

View File

@@ -63,6 +63,8 @@ wxWebResponseCURL::wxWebResponseCURL(wxWebRequest& request) :
{
curl_easy_setopt(GetHandle(), CURLOPT_WRITEDATA, static_cast<void*>(this));
curl_easy_setopt(GetHandle(), CURLOPT_HEADERDATA, static_cast<void*>(this));
Init();
}
size_t wxWebResponseCURL::WriteData(void* buffer, size_t size)
@@ -192,7 +194,6 @@ void wxWebRequestCURL::Start()
return;
m_response.reset(new wxWebResponseCURL(*this));
m_response->Init();
if ( m_dataSize )
{

View File

@@ -220,8 +220,11 @@ void wxWebRequestWinHTTP::CreateResponse()
if ( ::WinHttpReceiveResponse(m_request, NULL) )
{
m_response.reset(new wxWebResponseWinHTTP(*this));
if ( !m_response->Init() )
// wxWebResponseWinHTTP ctor could have changed the state if its
// initialization failed, so check for this.
if ( GetState() == State_Failed )
return;
int status = m_response->GetStatus();
if ( status == 401 || status == 407)
{
@@ -364,6 +367,8 @@ wxWebResponseWinHTTP::wxWebResponseWinHTTP(wxWebRequestWinHTTP& request):
if ( contentLengthStr.empty() ||
!contentLengthStr.ToLongLong(&m_contentLength) )
m_contentLength = -1;
Init();
}
wxString wxWebResponseWinHTTP::GetURL() const

View File

@@ -140,7 +140,6 @@ void wxWebRequestURLSession::Start()
[session.GetDelegate() registerRequest:this task:m_task];
m_response.reset(new wxWebResponseURLSession(*this, m_task));
m_response->Init();
SetState(State_Active);
[m_task resume];
@@ -191,6 +190,8 @@ wxWebResponseURLSession::wxWebResponseURLSession(wxWebRequest& request, WX_NSURL
wxWebResponse(request)
{
m_task = [task retain];
Init();
}
wxWebResponseURLSession::~wxWebResponseURLSession()