1
0

downloadResult.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #include "./downloadResult.h"
  2. #include "./loginStatus.h"
  3. #include "../api.h"
  4. #include "../resource.h"
  5. #include "../jnetlib/api_httpget.h"
  6. #include <strsafe.h>
  7. LoginDownloadResult::LoginDownloadResult(api_downloadManager *pManager, UINT uType, Callback fnCallback, void *pData, LoginStatus *pStatus)
  8. : ref(1), manager(pManager), flags(0), callback(fnCallback), data(pData),
  9. address(NULL), result(api_downloadManager::TICK_NODATA),
  10. cookie(0), completed(NULL), status(pStatus), statusCookie((UINT)-1)
  11. {
  12. InitializeCriticalSection(&lock);
  13. SetType(uType);
  14. SetState(stateInitializing);
  15. if (NULL != status)
  16. status->AddRef();
  17. if (NULL != manager)
  18. manager->AddRef();
  19. }
  20. LoginDownloadResult::~LoginDownloadResult()
  21. {
  22. EnterCriticalSection(&lock);
  23. if(NULL != manager)
  24. {
  25. if (0 != cookie)
  26. {
  27. manager->ReleaseDownload(cookie);
  28. cookie = 0;
  29. }
  30. manager->Release();
  31. }
  32. if (NULL != completed)
  33. CloseHandle(completed);
  34. if (NULL != address)
  35. free(address);
  36. if (NULL != status)
  37. {
  38. if (((UINT)-1) != statusCookie)
  39. status->Remove(statusCookie);
  40. status->Release();
  41. }
  42. LeaveCriticalSection(&lock);
  43. DeleteCriticalSection(&lock);
  44. }
  45. HRESULT LoginDownloadResult::CreateInstance(api_downloadManager *pManager, UINT uType, Callback fnCallback, void *pData, LoginStatus *pStatus, LoginDownloadResult **instance)
  46. {
  47. if (NULL == instance) return E_POINTER;
  48. if (NULL == pManager)
  49. {
  50. *instance = NULL;
  51. return E_INVALIDARG;
  52. }
  53. *instance = new LoginDownloadResult(pManager, uType, fnCallback, pData, pStatus);
  54. if (NULL == *instance) return E_OUTOFMEMORY;
  55. return S_OK;
  56. }
  57. size_t LoginDownloadResult::AddRef()
  58. {
  59. return InterlockedIncrement((LONG*)&ref);
  60. }
  61. size_t LoginDownloadResult::Release()
  62. {
  63. if (0 == ref)
  64. return ref;
  65. LONG r = InterlockedDecrement((LONG*)&ref);
  66. if (0 == r)
  67. delete(this);
  68. return r;
  69. }
  70. int LoginDownloadResult::QueryInterface(GUID interface_guid, void **object)
  71. {
  72. if (NULL == object) return E_POINTER;
  73. *object = NULL;
  74. return E_NOINTERFACE;
  75. }
  76. void LoginDownloadResult::SetState(UINT uState)
  77. {
  78. flags = (flags & ~stateMask) | (uState & stateMask);
  79. }
  80. void LoginDownloadResult::SetType(UINT uType)
  81. {
  82. flags = (flags & ~typeMask) | (uType & typeMask);
  83. }
  84. void LoginDownloadResult::SetFlags(UINT uFlags, UINT uMask)
  85. {
  86. uMask &= flagsMask;
  87. flags = (flags & ~uMask) | (uFlags & uMask);
  88. }
  89. HRESULT LoginDownloadResult::GetState(UINT *state)
  90. {
  91. if (NULL == state)
  92. return E_POINTER;
  93. EnterCriticalSection(&lock);
  94. *state = (flags & stateMask);
  95. LeaveCriticalSection(&lock);
  96. return S_OK;
  97. }
  98. HRESULT LoginDownloadResult::GetType(UINT *type)
  99. {
  100. if (NULL == type)
  101. return E_POINTER;
  102. EnterCriticalSection(&lock);
  103. *type = (flags & typeMask);
  104. LeaveCriticalSection(&lock);
  105. return S_OK;
  106. }
  107. HRESULT LoginDownloadResult::GetFile(LPCWSTR *ppszPath)
  108. {
  109. EnterCriticalSection(&lock);
  110. HRESULT hr;
  111. if (NULL == cookie || NULL == manager)
  112. {
  113. hr = E_UNEXPECTED;
  114. }
  115. else if (stateCompleted != (stateMask & flags))
  116. {
  117. hr = E_DWNLD_BUSY;
  118. }
  119. else
  120. {
  121. switch(result)
  122. {
  123. case api_downloadManager::TICK_SUCCESS: hr = E_DWNLD_OK; break;
  124. case api_downloadManager::TICK_FAILURE: hr = (0 != (flagUserAbort & flags)) ? E_DWNLD_ABORT : E_DWNLD_FAIL; break;
  125. case api_downloadManager::TICK_TIMEOUT: hr = E_DWNLD_TIMEOUT; break;
  126. case api_downloadManager::TICK_CANT_CONNECT: hr = E_DWNLD_CANT_CONNECT; break;
  127. case api_downloadManager::TICK_WRITE_ERROR: hr = E_DWNLD_WRITE_ERROR; break;
  128. default: hr = E_DWNLD_BUSY; break;
  129. }
  130. }
  131. if (NULL != ppszPath)
  132. {
  133. if (SUCCEEDED(hr))
  134. *ppszPath = manager->GetLocation(cookie);
  135. else
  136. *ppszPath = NULL;
  137. }
  138. LeaveCriticalSection(&lock);
  139. return hr;
  140. }
  141. HRESULT LoginDownloadResult::GetWaitHandle(HANDLE *handle)
  142. {
  143. if (NULL == handle)
  144. return E_POINTER;
  145. HRESULT hr = S_OK;
  146. EnterCriticalSection(&lock);
  147. if (NULL == completed)
  148. {
  149. completed = CreateEvent(NULL, TRUE, FALSE, NULL);
  150. if (NULL == completed)
  151. {
  152. *handle = NULL;
  153. DWORD error = GetLastError();
  154. hr = HRESULT_FROM_WIN32(error);
  155. }
  156. }
  157. if (SUCCEEDED(hr) && FALSE == DuplicateHandle(GetCurrentProcess(), completed,
  158. GetCurrentProcess(), handle, 0, FALSE, DUPLICATE_SAME_ACCESS))
  159. {
  160. *handle = NULL;
  161. DWORD error = GetLastError();
  162. hr = HRESULT_FROM_WIN32(error);
  163. }
  164. LeaveCriticalSection(&lock);
  165. return hr;
  166. }
  167. HRESULT LoginDownloadResult::GetData(void **data)
  168. {
  169. if (NULL == data)
  170. return E_POINTER;
  171. EnterCriticalSection(&lock);
  172. *data = this->data;
  173. LeaveCriticalSection(&lock);
  174. return S_OK;
  175. }
  176. HRESULT LoginDownloadResult::RequestAbort(BOOL fDrop)
  177. {
  178. EnterCriticalSection(&lock);
  179. if (FALSE != fDrop)
  180. {
  181. data = NULL;
  182. callback = NULL;
  183. }
  184. if (0 != cookie && NULL != manager && 0 == (flagUserAbort & flags))
  185. {
  186. manager->CancelDownload(cookie);
  187. SetState(stateAborting);
  188. SetFlags(flagUserAbort, flagUserAbort);
  189. }
  190. LeaveCriticalSection(&lock);
  191. return S_OK;
  192. }
  193. HRESULT LoginDownloadResult::GetUrl(LPSTR pszBuffer, UINT cchBufferMax)
  194. {
  195. if (NULL == pszBuffer)
  196. return E_POINTER;
  197. HRESULT hr;
  198. EnterCriticalSection(&lock);
  199. if(NULL == manager || 0 == cookie)
  200. hr = E_UNEXPECTED;
  201. else
  202. {
  203. api_httpreceiver *receiver = manager->GetReceiver(cookie);
  204. if (NULL == receiver) hr = E_FAIL;
  205. else
  206. {
  207. LPCSTR url = receiver->get_url();
  208. hr = StringCchCopyExA(pszBuffer, cchBufferMax, url, NULL, NULL, STRSAFE_IGNORE_NULLS);
  209. }
  210. }
  211. LeaveCriticalSection(&lock);
  212. return hr;
  213. }
  214. void LoginDownloadResult::SetStatus()
  215. {
  216. EnterCriticalSection(&lock);
  217. if (NULL != status && ((UINT)-1 == statusCookie))
  218. {
  219. LPCWSTR pszStatus;
  220. switch(typeMask & flags)
  221. {
  222. case typeProviderList: pszStatus = MAKEINTRESOURCE(IDS_STATUS_UPDATEBEGIN); break;
  223. default: pszStatus = NULL; break;
  224. }
  225. if (NULL != pszStatus)
  226. {
  227. BSTR bstrText;
  228. if (FALSE == IS_INTRESOURCE(pszStatus))
  229. bstrText = SysAllocString(pszStatus);
  230. else
  231. {
  232. WCHAR szBuffer[256] = {0};
  233. WASABI_API_LNGSTRINGW_BUF((INT)(INT_PTR)pszStatus, szBuffer, ARRAYSIZE(szBuffer));
  234. bstrText = SysAllocString(szBuffer);
  235. }
  236. statusCookie = status->Add(bstrText);
  237. if (((UINT)-1) == statusCookie)
  238. SysFreeString(bstrText);
  239. }
  240. }
  241. LeaveCriticalSection(&lock);
  242. }
  243. void LoginDownloadResult::RemoveStatus()
  244. {
  245. EnterCriticalSection(&lock);
  246. if (NULL != status)
  247. {
  248. if (((UINT)-1) != statusCookie)
  249. {
  250. status->Remove(statusCookie);
  251. statusCookie = (UINT)-1;
  252. }
  253. }
  254. LeaveCriticalSection(&lock);
  255. }
  256. HRESULT LoginDownloadResult::CreateDownloadFileName(LPSTR pszBuffer, UINT cchBufferMax)
  257. {
  258. if (NULL == pszBuffer)
  259. return E_POINTER;
  260. HRESULT hr;
  261. EnterCriticalSection(&lock);
  262. if(NULL == manager || 0 == cookie)
  263. hr = E_UNEXPECTED;
  264. else
  265. {
  266. api_httpreceiver *receiver = manager->GetReceiver(cookie);
  267. if (NULL == receiver) hr = E_FAIL;
  268. else
  269. {
  270. LPCSTR url = receiver->get_url();
  271. LPCSTR contentDisposition = receiver->getheader("content-disposition");
  272. LPCSTR contentType = receiver->getheader("content-type");
  273. hr = S_OK;
  274. }
  275. }
  276. LeaveCriticalSection(&lock);
  277. return hr;
  278. }
  279. void LoginDownloadResult::DownloadCompleted(int errorCode)
  280. {
  281. EnterCriticalSection(&lock);
  282. result = errorCode;
  283. SetState(stateCompleted);
  284. HANDLE hEvent = completed;
  285. Callback cb = callback;
  286. LeaveCriticalSection(&lock);
  287. if (NULL != hEvent)
  288. SetEvent(hEvent);
  289. if (NULL != cb)
  290. cb(this, data);
  291. RemoveStatus();
  292. Release();
  293. }
  294. void LoginDownloadResult::Event_DownloadInit(DownloadToken token)
  295. {
  296. EnterCriticalSection(&lock);
  297. AddRef();
  298. cookie = token;
  299. SetState(stateConnecting);
  300. if (NULL != manager)
  301. {
  302. manager->RetainDownload(cookie);
  303. if (typeProviderList == (typeMask & flags))
  304. {
  305. api_httpreceiver *receiver = manager->GetReceiver(token);
  306. if (NULL != receiver)
  307. receiver->AllowCompression();
  308. }
  309. }
  310. LeaveCriticalSection(&lock);
  311. SetStatus();
  312. }
  313. void LoginDownloadResult::Event_DownloadConnect(DownloadToken token)
  314. {
  315. EnterCriticalSection(&lock);
  316. SetState(stateReceiving);
  317. LeaveCriticalSection(&lock);
  318. }
  319. void LoginDownloadResult::Event_DownloadTick(DownloadToken token)
  320. {
  321. EnterCriticalSection(&lock);
  322. if (stateConnecting == (stateMask & flags))
  323. SetState(stateReceiving);
  324. LeaveCriticalSection(&lock);
  325. }
  326. void LoginDownloadResult::Event_DownloadFinish(DownloadToken token)
  327. {
  328. DownloadCompleted(api_downloadManager::TICK_SUCCESS);
  329. }
  330. void LoginDownloadResult::Event_DownloadError(DownloadToken token, int errorCode)
  331. {
  332. DownloadCompleted(errorCode);
  333. }
  334. void LoginDownloadResult::Event_DownloadCancel(DownloadToken token)
  335. {
  336. DownloadCompleted(api_downloadManager::TICK_NODATA);
  337. }
  338. #define CBCLASS LoginDownloadResult
  339. START_DISPATCH;
  340. CB(ADDREF, AddRef)
  341. CB(RELEASE, Release)
  342. CB(QUERYINTERFACE, QueryInterface)
  343. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONINIT, Event_DownloadInit)
  344. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONCONNECT, Event_DownloadConnect)
  345. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONFINISH, Event_DownloadFinish)
  346. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONERROR, Event_DownloadError)
  347. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONCANCEL, Event_DownloadCancel)
  348. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONTICK, Event_DownloadTick)
  349. END_DISPATCH;
  350. #undef CBCLASS