wasabiHelper.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "main.h"
  2. #include "./wasabiHelper.h"
  3. #include <api/service/waservicefactory.h>
  4. EXTERN_C const GUID pngLoaderGUID =
  5. { 0x5e04fb28, 0x53f5, 0x4032, { 0xbd, 0x29, 0x3, 0x2b, 0x87, 0xec, 0x37, 0x25 } };
  6. WasabiHelper::WasabiHelper(api_service *serviceManager)
  7. : ref(1), serviceManager(NULL), applicationApi(NULL), configApi(NULL), memoryApi(NULL),
  8. pngLoader(NULL), languageManager(NULL), securityApi(NULL), threadpoolApi(NULL), languageModule(NULL)
  9. {
  10. this->serviceManager = serviceManager;
  11. }
  12. WasabiHelper::~WasabiHelper()
  13. {
  14. ReleaseWasabiInterface(&applicationApiServiceGuid, applicationApi);
  15. ReleaseWasabiInterface(&AgaveConfigGUID, configApi);
  16. ReleaseWasabiInterface(&languageApiGUID, languageManager);
  17. ReleaseWasabiInterface(&pngLoaderGUID, pngLoader);
  18. ReleaseWasabiInterface(&memMgrApiServiceGuid, memoryApi);
  19. ReleaseWasabiInterface(&JSAPI2::api_securityGUID, securityApi);
  20. ReleaseWasabiInterface(&ThreadPoolGUID, threadpoolApi);
  21. }
  22. HRESULT WasabiHelper::CreateInstance(api_service *serviceManager, WasabiHelper **instance)
  23. {
  24. if (NULL == instance) return E_POINTER;
  25. if (NULL == serviceManager) return E_INVALIDARG;
  26. *instance = new WasabiHelper(serviceManager);
  27. if (*instance == NULL) return E_OUTOFMEMORY;
  28. return S_OK;
  29. }
  30. size_t WasabiHelper::AddRef()
  31. {
  32. return InterlockedIncrement((LONG*)&ref);
  33. }
  34. size_t WasabiHelper::Release()
  35. {
  36. if (0 == ref)
  37. return ref;
  38. LONG r = InterlockedDecrement((LONG*)&ref);
  39. if (0 == r)
  40. delete(this);
  41. return r;
  42. }
  43. int WasabiHelper::QueryInterface(GUID interface_guid, void **object)
  44. {
  45. if (NULL == object) return E_POINTER;
  46. if (IsEqualIID(interface_guid, IFC_WasabiHelper))
  47. *object = static_cast<ifc_wasabihelper*>(this);
  48. else
  49. {
  50. *object = NULL;
  51. return E_NOINTERFACE;
  52. }
  53. if (NULL == *object)
  54. return E_UNEXPECTED;
  55. AddRef();
  56. return S_OK;
  57. }
  58. HRESULT WasabiHelper::QueryWasabiInterface(const GUID *iid, void **ppi)
  59. {
  60. if (NULL == ppi) return E_POINTER;
  61. *ppi = NULL;
  62. if (NULL == iid) return E_INVALIDARG;
  63. if (NULL == serviceManager) return E_UNEXPECTED;
  64. waServiceFactory *serviceFactory = serviceManager->service_getServiceByGuid(*iid);
  65. if (NULL == serviceFactory) return E_NOINTERFACE;
  66. *ppi = serviceFactory->getInterface();
  67. if (NULL == *ppi) return E_OUTOFMEMORY;
  68. return S_OK;
  69. }
  70. HRESULT WasabiHelper::ReleaseWasabiInterface(const GUID *iid, void *pi)
  71. {
  72. if (NULL == iid || NULL == pi) return E_INVALIDARG;
  73. if (NULL == serviceManager) return E_UNEXPECTED;
  74. waServiceFactory *serviceFactory = serviceManager->service_getServiceByGuid(*iid);
  75. if (NULL == serviceFactory) return E_NOINTERFACE;
  76. serviceFactory->releaseInterface(pi);
  77. return S_OK;
  78. }
  79. HRESULT WasabiHelper::GetServiceManager(api_service **manager)
  80. {
  81. if (NULL == manager) return E_POINTER;
  82. *manager = serviceManager;
  83. if (NULL == *manager) return E_UNEXPECTED;
  84. (*manager)->AddRef();
  85. return S_OK;
  86. }
  87. HRESULT WasabiHelper::GetApplicationApi(api_application **application)
  88. {
  89. if (NULL == application) return E_POINTER;
  90. HRESULT hr = (NULL == applicationApi)? QueryWasabiInterface(&applicationApiServiceGuid, (void**)&applicationApi) : S_OK;
  91. *application = applicationApi;
  92. if (NULL != applicationApi)
  93. applicationApi->AddRef();
  94. return hr;
  95. }
  96. HRESULT WasabiHelper::GetConfigApi(api_config ** config)
  97. {
  98. if (NULL == config) return E_POINTER;
  99. HRESULT hr = (NULL == configApi)? QueryWasabiInterface(&AgaveConfigGUID, (void**)&configApi) : S_OK;
  100. *config = configApi;
  101. if (NULL != configApi)
  102. configApi->AddRef();
  103. return hr;
  104. }
  105. HRESULT WasabiHelper::GetMemoryManager(api_memmgr **memoryManager)
  106. {
  107. if (NULL == memoryManager) return E_POINTER;
  108. HRESULT hr = (NULL == memoryApi)? QueryWasabiInterface(&memMgrApiServiceGuid, (void**)&memoryApi) : S_OK;
  109. *memoryManager = memoryApi;
  110. if (NULL != memoryApi)
  111. memoryApi->AddRef();
  112. return hr;
  113. }
  114. HRESULT WasabiHelper::GetPngLoader(svc_imageLoader **loader)
  115. {
  116. if (NULL == loader) return E_POINTER;
  117. HRESULT hr = (NULL == pngLoader)? QueryWasabiInterface(&pngLoaderGUID, (void**)&pngLoader) : S_OK;
  118. *loader = pngLoader;
  119. if (NULL != pngLoader)
  120. pngLoader->AddRef();
  121. return hr;
  122. }
  123. HRESULT WasabiHelper::InitLanguageSupport()
  124. {
  125. if (NULL != languageManager && NULL != languageModule)
  126. return S_FALSE;
  127. if(NULL == languageManager)
  128. {
  129. languageModule = NULL;
  130. HRESULT hr = QueryWasabiInterface(&languageApiGUID, (void**)&languageManager);
  131. if (FAILED(hr))
  132. return hr;
  133. }
  134. if (NULL == languageModule)
  135. {
  136. languageModule = languageManager->StartLanguageSupport(Plugin_GetInstance(), omBrowserLangGUID);
  137. if (NULL == languageModule)
  138. {
  139. languageManager->Release();
  140. languageManager = NULL;
  141. return E_FAIL;
  142. }
  143. }
  144. return S_OK;
  145. }
  146. HRESULT WasabiHelper::GetLanguageManager(api_language **language)
  147. {
  148. if (NULL == language) return E_POINTER;
  149. HRESULT hr = InitLanguageSupport();
  150. if (FAILED(hr)) return hr;
  151. *language = (SUCCEEDED(hr)) ? languageManager : NULL;
  152. if (NULL != *language)
  153. (*language)->AddRef();
  154. return hr;
  155. }
  156. HRESULT WasabiHelper::GetLanguageModule(HINSTANCE *module)
  157. {
  158. if (NULL == module) return E_POINTER;
  159. HRESULT hr = InitLanguageSupport();
  160. if (FAILED(hr)) return hr;
  161. *module = languageModule;
  162. return S_OK;
  163. }
  164. HRESULT WasabiHelper::GetSecurityApi(JSAPI2::api_security **security)
  165. {
  166. if (NULL == security) return E_POINTER;
  167. HRESULT hr = (NULL == securityApi)? QueryWasabiInterface(&JSAPI2::api_securityGUID, (void**)&securityApi) : S_OK;
  168. *security = securityApi;
  169. if (NULL != securityApi)
  170. securityApi->AddRef();
  171. return hr;
  172. }
  173. HRESULT WasabiHelper::GetThreadpoolApi(api_threadpool **threadpool)
  174. {
  175. if (NULL == threadpool) return E_POINTER;
  176. HRESULT hr = (NULL == threadpoolApi)? QueryWasabiInterface(&ThreadPoolGUID, (void**)&threadpoolApi) : S_OK;
  177. *threadpool = threadpoolApi;
  178. if (NULL != *threadpool)
  179. threadpoolApi->AddRef();
  180. return hr;
  181. }
  182. #define CBCLASS WasabiHelper
  183. START_DISPATCH;
  184. CB(ADDREF, AddRef)
  185. CB(RELEASE, Release)
  186. CB(QUERYINTERFACE, QueryInterface)
  187. CB(API_QUERYWASABIINTERFACE, QueryWasabiInterface)
  188. CB(API_RELEASEWASABIINTERFACE, ReleaseWasabiInterface)
  189. CB(API_GETSERVICEMANAGER, GetServiceManager)
  190. CB(API_GETAPPLICATION, GetApplicationApi)
  191. CB(API_GETCONFIG, GetConfigApi)
  192. CB(API_GETMEMORYMANAGER, GetMemoryManager)
  193. CB(API_GETPNGLOADER, GetPngLoader)
  194. CB(API_GETLANGUAGEMANAGER, GetLanguageManager)
  195. CB(API_GETLANGUAGEMODULE, GetLanguageModule)
  196. CB(API_GETSECURITY, GetSecurityApi)
  197. CB(API_GETTHREADPOOL, GetThreadpoolApi)
  198. END_DISPATCH;
  199. #undef CBCLASS