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.

247 lines
7.1 KiB

  1. // mediahlpr.h: Media Bar helper objects that need to be shared between shdocvw & browseui
  2. #ifndef _MEDIAHLPR_H_
  3. #define _MEDIAHLPR_H_
  4. //+----------------------------------------------------------------------------------------
  5. // CMediaBarHelper - Helper object for disabling autoplay per navigation
  6. //-----------------------------------------------------------------------------------------
  7. class
  8. CMediaBarHelper :
  9. public CComObjectRootEx<CComSingleThreadModel>,
  10. public IServiceProvider,
  11. public IDispatchImpl<DWebBrowserEvents2, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw>
  12. {
  13. public:
  14. CMediaBarHelper() :
  15. _dwCPCookie(0),
  16. _dwServiceCookie(0),
  17. _fDisableOnce(false)
  18. {
  19. }
  20. ~CMediaBarHelper()
  21. {
  22. // for putting break points
  23. return;
  24. }
  25. BEGIN_COM_MAP(CMediaBarHelper)
  26. COM_INTERFACE_ENTRY(IServiceProvider)
  27. COM_INTERFACE_ENTRY(IDispatch)
  28. COM_INTERFACE_ENTRY_IID(DIID_DWebBrowserEvents2, IDispatch)
  29. END_COM_MAP();
  30. // *** IServiceProvider methods ***
  31. virtual STDMETHODIMP QueryService(REFGUID guidService, REFIID riid, void ** ppvObj)
  32. {
  33. HRESULT hres = E_UNEXPECTED;
  34. if (IsEqualIID(guidService, CLSID_MediaBand))
  35. {
  36. hres = QueryInterface(riid, ppvObj);
  37. // If we are supposed to disable only the first autoplay, then
  38. // we need to revoke the service after it has been queried for once.
  39. if (_fDisableOnce && _spCP.p)
  40. {
  41. CComPtr<IConnectionPointContainer> spCPC;
  42. // Revoke the service
  43. HRESULT hr = _spCP->GetConnectionPointContainer(&spCPC);
  44. if (SUCCEEDED(hr))
  45. {
  46. hr = ProfferService(spCPC,
  47. CLSID_MediaBand,
  48. NULL,
  49. &_dwServiceCookie);
  50. ASSERT(SUCCEEDED(hr));
  51. _dwServiceCookie = 0;
  52. // unhook the web oc events so that we are destroyed
  53. UnHookWebOCEvents();
  54. }
  55. }
  56. }
  57. return hres;
  58. }
  59. static HRESULT ProfferService(IUnknown *punkSite,
  60. REFGUID sidWhat,
  61. IServiceProvider *pService,
  62. DWORD *pdwCookie)
  63. {
  64. IProfferService *pps;
  65. HRESULT hr = IUnknown_QueryService(punkSite, SID_SProfferService, IID_PPV_ARG(IProfferService, &pps));
  66. if (SUCCEEDED(hr))
  67. {
  68. if (pService)
  69. hr = pps->ProfferService(sidWhat, pService, pdwCookie);
  70. else
  71. {
  72. hr = pps->RevokeService(*pdwCookie);
  73. *pdwCookie = 0;
  74. }
  75. pps->Release();
  76. }
  77. return hr;
  78. }
  79. static HRESULT DisableFirstAutoPlay(IUnknown * pUnk, bool fDisableAll = false)
  80. {
  81. CComObject<CMediaBarHelper> * pMediaBarHelper = NULL;
  82. HRESULT hr = CComObject<CMediaBarHelper>::CreateInstance(&pMediaBarHelper);
  83. if (SUCCEEDED(hr) && pMediaBarHelper)
  84. {
  85. pMediaBarHelper->AddRef();
  86. hr = ProfferService(pUnk,
  87. CLSID_MediaBand,
  88. SAFECAST(pMediaBarHelper, IServiceProvider *),
  89. &(pMediaBarHelper->_dwServiceCookie));
  90. if (SUCCEEDED(hr) && !fDisableAll)
  91. {
  92. pMediaBarHelper->_fDisableOnce = true;
  93. // ISSUE: need to unhook events
  94. hr = pMediaBarHelper->HookWebOCEvents(pUnk);
  95. }
  96. else if(FAILED(hr) && pMediaBarHelper->_dwServiceCookie)
  97. {
  98. // Revoke the service
  99. hr = ProfferService(pUnk,
  100. CLSID_MediaBand,
  101. NULL,
  102. &(pMediaBarHelper->_dwServiceCookie));
  103. }
  104. pMediaBarHelper->Release();
  105. }
  106. return hr;
  107. }
  108. // Hook Content Pane WebOC Events
  109. HRESULT HookWebOCEvents(IUnknown * pUnk)
  110. {
  111. HRESULT hr = E_FAIL;
  112. CComPtr<IConnectionPointContainer> spDocCPC;
  113. CComPtr<IDispatch> spDocDispatch;
  114. CComPtr<IWebBrowser2> spWebBrowser;
  115. if (!pUnk)
  116. {
  117. goto done;
  118. }
  119. hr = IUnknown_QueryService(pUnk, SID_SWebBrowserApp, IID_IWebBrowser2, (LPVOID *)&spWebBrowser);
  120. if (FAILED(hr))
  121. {
  122. goto done;
  123. }
  124. // Get a connection point to the container
  125. hr = spWebBrowser->QueryInterface(IID_IConnectionPointContainer, (void**)&spDocCPC);
  126. if (FAILED(hr))
  127. {
  128. goto done;
  129. }
  130. hr = spDocCPC->FindConnectionPoint( DIID_DWebBrowserEvents2, &_spCP );
  131. if (FAILED(hr))
  132. {
  133. goto done;
  134. }
  135. hr = _spCP->Advise(static_cast<IUnknown*>(static_cast<DWebBrowserEvents2*>(this)), &_dwCPCookie);
  136. if (FAILED(hr))
  137. {
  138. goto done;
  139. }
  140. hr = S_OK;
  141. done:
  142. return hr;
  143. }
  144. HRESULT UnHookWebOCEvents()
  145. {
  146. HRESULT hr = E_FAIL;
  147. if (_spCP)
  148. {
  149. CComPtr<IConnectionPointContainer> spCPC;
  150. // Revoke the service
  151. hr = _spCP->GetConnectionPointContainer(&spCPC);
  152. if (SUCCEEDED(hr) && _dwServiceCookie)
  153. {
  154. hr = ProfferService(spCPC,
  155. CLSID_MediaBand,
  156. NULL,
  157. &_dwServiceCookie);
  158. _dwServiceCookie = 0;
  159. }
  160. // unhook the events
  161. if (_dwCPCookie != 0)
  162. {
  163. hr = _spCP->Unadvise(_dwCPCookie);
  164. _dwCPCookie = 0;
  165. }
  166. _spCP.Release();
  167. }
  168. _dwCPCookie = 0;
  169. return hr;
  170. }
  171. STDMETHODIMP Invoke(
  172. /* [in] */ DISPID dispIdMember,
  173. /* [in] */ REFIID /*riid*/,
  174. /* [in] */ LCID /*lcid*/,
  175. /* [in] */ WORD /*wFlags*/,
  176. /* [out][in] */ DISPPARAMS* pDispParams,
  177. /* [out] */ VARIANT* pVarResult,
  178. /* [out] */ EXCEPINFO* /*pExcepInfo*/,
  179. /* [out] */ UINT* puArgErr)
  180. {
  181. HRESULT hr = E_FAIL;
  182. switch (dispIdMember)
  183. {
  184. case DISPID_ONQUIT: // 253 (see exdispid.h)
  185. case DISPID_NAVIGATEERROR: // 271
  186. // These events sometimes comes before the QS, so we ignore them
  187. // case DISPID_DOCUMENTCOMPLETE: // 259
  188. // case DISPID_NAVIGATECOMPLETE2:// 252
  189. {
  190. hr = UnHookWebOCEvents();
  191. if (FAILED(hr))
  192. {
  193. goto done;
  194. }
  195. }
  196. break;
  197. }
  198. hr = S_OK;
  199. done:
  200. return hr;
  201. }
  202. public:
  203. CComPtr<IConnectionPoint> _spCP;
  204. DWORD _dwCPCookie;
  205. DWORD _dwServiceCookie;
  206. bool _fDisableOnce;
  207. };
  208. #endif // _MEDIAHLPR_H_