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.

168 lines
5.4 KiB

  1. /***************************************************************************\
  2. *
  3. * File: TicketManager.h
  4. *
  5. * Description:
  6. *
  7. * This file contains the definition of relevant classes, structs, and types
  8. * for the DUser Ticket Manager.
  9. *
  10. * The following classes are defined for public use:
  11. *
  12. * TicketManager
  13. * A facility which can assign a unique "ticket" to a BaseObject.
  14. *
  15. * History:
  16. * 9/20/2000: DwayneN: Created
  17. *
  18. * Copyright (C) 2000 by Microsoft Corporation. All rights reserved.
  19. *
  20. \***************************************************************************/
  21. #if !defined(SERVICES__TicketManager_h__INCLUDED)
  22. #define SERVICES__TicketManager_h__INCLUDED
  23. #pragma once
  24. /***************************************************************************\
  25. *
  26. * DuTicket
  27. *
  28. * Tickets are created to give an external identity to a gadget. However,
  29. * this identity is not necessarily permanent, and may have a limited
  30. * lifetime. External apps should not hold on to these temporary tickets
  31. * for long periods of time because they will eventually expire.
  32. *
  33. * One primary consumer of these tickets is the ActiveAccessibility APIs.
  34. * Because of this, we must work within some constraints:
  35. * - Tickets must be 32-bits in size.
  36. * - Tickets can't be negative, so the upper bit must be clear.
  37. * - Tickets can't be zero.
  38. *
  39. * A description of the fields in a ticket follow:
  40. *
  41. * Unused:
  42. * As explained above, the upper bit can not be used, and must be 0.
  43. *
  44. * Type:
  45. * We encode the actual type of the BaseObject so that we can further
  46. * validate uses of the ticket.
  47. *
  48. * Uniqueness:
  49. * We encode a uniqueness value that is essentially an ever-increasing number
  50. * to provide some temporal distance between subsequent uses of the same
  51. * index. The uniqueness can never be 0 - to satisfy the requirement that
  52. * the ticket itself can never be 0.
  53. *
  54. * Index
  55. * The actual BaseObject is stored in a table in the TicketManager. This
  56. * index is stored here.
  57. *
  58. \***************************************************************************/
  59. struct DuTicket
  60. {
  61. DWORD Index : 16;
  62. DWORD Uniqueness : 8;
  63. DWORD Type : 7;
  64. DWORD Unused : 1;
  65. inline static DuTicket & CastFromDWORD(DWORD & dw);
  66. inline static DWORD & CastToDWORD(DuTicket & ticket);
  67. };
  68. /***************************************************************************\
  69. *
  70. * DuTicketData
  71. *
  72. * The DuTicketData structure is used to store the data inside the ticket
  73. * manager. A brief description of the fields follows:
  74. *
  75. * pObject
  76. * A pointer to the actual BaseObject associated with a given ticket.
  77. *
  78. * dwExpirationTick
  79. * How many ticks until the ticket is invalidated.
  80. *
  81. * idxFree
  82. * This is actually a logically separate array that contains a "free stack"
  83. * to make inserting into the ticket manager quick.
  84. *
  85. * cUniqueness
  86. * The uniqueness value for this entry in the ticket manager. Tickets must
  87. * have a matching uniqueness in order for them to actually access the
  88. * object.
  89. *
  90. \***************************************************************************/
  91. struct DuTicketData
  92. {
  93. BaseObject * pObject;
  94. WORD idxFree;
  95. BYTE cUniqueness;
  96. };
  97. //
  98. // Note: This class is only defined this way because its the only way I
  99. // could get the debugger extensions to recognize the symbol name.
  100. //
  101. class DuTicketDataArray : public GArrayF<DuTicketData, ProcessHeap>
  102. {
  103. public:
  104. };
  105. /***************************************************************************\
  106. *
  107. * DuTicketManager
  108. *
  109. * The DuTicketManager class provides a mechanism by which a relatively
  110. * permanent "ticket" can be assigned to a given BaseObject. This "ticket"
  111. * can be used later to safely access the BaseObject. If the BaseObject has
  112. * been destroyed, an error will be returned but the system will not fault.
  113. *
  114. * This is especially important when you must pass the identity of a DUser
  115. * object to an outside program. It would be unsafe to pass the raw pointer
  116. * since doing so may require an unsafe dereference when the outside program
  117. * attempts to extract information about the object.
  118. *
  119. * This is a one-way mapping only. The TicketManager class can correctly
  120. * return the BaseObject assigned to a given ticket. However, to find the
  121. * ticket associated with a BaseObject is an expensive operation and is
  122. * best stored on the BaseObject itself.
  123. *
  124. \***************************************************************************/
  125. class DuTicketManager
  126. {
  127. // Construction
  128. public:
  129. DuTicketManager();
  130. ~DuTicketManager();
  131. SUPPRESS(DuTicketManager);
  132. // Operations
  133. public:
  134. HRESULT Add(IN BaseObject * pObject, OUT DWORD * pdwTicket);
  135. HRESULT Remove(IN DWORD dwTicket, OUT OPTIONAL BaseObject ** ppObject);
  136. HRESULT Lookup(IN DWORD dwTicket, OUT OPTIONAL BaseObject ** ppObject);
  137. // Implementation
  138. protected:
  139. HRESULT Expand();
  140. HRESULT PushFree(int idxFree);
  141. HRESULT PopFree(int & idxFree);
  142. HRESULT Find(BaseObject * pObject, int & iFound);
  143. // Data
  144. private:
  145. DuTicketDataArray
  146. m_arTicketData;
  147. int m_idxFreeStackTop;
  148. int m_idxFreeStackBottom;
  149. CritLock m_crit;
  150. };
  151. #include "TicketManager.inl"
  152. #endif // SERVICES__TicketManager_h__INCLUDED