storageUrl.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "main.h"
  2. #include "./storageUrl.h"
  3. #include "./ifc_omwebstorage.h"
  4. #include "./resource.h"
  5. #include "./storageDwnld.h"
  6. #include "./ifc_omservice.h"
  7. #include "./ifc_omserviceenum.h"
  8. #include "./ifc_omservicehost.h"
  9. #include "./ifc_wasabihelper.h"
  10. #include "./enumXmlBuffer.h"
  11. #include "../Components/wac_downloadManager/wac_downloadManager_api.h"
  12. #include <strsafe.h>
  13. OmStorageUrl::OmStorageUrl()
  14. : ref(1), manager(NULL)
  15. {
  16. }
  17. OmStorageUrl::~OmStorageUrl()
  18. {
  19. if (NULL != manager)
  20. {
  21. ifc_wasabihelper *wasabi = NULL;
  22. if (SUCCEEDED(Plugin_GetWasabiHelper(&wasabi)) && wasabi != NULL)
  23. {
  24. wasabi->ReleaseWasabiInterface(&DownloadManagerGUID, (void**)&manager);
  25. wasabi->Release();
  26. }
  27. manager = NULL;
  28. }
  29. }
  30. HRESULT OmStorageUrl::CreateInstance(OmStorageUrl **instance)
  31. {
  32. if (NULL == instance) return E_POINTER;
  33. *instance = new OmStorageUrl();
  34. if (NULL == *instance) return E_OUTOFMEMORY;
  35. return S_OK;
  36. }
  37. size_t OmStorageUrl::AddRef()
  38. {
  39. return InterlockedIncrement((LONG*)&ref);
  40. }
  41. size_t OmStorageUrl::Release()
  42. {
  43. if (0 == ref)
  44. return ref;
  45. LONG r = InterlockedDecrement((LONG*)&ref);
  46. if (0 == r)
  47. delete(this);
  48. return r;
  49. }
  50. int OmStorageUrl::QueryInterface(GUID interface_guid, void **object)
  51. {
  52. if (NULL == object) return E_POINTER;
  53. if (IsEqualIID(interface_guid, IFC_OmStorage))
  54. *object = static_cast<ifc_omstorage*>(this);
  55. else if (IsEqualIID(interface_guid, SUID_OmStorageUrl))
  56. *object = static_cast<ifc_omstorage*>(this);
  57. else
  58. {
  59. *object = NULL;
  60. return E_NOINTERFACE;
  61. }
  62. if (NULL == *object)
  63. return E_UNEXPECTED;
  64. AddRef();
  65. return S_OK;
  66. }
  67. HRESULT OmStorageUrl::GetId(GUID *storageUid)
  68. {
  69. if (NULL == storageUid) return E_POINTER;
  70. *storageUid = SUID_OmStorageUrl;
  71. return S_OK;
  72. }
  73. HRESULT OmStorageUrl::GetType(GUID *storageType)
  74. {
  75. if (NULL == storageType) return E_POINTER;
  76. *storageType = STID_OmWebStorage;
  77. return S_OK;
  78. }
  79. UINT OmStorageUrl::GetCapabilities()
  80. {
  81. return capLoad | capPublic;
  82. }
  83. HRESULT OmStorageUrl::GetDescription(LPWSTR pszBuffer, UINT cchBufferMax)
  84. {
  85. if (NULL == pszBuffer) return E_POINTER;
  86. Plugin_LoadString(IDS_STORAGE_URL, pszBuffer, cchBufferMax);
  87. return S_OK;
  88. }
  89. HRESULT OmStorageUrl::Load(LPCWSTR pszAddress, ifc_omservicehost *host, ifc_omserviceenum **ppEnum)
  90. {
  91. if (NULL == ppEnum) return E_POINTER;
  92. *ppEnum = NULL;
  93. ifc_omstorageasync *async = NULL;
  94. HRESULT hr = BeginLoad(pszAddress, host, NULL, NULL, &async);
  95. if (SUCCEEDED(hr) && async != NULL)
  96. {
  97. hr = EndLoad(async, ppEnum);
  98. async->Release();
  99. }
  100. return hr;
  101. }
  102. HRESULT OmStorageUrl::Save(ifc_omservice **serviceList, ULONG listCount, UINT saveFlags, ULONG *savedCount)
  103. {
  104. return E_NOTIMPL;
  105. }
  106. HRESULT OmStorageUrl::Delete(ifc_omservice **serviceList, ULONG listCount, ULONG *deletedCount)
  107. {
  108. return E_NOTIMPL;
  109. }
  110. HRESULT OmStorageUrl::BeginLoad(LPCWSTR pszAddress, ifc_omservicehost *serviceHost, ifc_omstorageasync::AsyncCallback callback, void *data, ifc_omstorageasync **async)
  111. {
  112. if (NULL == async) return E_POINTER;
  113. *async = NULL;
  114. if (NULL == pszAddress || L'\0' == *pszAddress)
  115. return E_INVALIDARG;
  116. LPSTR address = Plugin_WideCharToMultiByte(CP_ACP, 0, pszAddress, -1, NULL, NULL);
  117. if (NULL == address) return E_OUTOFMEMORY;
  118. HRESULT hr(S_OK);
  119. if (NULL == manager)
  120. {
  121. ifc_wasabihelper *wasabi = NULL;
  122. hr = Plugin_GetWasabiHelper(&wasabi);
  123. if (SUCCEEDED(hr) && wasabi != NULL)
  124. {
  125. hr = wasabi->QueryWasabiInterface(&DownloadManagerGUID, (void**)&manager);
  126. wasabi->Release();
  127. }
  128. }
  129. if (SUCCEEDED(hr))
  130. {
  131. OmStorageDwnld *downloader = NULL;
  132. hr = OmStorageDwnld::CreateInstance(manager, TRUE, &downloader);
  133. if (SUCCEEDED(hr) && downloader != NULL)
  134. {
  135. if (NULL != callback) downloader->SetCallback(callback);
  136. if (NULL != data) downloader->SetData(data);
  137. downloader->SetServiceHost(serviceHost);
  138. *async = downloader;
  139. if ( manager->DownloadEx( address, downloader, api_downloadManager::DOWNLOADEX_BUFFER ) == 0 )
  140. {
  141. hr = E_FAIL;
  142. downloader->Release();
  143. *async = NULL;
  144. }
  145. }
  146. }
  147. Plugin_FreeAnsiString(address);
  148. return hr;
  149. }
  150. HRESULT OmStorageUrl::EndLoad(ifc_omstorageasync *async, ifc_omserviceenum **ppEnum)
  151. {
  152. OmStorageDwnld *downloader = (OmStorageDwnld*)async;
  153. if (NULL == downloader) return E_INVALIDARG;
  154. if (NULL != ppEnum)
  155. *ppEnum = NULL;
  156. UINT state = 0;
  157. if (FAILED(async->GetState(&state)) || ifc_omstorageasync::stateCompleted != state)
  158. {
  159. HANDLE completed = NULL;
  160. if (FAILED(async->GetWaitHandle(&completed)) || completed == NULL)
  161. return E_UNEXPECTED;
  162. while(WAIT_OBJECT_0 != WaitForSingleObjectEx(completed, INFINITE, TRUE));
  163. CloseHandle(completed);
  164. }
  165. HRESULT hr = downloader->GetResultCode();
  166. if (SUCCEEDED(hr))
  167. {
  168. void *buffer = NULL;
  169. size_t bufferSize;
  170. hr = downloader->GetBuffer(&buffer, &bufferSize);
  171. if (SUCCEEDED(hr))
  172. {
  173. if (NULL != ppEnum)
  174. {
  175. EnumXmlBuffer *enumerator = NULL;
  176. ifc_omservicehost *serviceHost = NULL;
  177. if (FAILED(downloader->GetServiceHost(&serviceHost)))
  178. serviceHost = NULL;
  179. hr = EnumXmlBuffer::CreateInstance(buffer, bufferSize, async, serviceHost, &enumerator);
  180. if (SUCCEEDED(hr))
  181. {
  182. *ppEnum = enumerator;
  183. }
  184. if (NULL != serviceHost)
  185. serviceHost->Release();
  186. }
  187. }
  188. }
  189. return hr;
  190. }
  191. HRESULT OmStorageUrl::RequestAbort(ifc_omstorageasync *async, BOOL fDrop)
  192. {
  193. OmStorageDwnld *downloader = (OmStorageDwnld*)async;
  194. if (NULL == downloader) return E_INVALIDARG;
  195. return downloader->RequestAbort(fDrop);
  196. }
  197. #define CBCLASS OmStorageUrl
  198. START_DISPATCH;
  199. CB(ADDREF, AddRef)
  200. CB(RELEASE, Release)
  201. CB(QUERYINTERFACE, QueryInterface)
  202. CB(API_GETID, GetId);
  203. CB(API_GETTYPE, GetType);
  204. CB(API_GETCAPABILITIES, GetCapabilities);
  205. CB(API_GETDESCRIPTION, GetDescription);
  206. CB(API_LOAD, Load);
  207. CB(API_SAVE, Save);
  208. CB(API_DELETE, Delete);
  209. CB(API_BEGINLOAD, BeginLoad);
  210. CB(API_ENDLOAD, EndLoad);
  211. CB(API_REQUESTABORT, RequestAbort);
  212. END_DISPATCH;
  213. #undef CBCLASS