1
0

serviceHost.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "main.h"
  2. #include "./serviceHost.h"
  3. #include "./wasabi.h"
  4. #include "./resource.h"
  5. #include "./external.h"
  6. #include "./navigation.h"
  7. #include "./commands.h"
  8. #include "./serviceHelper.h"
  9. #include <ifc_omservice.h>
  10. #include <ifc_omserviceeditor.h>
  11. #include <ifc_omservicecommand.h>
  12. #include <browserView.h>
  13. #include "../winamp/wa_ipc.h"
  14. #include "../winamp/IWasabiDispatchable.h"
  15. #include "../winamp/JSAPI_Info.h"
  16. #include <strsafe.h>
  17. #define IS_INVALIDISPATCH(__disp) (((IDispatch *)1) == (__disp) || NULL == (__disp))
  18. static WebDevServiceHost *cachedInstance = NULL;
  19. WebDevServiceHost::WebDevServiceHost()
  20. : ref(1)
  21. {
  22. }
  23. WebDevServiceHost::~WebDevServiceHost()
  24. {
  25. }
  26. HRESULT WebDevServiceHost::CreateInstance(WebDevServiceHost **instance)
  27. {
  28. if (NULL == instance)
  29. return E_POINTER;
  30. *instance = new WebDevServiceHost();
  31. if (NULL == instance) return E_OUTOFMEMORY;
  32. return S_OK;
  33. }
  34. HRESULT WebDevServiceHost::GetCachedInstance(WebDevServiceHost **instance)
  35. {
  36. if (NULL == instance)
  37. return E_POINTER;
  38. if (NULL == cachedInstance)
  39. {
  40. HRESULT hr = CreateInstance(&cachedInstance);
  41. if (FAILED(hr))
  42. {
  43. *instance = NULL;
  44. return hr;
  45. }
  46. }
  47. cachedInstance->AddRef();
  48. *instance = cachedInstance;
  49. return S_OK;
  50. }
  51. HRESULT WebDevServiceHost::ReleseCache()
  52. {
  53. if (NULL == cachedInstance)
  54. return S_FALSE;
  55. WebDevServiceHost *t = cachedInstance;
  56. cachedInstance = NULL;
  57. t->Release();
  58. return S_OK;
  59. }
  60. size_t WebDevServiceHost::AddRef()
  61. {
  62. return InterlockedIncrement((LONG*)&ref);
  63. }
  64. size_t WebDevServiceHost::Release()
  65. {
  66. if (0 == ref)
  67. return ref;
  68. LONG r = InterlockedDecrement((LONG*)&ref);
  69. if (0 == r)
  70. delete(this);
  71. return r;
  72. }
  73. int WebDevServiceHost::QueryInterface(GUID interface_guid, void **object)
  74. {
  75. if (NULL == object) return E_POINTER;
  76. if (IsEqualIID(interface_guid, IFC_OmServiceHost))
  77. *object = static_cast<ifc_omservicehost*>(this);
  78. else if (IsEqualIID(interface_guid, IFC_OmServiceEvent))
  79. *object = static_cast<ifc_omserviceevent*>(this);
  80. else
  81. {
  82. *object = NULL;
  83. return E_NOINTERFACE;
  84. }
  85. if (NULL == *object)
  86. return E_UNEXPECTED;
  87. AddRef();
  88. return S_OK;
  89. }
  90. HRESULT WebDevServiceHost::GetExternal(ifc_omservice *service, IDispatch **ppDispatch)
  91. {
  92. if (NULL == ppDispatch)
  93. return E_POINTER;
  94. if (NULL != *ppDispatch)
  95. {
  96. // try to connect our external
  97. IWasabiDispatchable *pWasabi;
  98. if (SUCCEEDED((*ppDispatch)->QueryInterface(IID_IWasabiDispatchable, (void**)&pWasabi)))
  99. {
  100. JSAPI::ifc_info *pInfo;
  101. if (SUCCEEDED(pWasabi->QueryDispatchable(JSAPI::IID_JSAPI_ifc_info, (Dispatchable**)&pInfo)))
  102. {
  103. ExternalDispatch *pExternal;
  104. if (SUCCEEDED(ExternalDispatch::CreateInstance(&pExternal)))
  105. {
  106. pInfo->AddAPI(pExternal->GetName(), pExternal);
  107. pExternal->Release();
  108. }
  109. pInfo->Release();
  110. }
  111. pWasabi->Release();
  112. }
  113. }
  114. return S_OK;
  115. }
  116. HRESULT WebDevServiceHost::GetBasePath(ifc_omservice *service, LPWSTR pszBuffer, UINT cchBufferMax)
  117. {
  118. if (NULL == pszBuffer)
  119. return E_POINTER;
  120. return StringCchCopy(pszBuffer, cchBufferMax, L".\\Plugins\\webDev");
  121. }
  122. HRESULT WebDevServiceHost::GetDefaultName(ifc_omservice *service, LPWSTR pszBuffer, UINT cchBufferMax)
  123. {
  124. if (NULL == pszBuffer)
  125. return E_POINTER;
  126. if (NULL == service)
  127. return E_INVALIDARG;
  128. return StringCchPrintf(pszBuffer, cchBufferMax, L"wdService_{%010u}.ini", service->GetId());
  129. }
  130. HRESULT WebDevServiceHost::QueryCommandState(ifc_omservice *service, HWND hBrowser, const GUID *commandGroup, UINT commandId)
  131. {
  132. if (NULL == service || NULL == commandGroup)
  133. return E_NOTIMPL;
  134. if (IsEqualGUID(*commandGroup, CMDGROUP_ADDRESSBAR))
  135. {
  136. switch(commandId)
  137. {
  138. case ADDRESSCOMMAND_VISIBLE:
  139. if (S_FALSE == ServiceHelper_IsSpecial(service))
  140. return CMDSTATE_ENABLED;
  141. return CMDSTATE_UNKNOWN;
  142. case ADDRESSCOMMAND_READONLY:
  143. if (S_FALSE == ServiceHelper_IsSpecial(service))
  144. return CMDSTATE_DISABLED;
  145. return CMDSTATE_ENABLED;
  146. }
  147. }
  148. return E_NOTIMPL;
  149. }
  150. HRESULT WebDevServiceHost::ExecuteCommand(ifc_omservice *service, HWND hBrowser, const GUID *commandGroup, UINT commandId, ULONG_PTR commandArg)
  151. {
  152. return E_NOTIMPL;
  153. }
  154. HRESULT WebDevServiceHost::GetUrl(ifc_omservice *service, LPWSTR pszBuffer, UINT cchBufferMax)
  155. {
  156. return E_NOTIMPL;
  157. }
  158. void WebDevServiceHost::ServiceChange(ifc_omservice *service, UINT nModified)
  159. {
  160. if (NULL == service) return;
  161. Navigation *navigation;
  162. if (SUCCEEDED(Plugin_GetNavigation(&navigation)))
  163. {
  164. navigation->UpdateService(service, nModified);
  165. navigation->Release();
  166. }
  167. if ( 0 != (ifc_omserviceeditor::modifiedUrl & nModified))
  168. {
  169. Command_PostNavigateSvc(service, MAKEINTRESOURCE
  170. (NAVIGATE_HOME), TRUE);
  171. }
  172. if (0 != (ifc_omserviceeditor::modifiedFlags & nModified))
  173. {
  174. ServiceHelper_RegisterPreAuthorized(service);
  175. }
  176. }
  177. #define CBCLASS WebDevServiceHost
  178. START_MULTIPATCH;
  179. START_PATCH(MPIID_OMSVCHOST)
  180. M_CB(MPIID_OMSVCHOST, ifc_omservicehost, ADDREF, AddRef);
  181. M_CB(MPIID_OMSVCHOST, ifc_omservicehost, RELEASE, Release);
  182. M_CB(MPIID_OMSVCHOST, ifc_omservicehost, QUERYINTERFACE, QueryInterface);
  183. M_CB(MPIID_OMSVCHOST, ifc_omservicehost, API_GETEXTERNAL, GetExternal);
  184. M_CB(MPIID_OMSVCHOST, ifc_omservicehost, API_GETBASEPATH, GetBasePath);
  185. M_CB(MPIID_OMSVCHOST, ifc_omservicehost, API_GETDEFAULTNAME, GetDefaultName);
  186. M_CB(MPIID_OMSVCHOST, ifc_omservicehost, API_QUERYCOMMANDSTATE, QueryCommandState);
  187. M_CB(MPIID_OMSVCHOST, ifc_omservicehost, API_EXECUTECOMMAND, ExecuteCommand);
  188. M_CB(MPIID_OMSVCHOST, ifc_omservicehost, API_GETURL, GetUrl);
  189. NEXT_PATCH(MPIID_OMSVCEVENT)
  190. M_CB(MPIID_OMSVCEVENT, ifc_omserviceevent, ADDREF, AddRef);
  191. M_CB(MPIID_OMSVCEVENT, ifc_omserviceevent, RELEASE, Release);
  192. M_CB(MPIID_OMSVCEVENT, ifc_omserviceevent, QUERYINTERFACE, QueryInterface);
  193. M_VCB(MPIID_OMSVCEVENT, ifc_omserviceevent, API_SERVICECHANGE, ServiceChange);
  194. END_PATCH
  195. END_MULTIPATCH;
  196. #undef CBCLASS