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.

394 lines
13 KiB

  1. //******************************************************************************
  2. //
  3. // FILTPROX.H
  4. //
  5. // Copyright (C) 1996-1999 Microsoft Corporation
  6. //
  7. //******************************************************************************
  8. #ifndef __WBEM_FILTER_PROXY__H_
  9. #define __WBEM_FILTER_PROXY__H_
  10. #include <evaltree.h>
  11. class CTimeKeeper
  12. {
  13. protected:
  14. CCritSec m_cs;
  15. FILETIME m_ftLastEvent;
  16. DWORD m_dwEventCount;
  17. long m_lTimeHandle;
  18. bool m_bHandleInit;
  19. public:
  20. CTimeKeeper() : m_dwEventCount(0), m_lTimeHandle(0), m_bHandleInit(false)
  21. {
  22. m_ftLastEvent.dwLowDateTime = m_ftLastEvent.dwHighDateTime = 0;
  23. }
  24. bool DecorateObject(_IWmiObject* pObj);
  25. };
  26. class CWrappingMetaData : public CMetaData
  27. {
  28. protected:
  29. IWbemMetaData* m_pDest;
  30. public:
  31. CWrappingMetaData(IWbemMetaData* pDest) : m_pDest(pDest)
  32. {m_pDest->AddRef();}
  33. ~CWrappingMetaData()
  34. {m_pDest->Release();}
  35. STDMETHOD(GetClass)(LPCWSTR wszName, IWbemContext* pContext,
  36. IWbemClassObject** ppClass)
  37. {return m_pDest->GetClass(wszName, pContext, ppClass);}
  38. virtual HRESULT GetClass(LPCWSTR wszName, IWbemContext* pContext,
  39. _IWmiObject** ppClass);
  40. };
  41. class CEventBatch
  42. {
  43. public:
  44. CEventBatch();
  45. ~CEventBatch();
  46. BOOL EnsureAdditionalSize(DWORD nAdditionalNeeded);
  47. BOOL AddEvent(IWbemClassObject *pObj, CSortedArray *pTrues);
  48. void RemoveAll();
  49. void SetItemCount(DWORD nItems) { m_nItems = nItems; }
  50. DWORD GetItemCount() { return m_nItems; }
  51. IWbemClassObject **GetObjs() { return m_ppObjs; }
  52. WBEM_REM_TARGETS *GetTargets() { return m_pTargets; }
  53. protected:
  54. DWORD m_nItems,
  55. m_dwSize;
  56. IWbemClassObject **m_ppObjs;
  57. WBEM_REM_TARGETS *m_pTargets;
  58. };
  59. class CFilterProxyManager;
  60. class CFilterProxy : public IWbemEventSink, public IMarshal
  61. {
  62. protected:
  63. long m_lRef;
  64. CFilterProxyManager* m_pManager;
  65. CWrappingMetaData* m_pMetaData;
  66. CEvalTree m_SourceDefinition;
  67. CEvalTree m_Filter;
  68. WORD m_wSourceVersion;
  69. WORD m_wAppliedSourceVersion;
  70. long m_lSDLength;
  71. BYTE* m_pSD;
  72. IWbemEventProvider* m_pProvider;
  73. IWbemEventProviderQuerySink* m_pQuerySink;
  74. bool m_bRunning;
  75. bool m_bUtilizeGuarantee;
  76. CCritSec m_cs;
  77. static CTimeKeeper mstatic_TimeKeeper;
  78. /////////////////////////////////////////////////////////////////////////
  79. // Batching members
  80. void BatchEvent(IWbemClassObject *pObj, CSortedArray *pTrues);
  81. HRESULT BatchMany(long nEvents, IUnknown **ppObjects);
  82. BOOL IsBatching() { return m_bBatching; }
  83. WBEM_BATCH_TYPE m_typeBatch;
  84. DWORD m_dwCurrentBufferSize,
  85. m_dwMaxBufferSize,
  86. m_dwMaxSendLatency,
  87. m_dwLastSentStamp;
  88. BOOL m_bBatching;
  89. public:
  90. static BYTE mstatic_EmptySD;
  91. HRESULT Lock();
  92. HRESULT Unlock();
  93. HRESULT AddFilter(IWbemContext* pContext, LPCWSTR wszQuery,
  94. QL_LEVEL_1_RPN_EXPRESSION* pExp,
  95. WBEM_REMOTE_TARGET_ID_TYPE Id);
  96. HRESULT RemoveFilter(IWbemContext* pContext,
  97. WBEM_REMOTE_TARGET_ID_TYPE Id);
  98. HRESULT RemoveAllFilters(IWbemContext* pContext);
  99. HRESULT AddDefinitionQuery(IWbemContext* pContext, LPCWSTR wszQuery);
  100. HRESULT RemoveAllDefinitionQueries(IWbemContext* pContext);
  101. HRESULT AllowUtilizeGuarantee();
  102. HRESULT TransferFiltersFromMain(CFilterProxy* pMain);
  103. HRESULT FilterEvent( _IWmiObject* pEvent, CSortedArray& raTrues );
  104. public:
  105. CFilterProxy(CFilterProxyManager* pManager, IUnknown* pCallback = NULL);
  106. ~CFilterProxy();
  107. HRESULT SetRunning();
  108. // IUnknown
  109. ULONG STDMETHODCALLTYPE AddRef();
  110. ULONG STDMETHODCALLTYPE Release();
  111. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv);
  112. // IWbemObjectSink
  113. HRESULT STDMETHODCALLTYPE Indicate(long lNumObjects,
  114. IWbemClassObject** apObjects);
  115. HRESULT STDMETHODCALLTYPE SetStatus(long lFlags, HRESULT hResult,
  116. BSTR strResult, IWbemClassObject* pErrorObj);
  117. HRESULT STDMETHODCALLTYPE IndicateWithSD(
  118. long lNumObjects,
  119. IUnknown** apObjects,
  120. long lSDLength,
  121. BYTE* pSD);
  122. HRESULT STDMETHODCALLTYPE SetSinkSecurity(
  123. long lSDLength,
  124. BYTE* pSD);
  125. HRESULT STDMETHODCALLTYPE IsActive();
  126. HRESULT STDMETHODCALLTYPE GetRestrictedSink(
  127. long lNumQueries,
  128. const LPCWSTR* awszQueries,
  129. IUnknown* pCallback,
  130. IWbemEventSink** ppSink);
  131. HRESULT STDMETHODCALLTYPE SetBatchingParameters(
  132. LONG lFlags,
  133. DWORD dwMaxBufferSize,
  134. DWORD dwMaxSendLatency);
  135. // IMarshal
  136. STDMETHOD(GetUnmarshalClass)(REFIID riid, void* pv, DWORD dwDestContext,
  137. void* pvReserved, DWORD mshlFlags, CLSID* pClsid);
  138. STDMETHOD(GetMarshalSizeMax)(REFIID riid, void* pv, DWORD dwDestContext,
  139. void* pvReserved, DWORD mshlFlags, ULONG* plSize);
  140. STDMETHOD(MarshalInterface)(IStream* pStream, REFIID riid, void* pv,
  141. DWORD dwDestContext, void* pvReserved, DWORD mshlFlags);
  142. STDMETHOD(UnmarshalInterface)(IStream* pStream, REFIID riid, void** ppv)
  143. {return E_NOTIMPL;}
  144. STDMETHOD(ReleaseMarshalData)(IStream* pStream)
  145. {return E_NOTIMPL;}
  146. STDMETHOD(DisconnectObject)(DWORD dwReserved)
  147. {return E_NOTIMPL;}
  148. protected:
  149. HRESULT ProcessOne(IUnknown* pObj, long lSDLength, BYTE* pSD);
  150. HRESULT ProcessMany(long lNumObjects, IUnknown** apObjects,
  151. long lSDLength, BYTE* pSD);
  152. static void SetGenerationTime(_IWmiObject* pObj);
  153. };
  154. class CFilterProxyManagerBase
  155. {
  156. CLifeControl* m_pControl;
  157. public:
  158. CFilterProxyManagerBase( CLifeControl* pControl ) : m_pControl(pControl)
  159. {
  160. if( m_pControl != NULL )
  161. {
  162. m_pControl->ObjectCreated( (IUnknown*)this );
  163. }
  164. }
  165. ~CFilterProxyManagerBase()
  166. {
  167. if( m_pControl != NULL )
  168. {
  169. m_pControl->ObjectDestroyed( (IUnknown*)this );
  170. }
  171. }
  172. };
  173. class CFilterProxyManager : public IMarshal, CFilterProxyManagerBase
  174. {
  175. protected:
  176. long m_lRef;
  177. long m_lExtRef;
  178. IWbemMultiTarget* m_pMultiTarget;
  179. CWrappingMetaData* m_pMetaData;
  180. IWbemFilterStub* m_pStub;
  181. IWbemContext* m_pSpecialContext;
  182. CCritSec m_cs;
  183. CWbemCriticalSection m_Lock;
  184. CUniquePointerArray<CFilterProxy> m_apProxies;
  185. typedef std::map< WBEM_REMOTE_TARGET_ID_TYPE,
  186. WString,
  187. std::less< WBEM_REMOTE_TARGET_ID_TYPE >,
  188. wbem_allocator< WString > > TMap;
  189. typedef TMap::iterator TIterator;
  190. TMap m_mapQueries;
  191. /////////////////////////////////////////////////////////////////////////
  192. // Protected batching members
  193. typedef std::map< CFilterProxy*,
  194. DWORD,
  195. std::less< CFilterProxy* >,
  196. wbem_allocator< DWORD > > CLatencyMap;
  197. typedef CLatencyMap::iterator CLatencyMapItor;
  198. CLatencyMap m_mapLatencies;
  199. HANDLE m_heventDone,
  200. m_hthreadSend,
  201. m_heventEventsPending,
  202. m_heventBufferNotFull,
  203. m_heventBufferFull;
  204. DWORD m_dwMaxSendLatency,
  205. m_dwLastSentStamp;
  206. CCritSec m_csBuffer;
  207. CEventBatch m_batch;
  208. IStream *m_pMultiTargetStream;
  209. void LockBatching() { EnterCriticalSection(&m_csBuffer); }
  210. void UnlockBatching() { LeaveCriticalSection(&m_csBuffer); }
  211. BOOL IsBatching() { return m_hthreadSend != NULL; }
  212. CEventBatch *GetBatch(LPBYTE pSD, DWORD dwLength);
  213. void CalcMaxSendLatency();
  214. static DWORD WINAPI SendThreadProc(CFilterProxyManager *pThis);
  215. BOOL StartSendThread();
  216. void StopSendThread();
  217. protected:
  218. class XProxy : public IWbemLocalFilterProxy
  219. {
  220. protected:
  221. CFilterProxyManager* m_pObject;
  222. public:
  223. XProxy(CFilterProxyManager* pObject) : m_pObject(pObject){}
  224. // IUnknown
  225. ULONG STDMETHODCALLTYPE AddRef();
  226. ULONG STDMETHODCALLTYPE Release();
  227. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv);
  228. // IWbemFilterProxy
  229. HRESULT STDMETHODCALLTYPE Initialize(IWbemMetaData* pMetaData,
  230. IWbemMultiTarget* pMultiTarget);
  231. HRESULT STDMETHODCALLTYPE Lock();
  232. HRESULT STDMETHODCALLTYPE Unlock();
  233. HRESULT STDMETHODCALLTYPE AddFilter(IWbemContext* pContext,
  234. LPCWSTR wszQuery,
  235. WBEM_REMOTE_TARGET_ID_TYPE Id);
  236. HRESULT STDMETHODCALLTYPE RemoveFilter(IWbemContext* pContext,
  237. WBEM_REMOTE_TARGET_ID_TYPE Id);
  238. HRESULT STDMETHODCALLTYPE RemoveAllFilters(IWbemContext* pContext);
  239. HRESULT STDMETHODCALLTYPE AddDefinitionQuery(IWbemContext* pContext,
  240. LPCWSTR wszQuery);
  241. HRESULT STDMETHODCALLTYPE RemoveAllDefinitionQueries(
  242. IWbemContext* pContext);
  243. HRESULT STDMETHODCALLTYPE AllowUtilizeGuarantee();
  244. HRESULT STDMETHODCALLTYPE Disconnect();
  245. HRESULT STDMETHODCALLTYPE SetStub(IWbemFilterStub* pStub);
  246. HRESULT STDMETHODCALLTYPE LocalAddFilter(IWbemContext* pContext,
  247. LPCWSTR wszQuery,
  248. void* pExp,
  249. WBEM_REMOTE_TARGET_ID_TYPE Id);
  250. HRESULT STDMETHODCALLTYPE GetMainSink(IWbemEventSink** ppSink);
  251. } m_XProxy;
  252. IWbemContext* GetProperContext(IWbemContext* pCurrentContext);
  253. public:
  254. CFilterProxyManager(CLifeControl* pControl = NULL);
  255. ~CFilterProxyManager();
  256. HRESULT SetStub(IWbemFilterStub* pStub);
  257. HRESULT AddFilter(IWbemContext* pContext, LPCWSTR wszQuery,
  258. QL_LEVEL_1_RPN_EXPRESSION* pExp,
  259. WBEM_REMOTE_TARGET_ID_TYPE Id);
  260. INTERNAL IWbemEventSink* GetMainProxy();
  261. HRESULT RemoveProxy(CFilterProxy* pProxy);
  262. HRESULT GetMetaData(RELEASE_ME CWrappingMetaData** ppMeta);
  263. HRESULT DeliverEvent(long lNumToSend, IWbemClassObject** apEvents,
  264. WBEM_REM_TARGETS* aTargets,
  265. long lSDLength, BYTE* pSD);
  266. HRESULT DeliverEventMT(long lNumToSend, IWbemClassObject** apEvents,
  267. WBEM_REM_TARGETS* aTargets,
  268. long lSDLength, BYTE* pSD,
  269. IWbemMultiTarget *pMultiTarget);
  270. ULONG STDMETHODCALLTYPE AddRefProxy();
  271. ULONG STDMETHODCALLTYPE ReleaseProxy();
  272. HRESULT SetStatus(long lFlags, HRESULT hResult,
  273. BSTR strResult, IWbemClassObject* pErrorObj);
  274. // Batching
  275. void AddEvent(IWbemClassObject *pObj, CSortedArray *pTrues);
  276. HRESULT SetProxyLatency(CFilterProxy *pProxy, DWORD dwLatency);
  277. void RemoveProxyLatency(CFilterProxy *pProxy);
  278. DWORD GetLastSentStamp();
  279. void WaitForEmptyBatch();
  280. // IUnknown
  281. ULONG STDMETHODCALLTYPE AddRef();
  282. ULONG STDMETHODCALLTYPE Release();
  283. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv);
  284. // IWbemFilterProxy
  285. HRESULT STDMETHODCALLTYPE Initialize(IWbemMetaData* pMetaData,
  286. IWbemMultiTarget* pMultiTarget);
  287. HRESULT STDMETHODCALLTYPE Lock();
  288. HRESULT STDMETHODCALLTYPE Unlock();
  289. HRESULT STDMETHODCALLTYPE AddFilter(IWbemContext* pContext,
  290. LPCWSTR wszQuery,
  291. WBEM_REMOTE_TARGET_ID_TYPE Id);
  292. HRESULT STDMETHODCALLTYPE RemoveFilter(IWbemContext* pContext,
  293. WBEM_REMOTE_TARGET_ID_TYPE Id);
  294. HRESULT STDMETHODCALLTYPE RemoveAllFilters(IWbemContext* pContext);
  295. HRESULT STDMETHODCALLTYPE AddDefinitionQuery(IWbemContext* pContext,
  296. LPCWSTR wszQuery);
  297. HRESULT STDMETHODCALLTYPE RemoveAllDefinitionQueries(
  298. IWbemContext* pContext);
  299. HRESULT STDMETHODCALLTYPE AllowUtilizeGuarantee();
  300. HRESULT STDMETHODCALLTYPE Disconnect();
  301. HRESULT STDMETHODCALLTYPE GetRestrictedSink(
  302. long lNumQueries,
  303. const LPCWSTR* awszQueries,
  304. IUnknown* pCallback,
  305. IWbemEventSink** ppSink);
  306. // IMarshal
  307. STDMETHOD(GetUnmarshalClass)(REFIID riid, void* pv, DWORD dwDestContext,
  308. void* pvReserved, DWORD mshlFlags, CLSID* pClsid);
  309. STDMETHOD(GetMarshalSizeMax)(REFIID riid, void* pv, DWORD dwDestContext,
  310. void* pvReserved, DWORD mshlFlags, ULONG* plSize);
  311. STDMETHOD(MarshalInterface)(IStream* pStream, REFIID riid, void* pv,
  312. DWORD dwDestContext, void* pvReserved, DWORD mshlFlags);
  313. STDMETHOD(UnmarshalInterface)(IStream* pStream, REFIID riid, void** ppv);
  314. STDMETHOD(ReleaseMarshalData)(IStream* pStream);
  315. STDMETHOD(DisconnectObject)(DWORD dwReserved);
  316. };
  317. #endif