serviceManager.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "main.h"
  2. #include "./serviceManager.h"
  3. #include "./service.h"
  4. #include "./ifc_wasabihelper.h"
  5. #include "./storageIni.h"
  6. #include "./storageXml.h"
  7. #include "./storageUrl.h"
  8. #include "./storageEnum.h"
  9. #include <shlwapi.h>
  10. #include <strsafe.h>
  11. OmServiceManager::OmServiceManager()
  12. : ref(1), registerStorage(TRUE)
  13. {
  14. }
  15. OmServiceManager::~OmServiceManager()
  16. {
  17. size_t index = storageList.size();
  18. while(index--)
  19. {
  20. storageList[index]->Release();
  21. }
  22. }
  23. OmServiceManager *OmServiceManager::CreateInstance()
  24. {
  25. return new OmServiceManager();
  26. }
  27. size_t OmServiceManager::AddRef()
  28. {
  29. return InterlockedIncrement((LONG*)&ref);
  30. }
  31. size_t OmServiceManager::Release()
  32. {
  33. if (0 == ref)
  34. return ref;
  35. LONG r = InterlockedDecrement((LONG*)&ref);
  36. if (0 == r)
  37. delete(this);
  38. return r;
  39. }
  40. int OmServiceManager::QueryInterface(GUID interface_guid, void **object)
  41. {
  42. if (NULL == object) return E_POINTER;
  43. if (IsEqualIID(interface_guid, IFC_OmServiceManager))
  44. *object = static_cast<ifc_omservicemanager*>(this);
  45. else
  46. {
  47. *object = NULL;
  48. return E_NOINTERFACE;
  49. }
  50. if (NULL == *object)
  51. return E_UNEXPECTED;
  52. AddRef();
  53. return S_OK;
  54. }
  55. HRESULT OmServiceManager::RegisterDefaultStorage()
  56. {
  57. ifc_omstorage *storage;
  58. if (SUCCEEDED(OmStorageIni::CreateInstance((OmStorageIni**)&storage)))
  59. {
  60. RegisterStorage(storage);
  61. storage->Release();
  62. }
  63. if (SUCCEEDED(OmStorageXml::CreateInstance((OmStorageXml**)&storage)))
  64. {
  65. RegisterStorage(storage);
  66. storage->Release();
  67. }
  68. if (SUCCEEDED(OmStorageUrl::CreateInstance((OmStorageUrl**)&storage)))
  69. {
  70. RegisterStorage(storage);
  71. storage->Release();
  72. }
  73. return S_OK;
  74. }
  75. HRESULT OmServiceManager::RegisterStorage(ifc_omstorage *storage)
  76. {
  77. if (NULL == storage) return E_INVALIDARG;
  78. HRESULT hr;
  79. GUID storageId, testId;
  80. if (FAILED(storage->GetId(&storageId)))
  81. return E_FAIL;
  82. size_t index = storageList.size();
  83. while(index--)
  84. {
  85. if(SUCCEEDED(storageList[index]->GetId(&testId)) && IsEqualGUID(storageId, testId))
  86. break;
  87. }
  88. if (((size_t)-1) == index)
  89. {
  90. storageList.push_back(storage);
  91. storage->AddRef();
  92. hr = S_OK;
  93. }
  94. else
  95. {
  96. hr = S_FALSE;
  97. }
  98. return hr;
  99. }
  100. HRESULT OmServiceManager::UnregisterStorage(const GUID *storageId)
  101. {
  102. if (NULL == storageId) return E_INVALIDARG;
  103. HRESULT hr = S_FALSE;
  104. GUID testId;
  105. size_t index = storageList.size();
  106. while(index--)
  107. {
  108. ifc_omstorage *storage = storageList[index];
  109. if(SUCCEEDED(storage->GetId(&testId)) && IsEqualGUID(*storageId, testId))
  110. {
  111. storageList.erase(storageList.begin() + index);
  112. storage->Release();
  113. hr = S_OK;
  114. break;
  115. }
  116. }
  117. return hr;
  118. }
  119. HRESULT OmServiceManager::QueryStorage(const GUID *storageId, ifc_omstorage **storageOut)
  120. {
  121. if (NULL == storageOut) return E_POINTER;
  122. *storageOut = NULL;
  123. if (NULL == storageId)
  124. return E_INVALIDARG;
  125. if (FALSE != registerStorage && SUCCEEDED(RegisterDefaultStorage()))
  126. registerStorage = FALSE;
  127. HRESULT hr = S_FALSE;
  128. GUID testId;
  129. size_t index = storageList.size();
  130. while(index--)
  131. {
  132. ifc_omstorage *storage = storageList[index];
  133. if(SUCCEEDED(storage->GetId(&testId)) && IsEqualGUID(*storageId, testId))
  134. {
  135. storage->AddRef();
  136. *storageOut = storage;
  137. hr = S_OK;
  138. break;
  139. }
  140. }
  141. return hr;
  142. }
  143. HRESULT OmServiceManager::EnumStorage(const GUID *filterType, UINT filterCapabilities, ifc_omstorageenumerator **enumOut)
  144. {
  145. if (FALSE != registerStorage && SUCCEEDED(RegisterDefaultStorage()))
  146. registerStorage = FALSE;
  147. return OmStorageEnumerator::CreateInstance(storageList.size() ? &storageList.at(0) : nullptr, storageList.size(),
  148. filterType, filterCapabilities,
  149. (OmStorageEnumerator**)enumOut);
  150. }
  151. HRESULT OmServiceManager::CreateService(UINT serviceId, ifc_omservicehost *host, ifc_omservice **serviceOut)
  152. {
  153. if (NULL == serviceOut) return E_POINTER;
  154. OmService *service = NULL;
  155. HRESULT hr = OmService::CreateInstance(serviceId, host, &service);
  156. if (FAILED(hr))
  157. {
  158. *serviceOut = NULL;
  159. return hr;
  160. }
  161. *serviceOut = service;
  162. return hr;
  163. }
  164. #define CBCLASS OmServiceManager
  165. START_DISPATCH;
  166. CB(ADDREF, AddRef)
  167. CB(RELEASE, Release)
  168. CB(QUERYINTERFACE, QueryInterface)
  169. CB(API_REGISTERSTORAGE, RegisterStorage)
  170. CB(API_UNREGISTERSTORAGE, UnregisterStorage)
  171. CB(API_QUERYSTORAGE, QueryStorage)
  172. CB(API_ENUMSTORAGE, EnumStorage)
  173. CB(API_CREATESERVICE, CreateService)
  174. END_DISPATCH;
  175. #undef CBCLASS