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.

254 lines
7.0 KiB

  1. /*
  2. * a c c t c a c h . c p p
  3. *
  4. * Author: Greg Friedman
  5. *
  6. * Purpose: Runtime store for cached account properties.
  7. *
  8. * Copyright (C) Microsoft Corp. 1998.
  9. */
  10. #include "pch.hxx"
  11. #include "acctcach.h"
  12. #include "tmap.h"
  13. #include "simpstr.h"
  14. // explicit template instantiations
  15. template class TMap<CACHEDACCOUNTPROP, CSimpleString>;
  16. template class TPair<CACHEDACCOUNTPROP, CSimpleString>;
  17. typedef TMap<CACHEDACCOUNTPROP, CSimpleString> CAccountPropMap;
  18. typedef TPair<CACHEDACCOUNTPROP, CSimpleString> CAccountPropPair;
  19. template class TMap<CSimpleString, CAccountPropMap*>;
  20. template class TPair<CSimpleString, CAccountPropMap*>;
  21. typedef TMap<CSimpleString, CAccountPropMap*> CAccountCacheMap;
  22. typedef TPair<CSimpleString, CAccountPropMap*> CAccountCachePair;
  23. static CAccountCacheMap *g_pAccountCache;
  24. // REVIEW!!! We are leaking the prop arrays right now!!!
  25. // the map template needs to be able to take a pair free func
  26. //----------------------------------------------------------------------
  27. // Internal Functions
  28. //----------------------------------------------------------------------
  29. //----------------------------------------------------------------------
  30. // _FreeAccountCachePair
  31. //----------------------------------------------------------------------
  32. static void __cdecl _FreeAccountCachePair(CAccountCachePair *pPair)
  33. {
  34. if (NULL != pPair)
  35. {
  36. delete pPair->m_value;
  37. delete pPair;
  38. }
  39. }
  40. //----------------------------------------------------------------------
  41. // _HrInitAccountPropCache
  42. //----------------------------------------------------------------------
  43. static HRESULT _HrInitAccountPropCache(void)
  44. {
  45. HRESULT hr = S_OK;
  46. Assert(NULL == g_pAccountCache);
  47. if (NULL != g_pAccountCache)
  48. return E_FAIL;
  49. g_pAccountCache = new CAccountCacheMap();
  50. if (NULL == g_pAccountCache)
  51. {
  52. hr = E_OUTOFMEMORY;
  53. goto exit;
  54. }
  55. g_pAccountCache->SetPairFreeFunction(_FreeAccountCachePair);
  56. exit:
  57. return S_OK;
  58. }
  59. //----------------------------------------------------------------------
  60. // _HrFindAccountPropertyMap
  61. //----------------------------------------------------------------------
  62. static HRESULT _HrFindAccountPropertyMap(LPSTR pszAccountId,
  63. CAccountPropMap **ppm,
  64. BOOL fCreate)
  65. {
  66. HRESULT hr = S_OK;
  67. CSimpleString ss;
  68. CAccountCachePair *pPair = NULL;
  69. CAccountPropMap *pMap = NULL;
  70. Assert(NULL != ppm);
  71. *ppm = NULL;
  72. if (NULL == g_pAccountCache)
  73. {
  74. if (fCreate)
  75. hr = _HrInitAccountPropCache();
  76. if (NULL == g_pAccountCache)
  77. goto exit;
  78. }
  79. if (FAILED(hr = ss.SetString(pszAccountId)))
  80. goto exit;
  81. pPair = g_pAccountCache->Find(ss);
  82. if (NULL != pPair)
  83. *ppm = pPair->m_value;
  84. else if (fCreate)
  85. {
  86. pMap = new CAccountPropMap();
  87. if (NULL == pMap)
  88. {
  89. hr = E_OUTOFMEMORY;
  90. goto exit;
  91. }
  92. if (FAILED(hr = g_pAccountCache->Add(ss, pMap)))
  93. {
  94. delete pMap;
  95. goto exit;
  96. }
  97. *ppm = pMap;
  98. }
  99. exit:
  100. return hr;
  101. }
  102. //----------------------------------------------------------------------
  103. // FreeAccountPropCache
  104. //----------------------------------------------------------------------
  105. void FreeAccountPropCache(void)
  106. {
  107. EnterCriticalSection(&g_csAccountPropCache);
  108. if (NULL != g_pAccountCache)
  109. {
  110. delete g_pAccountCache;
  111. g_pAccountCache = NULL;
  112. }
  113. LeaveCriticalSection(&g_csAccountPropCache);
  114. }
  115. //----------------------------------------------------------------------
  116. // HrCacheAccountPropStrA
  117. //----------------------------------------------------------------------
  118. HRESULT HrCacheAccountPropStrA(LPSTR pszAccountId,
  119. CACHEDACCOUNTPROP cap,
  120. LPCSTR pszProp)
  121. {
  122. HRESULT hr = S_OK;
  123. CAccountPropMap *pMap = NULL;
  124. CAccountPropPair *pPair = NULL;
  125. CSimpleString ssProp;
  126. if (NULL == pszAccountId || NULL == pszProp)
  127. return E_INVALIDARG;
  128. EnterCriticalSection(&g_csAccountPropCache);
  129. // find the account property map. create one if it doesn't exist
  130. if (FAILED(hr = _HrFindAccountPropertyMap(pszAccountId, &pMap, TRUE)))
  131. goto exit;
  132. if (FAILED(hr = ssProp.SetString(pszProp)))
  133. goto exit;
  134. // look for the property in the map
  135. pPair = pMap->Find(cap);
  136. if (NULL == pPair)
  137. hr = pMap->Add(cap, ssProp);
  138. else
  139. pPair->m_value = ssProp;
  140. exit:
  141. LeaveCriticalSection(&g_csAccountPropCache);
  142. return hr;
  143. }
  144. //----------------------------------------------------------------------
  145. // CacheAccountPropStrA
  146. //----------------------------------------------------------------------
  147. BOOL GetAccountPropStrA(LPSTR pszAccountId,
  148. CACHEDACCOUNTPROP cap,
  149. LPSTR *ppszProp)
  150. {
  151. HRESULT hr = S_OK;
  152. CAccountPropMap *pMap = NULL;
  153. CAccountPropPair *pPair = NULL;
  154. BOOL fResult = FALSE;
  155. Assert(NULL != pszAccountId && NULL != ppszProp);
  156. if (NULL == g_pAccountCache)
  157. return FALSE;
  158. if (NULL == pszAccountId || NULL == ppszProp)
  159. return FALSE;
  160. *ppszProp = NULL;
  161. EnterCriticalSection(&g_csAccountPropCache);
  162. // find the account property map. don't create one if it doesn't exist
  163. if (FAILED(hr = _HrFindAccountPropertyMap(pszAccountId, &pMap, FALSE)))
  164. goto exit;
  165. if (NULL == pMap)
  166. goto exit;
  167. pPair = pMap->Find(cap);
  168. if (NULL != pPair)
  169. {
  170. Assert(!pPair->m_value.IsNull());
  171. if (!pPair->m_value.IsNull())
  172. {
  173. *ppszProp = PszDupA(pPair->m_value.GetString());
  174. if (NULL != *ppszProp)
  175. fResult = TRUE;
  176. }
  177. }
  178. exit:
  179. LeaveCriticalSection(&g_csAccountPropCache);
  180. return fResult;
  181. }
  182. //----------------------------------------------------------------------
  183. // AccountCache_AccountChanged
  184. //----------------------------------------------------------------------
  185. void AccountCache_AccountChanged(LPSTR pszAccountId)
  186. {
  187. // delete the data associated with the account that was changed
  188. EnterCriticalSection(&g_csAccountPropCache);
  189. if (NULL != g_pAccountCache)
  190. {
  191. CSimpleString ss;
  192. if (SUCCEEDED(ss.SetString(pszAccountId)))
  193. g_pAccountCache->Remove(ss);
  194. }
  195. LeaveCriticalSection(&g_csAccountPropCache);
  196. }
  197. //----------------------------------------------------------------------
  198. // AccountCache_AccountDeleted
  199. //----------------------------------------------------------------------
  200. void AccountCache_AccountDeleted(LPSTR pszAccountId)
  201. {
  202. AccountCache_AccountChanged(pszAccountId);
  203. }