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.

184 lines
5.9 KiB

  1. // CMSEventBinder.h : Declaration of the CMSEventBinder
  2. #ifndef __MSEVENTBINDER_H_
  3. #define __MSEVENTBINDER_H_
  4. #include <map>
  5. #include <Mshtml.h>
  6. #include <activscp.h>
  7. #include <Atlctl.h>
  8. #include <Exdisp.h>
  9. #include <objectwithsiteimplsec.h>
  10. #include "segimpl.h"
  11. #include "seg.h"
  12. #include "resource.h" // main symbols
  13. #include "mslcid.h"
  14. typedef CComQIPtr<IActiveScriptSite> PQIASSite;
  15. typedef CComQIPtr<IActiveScript> PQIAScript;
  16. /////////////////////////////////////////////////////
  17. class __declspec(uuid("FCBF24F7-FB97-4fa3-B57E-97BCB5AF1D26")) ATL_NO_VTABLE CMSEventHandlerBase:
  18. public CComObjectRootEx<CComSingleThreadModel>,
  19. public IObjectWithSiteImplSec<CMSEventHandlerBase>,
  20. public IDispatchImpl<IDispatch, &IID_IDispatch>
  21. {
  22. BEGIN_COM_MAP(CMSEventHandlerBase)
  23. COM_INTERFACE_ENTRY(IDispatch)
  24. COM_INTERFACE_ENTRY(IObjectWithSite)
  25. END_COM_MAP()
  26. protected:
  27. CMSEventHandlerBase(){}
  28. // Cookie for canceling the advise
  29. DWORD cancelCookie;
  30. public:
  31. virtual ~CMSEventHandlerBase(){
  32. if(cancelCookie!=-1){
  33. Cancel(cancelCookie);
  34. }
  35. }
  36. // Id of the handler function
  37. DISPID ID_handler;
  38. // The DISPID of the event
  39. DISPID ID_event;
  40. // GUID of the Interface whose event we want to know about
  41. GUID gEventInf;
  42. // Connection Point that the advise is on
  43. CComQIPtr<IConnectionPoint> cancelPoint;
  44. // IDispatch where the handler function is from
  45. CComQIPtr<IDispatch> pDScript;
  46. // Override invoke to throw events on up
  47. STDMETHOD(Invoke)(DISPID dispidMember, REFIID riid,
  48. LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
  49. EXCEPINFO* pexcepinfo, UINT* puArgErr){
  50. if(dispidMember == ID_event) {
  51. return pDScript->Invoke(ID_handler, riid,
  52. lcid, wFlags, pdispparams, pvarResult,
  53. pexcepinfo, puArgErr);
  54. } else {
  55. return E_NOTIMPL;
  56. }
  57. }
  58. // Unadvise
  59. STDMETHOD(Cancel)(DWORD dwCancel){
  60. HRESULT hr = E_INVALIDARG;
  61. if(dwCancel == cancelCookie && cancelCookie != -1){
  62. hr = cancelPoint->Unadvise(dwCancel);
  63. if (SUCCEEDED(hr)){
  64. cancelPoint.Release();
  65. pDScript.Release();
  66. cancelCookie = -1;
  67. }
  68. }
  69. return hr;
  70. }
  71. DWORD getCookie(){
  72. return cancelCookie;
  73. }
  74. STDMETHOD(setCookie)(DWORD newValue){
  75. HRESULT hr = E_FAIL;
  76. if(cancelCookie==-1){
  77. cancelCookie = newValue;
  78. hr = S_OK;
  79. }
  80. return hr;
  81. }
  82. };
  83. class __declspec(uuid("C092B145-B318-41a7-B890-C77C5DA41CFD")) CMSEventHandler :
  84. public CComObject<CMSEventHandlerBase>
  85. {
  86. public:
  87. typedef CComObject<CMSEventHandlerBase> base;
  88. CMSEventHandler(DISPID handler, DISPID event, GUID gInf, IDispatch* IDispSite){
  89. ID_handler = handler;
  90. ID_event = event;
  91. gEventInf = gInf;
  92. pDScript = IDispSite;
  93. cancelCookie = -1;
  94. }
  95. STDMETHOD(QueryInterface) (REFIID iid, void **pp) {
  96. try {
  97. if (iid == gEventInf) {
  98. CComQIPtr<IDispatch> pdispRet(static_cast<IDispatch *>(this));
  99. pdispRet.CopyTo(pp);
  100. pdispRet.Release();
  101. return NOERROR;
  102. }
  103. return base::QueryInterface(iid, pp);
  104. } catch(...) {
  105. return E_NOINTERFACE;
  106. }
  107. }
  108. virtual ~CMSEventHandler() {
  109. if(cancelCookie!=-1){
  110. Cancel(cancelCookie);
  111. }
  112. }
  113. };
  114. typedef std::map<DWORD, CComQIPtr<IDispatch> > CancelMap;
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CMSEventBinder
  117. class ATL_NO_VTABLE __declspec(uuid("577FAA18-4518-445E-8F70-1473F8CF4BA4")) CMSEventBinder :
  118. public CComObjectRootEx<CComSingleThreadModel>,
  119. public CComCoClass<CMSEventBinder, &__uuidof(CMSEventBinder)>,
  120. public IObjectWithSiteImplSec<CMSEventBinder>,
  121. public ISupportErrorInfo,
  122. public IObjectSafetyImpl<CMSEventBinder, INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA>,
  123. public IProvideClassInfo2Impl<&CLSID_MSEventBinder, &IID_IMSEventBinder, &LIBID_MSVidCtlLib>,
  124. public IDispatchImpl<IMSEventBinder, &IID_IMSEventBinder, &LIBID_MSVidCtlLib>
  125. {
  126. public:
  127. CMSEventBinder(){}
  128. virtual ~CMSEventBinder(){
  129. CleanupConnection();
  130. }
  131. REGISTER_AUTOMATION_OBJECT(IDS_PROJNAME,
  132. IDS_REG_MSEVENTBINDER_PROGID,
  133. IDS_REG_MSEVENTBINDER_DESC,
  134. LIBID_MSVidCtlLib,
  135. __uuidof(CMSEventBinder));
  136. DECLARE_PROTECT_FINAL_CONSTRUCT()
  137. BEGIN_COM_MAP(CMSEventBinder)
  138. COM_INTERFACE_ENTRY(IDispatch)
  139. COM_INTERFACE_ENTRY(IMSEventBinder)
  140. COM_INTERFACE_ENTRY(ISupportErrorInfo)
  141. COM_INTERFACE_ENTRY(IObjectSafety)
  142. COM_INTERFACE_ENTRY(IProvideClassInfo)
  143. COM_INTERFACE_ENTRY(IProvideClassInfo2)
  144. COM_INTERFACE_ENTRY(IObjectWithSite)
  145. END_COM_MAP()
  146. // IMSEventBinder
  147. public:
  148. // ISupportsErrorInfo
  149. STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
  150. STDMETHOD(Bind)(LPDISPATCH pEventObject, BSTR EventName, BSTR EventHandler, LONG *CancelCookie);
  151. STDMETHOD(Unbind)(DWORD CancelCookie);
  152. protected:
  153. HRESULT CleanupConnection();
  154. private:
  155. CancelMap m_CancelMap; // map of cookies to CMSEventHandlers
  156. };
  157. #endif //__MSEVENTBINDER_H_