1
0

serviceHelper.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "main.h"
  2. #include "./serviceHelper.h"
  3. #include "./wasabi.h"
  4. #include "./serviceHost.h"
  5. #include <ifc_omservice.h>
  6. #include <storageIni.h>
  7. #include <ifc_omserviceeditor.h>
  8. #include <ifc_omserviceeditor.h>
  9. #include <ifc_mlnavigationhelper.h>
  10. #include <strsafe.h>
  11. HRESULT ServiceHelper_QueryStorage(ifc_omstorage **storage)
  12. {
  13. if (NULL == storage) return E_POINTER;
  14. if (NULL == OMSERVICEMNGR)
  15. {
  16. *storage = NULL;
  17. return E_UNEXPECTED;
  18. }
  19. return OMSERVICEMNGR->QueryStorage(&SUID_OmStorageIni, storage);
  20. }
  21. HRESULT ServiceHelper_Create(UINT serviceId, LPCWSTR pszName, LPCWSTR pszIcon, LPCWSTR pszUrl, UINT flags, BOOL fSave, ifc_omservice **serviceOut)
  22. {
  23. if (NULL == serviceOut)
  24. return E_POINTER;
  25. *serviceOut = NULL;
  26. if (NULL == OMSERVICEMNGR)
  27. return E_UNEXPECTED;
  28. WebDevServiceHost *serviceHost;
  29. if (FAILED(WebDevServiceHost::GetCachedInstance(&serviceHost)))
  30. serviceHost = NULL;
  31. HRESULT hr = OMSERVICEMNGR->CreateService(serviceId, serviceHost, serviceOut);
  32. if (SUCCEEDED(hr))
  33. {
  34. ifc_omserviceeditor *editor;
  35. if (SUCCEEDED((*serviceOut)->QueryInterface(IFC_OmServiceEditor, (void**)&editor)))
  36. {
  37. WCHAR szBuffer[4096] = {0};
  38. editor->BeginUpdate();
  39. if (NULL != pszName && IS_INTRESOURCE(pszName))
  40. {
  41. if (NULL != WASABI_API_LNGSTRINGW_BUF((INT)(INT_PTR)pszName, szBuffer, ARRAYSIZE(szBuffer)))
  42. editor->SetName(szBuffer, FALSE);
  43. }
  44. else
  45. editor->SetName(pszName, FALSE);
  46. if (NULL != pszIcon && IS_INTRESOURCE(pszIcon))
  47. {
  48. if (SUCCEEDED(Plugin_MakeResourcePath(szBuffer, ARRAYSIZE(szBuffer), RT_RCDATA, pszIcon, RESPATH_COMPACT)))
  49. editor->SetIcon(szBuffer, FALSE);
  50. }
  51. else
  52. editor->SetIcon(pszIcon, FALSE);
  53. if (NULL != pszUrl && IS_INTRESOURCE(pszUrl))
  54. {
  55. if (SUCCEEDED(Plugin_MakeResourcePath(szBuffer, ARRAYSIZE(szBuffer), RT_HTML, pszUrl, RESPATH_TARGETIE | RESPATH_COMPACT)))
  56. editor->SetUrl(szBuffer, FALSE);
  57. }
  58. else
  59. editor->SetUrl(pszUrl, FALSE);
  60. editor->SetFlags(flags, 0xFFFFFFFF);
  61. if (FALSE != fSave)
  62. {
  63. hr = ServiceHelper_Save(*serviceOut);
  64. if (FAILED(hr))
  65. {
  66. (*serviceOut)->Release();
  67. *serviceOut = NULL;
  68. }
  69. }
  70. else
  71. {
  72. editor->SetModified(0, (UINT)-1);
  73. }
  74. editor->EndUpdate();
  75. editor->Release();
  76. }
  77. }
  78. if (NULL != serviceHost)
  79. serviceHost->Release();
  80. return hr;
  81. }
  82. HRESULT ServiceHelper_Save(ifc_omservice *service)
  83. {
  84. if (NULL == service)
  85. return E_INVALIDARG;
  86. if (NULL == OMSERVICEMNGR)
  87. return E_UNEXPECTED;
  88. HRESULT hr;
  89. ifc_omstorage *storage;
  90. hr = ServiceHelper_QueryStorage(&storage);
  91. if (SUCCEEDED(hr))
  92. {
  93. hr = storage->Save(&service, 1, ifc_omstorage::saveModifiedOnly | ifc_omstorage::saveClearModified, NULL);
  94. storage->Release();
  95. }
  96. return hr;
  97. }
  98. HRESULT ServiceHelper_Delete(ifc_omservice *service)
  99. {
  100. if (NULL == service)
  101. return E_INVALIDARG;
  102. if (NULL == OMSERVICEMNGR)
  103. return E_UNEXPECTED;
  104. ifc_omstorage *storage;
  105. HRESULT hr = ServiceHelper_QueryStorage(&storage);
  106. if (SUCCEEDED(hr))
  107. {
  108. hr = storage->Delete(&service, 1, NULL);
  109. storage->Release();
  110. }
  111. return hr;
  112. }
  113. HRESULT ServiceHelper_Reload(ifc_omservice *service)
  114. {
  115. if (NULL == service)
  116. return E_INVALIDARG;
  117. if (NULL == OMSERVICEMNGR)
  118. return E_UNEXPECTED;
  119. ifc_omstorage *storage;
  120. HRESULT hr = ServiceHelper_QueryStorage(&storage);
  121. if (SUCCEEDED(hr))
  122. {
  123. hr = storage->Reload(&service, 1, NULL);
  124. storage->Release();
  125. }
  126. return hr;
  127. }
  128. HRESULT ServiceHelper_UpdateIcon(ifc_omserviceeditor *editor, LPCWSTR pszImage)
  129. {
  130. WCHAR szBuffer[8192];
  131. szBuffer[0] = L'\0';
  132. if (NULL == editor)
  133. return E_INVALIDARG;
  134. ifc_omservice *service;
  135. if (SUCCEEDED(editor->QueryInterface(IFC_OmService, (void**)&service)))
  136. {
  137. if (FAILED(service->GetIcon(szBuffer, ARRAYSIZE(szBuffer))))
  138. szBuffer[0] = L'\0';
  139. service->Release();
  140. }
  141. HRESULT hr = editor->SetIcon(pszImage, FALSE);
  142. if (FAILED(hr) || S_FALSE == hr) return hr;
  143. if (L'\0' != szBuffer)
  144. {
  145. ifc_mlnavigationhelper *navHelper;
  146. if (SUCCEEDED(OMUTILITY->GetMlNavigationHelper(Plugin_GetLibrary(), &navHelper)))
  147. {
  148. navHelper->ReleaseIndex(szBuffer);
  149. navHelper->Release();
  150. }
  151. }
  152. return S_OK;
  153. }
  154. HRESULT ServiceHelper_IsSpecial(ifc_omservice *service)
  155. {
  156. if (NULL == service)
  157. return E_INVALIDARG;
  158. UINT serviceFlags;
  159. HRESULT hr = service->GetFlags(&serviceFlags);
  160. if (SUCCEEDED(hr))
  161. {
  162. hr = (0 != ((WDSVCF_ROOT | WDSVCF_SPECIAL) & serviceFlags)) ? S_OK : S_FALSE;
  163. }
  164. return hr;
  165. }
  166. HRESULT ServiceHelper_IsPreAuthorized(ifc_omservice *service)
  167. {
  168. if (NULL == service)
  169. return E_INVALIDARG;
  170. UINT serviceFlags;
  171. HRESULT hr = service->GetFlags(&serviceFlags);
  172. if (SUCCEEDED(hr))
  173. {
  174. hr = (0 != (WDSVCF_PREAUTHORIZED & serviceFlags)) ? S_OK : S_FALSE;
  175. }
  176. return hr;
  177. }
  178. HRESULT ServiceHelper_RegisterPreAuthorized(ifc_omservice *service)
  179. {
  180. if (NULL == AGAVE_API_JSAPI2_SECURITY)
  181. return E_UNEXPECTED;
  182. HRESULT hr;
  183. WCHAR szBuffer[64];
  184. hr = StringCchPrintf(szBuffer, ARRAYSIZE(szBuffer), L"%u", service->GetId());
  185. if (SUCCEEDED(hr))
  186. {
  187. UINT flags;
  188. bool bypassEnabled = (SUCCEEDED(service->GetFlags(&flags)) && 0 != (WDSVCF_PREAUTHORIZED & flags));
  189. AGAVE_API_JSAPI2_SECURITY->SetBypass(szBuffer, bypassEnabled);
  190. }
  191. return hr;
  192. }