Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
7.0 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. // MethodContext.cpp
  6. //
  7. // Purpose: Internal and External Method context classes
  8. //
  9. //***************************************************************************
  10. #include "precomp.h"
  11. #include <assertbreak.h>
  12. #include <stopwatch.h>
  13. #include <smartptr.h>
  14. MethodContext::MethodContext(IWbemContext __RPC_FAR *piContext, CWbemProviderGlue *pGlue):
  15. m_pStatusObject(NULL),
  16. m_pContext(NULL)
  17. {
  18. #ifdef PROVIDER_INSTRUMENTATION
  19. pStopWatch = NULL;
  20. #endif
  21. m_pGlue = pGlue;
  22. if (piContext)
  23. {
  24. piContext->AddRef();
  25. }
  26. m_pContext = piContext;
  27. }
  28. MethodContext::~MethodContext()
  29. {
  30. PROVIDER_INSTRUMENTATION_START2(pStopWatch, StopWatch::WinMgmtTimer);
  31. if (m_pContext)
  32. {
  33. m_pContext->Release();
  34. }
  35. PROVIDER_INSTRUMENTATION_START2(pStopWatch, StopWatch::FrameworkTimer);
  36. if (m_pStatusObject)
  37. {
  38. m_pStatusObject->Release();
  39. }
  40. }
  41. // might be NULL
  42. IWbemContext __RPC_FAR *MethodContext::GetIWBEMContext()
  43. {
  44. IWbemContext __RPC_FAR *pContext = NULL;
  45. BeginWrite();
  46. try
  47. {
  48. if (pContext = m_pContext)
  49. {
  50. m_pContext->AddRef();
  51. }
  52. }
  53. catch ( ... )
  54. {
  55. EndWrite();
  56. throw;
  57. }
  58. EndWrite();
  59. return pContext;
  60. }
  61. LONG MethodContext::AddRef(void)
  62. {
  63. return CThreadBase::AddRef();
  64. }
  65. LONG MethodContext::Release(void)
  66. {
  67. return CThreadBase::Release();
  68. }
  69. CWbemProviderGlue *MethodContext::GetProviderGlue()
  70. {
  71. return m_pGlue;
  72. }
  73. // returns false if the object has already been set
  74. bool MethodContext::SetStatusObject(IWbemClassObject __RPC_FAR *pObj)
  75. {
  76. BeginWrite();
  77. bool bRet;
  78. try
  79. {
  80. if (bRet = (m_pStatusObject == NULL))
  81. {
  82. m_pStatusObject = pObj;
  83. pObj->AddRef();
  84. }
  85. }
  86. catch ( ... )
  87. {
  88. EndWrite();
  89. throw;
  90. }
  91. EndWrite();
  92. ASSERT_BREAK(bRet);
  93. return bRet;
  94. }
  95. IWbemClassObject __RPC_FAR *MethodContext::GetStatusObject()
  96. {
  97. IWbemClassObject __RPC_FAR *pOut = NULL;
  98. BeginWrite();
  99. try
  100. {
  101. if (pOut = m_pStatusObject)
  102. {
  103. pOut->AddRef();
  104. }
  105. }
  106. catch ( ... )
  107. {
  108. EndWrite();
  109. throw;
  110. }
  111. EndWrite();
  112. return pOut;
  113. }
  114. // Not meaningful unless we are at a ExternalMethodContext object
  115. void MethodContext::QueryPostProcess(void)
  116. {
  117. }
  118. //------------------------------------------------------------------------------------------
  119. ExternalMethodContext::ExternalMethodContext(IWbemObjectSink __RPC_FAR *pResponseHandler,
  120. IWbemContext __RPC_FAR *pContext,
  121. CWbemProviderGlue *pGlue,
  122. void *pReserved
  123. ) : MethodContext(pContext, pGlue)
  124. {
  125. pResponseHandler->AddRef();
  126. m_pResponseHandler = pResponseHandler;
  127. m_pReserved = pReserved ;
  128. }
  129. LONG ExternalMethodContext::AddRef(void)
  130. {
  131. m_pResponseHandler->AddRef();
  132. return MethodContext::AddRef();
  133. }
  134. LONG ExternalMethodContext::Release(void)
  135. {
  136. m_pResponseHandler->Release();
  137. return MethodContext::Release();
  138. }
  139. HRESULT ExternalMethodContext::Commit(CInstance *pInstance)
  140. {
  141. HRESULT hRes = WBEM_E_FAILED;
  142. IWbemClassObjectPtr p (pInstance->GetClassObjectInterface(), false);
  143. IWbemClassObject *p2 = (IWbemClassObject *)p;
  144. if (p != NULL)
  145. {
  146. PROVIDER_INSTRUMENTATION_START2(pStopWatch, StopWatch::WinMgmtTimer);
  147. hRes = m_pResponseHandler->Indicate(1, &p2);
  148. PROVIDER_INSTRUMENTATION_START2(pStopWatch, StopWatch::ProviderTimer);
  149. }
  150. return hRes;
  151. }
  152. // Call this function to let cimom know that it will have to re-process
  153. // the instances after it gets them back. Otherwise it assumes that
  154. // the query has been fully processed by the provider. Most (all?) providers
  155. // should call this function.
  156. void ExternalMethodContext::QueryPostProcess(void)
  157. {
  158. PROVIDER_INSTRUMENTATION_START2(pStopWatch, StopWatch::WinMgmtTimer);
  159. m_pResponseHandler->SetStatus(WBEM_STATUS_REQUIREMENTS, 0, NULL, NULL);
  160. PROVIDER_INSTRUMENTATION_START2(pStopWatch, StopWatch::FrameworkTimer);
  161. }
  162. //------------------------------------------------------------------------------------------
  163. InternalMethodContext::InternalMethodContext( TRefPointerCollection<CInstance> *pList ,
  164. IWbemContext __RPC_FAR *pContext,
  165. CWbemProviderGlue *pGlue) : MethodContext(pContext, pGlue)
  166. {
  167. // A NULL List only means we really won't be doing anything when we
  168. // are told to commit. Otherwise, we will store an instance pointer
  169. // in the supplied list.
  170. if ( NULL != pList )
  171. {
  172. pList->AddRef();
  173. }
  174. m_pInstances = pList;
  175. }
  176. InternalMethodContext::~InternalMethodContext( void )
  177. {
  178. }
  179. LONG InternalMethodContext::AddRef(void)
  180. {
  181. if ( NULL != m_pInstances )
  182. {
  183. m_pInstances->AddRef();
  184. }
  185. return MethodContext::AddRef();
  186. }
  187. LONG InternalMethodContext::Release(void)
  188. {
  189. if ( NULL != m_pInstances )
  190. {
  191. m_pInstances->Release();
  192. }
  193. return MethodContext::Release();
  194. }
  195. HRESULT InternalMethodContext::Commit(CInstance *pInstance)
  196. {
  197. HRESULT hr = WBEM_S_NO_ERROR;
  198. if ( NULL != m_pInstances )
  199. {
  200. if (!m_pInstances->Add(pInstance)) {
  201. hr = WBEM_E_FAILED;
  202. }
  203. }
  204. return hr;
  205. }
  206. //========================================================================
  207. InternalMethodContextAsynch::InternalMethodContextAsynch(Provider *pThat,
  208. LPProviderInstanceCallback pCallback,
  209. IWbemContext __RPC_FAR *pContext,
  210. MethodContext *pUsersContext,
  211. void *pUserData
  212. ) : MethodContext(pContext, pUsersContext->GetProviderGlue())
  213. {
  214. ASSERT_BREAK(pThat != NULL);
  215. ASSERT_BREAK(pCallback != NULL);
  216. m_pThat = pThat;
  217. m_pCallback = pCallback;
  218. m_pUserData = pUserData;
  219. m_pUsersContext = pUsersContext;
  220. if ( NULL != m_pThat )
  221. {
  222. m_pThat->AddRef();
  223. }
  224. if (NULL != m_pUsersContext)
  225. {
  226. m_pUsersContext->AddRef();
  227. }
  228. }
  229. InternalMethodContextAsynch::~InternalMethodContextAsynch()
  230. {
  231. }
  232. HRESULT InternalMethodContextAsynch::Commit(CInstance *pInstance)
  233. {
  234. return (*m_pCallback)(m_pThat, pInstance, m_pUsersContext, m_pUserData);
  235. }
  236. LONG InternalMethodContextAsynch::AddRef(void)
  237. {
  238. if ( NULL != m_pThat )
  239. {
  240. m_pThat->AddRef();
  241. }
  242. if (NULL != m_pUsersContext)
  243. {
  244. m_pUsersContext->AddRef();
  245. }
  246. return MethodContext::AddRef();
  247. }
  248. LONG InternalMethodContextAsynch::Release(void)
  249. {
  250. if ( NULL != m_pThat )
  251. {
  252. m_pThat->Release();
  253. }
  254. if (NULL != m_pUsersContext)
  255. {
  256. m_pUsersContext->Release();
  257. }
  258. return MethodContext::Release();
  259. }