external.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "main.h"
  2. #include "./resource.h"
  3. #include "./external.h"
  4. #include "./navigation.h"
  5. #include "./commands.h"
  6. #include "../winamp/jsapi.h"
  7. #include "../winamp/jsapi_CallbackParameters.h"
  8. #include "./serviceHost.h"
  9. #include "./serviceHelper.h"
  10. #include <browserView.h>
  11. #include <ifc_omservice.h>
  12. #include <ifc_omserviceeditor.h>
  13. #define DISPTABLE_CLASS ExternalDispatch
  14. DISPTABLE_BEGIN()
  15. DISPENTRY_ADD(DISPATCH_SERVICE_OPEN, L"serviceOpen", OnServiceOpen)
  16. DISPENTRY_ADD(DISPATCH_SERVICE_CREATE, L"serviceCreate", OnServiceCreate)
  17. DISPENTRY_ADD(DISPATCH_SERVICE_GETINFO, L"serviceGetInfo", OnServiceGetInfo)
  18. DISPENTRY_ADD(DISPATCH_SERVICE_SETINFO, L"serviceSetInfo", OnServiceSetInfo)
  19. DISPTABLE_END
  20. #undef DISPTABLE_CLASS
  21. static BOOL DispParam_GetStringOpt(LPCWSTR *str, DISPPARAMS *paramInfo, UINT paramNumber, UINT *argErr)
  22. {
  23. if (paramInfo->cArgs < paramNumber ||
  24. VT_NULL == paramInfo->rgvarg[paramInfo->cArgs - paramNumber].vt)
  25. {
  26. *str = NULL;
  27. return FALSE;
  28. }
  29. JSAPI_GETSTRING((*str), paramInfo, paramNumber, argErr);
  30. return TRUE;
  31. }
  32. ExternalDispatch::ExternalDispatch()
  33. : ref(1)
  34. {
  35. }
  36. ExternalDispatch::~ExternalDispatch()
  37. {
  38. }
  39. HRESULT ExternalDispatch::CreateInstance(ExternalDispatch **instance)
  40. {
  41. if (NULL == instance) return E_POINTER;
  42. *instance = new ExternalDispatch();
  43. if (NULL == *instance) return E_OUTOFMEMORY;
  44. return S_OK;
  45. }
  46. LPCWSTR ExternalDispatch::GetName()
  47. {
  48. return L"WebDev";
  49. }
  50. ULONG ExternalDispatch::AddRef(void)
  51. {
  52. return InterlockedIncrement((LONG*)&ref);
  53. }
  54. ULONG ExternalDispatch::Release(void)
  55. {
  56. if (0 == ref)
  57. return ref;
  58. LONG r = InterlockedDecrement((LONG*)&ref);
  59. if (0 == r)
  60. delete(this);
  61. return r;
  62. }
  63. STDMETHODIMP ExternalDispatch::QueryInterface(REFIID riid, void **ppvObject)
  64. {
  65. if (NULL == ppvObject) return E_POINTER;
  66. if (IsEqualIID(riid, IID_IDispatch))
  67. *ppvObject = static_cast<IDispatch*>(this);
  68. else if (IsEqualIID(riid, IID_IUnknown))
  69. *ppvObject = static_cast<IUnknown*>(this);
  70. else
  71. {
  72. *ppvObject = NULL;
  73. return E_NOINTERFACE;
  74. }
  75. AddRef();
  76. return S_OK;
  77. }
  78. HRESULT ExternalDispatch::OnServiceOpen(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  79. {
  80. JSAPI_VERIFY_METHOD(wFlags);
  81. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 1, 2);
  82. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  83. UINT serviceId;
  84. JSAPI_GETUNSIGNED_AS_NUMBER(serviceId, pdispparams, 1, puArgErr);
  85. LPCWSTR forceUrl = NULL;
  86. if (pdispparams->cArgs > 1)
  87. {
  88. switch(pdispparams->rgvarg[0].vt)
  89. {
  90. case VT_BSTR: forceUrl = pdispparams->rgvarg[0].bstrVal; break;
  91. case VT_I4: forceUrl = MAKEINTRESOURCE(pdispparams->rgvarg[0].lVal); break;
  92. }
  93. }
  94. if (FALSE == DispParam_GetStringOpt(&forceUrl, pdispparams,2, puArgErr))
  95. forceUrl = NULL;
  96. HRESULT hr;
  97. Navigation *navigation;
  98. hr = Plugin_GetNavigation(&navigation);
  99. if (SUCCEEDED(hr))
  100. {
  101. ifc_omservice *service;
  102. if (NULL != navigation->FindService(serviceId, &service))
  103. {
  104. hr = Command_NavigateService(service, forceUrl, FALSE);
  105. service->Release();
  106. }
  107. else
  108. {
  109. hr = E_FAIL;
  110. }
  111. navigation->Release();
  112. }
  113. JSAPI_SET_RESULT(pvarResult, boolVal, (SUCCEEDED(hr) ? VARIANT_TRUE : VARIANT_FALSE));
  114. return S_OK;
  115. }
  116. HRESULT ExternalDispatch::OnServiceCreate(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  117. {
  118. JSAPI_VERIFY_METHOD(wFlags);
  119. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  120. HRESULT hr = Command_CreateService();
  121. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  122. JSAPI_SET_RESULT(pvarResult, boolVal, (SUCCEEDED(hr) ? VARIANT_TRUE : VARIANT_FALSE));
  123. return S_OK;
  124. }
  125. HRESULT ExternalDispatch::OnServiceGetInfo(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  126. {
  127. JSAPI_VERIFY_METHOD(wFlags);
  128. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 1);
  129. JSAPI_INIT_RESULT(pvarResult, VT_DISPATCH);
  130. UINT serviceId;
  131. JSAPI_GETUNSIGNED_AS_NUMBER(serviceId, pdispparams, 1, puArgErr);
  132. Navigation *navigation;
  133. HRESULT hr = Plugin_GetNavigation(&navigation);
  134. if (SUCCEEDED(hr))
  135. {
  136. ifc_omservice *service;
  137. if (NULL != navigation->FindService(serviceId, &service))
  138. {
  139. WCHAR szBuffer[2048];
  140. JSAPI::CallbackParameters *params = new JSAPI::CallbackParameters;
  141. params->AddLong(L"id", service->GetId());
  142. if (FAILED(service->GetName(szBuffer, ARRAYSIZE(szBuffer)))) szBuffer[0] = L'\0';
  143. params->AddString(L"name", szBuffer);
  144. if (FAILED(service->GetUrl(szBuffer, ARRAYSIZE(szBuffer)))) szBuffer[0] = L'\0';
  145. params->AddString(L"url", szBuffer);
  146. if (FAILED(service->GetIcon(szBuffer, ARRAYSIZE(szBuffer)))) szBuffer[0] = L'\0';
  147. params->AddString(L"icon", szBuffer);
  148. params->AddBoolean(L"preauthorized", (S_OK == ServiceHelper_IsPreAuthorized(service)));
  149. service->Release();
  150. V_DISPATCH(pvarResult) = params;
  151. }
  152. else
  153. {
  154. hr = E_FAIL;
  155. }
  156. navigation->Release();
  157. }
  158. if (FAILED(hr))
  159. {
  160. V_DISPATCH(pvarResult) = 0;
  161. }
  162. return S_OK;
  163. }
  164. HRESULT ExternalDispatch::OnServiceSetInfo(WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, unsigned int FAR *puArgErr)
  165. {
  166. JSAPI_VERIFY_METHOD(wFlags);
  167. JSAPI_VERIFY_PARAMCOUNT_OPTIONAL(pdispparams, 2, 5);
  168. UINT serviceId;
  169. JSAPI_GETUNSIGNED_AS_NUMBER(serviceId, pdispparams, 1, puArgErr);
  170. Navigation *navigation;
  171. HRESULT hr = Plugin_GetNavigation(&navigation);
  172. if (SUCCEEDED(hr))
  173. {
  174. ifc_omservice *service;
  175. HNAVITEM hItem = navigation->FindService(serviceId, &service);
  176. if (NULL != hItem)
  177. {
  178. ifc_omserviceeditor *editor;
  179. hr = service->QueryInterface(IFC_OmServiceEditor, (void**)&editor);
  180. if (SUCCEEDED(hr))
  181. {
  182. LPCWSTR value = NULL;
  183. editor->BeginUpdate();
  184. if (FALSE != DispParam_GetStringOpt(&value, pdispparams, 2, puArgErr) && FAILED(editor->SetName(value, FALSE)))
  185. hr = E_FAIL;
  186. if (FALSE != DispParam_GetStringOpt(&value, pdispparams, 3, puArgErr) && FAILED(ServiceHelper_UpdateIcon(editor, value)))
  187. hr = E_FAIL;
  188. if (FALSE != DispParam_GetStringOpt(&value, pdispparams, 4, puArgErr) && FAILED(editor->SetUrl(value, FALSE)))
  189. hr = E_FAIL;
  190. VARIANT_BOOL authorized = JSAPI_PARAM_OPTIONAL(pdispparams, 5, boolVal, VARIANT_FALSE);
  191. if (S_OK == editor->SetFlags((VARIANT_TRUE == authorized) ? SVCF_PREAUTHORIZED : 0, SVCF_PREAUTHORIZED))
  192. {
  193. if (VARIANT_TRUE == authorized)
  194. ServiceHelper_ResetPermissions(service, 0);
  195. }
  196. editor->EndUpdate();
  197. editor->Release();
  198. }
  199. if(SUCCEEDED(hr))
  200. hr = ServiceHelper_Save(service);
  201. service->Release();
  202. }
  203. else
  204. {
  205. hr = E_FAIL;
  206. }
  207. navigation->Release();
  208. }
  209. JSAPI_INIT_RESULT(pvarResult, VT_BOOL);
  210. JSAPI_SET_RESULT(pvarResult, boolVal, (SUCCEEDED(hr) ? VARIANT_TRUE : VARIANT_FALSE));
  211. return S_OK;
  212. }