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.

215 lines
7.6 KiB

  1. /////////////////////////////////////////////////////////////////////////////////////
  2. // TuningSpaceContainer.h : Declaration of the CSystemTuningSpaces
  3. // Copyright (c) Microsoft Corporation 1999.
  4. #ifndef __TUNINGSPACECONTAINER_H_
  5. #define __TUNINGSPACECONTAINER_H_
  6. #pragma once
  7. #include <regexthread.h>
  8. #include <objectwithsiteimplsec.h>
  9. #include "tuningspacecollectionimpl.h"
  10. namespace BDATuningModel {
  11. const int DEFAULT_MAX_COUNT = 32; // by default only allow 32 tuning spaces in order to prevent
  12. // dnos attacks from filling up disk/registry with bogus
  13. // tuning space entries.
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CSystemTuningSpaces
  16. class CSystemTuningSpaces :
  17. public CComObjectRootEx<CComMultiThreadModel>,
  18. public CComCoClass<CSystemTuningSpaces, &CLSID_SystemTuningSpaces>,
  19. public ISupportErrorInfo,
  20. public IObjectWithSiteImplSec<CSystemTuningSpaces>,
  21. public IObjectSafetyImpl<CSystemTuningSpaces, INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA>,
  22. public TuningSpaceCollectionImpl<CSystemTuningSpaces, ITuningSpaceContainer, &__uuidof(ITuningSpaceContainer), &LIBID_TunerLib> {
  23. public:
  24. CSystemTuningSpaces() :
  25. m_CurrentAccess(KEY_READ),
  26. m_MaxCount(DEFAULT_MAX_COUNT),
  27. m_cookieRegExp(0),
  28. m_pRET(NULL) {
  29. }
  30. virtual ~CSystemTuningSpaces() {
  31. ATL_LOCK();
  32. if (m_pRET) {
  33. HRESULT hr = m_pRET->CallWorker(CRegExThread::RETHREAD_EXIT);
  34. ASSERT(SUCCEEDED(hr));
  35. delete m_pRET;
  36. m_pRET = NULL;
  37. }
  38. m_mapTuningSpaces.clear();
  39. m_mapTuningSpaceNames.clear();
  40. }
  41. HRESULT FinalConstruct();
  42. void FinalRelease();
  43. REGISTER_AUTOMATION_OBJECT_WITH_TM(IDS_REG_TUNEROBJ,
  44. IDS_REG_TUNINGSPACECONTAINER_PROGID,
  45. IDS_REG_TUNINGSPACECONTAINER_DESC,
  46. LIBID_TunerLib,
  47. CLSID_SystemTuningSpaces, tvBoth);
  48. DECLARE_NOT_AGGREGATABLE(CSystemTuningSpaces)
  49. DECLARE_PROTECT_FINAL_CONSTRUCT()
  50. BEGIN_COM_MAP(CSystemTuningSpaces)
  51. COM_INTERFACE_ENTRY(ITuningSpaceContainer)
  52. COM_INTERFACE_ENTRY(IDispatch)
  53. COM_INTERFACE_ENTRY(IObjectWithSite)
  54. COM_INTERFACE_ENTRY(IObjectSafety)
  55. COM_INTERFACE_ENTRY(ISupportErrorInfo)
  56. END_COM_MAP_WITH_FTM()
  57. HRESULT RegisterTuningSpaces(HINSTANCE hInst);
  58. HRESULT UnregisterTuningSpaces();
  59. PUnknown m_pSite;
  60. public:
  61. // ISupportsErrorInfo
  62. STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
  63. // ITuningSpaceContainer
  64. STDMETHOD(get_Item)(/*[in]*/ VARIANT varIndex, /*[out, retval]*/ ITuningSpace **ppTuningSpace);
  65. STDMETHOD(put_Item)(/*[in]*/VARIANT varIndex, /*[in]*/ITuningSpace *pTuningSpace);
  66. STDMETHOD(Add)(/*[in]*/ ITuningSpace *pTuningSpace, /*[out]*/ VARIANT* pvarIndex);
  67. STDMETHOD(Remove)(/*[in]*/ VARIANT varIndex);
  68. STDMETHOD(TuningSpacesForCLSID)(/*[in]*/ BSTR bstrSpace, /*[out, retval]*/ ITuningSpaces **ppTuningSpaces);
  69. STDMETHOD(_TuningSpacesForCLSID)(/*[in]*/ REFCLSID clsidSpace, /*[out, retval]*/ ITuningSpaces **ppTuningSpaces);
  70. STDMETHOD(TuningSpacesForName)(/*[in]*/ BSTR bstrName, /*[out, retval]*/ ITuningSpaces **ppTuningSpaces);
  71. STDMETHOD(FindID)(/*[in]*/ ITuningSpace* pTS, /*[out, retval]*/ long *pID);
  72. STDMETHOD(get_MaxCount)(/*[out, retval]*/ LONG *plCount);
  73. STDMETHOD(put_MaxCount)(LONG lCount);
  74. protected:
  75. typedef std::map<CComBSTR, ULONG> TuningSpaceNames_t; // unique name->id mapping
  76. CComPtr<ICreatePropBagOnRegKey> m_pFactory;
  77. HANDLE m_hMutex;
  78. // REV2: use registry change notification to refresh cache
  79. ULONG m_MaxCount; // prevent dnos attack from filling registry with bogus tuning spaces
  80. PQPropertyBag2 m_pTSBag;
  81. CRegKey m_RootKey;
  82. REGSAM m_CurrentAccess;
  83. TuningSpaceNames_t m_mapTuningSpaceNames;
  84. HRESULT OpenRootKeyAndBag(REGSAM NewAccess);
  85. HRESULT ChangeAccess(REGSAM DesiredAccess);
  86. CComBSTR GetUniqueName(ITuningSpace* pTS);
  87. ULONG GetID(CComBSTR& UniqueName);
  88. HRESULT DeleteID(ULONG id);
  89. HRESULT Add(CComBSTR& un, long PreferredID, PQTuningSpace pTS, VARIANT *pvarAssignedID);
  90. // takes a variant index and returns an iterator to the cache and possibly an interator
  91. // to the name cache depending on index type
  92. // on return if its != end() and itn == end() and name is wanted then look up with overloaded Find
  93. HRESULT Find(VARIANT varIndex, long& ID, TuningSpaceContainer_t::iterator &its, CComBSTR& UniqueName, TuningSpaceNames_t::iterator &itn);
  94. // takes a cache iterator and returns a name cache iterator
  95. HRESULT Find(TuningSpaceContainer_t::iterator &its, CComBSTR& UniqueName, TuningSpaceNames_t::iterator &itn);
  96. PQGIT m_pGIT;
  97. DWORD m_cookieRegExp;
  98. CRegExThread *m_pRET; // shared worker thread
  99. };
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CSystemTuningSpaces
  102. class ATL_NO_VTABLE DECLSPEC_UUID("969EE7DA-7058-4922-BA78-DA3905D0325F") CTuningSpacesBase :
  103. public CComObjectRootEx<CComMultiThreadModel>,
  104. public CComCoClass<CTuningSpacesBase, &__uuidof(CTuningSpacesBase)>,
  105. public ISupportErrorInfoImpl<&__uuidof(ITuningSpaces)>,
  106. public IObjectWithSiteImplSec<CTuningSpacesBase>,
  107. public TuningSpaceCollectionImpl<CTuningSpacesBase, ITuningSpaces, &IID_ITuningSpaces, &LIBID_TunerLib>,
  108. public IObjectSafetyImpl<CTuningSpacesBase, INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA>
  109. {
  110. public:
  111. CTuningSpacesBase() {}
  112. DECLARE_NOT_AGGREGATABLE(CTuningSpacesBase)
  113. DECLARE_PROTECT_FINAL_CONSTRUCT()
  114. BEGIN_COM_MAP(CTuningSpacesBase)
  115. COM_INTERFACE_ENTRY(ITuningSpaces)
  116. COM_INTERFACE_ENTRY(IDispatch)
  117. COM_INTERFACE_ENTRY(ISupportErrorInfo)
  118. COM_INTERFACE_ENTRY(IObjectSafety)
  119. COM_INTERFACE_ENTRY(IObjectWithSite)
  120. END_COM_MAP_WITH_FTM()
  121. };
  122. /////////////////////////////////////////////////////////////////////////////
  123. class CTuningSpaces : public CComObject<CTuningSpacesBase> {
  124. public:
  125. CTuningSpaces() {}
  126. // we'd like to say:
  127. // CTuningSpaces(TuningSpaceContainer_t& init) : m_mapTuningSpaces(init.begin(), init.end()) {}
  128. // but a compiler bug is causing it to match this against the explicit map ctor that just takes
  129. // a pred and an allocator. so, we'll do it the hard way.
  130. CTuningSpaces(TuningSpaceContainer_t& init) {
  131. for (TuningSpaceContainer_t::iterator i = init.begin(); i != init.end(); ++i) {
  132. CComVariant v((*i).second);
  133. if ((*i).second.vt != VT_UNKNOWN && (*i).second.vt != VT_DISPATCH) {
  134. THROWCOM(E_UNEXPECTED); //corrupt in-memory collection
  135. }
  136. PQTuningSpace pTS((*i).second.punkVal);
  137. PQTuningSpace newts;
  138. HRESULT hr = PQTuningSpace(pTS)->Clone(&newts);
  139. if (FAILED(hr)) {
  140. THROWCOM(hr);
  141. }
  142. m_mapTuningSpaces[(*i).first] = CComVariant(newts);
  143. }
  144. }
  145. STDMETHOD(get_Item)(/*[in]*/ VARIANT varIndex, /*[out, retval]*/ ITuningSpace **ppTuningSpace) {
  146. try {
  147. if (!ppTuningSpace) {
  148. return E_POINTER;
  149. }
  150. if (varIndex.vt != VT_UI4) {
  151. HRESULT hr = ::VariantChangeType(&varIndex, &varIndex, 0, VT_UI4);
  152. if (FAILED(hr))
  153. {
  154. return Error(IDS_E_TYPEMISMATCH, __uuidof(ITuningSpaces), hr);
  155. }
  156. }
  157. ATL_LOCK();
  158. TuningSpaceContainer_t::iterator its = m_mapTuningSpaces.find(varIndex.ulVal);
  159. if (its == m_mapTuningSpaces.end()) {
  160. return Error(IDS_E_NO_TS_MATCH, __uuidof(ITuningSpaces), E_INVALIDARG);
  161. }
  162. _ASSERT(((*its).second.vt == VT_UNKNOWN) || ((*its).second.vt == VT_DISPATCH));
  163. PQTuningSpace pTS((*its).second.punkVal);
  164. if (!pTS) {
  165. return Error(IDS_E_NOINTERFACE, __uuidof(ITuningSpaces), E_NOINTERFACE);
  166. }
  167. return pTS.CopyTo(ppTuningSpace);
  168. } catch(...) {
  169. return E_UNEXPECTED;
  170. }
  171. }
  172. };
  173. HRESULT RegisterTuningSpaces(HINSTANCE hInst);
  174. HRESULT UnregisterTuningSpaces();
  175. };
  176. #endif //__TUNINGSPACECONTAINER_H_