cacheDownloader.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include "main.h"
  2. #include "./cacheDownloader.h"
  3. #include "./cacheRecord.h"
  4. #include "./ifc_wasabihelper.h"
  5. #include "..\Components\wac_network\wac_network_http_receiver_api.h"
  6. #include <strsafe.h>
  7. CacheDownloader::CacheDownloader(CacheRecord *record, BOOL fEnableCompression)
  8. : ref(1), manager(NULL), cookie(NULL), owner(record), state(stateReady), enableCompression(fEnableCompression)
  9. {
  10. InitializeCriticalSection(&lock);
  11. }
  12. CacheDownloader::~CacheDownloader()
  13. {
  14. EnterCriticalSection( &lock );
  15. if ( manager != NULL )
  16. {
  17. if ( NULL != cookie )
  18. {
  19. Abort();
  20. }
  21. ifc_wasabihelper *wasabi;
  22. if ( SUCCEEDED( Plugin_GetWasabiHelper( &wasabi ) ) )
  23. {
  24. wasabi->ReleaseWasabiInterface( &DownloadManagerGUID, (void **)&manager );
  25. wasabi->Release();
  26. }
  27. manager = NULL;
  28. }
  29. LeaveCriticalSection( &lock );
  30. DeleteCriticalSection( &lock );
  31. }
  32. HRESULT CacheDownloader::CreateInstance(CacheRecord *record, LPCWSTR pszAddress, BOOL fEnableCompression, CacheDownloader **instance)
  33. {
  34. if (NULL == instance) return E_POINTER;
  35. *instance = NULL;
  36. if (NULL == record || NULL == pszAddress || L'\0' == *pszAddress)
  37. return E_INVALIDARG;
  38. HRESULT hr;
  39. *instance = new CacheDownloader(record, fEnableCompression);
  40. if (NULL == *instance)
  41. {
  42. hr = E_OUTOFMEMORY;
  43. }
  44. else
  45. {
  46. hr = (*instance)->Start(pszAddress);
  47. if (FAILED(hr))
  48. {
  49. (*instance)->Release();
  50. *instance = NULL;
  51. }
  52. }
  53. return hr;
  54. }
  55. size_t CacheDownloader::AddRef()
  56. {
  57. return InterlockedIncrement((LONG*)&ref);
  58. }
  59. size_t CacheDownloader::Release()
  60. {
  61. if (0 == ref)
  62. return ref;
  63. LONG r = InterlockedDecrement((LONG*)&ref);
  64. if (0 == r)
  65. delete(this);
  66. return r;
  67. }
  68. int CacheDownloader::QueryInterface(GUID interface_guid, void **object)
  69. {
  70. if (NULL == object) return E_POINTER;
  71. *object = NULL;
  72. return E_NOINTERFACE;
  73. }
  74. HRESULT CacheDownloader::Start(LPCWSTR pszAddress)
  75. {
  76. HRESULT hr = S_OK;
  77. LPSTR address = Plugin_WideCharToMultiByte(CP_ACP, 0, pszAddress, -1, NULL, NULL);
  78. if (NULL == address) return E_OUTOFMEMORY;
  79. EnterCriticalSection(&lock);
  80. state = stateInitializing;
  81. LeaveCriticalSection(&lock);
  82. if ( manager == NULL )
  83. {
  84. ifc_wasabihelper *wasabi;
  85. hr = Plugin_GetWasabiHelper(&wasabi);
  86. if (SUCCEEDED(hr))
  87. {
  88. hr = wasabi->QueryWasabiInterface(&DownloadManagerGUID, (void**)&manager);
  89. wasabi->Release();
  90. }
  91. }
  92. if (SUCCEEDED(hr))
  93. {
  94. cookie = manager->DownloadEx(address, this, api_downloadManager::DOWNLOADEX_TEMPFILE);
  95. if (NULL == cookie)
  96. {
  97. hr = E_FAIL;
  98. }
  99. }
  100. if (FAILED(hr))
  101. {
  102. EnterCriticalSection(&lock);
  103. state = stateCompleted;
  104. LeaveCriticalSection(&lock);
  105. }
  106. Plugin_FreeAnsiString(address);
  107. return hr;
  108. }
  109. HRESULT CacheDownloader::Abort()
  110. {
  111. HRESULT hr;
  112. EnterCriticalSection(&lock);
  113. if (NULL == manager || NULL == cookie)
  114. {
  115. hr = E_UNEXPECTED;
  116. }
  117. else if (stateCompleted == state)
  118. {
  119. hr = S_FALSE;
  120. }
  121. else
  122. {
  123. state = stateAborting;
  124. manager->CancelDownload(cookie);
  125. hr = S_OK;
  126. }
  127. LeaveCriticalSection(&lock);
  128. return hr;
  129. }
  130. UINT CacheDownloader::GetState()
  131. {
  132. return state;
  133. }
  134. HRESULT CacheDownloader::SetOwner(CacheRecord *record)
  135. {
  136. owner = record;
  137. return S_OK;
  138. }
  139. HRESULT CacheDownloader::DownloadCompleted(INT errorCode)
  140. {
  141. AddRef();
  142. EnterCriticalSection(&lock);
  143. state = stateCompleted;
  144. if (NULL != owner)
  145. {
  146. LPCWSTR pszFile = (api_downloadManager::TICK_SUCCESS == errorCode) ? manager->GetLocation(cookie) : NULL;
  147. owner->DownloadCompleted(pszFile, errorCode);
  148. owner = NULL;
  149. }
  150. manager->ReleaseDownload(cookie);
  151. cookie = NULL;
  152. ifc_wasabihelper *wasabi;
  153. if (SUCCEEDED(Plugin_GetWasabiHelper(&wasabi)))
  154. {
  155. wasabi->ReleaseWasabiInterface(&DownloadManagerGUID, (void**)&manager);
  156. wasabi->Release();
  157. manager = NULL;
  158. }
  159. LeaveCriticalSection(&lock);
  160. Release();
  161. return S_OK;
  162. }
  163. void CacheDownloader::OnInit(DownloadToken token)
  164. {
  165. EnterCriticalSection(&lock);
  166. cookie = token;
  167. if (NULL != cookie)
  168. {
  169. manager->RetainDownload(cookie);
  170. }
  171. state = stateConnecting;
  172. if (FALSE != enableCompression)
  173. {
  174. api_httpreceiver *receiver = manager->GetReceiver(token);
  175. if (NULL != receiver)
  176. receiver->AllowCompression();
  177. }
  178. LeaveCriticalSection(&lock);
  179. }
  180. void CacheDownloader::OnFinish(DownloadToken token)
  181. {
  182. DownloadCompleted(api_downloadManager::TICK_SUCCESS);
  183. }
  184. void CacheDownloader::OnError(DownloadToken token, int errorCode)
  185. {
  186. DownloadCompleted(errorCode);
  187. }
  188. void CacheDownloader::OnCancel(DownloadToken token)
  189. {
  190. DownloadCompleted(api_downloadManager::TICK_NODATA);
  191. }
  192. void CacheDownloader::OnTick(DownloadToken token)
  193. {
  194. EnterCriticalSection(&lock);
  195. if (stateConnecting == state)
  196. state = stateReceiving;
  197. LeaveCriticalSection(&lock);
  198. }
  199. void CacheDownloader::OnConnect(DownloadToken dlToken)
  200. {
  201. EnterCriticalSection(&lock);
  202. state = stateReceiving;
  203. LeaveCriticalSection(&lock);
  204. }
  205. #define CBCLASS CacheDownloader
  206. START_DISPATCH;
  207. CB(ADDREF, AddRef)
  208. CB(RELEASE, Release)
  209. CB(QUERYINTERFACE, QueryInterface)
  210. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONFINISH, OnFinish)
  211. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONTICK, OnTick)
  212. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONERROR, OnError)
  213. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONCANCEL, OnCancel)
  214. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONCONNECT, OnConnect)
  215. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONINIT, OnInit)
  216. END_DISPATCH;
  217. #undef CBCLASS