| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 /* | 
|  | 2  * This file is part of Adblock Plus <https://adblockplus.org/>, | 
|  | 3  * Copyright (C) 2006-2016 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 } | 
|  | 30 | 
|  | 31 HRESULT STDMETHODCALLTYPE WebBrowserEventsListener::OnDocumentComplete(IDispatch
     * dispFrameBrowser, VARIANT* /*variantUrl*/) | 
|  | 32 { | 
|  | 33   if (!dispFrameBrowser) | 
|  | 34   { | 
|  | 35     return E_POINTER; | 
|  | 36   } | 
|  | 37 | 
|  | 38   // if it's a signal from another browser (sub-frame for-example) then ignore i
     t. | 
|  | 39   if (!m_browser.IsEqualObject(dispFrameBrowser)) | 
|  | 40   { | 
|  | 41     return S_OK; | 
|  | 42   } | 
|  | 43 | 
|  | 44   if (!m_isDocumentEvents2Connected) | 
|  | 45   { | 
|  | 46     ATL::CComPtr<IDispatch> dispDocument; | 
|  | 47     ATL::CComQIPtr<IHTMLDocument2> htmlDocument2; | 
|  | 48     bool isHtmlDocument2 = SUCCEEDED(m_browser->get_Document(&dispDocument)) && 
     (htmlDocument2 = dispDocument); | 
|  | 49     isHtmlDocument2 && (m_isDocumentEvents2Connected = SUCCEEDED(HTMLDocumentEve
     nts2Listener::DispEventAdvise(htmlDocument2))); | 
|  | 50   } | 
|  | 51 | 
|  | 52   // We can get here when readyStateChanged("complete") is already received, | 
|  | 53   // don't emit reloaded, because it's already emitted from OnReadyStateChange. | 
|  | 54   if (m_state == State::FirstTimeLoading) | 
|  | 55   { | 
|  | 56     m_state = State::Loaded; | 
|  | 57     emitReloaded(); | 
|  | 58   } | 
|  | 59   return S_OK; | 
|  | 60 } | 
|  | 61 | 
|  | 62 void STDMETHODCALLTYPE WebBrowserEventsListener::OnQuit() | 
|  | 63 { | 
|  | 64   if (m_isDocumentEvents2Connected) | 
|  | 65   { | 
|  | 66     ATL::CComPtr<IDispatch> dispDocument; | 
|  | 67     ATL::CComQIPtr<IHTMLDocument2> htmlDocument2; | 
|  | 68     if (SUCCEEDED(m_browser->get_Document(&dispDocument)) && (htmlDocument2 = di
     spDocument)) | 
|  | 69     { | 
|  | 70       HTMLDocumentEvents2Listener::DispEventUnadvise(htmlDocument2); | 
|  | 71     } | 
|  | 72   } | 
|  | 73   WebBrowserEvents2Listener::DispEventUnadvise(m_browser); | 
|  | 74 } | 
|  | 75 | 
|  | 76 void STDMETHODCALLTYPE WebBrowserEventsListener::OnReadyStateChange(IHTMLEventOb
     j* /*pEvtObj*/) | 
|  | 77 { | 
|  | 78   auto documentReadyState = [this]()->std::wstring | 
|  | 79   { | 
|  | 80     std::wstring notAvailableReadyState; | 
|  | 81     ATL::CComPtr<IDispatch> pDocDispatch; | 
|  | 82     m_browser->get_Document(&pDocDispatch); | 
|  | 83     ATL::CComQIPtr<IHTMLDocument2> htmlDocument2 = pDocDispatch; | 
|  | 84     if (!htmlDocument2) | 
|  | 85     { | 
|  | 86       assert(false && "htmlDocument2 in OnReadyStateChange should not be nullptr
     "); | 
|  | 87       return notAvailableReadyState; | 
|  | 88     } | 
|  | 89     ATL::CComBSTR readyState; | 
|  | 90     if (FAILED(htmlDocument2->get_readyState(&readyState)) || !readyState) | 
|  | 91     { | 
|  | 92       assert(false && "cannot obtain document readyState in OnReadyStateChange")
     ; | 
|  | 93       return notAvailableReadyState; | 
|  | 94     } | 
|  | 95     return std::wstring(readyState, readyState.Length()); | 
|  | 96   }(); | 
|  | 97   if (documentReadyState == L"loading") | 
|  | 98   { | 
|  | 99     m_state = State::Loading; | 
|  | 100   } | 
|  | 101   else if (documentReadyState == L"interactive") | 
|  | 102   { | 
|  | 103   } | 
|  | 104   else if (documentReadyState == L"complete") | 
|  | 105   { | 
|  | 106     if (m_state == State::Loading) | 
|  | 107     { | 
|  | 108       m_state = State::Loaded; | 
|  | 109       emitReloaded(); | 
|  | 110     } | 
|  | 111     else if (m_state == State::Loaded) | 
|  | 112     { | 
|  | 113       // It happens but very rearely, most often it appears on gmail. | 
|  | 114       // It seems IE prepares the 'browser' and then immediately says | 
|  | 115       // "complete" with the new URL. However all cases are related to | 
|  | 116       // some redirection technique and I could not reproduce it with local | 
|  | 117       // server which redirects, so let's wait for the user response on another | 
|  | 118       // web site when an advertisement is not blocked to better investigate | 
|  | 119       // when it happens. | 
|  | 120     } | 
|  | 121     else | 
|  | 122     { | 
|  | 123       assert(false); | 
|  | 124     } | 
|  | 125   } | 
|  | 126   else if (documentReadyState == L"uninitialized") | 
|  | 127   { | 
|  | 128   } | 
|  | 129   else | 
|  | 130   { | 
|  | 131     assert(false); | 
|  | 132   } | 
|  | 133 } | 
|  | 134 | 
|  | 135 void WebBrowserEventsListener::FinalRelease() | 
|  | 136 { | 
|  | 137   if (!!m_onDestroy) | 
|  | 138   { | 
|  | 139     m_onDestroy(); | 
|  | 140   } | 
|  | 141 } | 
|  | 142 | 
|  | 143 HRESULT WebBrowserEventsListener::Init(IWebBrowser2* webBrowser, const OnDestroy
     & onDestroy, const OnReloaded& onReloaded) | 
|  | 144 { | 
|  | 145   if (!(m_browser = webBrowser)) | 
|  | 146   { | 
|  | 147     return E_POINTER; | 
|  | 148   } | 
|  | 149   m_onDestroy = onDestroy; | 
|  | 150   m_onReloaded = onReloaded; | 
|  | 151   if (FAILED(WebBrowserEvents2Listener::DispEventAdvise(m_browser, &DIID_DWebBro
     wserEvents2))) | 
|  | 152   { | 
|  | 153     return E_FAIL; | 
|  | 154   } | 
|  | 155   return S_OK; | 
|  | 156 } | 
|  | 157 | 
|  | 158 void WebBrowserEventsListener::emitReloaded() | 
|  | 159 { | 
|  | 160   if (m_onReloaded) | 
|  | 161   { | 
|  | 162     m_onReloaded(); | 
|  | 163   } | 
|  | 164 } | 
| OLD | NEW | 
|---|