Leaked source code of windows server 2003
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.

296 lines
7.1 KiB

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