LEFT | RIGHT |
| 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 |
1 #include "PluginStdAfx.h" | 18 #include "PluginStdAfx.h" |
2 #include "WebBrowserEventsListener.h" | 19 #include "WebBrowserEventsListener.h" |
3 #include "PluginClientBase.h" // for UnescapeUrl | |
4 | 20 |
5 WebBrowserEventsListener::WebBrowserEventsListener() | 21 WebBrowserEventsListener::WebBrowserEventsListener() |
6 : m_isDocumentEvents2Connected(false) | 22 : m_isDocumentEvents2Connected(false) |
7 , m_state(State::FirstTimeLoading) | 23 , m_state(State::FirstTimeLoading) |
8 { | 24 { |
9 } | 25 } |
10 | 26 |
11 WebBrowserEventsListener::~WebBrowserEventsListener() | 27 WebBrowserEventsListener::~WebBrowserEventsListener() |
12 { | 28 { |
13 if (!!m_onDestroy) | |
14 { | |
15 m_onDestroy(); | |
16 } | |
17 } | 29 } |
18 | 30 |
19 HRESULT STDMETHODCALLTYPE WebBrowserEventsListener::OnDocumentComplete(IDispatch
* dispFrameBrowser, VARIANT* /*variantUrl*/) | 31 HRESULT STDMETHODCALLTYPE WebBrowserEventsListener::OnDocumentComplete(IDispatch
* dispFrameBrowser, VARIANT* /*variantUrl*/) |
20 { | 32 { |
21 if (!dispFrameBrowser) | 33 if (!dispFrameBrowser) |
22 { | 34 { |
23 return E_POINTER; | 35 return E_POINTER; |
24 } | 36 } |
25 | 37 |
26 // if it's a signal from another browser (sub-frame for-example) then ignore i
t. | 38 // if it's a signal from another browser (sub-frame for-example) then ignore i
t. |
27 if (!m_browser.IsEqualObject(dispFrameBrowser)) | 39 if (!m_browser.IsEqualObject(dispFrameBrowser)) |
28 { | 40 { |
29 return S_OK; | 41 return S_OK; |
30 } | 42 } |
31 | 43 |
32 if (!m_isDocumentEvents2Connected) | 44 if (!m_isDocumentEvents2Connected) |
33 { | 45 { |
34 ATL::CComPtr<IDispatch> dispDocument; | 46 ATL::CComPtr<IDispatch> dispDocument; |
35 ATL::CComQIPtr<IHTMLDocument2> htmlDocument2; | 47 ATL::CComQIPtr<IHTMLDocument2> htmlDocument2; |
36 bool isHtmlDocument2 = SUCCEEDED(m_browser->get_Document(&dispDocument)) &&
(htmlDocument2 = dispDocument); | 48 bool isHtmlDocument2 = SUCCEEDED(m_browser->get_Document(&dispDocument)) &&
(htmlDocument2 = dispDocument); |
37 isHtmlDocument2 && (m_isDocumentEvents2Connected = SUCCEEDED(HTMLDocumentEve
nts2Listener::DispEventAdvise(htmlDocument2))); | 49 isHtmlDocument2 && (m_isDocumentEvents2Connected = SUCCEEDED(HTMLDocumentEve
nts2Listener::DispEventAdvise(htmlDocument2))); |
38 } | 50 } |
39 | 51 |
| 52 // We can get here when readyStateChanged("complete") is already received, |
| 53 // don't emit reloaded, because it's already emitted from OnReadyStateChange. |
40 if (m_state == State::FirstTimeLoading) | 54 if (m_state == State::FirstTimeLoading) |
41 { | 55 { |
42 m_state = State::Loaded; | 56 m_state = State::Loaded; |
43 emitReloaded(); | 57 emitReloaded(); |
44 } | 58 } |
45 return S_OK; | 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); |
46 } | 74 } |
47 | 75 |
48 void STDMETHODCALLTYPE WebBrowserEventsListener::OnReadyStateChange(IHTMLEventOb
j* /*pEvtObj*/) | 76 void STDMETHODCALLTYPE WebBrowserEventsListener::OnReadyStateChange(IHTMLEventOb
j* /*pEvtObj*/) |
49 { | 77 { |
50 auto documentReadyState = [this]()->std::wstring | 78 auto documentReadyState = [this]()->std::wstring |
51 { | 79 { |
52 std::wstring notAvailableReadyState; | 80 std::wstring notAvailableReadyState; |
53 ATL::CComPtr<IDispatch> pDocDispatch; | 81 ATL::CComPtr<IDispatch> pDocDispatch; |
54 m_browser->get_Document(&pDocDispatch); | 82 m_browser->get_Document(&pDocDispatch); |
55 ATL::CComQIPtr<IHTMLDocument2> htmlDocument2 = pDocDispatch; | 83 ATL::CComQIPtr<IHTMLDocument2> htmlDocument2 = pDocDispatch; |
(...skipping 17 matching lines...) Expand all Loading... |
73 else if (documentReadyState == L"interactive") | 101 else if (documentReadyState == L"interactive") |
74 { | 102 { |
75 } | 103 } |
76 else if (documentReadyState == L"complete") | 104 else if (documentReadyState == L"complete") |
77 { | 105 { |
78 if (m_state == State::Loading) | 106 if (m_state == State::Loading) |
79 { | 107 { |
80 m_state = State::Loaded; | 108 m_state = State::Loaded; |
81 emitReloaded(); | 109 emitReloaded(); |
82 } | 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 } |
83 else | 121 else |
84 { | 122 { |
85 assert(false); | 123 assert(false); |
86 } | 124 } |
87 } | 125 } |
88 else if (documentReadyState == L"uninitialized") | 126 else if (documentReadyState == L"uninitialized") |
89 { | 127 { |
90 } | 128 } |
91 else | 129 else |
92 { | 130 { |
93 assert(false); | 131 assert(false); |
94 } | 132 } |
95 } | 133 } |
96 | 134 |
97 HRESULT WebBrowserEventsListener::Init(IWebBrowser2* webBrowser, const OnDestroy
& onDestroy) | 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) |
98 { | 144 { |
99 if (!(m_browser = webBrowser)) | 145 if (!(m_browser = webBrowser)) |
100 { | 146 { |
101 return E_POINTER; | 147 return E_POINTER; |
102 } | 148 } |
103 m_onDestroy = onDestroy; | 149 m_onDestroy = onDestroy; |
| 150 m_onReloaded = onReloaded; |
104 if (FAILED(WebBrowserEvents2Listener::DispEventAdvise(m_browser, &DIID_DWebBro
wserEvents2))) | 151 if (FAILED(WebBrowserEvents2Listener::DispEventAdvise(m_browser, &DIID_DWebBro
wserEvents2))) |
105 { | 152 { |
106 return E_FAIL; | 153 return E_FAIL; |
107 } | 154 } |
108 return S_OK; | 155 return S_OK; |
109 } | 156 } |
110 | 157 |
111 void WebBrowserEventsListener::emitReloaded() | 158 void WebBrowserEventsListener::emitReloaded() |
112 { | 159 { |
113 if (reloaded) | 160 if (m_onReloaded) |
114 { | 161 { |
115 reloaded(); | 162 m_onReloaded(); |
116 } | 163 } |
117 } | 164 } |
LEFT | RIGHT |