| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- #include "./downloadResult.h"
- #include "./loginStatus.h"
- #include "../api.h"
- #include "../resource.h"
- #include "../jnetlib/api_httpget.h"
- #include <strsafe.h>
- LoginDownloadResult::LoginDownloadResult(api_downloadManager *pManager, UINT uType, Callback fnCallback, void *pData, LoginStatus *pStatus)
- : ref(1), manager(pManager), flags(0), callback(fnCallback), data(pData),
- address(NULL), result(api_downloadManager::TICK_NODATA),
- cookie(0), completed(NULL), status(pStatus), statusCookie((UINT)-1)
- {
- InitializeCriticalSection(&lock);
-
- SetType(uType);
- SetState(stateInitializing);
- if (NULL != status)
- status->AddRef();
- if (NULL != manager)
- manager->AddRef();
- }
- LoginDownloadResult::~LoginDownloadResult()
- {
- EnterCriticalSection(&lock);
-
- if(NULL != manager)
- {
- if (0 != cookie)
- {
- manager->ReleaseDownload(cookie);
- cookie = 0;
- }
- manager->Release();
- }
- if (NULL != completed)
- CloseHandle(completed);
-
- if (NULL != address)
- free(address);
- if (NULL != status)
- {
- if (((UINT)-1) != statusCookie)
- status->Remove(statusCookie);
- status->Release();
- }
- LeaveCriticalSection(&lock);
- DeleteCriticalSection(&lock);
- }
- HRESULT LoginDownloadResult::CreateInstance(api_downloadManager *pManager, UINT uType, Callback fnCallback, void *pData, LoginStatus *pStatus, LoginDownloadResult **instance)
- {
- if (NULL == instance) return E_POINTER;
-
- if (NULL == pManager)
- {
- *instance = NULL;
- return E_INVALIDARG;
- }
- *instance = new LoginDownloadResult(pManager, uType, fnCallback, pData, pStatus);
- if (NULL == *instance) return E_OUTOFMEMORY;
- return S_OK;
- }
- size_t LoginDownloadResult::AddRef()
- {
- return InterlockedIncrement((LONG*)&ref);
- }
- size_t LoginDownloadResult::Release()
- {
- if (0 == ref)
- return ref;
-
- LONG r = InterlockedDecrement((LONG*)&ref);
- if (0 == r)
- delete(this);
-
- return r;
- }
- int LoginDownloadResult::QueryInterface(GUID interface_guid, void **object)
- {
- if (NULL == object) return E_POINTER;
- *object = NULL;
- return E_NOINTERFACE;
- }
- void LoginDownloadResult::SetState(UINT uState)
- {
- flags = (flags & ~stateMask) | (uState & stateMask);
- }
- void LoginDownloadResult::SetType(UINT uType)
- {
- flags = (flags & ~typeMask) | (uType & typeMask);
- }
- void LoginDownloadResult::SetFlags(UINT uFlags, UINT uMask)
- {
- uMask &= flagsMask;
- flags = (flags & ~uMask) | (uFlags & uMask);
- }
- HRESULT LoginDownloadResult::GetState(UINT *state)
- {
- if (NULL == state)
- return E_POINTER;
- EnterCriticalSection(&lock);
- *state = (flags & stateMask);
- LeaveCriticalSection(&lock);
-
- return S_OK;
- }
- HRESULT LoginDownloadResult::GetType(UINT *type)
- {
- if (NULL == type)
- return E_POINTER;
- EnterCriticalSection(&lock);
- *type = (flags & typeMask);
- LeaveCriticalSection(&lock);
-
- return S_OK;
- }
- HRESULT LoginDownloadResult::GetFile(LPCWSTR *ppszPath)
- {
- EnterCriticalSection(&lock);
- HRESULT hr;
- if (NULL == cookie || NULL == manager)
- {
- hr = E_UNEXPECTED;
- }
- else if (stateCompleted != (stateMask & flags))
- {
- hr = E_DWNLD_BUSY;
- }
- else
- {
- switch(result)
- {
- case api_downloadManager::TICK_SUCCESS: hr = E_DWNLD_OK; break;
- case api_downloadManager::TICK_FAILURE: hr = (0 != (flagUserAbort & flags)) ? E_DWNLD_ABORT : E_DWNLD_FAIL; break;
- case api_downloadManager::TICK_TIMEOUT: hr = E_DWNLD_TIMEOUT; break;
- case api_downloadManager::TICK_CANT_CONNECT: hr = E_DWNLD_CANT_CONNECT; break;
- case api_downloadManager::TICK_WRITE_ERROR: hr = E_DWNLD_WRITE_ERROR; break;
- default: hr = E_DWNLD_BUSY; break;
- }
- }
-
- if (NULL != ppszPath)
- {
- if (SUCCEEDED(hr))
- *ppszPath = manager->GetLocation(cookie);
- else
- *ppszPath = NULL;
- }
- LeaveCriticalSection(&lock);
-
- return hr;
- }
-
- HRESULT LoginDownloadResult::GetWaitHandle(HANDLE *handle)
- {
- if (NULL == handle)
- return E_POINTER;
- HRESULT hr = S_OK;
- EnterCriticalSection(&lock);
- if (NULL == completed)
- {
- completed = CreateEvent(NULL, TRUE, FALSE, NULL);
- if (NULL == completed)
- {
- *handle = NULL;
- DWORD error = GetLastError();
- hr = HRESULT_FROM_WIN32(error);
- }
- }
- if (SUCCEEDED(hr) && FALSE == DuplicateHandle(GetCurrentProcess(), completed,
- GetCurrentProcess(), handle, 0, FALSE, DUPLICATE_SAME_ACCESS))
- {
- *handle = NULL;
- DWORD error = GetLastError();
- hr = HRESULT_FROM_WIN32(error);
- }
-
- LeaveCriticalSection(&lock);
- return hr;
- }
- HRESULT LoginDownloadResult::GetData(void **data)
- {
- if (NULL == data)
- return E_POINTER;
- EnterCriticalSection(&lock);
- *data = this->data;
- LeaveCriticalSection(&lock);
- return S_OK;
- }
- HRESULT LoginDownloadResult::RequestAbort(BOOL fDrop)
- {
- EnterCriticalSection(&lock);
- if (FALSE != fDrop)
- {
- data = NULL;
- callback = NULL;
- }
- if (0 != cookie && NULL != manager && 0 == (flagUserAbort & flags))
- {
- manager->CancelDownload(cookie);
- SetState(stateAborting);
- SetFlags(flagUserAbort, flagUserAbort);
- }
-
- LeaveCriticalSection(&lock);
- return S_OK;
- }
- HRESULT LoginDownloadResult::GetUrl(LPSTR pszBuffer, UINT cchBufferMax)
- {
- if (NULL == pszBuffer)
- return E_POINTER;
- HRESULT hr;
- EnterCriticalSection(&lock);
- if(NULL == manager || 0 == cookie)
- hr = E_UNEXPECTED;
- else
- {
- api_httpreceiver *receiver = manager->GetReceiver(cookie);
- if (NULL == receiver) hr = E_FAIL;
- else
- {
- LPCSTR url = receiver->get_url();
- hr = StringCchCopyExA(pszBuffer, cchBufferMax, url, NULL, NULL, STRSAFE_IGNORE_NULLS);
- }
- }
-
- LeaveCriticalSection(&lock);
- return hr;
- }
- void LoginDownloadResult::SetStatus()
- {
- EnterCriticalSection(&lock);
- if (NULL != status && ((UINT)-1 == statusCookie))
- {
- LPCWSTR pszStatus;
- switch(typeMask & flags)
- {
- case typeProviderList: pszStatus = MAKEINTRESOURCE(IDS_STATUS_UPDATEBEGIN); break;
- default: pszStatus = NULL; break;
- }
-
- if (NULL != pszStatus)
- {
- BSTR bstrText;
- if (FALSE == IS_INTRESOURCE(pszStatus))
- bstrText = SysAllocString(pszStatus);
- else
- {
- WCHAR szBuffer[256] = {0};
- WASABI_API_LNGSTRINGW_BUF((INT)(INT_PTR)pszStatus, szBuffer, ARRAYSIZE(szBuffer));
- bstrText = SysAllocString(szBuffer);
- }
- statusCookie = status->Add(bstrText);
- if (((UINT)-1) == statusCookie)
- SysFreeString(bstrText);
- }
- }
- LeaveCriticalSection(&lock);
- }
- void LoginDownloadResult::RemoveStatus()
- {
- EnterCriticalSection(&lock);
- if (NULL != status)
- {
- if (((UINT)-1) != statusCookie)
- {
- status->Remove(statusCookie);
- statusCookie = (UINT)-1;
- }
- }
- LeaveCriticalSection(&lock);
- }
- HRESULT LoginDownloadResult::CreateDownloadFileName(LPSTR pszBuffer, UINT cchBufferMax)
- {
- if (NULL == pszBuffer)
- return E_POINTER;
- HRESULT hr;
- EnterCriticalSection(&lock);
- if(NULL == manager || 0 == cookie)
- hr = E_UNEXPECTED;
- else
- {
- api_httpreceiver *receiver = manager->GetReceiver(cookie);
- if (NULL == receiver) hr = E_FAIL;
- else
- {
- LPCSTR url = receiver->get_url();
- LPCSTR contentDisposition = receiver->getheader("content-disposition");
- LPCSTR contentType = receiver->getheader("content-type");
- hr = S_OK;
- }
- }
-
- LeaveCriticalSection(&lock);
- return hr;
- }
- void LoginDownloadResult::DownloadCompleted(int errorCode)
- {
- EnterCriticalSection(&lock);
-
- result = errorCode;
- SetState(stateCompleted);
- HANDLE hEvent = completed;
- Callback cb = callback;
-
- LeaveCriticalSection(&lock);
- if (NULL != hEvent)
- SetEvent(hEvent);
-
- if (NULL != cb)
- cb(this, data);
- RemoveStatus();
- Release();
- }
- void LoginDownloadResult::Event_DownloadInit(DownloadToken token)
- {
- EnterCriticalSection(&lock);
- AddRef();
- cookie = token;
- SetState(stateConnecting);
- if (NULL != manager)
- {
- manager->RetainDownload(cookie);
-
- if (typeProviderList == (typeMask & flags))
- {
- api_httpreceiver *receiver = manager->GetReceiver(token);
- if (NULL != receiver)
- receiver->AllowCompression();
- }
- }
-
- LeaveCriticalSection(&lock);
- SetStatus();
-
- }
- void LoginDownloadResult::Event_DownloadConnect(DownloadToken token)
- {
- EnterCriticalSection(&lock);
- SetState(stateReceiving);
- LeaveCriticalSection(&lock);
- }
- void LoginDownloadResult::Event_DownloadTick(DownloadToken token)
- {
- EnterCriticalSection(&lock);
- if (stateConnecting == (stateMask & flags))
- SetState(stateReceiving);
- LeaveCriticalSection(&lock);
- }
- void LoginDownloadResult::Event_DownloadFinish(DownloadToken token)
- {
- DownloadCompleted(api_downloadManager::TICK_SUCCESS);
- }
- void LoginDownloadResult::Event_DownloadError(DownloadToken token, int errorCode)
- {
- DownloadCompleted(errorCode);
- }
- void LoginDownloadResult::Event_DownloadCancel(DownloadToken token)
- {
- DownloadCompleted(api_downloadManager::TICK_NODATA);
- }
- #define CBCLASS LoginDownloadResult
- START_DISPATCH;
- CB(ADDREF, AddRef)
- CB(RELEASE, Release)
- CB(QUERYINTERFACE, QueryInterface)
- VCB(IFC_DOWNLOADMANAGERCALLBACK_ONINIT, Event_DownloadInit)
- VCB(IFC_DOWNLOADMANAGERCALLBACK_ONCONNECT, Event_DownloadConnect)
- VCB(IFC_DOWNLOADMANAGERCALLBACK_ONFINISH, Event_DownloadFinish)
- VCB(IFC_DOWNLOADMANAGERCALLBACK_ONERROR, Event_DownloadError)
- VCB(IFC_DOWNLOADMANAGERCALLBACK_ONCANCEL, Event_DownloadCancel)
- VCB(IFC_DOWNLOADMANAGERCALLBACK_ONTICK, Event_DownloadTick)
- END_DISPATCH;
- #undef CBCLASS
|