OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include "PluginStdAfx.h" |
| 19 #include "WebBrowserEventsListener.h" |
| 20 |
| 21 WebBrowserEventsListener::WebBrowserEventsListener() |
| 22 : m_isDocumentEvents2Connected(false) |
| 23 , m_state(State::FirstTimeLoading) |
| 24 { |
| 25 } |
| 26 |
| 27 WebBrowserEventsListener::~WebBrowserEventsListener() |
| 28 { |
| 29 if (!!m_onDestroy) |
| 30 { |
| 31 m_onDestroy(); |
| 32 } |
| 33 } |
| 34 |
| 35 HRESULT STDMETHODCALLTYPE WebBrowserEventsListener::OnDocumentComplete(IDispatch
* dispFrameBrowser, VARIANT* /*variantUrl*/) |
| 36 { |
| 37 if (!dispFrameBrowser) |
| 38 { |
| 39 return E_POINTER; |
| 40 } |
| 41 |
| 42 // if it's a signal from another browser (sub-frame for-example) then ignore i
t. |
| 43 if (!m_browser.IsEqualObject(dispFrameBrowser)) |
| 44 { |
| 45 return S_OK; |
| 46 } |
| 47 |
| 48 if (!m_isDocumentEvents2Connected) |
| 49 { |
| 50 ATL::CComPtr<IDispatch> dispDocument; |
| 51 ATL::CComQIPtr<IHTMLDocument2> htmlDocument2; |
| 52 bool isHtmlDocument2 = SUCCEEDED(m_browser->get_Document(&dispDocument)) &&
(htmlDocument2 = dispDocument); |
| 53 isHtmlDocument2 && (m_isDocumentEvents2Connected = SUCCEEDED(HTMLDocumentEve
nts2Listener::DispEventAdvise(htmlDocument2))); |
| 54 } |
| 55 |
| 56 // We can get here when readyStateChanged("complete") is already received, |
| 57 // don't emit reloaded, because it's already emitted from OnReadyStateChange. |
| 58 if (m_state == State::FirstTimeLoading) |
| 59 { |
| 60 m_state = State::Loaded; |
| 61 emitReloaded(); |
| 62 } |
| 63 return S_OK; |
| 64 } |
| 65 |
| 66 void STDMETHODCALLTYPE WebBrowserEventsListener::OnReadyStateChange(IHTMLEventOb
j* /*pEvtObj*/) |
| 67 { |
| 68 auto documentReadyState = [this]()->std::wstring |
| 69 { |
| 70 std::wstring notAvailableReadyState; |
| 71 ATL::CComPtr<IDispatch> pDocDispatch; |
| 72 m_browser->get_Document(&pDocDispatch); |
| 73 ATL::CComQIPtr<IHTMLDocument2> htmlDocument2 = pDocDispatch; |
| 74 if (!htmlDocument2) |
| 75 { |
| 76 assert(false && "htmlDocument2 in OnReadyStateChange should not be nullptr
"); |
| 77 return notAvailableReadyState; |
| 78 } |
| 79 ATL::CComBSTR readyState; |
| 80 if (FAILED(htmlDocument2->get_readyState(&readyState)) || !readyState) |
| 81 { |
| 82 assert(false && "cannot obtain document readyState in OnReadyStateChange")
; |
| 83 return notAvailableReadyState; |
| 84 } |
| 85 return std::wstring(readyState, readyState.Length()); |
| 86 }(); |
| 87 if (documentReadyState == L"loading") |
| 88 { |
| 89 m_state = State::Loading; |
| 90 } |
| 91 else if (documentReadyState == L"interactive") |
| 92 { |
| 93 } |
| 94 else if (documentReadyState == L"complete") |
| 95 { |
| 96 if (m_state == State::Loading) |
| 97 { |
| 98 m_state = State::Loaded; |
| 99 emitReloaded(); |
| 100 } |
| 101 else if (m_state == State::Loaded) |
| 102 { |
| 103 // It happens but very rearely, most often it appears on gmail. |
| 104 // It seems IE prepares the 'browser' and then immediately says |
| 105 // "complete" with the new URL. However all cases are related to |
| 106 // some redirection technique and I could not reproduce it with local |
| 107 // server which redirects, so let's wait for the user response on another |
| 108 // web site when an advertisement is not blocked to better investigate |
| 109 // when it happens. |
| 110 } |
| 111 else |
| 112 { |
| 113 assert(false); |
| 114 } |
| 115 } |
| 116 else if (documentReadyState == L"uninitialized") |
| 117 { |
| 118 } |
| 119 else |
| 120 { |
| 121 assert(false); |
| 122 } |
| 123 } |
| 124 |
| 125 HRESULT WebBrowserEventsListener::Init(IWebBrowser2* webBrowser, const OnDestroy
& onDestroy, const OnReloaded& onReloaded) |
| 126 { |
| 127 if (!(m_browser = webBrowser)) |
| 128 { |
| 129 return E_POINTER; |
| 130 } |
| 131 m_onDestroy = onDestroy; |
| 132 m_onReloaded = onReloaded; |
| 133 if (FAILED(WebBrowserEvents2Listener::DispEventAdvise(m_browser, &DIID_DWebBro
wserEvents2))) |
| 134 { |
| 135 return E_FAIL; |
| 136 } |
| 137 return S_OK; |
| 138 } |
| 139 |
| 140 void WebBrowserEventsListener::emitReloaded() |
| 141 { |
| 142 if (m_onReloaded) |
| 143 { |
| 144 m_onReloaded(); |
| 145 } |
| 146 } |
OLD | NEW |