JSAPI2_MediaCore.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "JSAPI2_MediaCore.h"
  2. #include "main.h"
  3. #include "api.h"
  4. #include "JSAPI.h"
  5. #include "JSAPI2_Security.h"
  6. #include "JSAPI2_CallbackManager.h"
  7. JSAPI2::MediaCoreAPI::MediaCoreAPI(const wchar_t *_key, JSAPI::ifc_info *_info) : metadataGuard("MediaCoreAPI metadata Guard")
  8. {
  9. info = _info;
  10. key = _key;
  11. refCount = 1;
  12. }
  13. JSAPI2::MediaCoreAPI::~MediaCoreAPI()
  14. {
  15. JSAPI2::callbackManager.Deregister(this);
  16. }
  17. #define DISP_TABLE \
  18. CHECK_ID(IsRegisteredExtension)\
  19. CHECK_ID(GetMetadata)\
  20. CHECK_ID(AddMetadataHook)\
  21. CHECK_ID(RemoveMetadataHook)\
  22. #define CHECK_ID(str) JSAPI_DISP_ENUMIFY(str),
  23. enum {
  24. DISP_TABLE
  25. };
  26. #undef CHECK_ID
  27. #define CHECK_ID(str)\
  28. if (CSTR_EQUAL == CompareStringW(lcid, NORM_IGNORECASE, rgszNames[i], -1, L## #str, -1))\
  29. { rgdispid[i] = JSAPI_DISP_ENUMIFY(str); continue; }
  30. HRESULT JSAPI2::MediaCoreAPI::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  31. {
  32. bool unknowns = false;
  33. for (unsigned int i = 0;i != cNames;i++)
  34. {
  35. DISP_TABLE
  36. rgdispid[i] = DISPID_UNKNOWN;
  37. unknowns = true;
  38. }
  39. if (unknowns)
  40. return DISP_E_UNKNOWNNAME;
  41. else
  42. return S_OK;
  43. }
  44. HRESULT JSAPI2::MediaCoreAPI::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  45. {
  46. return E_NOTIMPL;
  47. }
  48. HRESULT JSAPI2::MediaCoreAPI::GetTypeInfoCount(unsigned int FAR * pctinfo)
  49. {
  50. return E_NOTIMPL;
  51. }
  52. HRESULT JSAPI2::MediaCoreAPI::IsRegisteredExtension(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  53. {
  54. JSAPI_VERIFY_METHOD(wFlags);
  55. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  56. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_BSTR, puArgErr);
  57. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  58. const wchar_t *extension = JSAPI_PARAM(pdispparams, 1).bstrVal;
  59. int start_offs=0;
  60. wchar_t filename[MAX_PATH] = {0};
  61. StringCbPrintfW(filename, sizeof(filename), L"test.%s", extension);
  62. In_Module *i = in_setmod_noplay(filename, &start_offs);
  63. if (i)
  64. V_BOOL(pvarResult) = VARIANT_TRUE;
  65. else
  66. V_BOOL(pvarResult) = VARIANT_FALSE;
  67. return S_OK;
  68. }
  69. HRESULT JSAPI2::MediaCoreAPI::GetMetadata(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  70. {
  71. JSAPI_VERIFY_METHOD(wFlags);
  72. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 2);
  73. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_BSTR, puArgErr);
  74. JSAPI_VERIFY_PARAMTYPE(pdispparams, 2, VT_BSTR, puArgErr);
  75. JSAPI_INIT_RESULT(pvarResult, VT_BSTR);
  76. if (security.GetActionAuthorization(L"mediacore", L"metadata", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  77. {
  78. wchar_t buffer[4096] = {0};
  79. extendedFileInfoStructW info;
  80. info.filename = JSAPI_PARAM(pdispparams, 1).bstrVal;
  81. info.metadata = JSAPI_PARAM(pdispparams, 2).bstrVal;
  82. info.ret = buffer;
  83. info.retlen = sizeof(buffer)/sizeof(*buffer);
  84. if (NULL != info.filename &&
  85. NULL != info.metadata)
  86. {
  87. if (0 == SendMessageW(hMainWindow, WM_WA_IPC, (WPARAM)&info, IPC_GET_EXTENDED_FILE_INFOW_HOOKABLE))
  88. info.ret = NULL;
  89. JSAPI_SET_RESULT(pvarResult, bstrVal, SysAllocString(info.ret));
  90. }
  91. else
  92. JSAPI_EMPTY_RESULT(pvarResult);
  93. }
  94. else
  95. {
  96. JSAPI_EMPTY_RESULT(pvarResult);
  97. }
  98. return S_OK;
  99. }
  100. void JSAPI2::MediaCoreAPI::RemoveMetadataHook(const wchar_t *filename)
  101. {
  102. Nullsoft::Utility::AutoLock metadataLock(metadataGuard);
  103. RemoveMetadataHook_again:
  104. MetadataMap::iterator itr;
  105. for (itr=metadataMap.begin();itr!=metadataMap.end();itr++)
  106. {
  107. if (!_wcsicmp(filename, itr->url.c_str()))
  108. {
  109. metadataMap.erase(itr);
  110. goto RemoveMetadataHook_again;
  111. }
  112. }
  113. }
  114. void JSAPI2::MediaCoreAPI::RemoveMetadataHook(const wchar_t *filename, const wchar_t *tag)
  115. {
  116. Nullsoft::Utility::AutoLock metadataLock(metadataGuard);
  117. RemoveMetadataHook_again2:
  118. MetadataMap::iterator itr;
  119. for (itr=metadataMap.begin();itr!=metadataMap.end();itr++)
  120. {
  121. if (!_wcsicmp(filename, itr->url.c_str())
  122. && !_wcsicmp(tag, itr->tag.c_str()))
  123. {
  124. metadataMap.erase(itr);
  125. goto RemoveMetadataHook_again2;
  126. }
  127. }
  128. }
  129. HRESULT JSAPI2::MediaCoreAPI::AddMetadataHook(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  130. {
  131. JSAPI_VERIFY_METHOD(wFlags);
  132. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 3);
  133. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_BSTR, puArgErr);
  134. JSAPI_VERIFY_PARAMTYPE(pdispparams, 2, VT_BSTR, puArgErr);
  135. JSAPI_VERIFY_PARAMTYPE(pdispparams, 3, VT_BSTR, puArgErr);
  136. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  137. if (security.GetActionAuthorization(L"mediacore", L"metadatahook", key, info, JSAPI2::api_security::ACTION_PROMPT) == JSAPI2::api_security::ACTION_ALLOWED)
  138. {
  139. const wchar_t *filename = JSAPI_PARAM(pdispparams, 1).bstrVal;
  140. const wchar_t *tag = JSAPI_PARAM(pdispparams, 2).bstrVal;
  141. const wchar_t *value = JSAPI_PARAM(pdispparams, 3).bstrVal;
  142. if (NULL == filename || L'\0' == *filename ||
  143. NULL == tag || L'\0' == *tag)
  144. {
  145. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  146. return S_OK;
  147. }
  148. JSAPI2::callbackManager.Register(this);
  149. metadata_info info;
  150. info.url = filename;
  151. info.tag = tag;
  152. info.metadata= value;
  153. Nullsoft::Utility::AutoLock metadataLock(metadataGuard);
  154. RemoveMetadataHook(filename, tag);
  155. metadataMap.push_back(info);
  156. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  157. }
  158. else
  159. {
  160. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  161. }
  162. return S_OK;
  163. }
  164. HRESULT JSAPI2::MediaCoreAPI::RemoveMetadataHook(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  165. {
  166. JSAPI_VERIFY_METHOD(wFlags);
  167. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 1, 2);
  168. JSAPI_VERIFY_PARAMTYPE(pdispparams, 1, VT_BSTR, puArgErr);
  169. JSAPI_VERIFY_PARAMTYPE_OPTIONAL(pdispparams, 2, VT_BSTR, puArgErr);
  170. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  171. const wchar_t *filename = JSAPI_PARAM(pdispparams, 1).bstrVal;
  172. const wchar_t *tag = JSAPI_PARAM_OPTIONAL(pdispparams, 2, bstrVal, 0);
  173. if (NULL == filename || L'\0' == *filename)
  174. {
  175. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_FALSE);
  176. return S_OK;
  177. }
  178. if (NULL != tag && L'\0' == *tag)
  179. tag = NULL;
  180. if (NULL != tag)
  181. RemoveMetadataHook(filename, tag);
  182. else
  183. RemoveMetadataHook(filename);
  184. JSAPI_SET_RESULT(pvarResult, boolVal, VARIANT_TRUE);
  185. return S_OK;
  186. }
  187. bool JSAPI2::MediaCoreAPI::OverrideMetadata(const wchar_t *filename, const wchar_t *tag, wchar_t *out, size_t outCch)
  188. {
  189. Nullsoft::Utility::AutoLock metadataLock(metadataGuard);
  190. MetadataMap::iterator itr;
  191. for (itr=metadataMap.begin();itr!=metadataMap.end();itr++)
  192. {
  193. if (!_wcsicmp(filename, itr->url.c_str()) && !_wcsicmp(tag, itr->tag.c_str()))
  194. {
  195. StringCchCopyW(out, outCch, itr->metadata.c_str());
  196. return true;
  197. }
  198. }
  199. return false;
  200. }
  201. #undef CHECK_ID
  202. #define CHECK_ID(str) case JSAPI_DISP_ENUMIFY(str): return str(wFlags, pdispparams, pvarResult, puArgErr);
  203. HRESULT JSAPI2::MediaCoreAPI::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  204. {
  205. switch (dispid)
  206. {
  207. DISP_TABLE
  208. }
  209. return DISP_E_MEMBERNOTFOUND;
  210. }
  211. STDMETHODIMP JSAPI2::MediaCoreAPI::QueryInterface(REFIID riid, PVOID *ppvObject)
  212. {
  213. if (!ppvObject)
  214. return E_POINTER;
  215. else if (IsEqualIID(riid, IID_IDispatch))
  216. *ppvObject = (IDispatch *)this;
  217. else if (IsEqualIID(riid, IID_IUnknown))
  218. *ppvObject = this;
  219. else
  220. {
  221. *ppvObject = NULL;
  222. return E_NOINTERFACE;
  223. }
  224. AddRef();
  225. return S_OK;
  226. }
  227. ULONG JSAPI2::MediaCoreAPI::AddRef(void)
  228. {
  229. return InterlockedIncrement(&refCount);
  230. }
  231. ULONG JSAPI2::MediaCoreAPI::Release(void)
  232. {
  233. LONG lRef = InterlockedDecrement(&refCount);
  234. if (lRef == 0) delete this;
  235. return lRef;
  236. }