ServiceWatcher.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "ServiceWatcher.h"
  2. #include <api/service/waservicefactory.h>
  3. static void *GetService(api_service *p_serviceManager, GUID p_serviceGUID)
  4. {
  5. waServiceFactory *sf = p_serviceManager->service_getServiceByGuid( p_serviceGUID);
  6. if (sf)
  7. return sf->getInterface();
  8. else
  9. return 0;
  10. }
  11. static void ReleaseService(api_service *p_serviceManager, GUID p_serviceGUID, void *p_service)
  12. {
  13. waServiceFactory *sf = p_serviceManager->service_getServiceByGuid( p_serviceGUID);
  14. if (sf)
  15. sf->releaseInterface( p_service);
  16. }
  17. void ServiceWatcher::WatchWith(api_service *_serviceApi)
  18. {
  19. serviceManager =_serviceApi;
  20. systemCallbacks=(api_syscb*)GetService( serviceManager, syscbApiServiceGuid);
  21. }
  22. void ServiceWatcher::StopWatching()
  23. {
  24. if (systemCallbacks)
  25. {
  26. systemCallbacks->syscb_deregisterCallback(this);
  27. ReleaseService( serviceManager, syscbApiServiceGuid, systemCallbacks);
  28. }
  29. systemCallbacks=0;
  30. }
  31. void ServiceWatcher::Clear()
  32. {
  33. //watchList.Reset();
  34. watchList.clear();
  35. }
  36. ServiceWatcher::~ServiceWatcher()
  37. {
  38. //StopWatching();
  39. }
  40. void ServiceWatcher::WatchForT(void **ptr, GUID watchGUID)
  41. {
  42. watchList[watchGUID]=ptr;
  43. if (!*ptr) // try to get it if we need it
  44. {
  45. *ptr = GetService( serviceManager, watchGUID);
  46. }
  47. }
  48. int ServiceWatcher::Notify(int msg, intptr_t param1, intptr_t param2)
  49. {
  50. switch (msg)
  51. {
  52. case SvcCallback::ONREGISTER:
  53. {
  54. waServiceFactory *sf = reinterpret_cast<waServiceFactory*>(param2);
  55. GUID serviceGUID = sf->getGuid();
  56. if (serviceGUID != INVALID_GUID)
  57. {
  58. WatchList::iterator itr = watchList.find(serviceGUID);
  59. if (itr!=watchList.end())
  60. {
  61. void **ptr = itr->second;
  62. if (ptr && !*ptr) // don't re-retrieve service if we already have it
  63. {
  64. *ptr = sf->getInterface();
  65. }
  66. }
  67. }
  68. }
  69. break;
  70. case SvcCallback::ONDEREGISTER:
  71. {
  72. waServiceFactory *sf = reinterpret_cast<waServiceFactory*>(param2);
  73. GUID serviceGUID = sf->getGuid();
  74. if (serviceGUID != INVALID_GUID)
  75. {
  76. WatchList::iterator itr = watchList.find(serviceGUID);
  77. if (itr!=watchList.end())
  78. {
  79. void **ptr = itr->second;
  80. if (ptr && *ptr)
  81. {
  82. // benski> probably not safe to do, so i'll leave it commented out: sf->releaseInterface(*ptr);
  83. *ptr = 0;
  84. }
  85. }
  86. }
  87. }
  88. break;
  89. default: return 0;
  90. }
  91. return 1;
  92. }
  93. #define CBCLASS ServiceWatcher
  94. START_DISPATCH;
  95. CB(SYSCALLBACK_GETEVENTTYPE, GetEventType);
  96. CB(SYSCALLBACK_NOTIFY, Notify);
  97. END_DISPATCH;
  98. #undef CBCLASS
  99. ServiceWatcherSingle::~ServiceWatcherSingle()
  100. {
  101. //StopWatching();
  102. }
  103. void ServiceWatcherSingle::StopWatching()
  104. {
  105. if (systemCallbacks)
  106. {
  107. systemCallbacks->syscb_deregisterCallback(this);
  108. ReleaseService( serviceManager, syscbApiServiceGuid, systemCallbacks);
  109. }
  110. systemCallbacks=0;
  111. }
  112. void ServiceWatcherSingle::WatchWith(api_service *_serviceApi)
  113. {
  114. serviceManager =_serviceApi;
  115. systemCallbacks=(api_syscb*)GetService( serviceManager, syscbApiServiceGuid);
  116. }
  117. void ServiceWatcherSingle::WatchForT(void **ptr, GUID watchGUID)
  118. {
  119. service=ptr;
  120. serviceGUID=watchGUID;
  121. if (ptr && !*ptr) // try to get it if we need it
  122. {
  123. *ptr = GetService( serviceManager, watchGUID);
  124. if (*ptr)
  125. OnRegister();
  126. }
  127. }
  128. int ServiceWatcherSingle::Notify(int msg, intptr_t param1, intptr_t param2)
  129. {
  130. switch (msg)
  131. {
  132. case SvcCallback::ONREGISTER:
  133. {
  134. if (service && !*service) // don't re-retrieve service if we already have it
  135. {
  136. waServiceFactory *sf = reinterpret_cast<waServiceFactory*>(param2);
  137. if (sf && sf->getGuid() == serviceGUID)
  138. {
  139. *service = sf->getInterface();
  140. if (*service)
  141. OnRegister();
  142. }
  143. }
  144. }
  145. break;
  146. case SvcCallback::ONDEREGISTER:
  147. {
  148. if (service && *service)
  149. {
  150. waServiceFactory *sf = reinterpret_cast<waServiceFactory*>(param2);
  151. if (serviceGUID == sf->getGuid())
  152. {
  153. OnDeregister();
  154. *service=0;
  155. }
  156. }
  157. }
  158. break;
  159. default: return 0;
  160. }
  161. return 1;
  162. }
  163. #define CBCLASS ServiceWatcherSingle
  164. START_DISPATCH;
  165. CB(SYSCALLBACK_GETEVENTTYPE, GetEventType);
  166. CB(SYSCALLBACK_NOTIFY, Notify);
  167. END_DISPATCH;
  168. #undef CBCLASS