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.

293 lines
8.3 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997 - 2000.
  5. //
  6. // File: QueryEngine.hxx
  7. //
  8. // Contents: Declaration of data structures and classes used to perform
  9. // queries and store the results.
  10. //
  11. // Classes: SQueryParams
  12. // CQueryEngine
  13. //
  14. // Notes: The term "query" is used loosely here to mean either an
  15. // LDAP directory search or a WinNt enumeration.
  16. //
  17. // History: 04-13-2000 DavidMun Created
  18. //
  19. //---------------------------------------------------------------------------
  20. #ifndef __QUERY_ENGINE_HXX_
  21. #define __QUERY_ENGINE_HXX_
  22. enum QUERY_LIMIT
  23. {
  24. QL_NO_LIMIT,
  25. QL_USE_REGISTRY_LIMIT
  26. };
  27. enum WORKER_THREAD_STATE
  28. {
  29. WTS_EXIT, // terminating
  30. WTS_WAIT, // waiting on thread event
  31. WTS_QUERY // performing query
  32. };
  33. const ULONG DEFAULT_PAGE_SIZE = 100;
  34. //+--------------------------------------------------------------------------
  35. //
  36. // Struct: SQueryParams (qp)
  37. //
  38. // Purpose: Contain all the parameters for a query
  39. //
  40. // History: 04-13-2000 DavidMun Created
  41. //
  42. //---------------------------------------------------------------------------
  43. struct SQueryParams
  44. {
  45. SQueryParams():
  46. hwndCredPromptParentDlg(NULL),
  47. hwndNotify(NULL),
  48. ADsScope(ADS_SCOPE_BASE),
  49. Limit(QL_USE_REGISTRY_LIMIT),
  50. hQueryCompleteEvent(INVALID_HANDLE_VALUE),
  51. CustomizerInteraction(CUSTINT_IGNORE_CUSTOM_OBJECTS),
  52. ulBindFlags(0),
  53. performExternalCustomization(FALSE),
  54. perfromDefaultCustomization(FALSE)
  55. {
  56. }
  57. SQueryParams(
  58. const SQueryParams &rhs)
  59. {
  60. operator=(rhs);
  61. }
  62. SQueryParams &
  63. operator =(
  64. const SQueryParams &rhs)
  65. {
  66. hwndCredPromptParentDlg = rhs.hwndCredPromptParentDlg;
  67. hwndNotify = rhs.hwndNotify;
  68. rpScope = rhs.rpScope;
  69. strADsPath = rhs.strADsPath;
  70. ulBindFlags = rhs.ulBindFlags;
  71. strLdapFilter = rhs.strLdapFilter;
  72. vstrWinNtFilter = rhs.vstrWinNtFilter;
  73. vakAttributesToRead = rhs.vakAttributesToRead;
  74. ADsScope = rhs.ADsScope;
  75. Limit = rhs.Limit;
  76. hQueryCompleteEvent = rhs.hQueryCompleteEvent;
  77. CustomizerInteraction = rhs.CustomizerInteraction;
  78. strCustomizerArg = rhs.strCustomizerArg;
  79. performExternalCustomization = rhs.performExternalCustomization;
  80. perfromDefaultCustomization = rhs.perfromDefaultCustomization;
  81. return *this;
  82. }
  83. ~SQueryParams()
  84. {
  85. hwndCredPromptParentDlg = NULL;
  86. hwndNotify = NULL;
  87. ADsScope = ADS_SCOPE_BASE;
  88. Limit = QL_USE_REGISTRY_LIMIT;
  89. hQueryCompleteEvent = INVALID_HANDLE_VALUE;
  90. CustomizerInteraction = CUSTINT_IGNORE_CUSTOM_OBJECTS;
  91. ulBindFlags = 0;
  92. }
  93. HWND hwndCredPromptParentDlg;
  94. HWND hwndNotify;
  95. RpScope rpScope;
  96. String strADsPath;
  97. ULONG ulBindFlags;
  98. String strLdapFilter;
  99. vector<String> vstrWinNtFilter;
  100. AttrKeyVector vakAttributesToRead;
  101. ADS_SCOPEENUM ADsScope;
  102. QUERY_LIMIT Limit;
  103. HANDLE hQueryCompleteEvent;
  104. CUSTOMIZER_INTERACTION CustomizerInteraction;
  105. String strCustomizerArg;
  106. BOOL performExternalCustomization;
  107. BOOL perfromDefaultCustomization;
  108. };
  109. //+--------------------------------------------------------------------------
  110. //
  111. // Class: CQueryEngine (qe)
  112. //
  113. // Purpose: Perform synchronous or asynchronous queries and hold the
  114. // results.
  115. //
  116. // History: 04-13-2000 DavidMun Created
  117. //
  118. //---------------------------------------------------------------------------
  119. class CQueryEngine
  120. {
  121. public:
  122. CQueryEngine(
  123. const CObjectPicker &rop);
  124. ~CQueryEngine();
  125. HRESULT
  126. Initialize();
  127. void
  128. Clear() const
  129. {
  130. TRACE_METHOD(CQueryEngine, Clear);
  131. CAutoCritSec AutoLock(&m_cs);
  132. m_vObjects.clear();
  133. m_hrLastQueryResult = S_OK;
  134. }
  135. HRESULT
  136. AsyncDirSearch(
  137. const SQueryParams &qp,
  138. ULONG *pusnThisWorkItem = NULL) const;
  139. HRESULT
  140. SyncDirSearch(
  141. const SQueryParams &qp) const;
  142. void
  143. StopWorkItem() const;
  144. Variant
  145. GetObjectAttr(
  146. size_t idxRow,
  147. ATTR_KEY at) const;
  148. CDsObject
  149. GetObject(
  150. size_t idxRow) const
  151. {
  152. CAutoCritSec Lock(&m_cs);
  153. ASSERT(idxRow < m_vObjects.size());
  154. return m_vObjects[idxRow];
  155. }
  156. size_t
  157. GetItemCount() const
  158. {
  159. CAutoCritSec Lock(&m_cs);
  160. return m_vObjects.size();
  161. }
  162. HRESULT
  163. GetLastQueryResult() const
  164. {
  165. return m_hrLastQueryResult;
  166. }
  167. private:
  168. CQueryEngine &
  169. operator =(const CQueryEngine &);
  170. void
  171. _AddObject(
  172. const CDsObject dso)
  173. {
  174. CAutoCritSec Lock(&m_cs);
  175. m_vObjects.push_back(dso);
  176. }
  177. static DWORD WINAPI
  178. _tThread_Proc(
  179. LPVOID pvThis);
  180. // auxiliar in _tPerformLdapQuery
  181. HRESULT
  182. _tAddApprovedObjects
  183. (
  184. CDsObjectList &dsolToAdd
  185. );
  186. void
  187. _tPerformLdapQuery();
  188. void
  189. _tPerformWinNtEnum();
  190. void
  191. _tAddCustomObjects();
  192. void
  193. _tAddFromDataObject(
  194. IDataObject *pdo);
  195. //
  196. // Member variables for threading:
  197. //
  198. // m_DesiredThreadState - written to by main thread, read by worker
  199. // thread.
  200. //
  201. // m_CurrentThreadState - read/written by worker thread. Always
  202. // reflects the state the thread is in or is transitioning to.
  203. //
  204. // m_hThreadEvent - worker thread blocks on this handle with
  205. // m_CurrentThreadState set to WTS_WAIT.
  206. //
  207. // m_cs - protects m_vObjects so that main thread reads and worker
  208. // thread writes do not collide.
  209. //
  210. // m_usnCurWorkItem - an update sequence counter (just a monotonically
  211. // increasing counter) which corresponds to the work item
  212. // the thread is currently processing. Also represents the
  213. // work item from which objects in m_vObjects were generated.
  214. //
  215. // m_usnNextWorkItem - incremented whenever the main thread submits
  216. // a new work item.
  217. //
  218. // How these are used:
  219. //
  220. // The main thread assigns a value to m_DesiredThreadState, increments
  221. // m_usnNextWorkItem, then sets m_hThreadEvent. The worker thread
  222. // unblocks on m_hThreadEvent, then sets m_CurrentThreadState to
  223. // m_DesiredThreadState, m_usnCurWorkItem to m_usnNextWorkItem, and
  224. // performs the required work.
  225. //
  226. // If m_DesiredThreadState is WTS_EXIT the worker thread terminates,
  227. // otherwise it performs the requested work. When the work is
  228. // complete, worker thread changes m_CurrentThreadState to WTS_WAIT
  229. // and resumes waiting on m_hThreadEvent.
  230. //
  231. // While processing work for m_usnCurWorkItem the worker thread
  232. // periodically checks to see if m_usnNextWorkItem is greater than
  233. // m_usnCurWorkItem. If it is, it abandons its current work,
  234. // switches to WTS_WAIT state, and resumes waiting on m_hThreadEvent.
  235. //
  236. const CObjectPicker &m_rop;
  237. HANDLE m_hThread;
  238. HANDLE m_hThreadEvent;
  239. mutable WORKER_THREAD_STATE m_CurrentThreadState;
  240. mutable WORKER_THREAD_STATE m_DesiredThreadState;
  241. mutable SQueryParams m_CurQueryParams;
  242. mutable SQueryParams m_NextQueryParams;
  243. mutable ULONG m_usnCurWorkItem;
  244. mutable ULONG m_usnNextWorkItem;
  245. mutable CRITICAL_SECTION m_cs;
  246. mutable vector<CDsObject> m_vObjects;
  247. mutable HRESULT m_hrLastQueryResult;
  248. };
  249. #endif // __QUERY_ENGINE_HXX_