123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- #include "main.h"
- #include "./storageDwnld.h"
- #include "./ifc_omwebstorage.h"
- #include "./ifc_wasabihelper.h"
- #include "./ifc_omservicehost.h"
- #include "..\Components\wac_network\wac_network_http_receiver_api.h"
- OmStorageDwnld::OmStorageDwnld(api_downloadManager *downloadManager, BOOL enableCompression)
- : ref(1), userCallback(NULL), userData(NULL), completed(NULL), flags(0),
- opState(stateInitializing), resultCode(api_downloadManager::TICK_SUCCESS),
- cookie(NULL), manager(downloadManager), serviceHost(NULL)
- {
- if (NULL != manager)
- manager->AddRef();
- if (FALSE != enableCompression)
- flags |= flagEnableCompression;
- InitializeCriticalSection(&lock);
- }
- OmStorageDwnld::~OmStorageDwnld()
- {
- EnterCriticalSection(&lock);
- if (NULL != manager)
- {
- if (NULL != cookie)
- {
- manager->ReleaseDownload(cookie);
- cookie = NULL;
- }
- manager->Release();
- manager = NULL;
- }
- if (NULL != completed)
- {
- CloseHandle(completed);
- completed = NULL;
- }
- if (NULL != serviceHost)
- {
- serviceHost->Release();
- serviceHost = NULL;
- }
-
- LeaveCriticalSection(&lock);
- DeleteCriticalSection(&lock);
- }
- HRESULT OmStorageDwnld::CreateInstance(api_downloadManager *downloadManager, BOOL enableCompression, OmStorageDwnld **instance)
- {
- if (NULL == instance) return E_POINTER;
-
- *instance = NULL;
- if (NULL == downloadManager)
- return E_INVALIDARG;
- *instance = new OmStorageDwnld(downloadManager, enableCompression);
- if (NULL == *instance) return E_OUTOFMEMORY;
-
- return S_OK;
- }
- size_t OmStorageDwnld::AddRef()
- {
- return InterlockedIncrement((LONG*)&ref);
- }
- size_t OmStorageDwnld::Release()
- {
- if (0 == ref)
- return ref;
-
- LONG r = InterlockedDecrement((LONG*)&ref);
- if (0 == r)
- delete(this);
-
- return r;
- }
- int OmStorageDwnld::QueryInterface(GUID interface_guid, void **object)
- {
- if (NULL == object) return E_POINTER;
-
- if (IsEqualIID(interface_guid, IFC_OmStorageAsync))
- *object = static_cast<ifc_omstorageasync*>(this);
- else
- {
- *object = NULL;
- return E_NOINTERFACE;
- }
- if (NULL == *object)
- return E_UNEXPECTED;
- AddRef();
- return S_OK;
- }
- HRESULT OmStorageDwnld::GetState(UINT *state)
- {
- if (NULL == state)
- return E_POINTER;
- EnterCriticalSection(&lock);
- *state = opState;
- LeaveCriticalSection(&lock);
-
- return S_OK;
- }
- HRESULT OmStorageDwnld::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 OmStorageDwnld::GetData(void **data)
- {
- if (NULL == data)
- return E_POINTER;
- EnterCriticalSection(&lock);
- *data = userData;
- LeaveCriticalSection(&lock);
- return S_OK;
- }
- HRESULT OmStorageDwnld::GetCallback(AsyncCallback *callback)
- {
- if (NULL == callback)
- return E_POINTER;
- EnterCriticalSection(&lock);
- *callback = userCallback;
- LeaveCriticalSection(&lock);
- return S_OK;
- }
- void OmStorageDwnld::OnInit(DownloadToken token)
- {
- EnterCriticalSection(&lock);
- AddRef();
- cookie = token;
- opState = stateConnecting;
- if (NULL != manager)
- {
- manager->RetainDownload(cookie);
-
- if (0 != (flagEnableCompression & flags))
- {
- api_httpreceiver *receiver = manager->GetReceiver(token);
- if (NULL != receiver)
- receiver->AllowCompression();
- }
- }
- LeaveCriticalSection(&lock);
- }
- void OmStorageDwnld::OnConnect(DownloadToken token)
- {
- EnterCriticalSection(&lock);
- opState = stateReceiving;
- LeaveCriticalSection(&lock);
- }
- void OmStorageDwnld::OnTick(DownloadToken token)
- {
- EnterCriticalSection(&lock);
- if (stateConnecting == opState)
- opState = stateReceiving;
- LeaveCriticalSection(&lock);
- }
- void OmStorageDwnld::OnFinish(DownloadToken token)
- {
- DownloadCompleted(api_downloadManager::TICK_SUCCESS);
- }
- void OmStorageDwnld::OnError(DownloadToken token, int errorCode)
- {
- DownloadCompleted(errorCode);
- }
- void OmStorageDwnld::OnCancel(DownloadToken token)
- {
- DownloadCompleted(api_downloadManager::TICK_NODATA);
- }
- void OmStorageDwnld::DownloadCompleted( INT errorCode )
- {
- EnterCriticalSection( &lock );
- resultCode = errorCode;
- opState = stateCompleted;
- HANDLE event = completed;
- LeaveCriticalSection( &lock );
- if ( NULL != event )
- {
- SetEvent( event );
- }
- EnterCriticalSection( &lock );
- AsyncCallback cb = userCallback;
- LeaveCriticalSection( &lock );
- if ( NULL != cb )
- {
- cb( this );
- }
- Release();
- }
- HRESULT OmStorageDwnld::SetData(void *data)
- {
- EnterCriticalSection(&lock);
- userData = data;
- LeaveCriticalSection(&lock);
- return S_OK;
- }
- HRESULT OmStorageDwnld::SetCallback(AsyncCallback callback)
- {
- EnterCriticalSection(&lock);
- userCallback = callback;
- LeaveCriticalSection(&lock);
- return S_OK;
- }
- HRESULT OmStorageDwnld::GetResultCode()
- {
- EnterCriticalSection(&lock);
- HRESULT hr;
- if (NULL == cookie || NULL == manager)
- {
- hr = E_UNEXPECTED;
- }
- else
- {
- switch(resultCode)
- {
- 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;
- }
- }
- LeaveCriticalSection(&lock);
- return hr;
- }
- HRESULT OmStorageDwnld::GetBuffer(void **buffer, size_t *bufferSize)
- {
- if (NULL == buffer) return E_POINTER;
- EnterCriticalSection(&lock);
- HRESULT hr = GetResultCode();
- if (SUCCEEDED(hr) && 0 != manager->GetBuffer(cookie, buffer, bufferSize))
- hr = E_DWNLD_FAIL;
- LeaveCriticalSection(&lock);
- if (FAILED(hr))
- {
- *buffer = NULL;
- *bufferSize = 0;
- }
- return hr;
- }
- HRESULT OmStorageDwnld::RequestAbort(BOOL fDrop)
- {
- EnterCriticalSection(&lock);
- if (FALSE != fDrop)
- {
- userData = NULL;
- userCallback = NULL;
- }
- if (NULL != cookie && NULL != manager)
- {
- opState = stateAborting;
- flags |= flagUserAbort;
- manager->CancelDownload(cookie);
- }
- LeaveCriticalSection(&lock);
- return S_OK;
- }
- HRESULT OmStorageDwnld::SetServiceHost(ifc_omservicehost *host)
- {
- EnterCriticalSection(&lock);
-
- if (NULL != serviceHost)
- serviceHost->Release();
- serviceHost = host;
- if (NULL != serviceHost)
- serviceHost->AddRef();
- LeaveCriticalSection(&lock);
- return S_OK;
- }
- HRESULT OmStorageDwnld::GetServiceHost(ifc_omservicehost **host)
- {
- if (NULL == host)
- return E_POINTER;
- EnterCriticalSection(&lock);
- *host = serviceHost;
- if (NULL != serviceHost)
- serviceHost->AddRef();
- LeaveCriticalSection(&lock);
- return S_OK;
- }
- #define CBCLASS OmStorageDwnld
- START_MULTIPATCH;
- START_PATCH(MPIID_OMSTORAGEASYNC)
- M_CB(MPIID_OMSTORAGEASYNC, ifc_omstorageasync, ADDREF, AddRef);
- M_CB(MPIID_OMSTORAGEASYNC, ifc_omstorageasync, RELEASE, Release);
- M_CB(MPIID_OMSTORAGEASYNC, ifc_omstorageasync, QUERYINTERFACE, QueryInterface);
- M_CB(MPIID_OMSTORAGEASYNC, ifc_omstorageasync, API_GETSTATE, GetState);
- M_CB(MPIID_OMSTORAGEASYNC, ifc_omstorageasync, API_GETWAITHANDLE, GetWaitHandle);
- M_CB(MPIID_OMSTORAGEASYNC, ifc_omstorageasync, API_GETDATA, GetData);
- NEXT_PATCH(MPIID_DOWNLOADCALLBACK)
- M_CB(MPIID_DOWNLOADCALLBACK, ifc_downloadManagerCallback, ADDREF, AddRef);
- M_CB(MPIID_DOWNLOADCALLBACK, ifc_downloadManagerCallback, RELEASE, Release);
- M_CB(MPIID_DOWNLOADCALLBACK, ifc_downloadManagerCallback, QUERYINTERFACE, QueryInterface);
- M_VCB(MPIID_DOWNLOADCALLBACK, ifc_downloadManagerCallback, IFC_DOWNLOADMANAGERCALLBACK_ONFINISH, OnFinish);
- M_VCB(MPIID_DOWNLOADCALLBACK, ifc_downloadManagerCallback, IFC_DOWNLOADMANAGERCALLBACK_ONTICK, OnTick);
- M_VCB(MPIID_DOWNLOADCALLBACK, ifc_downloadManagerCallback, IFC_DOWNLOADMANAGERCALLBACK_ONERROR, OnError);
- M_VCB(MPIID_DOWNLOADCALLBACK, ifc_downloadManagerCallback, IFC_DOWNLOADMANAGERCALLBACK_ONCANCEL, OnCancel);
- M_VCB(MPIID_DOWNLOADCALLBACK, ifc_downloadManagerCallback, IFC_DOWNLOADMANAGERCALLBACK_ONCONNECT, OnConnect);
- M_VCB(MPIID_DOWNLOADCALLBACK, ifc_downloadManagerCallback, IFC_DOWNLOADMANAGERCALLBACK_ONINIT, OnInit);
- END_PATCH
- END_MULTIPATCH;
- #undef CBCLASS
|