OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #ifndef _PLUGIN_DOM_TRAVERSER_BASE_H_ | 18 #ifndef _PLUGIN_DOM_TRAVERSER_BASE_H_ |
19 #define _PLUGIN_DOM_TRAVERSER_BASE_H_ | 19 #define _PLUGIN_DOM_TRAVERSER_BASE_H_ |
20 | 20 |
21 #include "PluginTabBase.h" | 21 #include "PluginFilter.h" |
22 #include "PluginUtil.h" | 22 #include "PluginUtil.h" |
23 | 23 |
24 class CPluginDomTraverserCacheBase | 24 class CPluginDomTraverserCacheBase |
25 { | 25 { |
26 public: | 26 public: |
27 | 27 |
28 long m_elements; | 28 long m_elements; |
29 | 29 |
30 CPluginDomTraverserCacheBase() : m_elements(0) {}; | 30 CPluginDomTraverserCacheBase() : m_elements(0) {}; |
31 void Init() { m_elements=0; } | 31 void Init() { m_elements=0; } |
32 }; | 32 }; |
33 | 33 |
34 template <class T> | 34 template <class T> |
35 class CPluginDomTraverserBase | 35 class CPluginDomTraverserBase |
36 { | 36 { |
37 | 37 |
38 public: | 38 public: |
39 | 39 |
40 CPluginDomTraverserBase(CPluginTab* tab); | 40 CPluginDomTraverserBase(CPluginFilter* filterSet); |
41 ~CPluginDomTraverserBase(); | 41 ~CPluginDomTraverserBase(); |
42 | 42 |
43 void TraverseHeader(bool isHeaderTraversed); | 43 void TraverseDocumentRoot(IWebBrowser2* pBrowser, const std::wstring& domain,
const std::wstring& documentUrl); |
44 | |
45 void TraverseDocument(IWebBrowser2* pBrowser, const std::wstring& domain, cons
t std::wstring& documentUrl); | |
46 void TraverseSubdocument(IWebBrowser2* pBrowser, const std::wstring& domain, c
onst std::wstring& documentUrl); | 44 void TraverseSubdocument(IWebBrowser2* pBrowser, const std::wstring& domain, c
onst std::wstring& documentUrl); |
47 | 45 |
48 virtual void ClearCache(); | |
49 | |
50 protected: | 46 protected: |
51 | 47 |
52 virtual bool OnIFrame(IHTMLElement* pEl, const std::wstring& url, const std::w
string& indent) { return true; } | 48 virtual bool OnIFrame(IHTMLElement* pEl, const std::wstring& url, const std::w
string& indent) { return true; } |
53 virtual bool OnElement(IHTMLElement* pEl, const std::wstring& tag, T* cache, b
ool isDebug, const std::wstring& indent) { return true; } | 49 virtual bool OnElement(IHTMLElement* pEl, const std::wstring& tag, T* cache, b
ool isDebug, const std::wstring& indent) { return true; } |
54 | 50 |
55 virtual bool IsEnabled(); | 51 virtual bool IsEnabled(); |
56 | 52 |
57 protected: | 53 protected: |
58 | 54 |
59 void TraverseDocument(IWebBrowser2* pBrowser, bool isMainDoc, const std::wstri
ng& indent); | 55 void TraverseDocument(IWebBrowser2* pBrowser, const std::wstring& indent); |
60 void TraverseChild(IHTMLElement* pEl, IWebBrowser2* pBrowser, const std::wstri
ng& indent, bool isCached=true); | 56 void TraverseChild(IHTMLElement* pEl, IWebBrowser2* pBrowser, const std::wstri
ng& indent, bool isCached=true); |
61 | 57 |
62 CComAutoCriticalSection m_criticalSection; | 58 CComAutoCriticalSection m_criticalSection; |
63 | 59 |
64 std::wstring m_domain; | 60 std::wstring m_domain; |
65 std::wstring m_documentUrl; | 61 std::wstring m_documentUrl; |
66 | 62 |
67 bool m_isHeaderTraversed; | |
68 | |
69 // Caching | 63 // Caching |
70 long m_cacheDomElementCount; | |
71 | |
72 int m_cacheIndexLast; | 64 int m_cacheIndexLast; |
73 int m_cacheElementsMax; | 65 int m_cacheElementsMax; |
74 std::set<std::wstring> m_cacheDocumentHasFrames; | 66 std::set<std::wstring> m_cacheDocumentHasFrames; |
75 std::set<std::wstring> m_cacheDocumentHasIframes; | 67 std::set<std::wstring> m_cacheDocumentHasIframes; |
76 | 68 |
77 T* m_cacheElements; | 69 T* m_cacheElements; |
78 | 70 |
79 CPluginTab* m_tab; | 71 CPluginFilter* filterSet; |
80 CComPtr<IWebBrowser2> m_pBrowser; | |
81 }; | 72 }; |
82 | 73 |
83 template <class T> | 74 template <class T> |
84 CPluginDomTraverserBase<T>::CPluginDomTraverserBase(CPluginTab* tab) : | 75 CPluginDomTraverserBase<T>::CPluginDomTraverserBase(CPluginFilter* filterSet) : |
85 m_tab(tab), m_isHeaderTraversed(false), m_cacheDomElementCount(0), m_cacheInde
xLast(0), m_cacheElementsMax(5000) | 76 filterSet(filterSet), m_cacheIndexLast(0), m_cacheElementsMax(5000) |
86 { | 77 { |
87 m_cacheElements = new T[m_cacheElementsMax]; | 78 m_cacheElements = new T[m_cacheElementsMax]; |
88 } | 79 } |
89 | 80 |
90 | 81 |
91 template <class T> | 82 template <class T> |
92 CPluginDomTraverserBase<T>::~CPluginDomTraverserBase() | 83 CPluginDomTraverserBase<T>::~CPluginDomTraverserBase() |
93 { | 84 { |
94 delete [] m_cacheElements; | 85 delete [] m_cacheElements; |
95 } | 86 } |
96 | 87 |
97 template <class T> | 88 template <class T> |
98 void CPluginDomTraverserBase<T>::TraverseHeader(bool isHeaderTraversed) | 89 void CPluginDomTraverserBase<T>::TraverseDocumentRoot(IWebBrowser2* pBrowser, co
nst std::wstring& domain, const std::wstring& documentUrl) |
99 { | |
100 m_isHeaderTraversed = isHeaderTraversed; | |
101 } | |
102 | |
103 template <class T> | |
104 void CPluginDomTraverserBase<T>::TraverseDocument(IWebBrowser2* pBrowser, const
std::wstring& domain, const std::wstring& documentUrl) | |
105 { | 90 { |
106 m_domain = domain; | 91 m_domain = domain; |
107 m_documentUrl = documentUrl; | 92 m_documentUrl = documentUrl; |
108 TraverseDocument(pBrowser, true, L""); | 93 TraverseDocument(pBrowser, L""); |
109 } | 94 } |
110 | 95 |
111 | 96 |
112 template <class T> | |
113 void CPluginDomTraverserBase<T>::TraverseSubdocument(IWebBrowser2* pBrowser, con
st std::wstring& domain, const std::wstring& documentUrl) | |
114 { | |
115 m_domain = domain; | |
116 m_documentUrl = documentUrl; | |
117 TraverseDocument(pBrowser, false, L""); | |
118 } | |
119 | |
120 | |
121 template <class T> | 97 template <class T> |
122 bool CPluginDomTraverserBase<T>::IsEnabled() | 98 bool CPluginDomTraverserBase<T>::IsEnabled() |
123 { | 99 { |
124 return CPluginSettings::GetInstance()->IsPluginEnabled(); | 100 return CPluginSettings::GetInstance()->IsPluginEnabled(); |
125 } | 101 } |
126 | 102 |
127 | 103 |
128 template <class T> | 104 template <class T> |
129 void CPluginDomTraverserBase<T>::TraverseDocument(IWebBrowser2* pBrowser, bool i
sMainDoc, const std::wstring& indent) | 105 void CPluginDomTraverserBase<T>::TraverseDocument(IWebBrowser2* pBrowser, const
std::wstring& indent) |
130 { | 106 { |
131 DWORD res = WaitForSingleObject(m_tab->m_filter.hideFiltersLoadedEvent, ENGINE
_STARTUP_TIMEOUT); | 107 DWORD res = WaitForSingleObject(filterSet->hideFiltersLoadedEvent, ENGINE_STAR
TUP_TIMEOUT); |
132 if (!IsEnabled()) return; | 108 if (!IsEnabled()) return; |
133 | 109 |
134 VARIANT_BOOL isBusy; | 110 VARIANT_BOOL isBusy; |
135 if (SUCCEEDED(pBrowser->get_Busy(&isBusy))) | 111 if (SUCCEEDED(pBrowser->get_Busy(&isBusy))) |
136 { | 112 { |
137 if (isBusy != VARIANT_FALSE) | 113 if (isBusy != VARIANT_FALSE) |
138 { | 114 { |
139 return; | 115 return; |
140 } | 116 } |
141 } | 117 } |
(...skipping 12 matching lines...) Expand all Loading... |
154 return; | 130 return; |
155 } | 131 } |
156 | 132 |
157 CComPtr<IHTMLElement> pBody; | 133 CComPtr<IHTMLElement> pBody; |
158 if (FAILED(pDoc->get_documentElement(&pBody)) || !pBody) | 134 if (FAILED(pDoc->get_documentElement(&pBody)) || !pBody) |
159 { | 135 { |
160 return; | 136 return; |
161 } | 137 } |
162 | 138 |
163 CComPtr<IHTMLElement> pBodyEl; | 139 CComPtr<IHTMLElement> pBodyEl; |
164 | 140 CComPtr<IHTMLElementCollection> pBodyCollection; |
165 if (m_isHeaderTraversed) | 141 if (FAILED(pDoc->getElementsByTagName(ATL::CComBSTR(L"body"), &pBodyCollection
)) || !pBodyCollection) |
166 { | 142 { |
167 pBodyEl = pBody; | 143 return; |
168 } | |
169 else | |
170 { | |
171 CComPtr<IHTMLElementCollection> pBodyCollection; | |
172 if (FAILED(pDoc->getElementsByTagName(ATL::CComBSTR(L"body"), &pBodyCollecti
on)) || !pBodyCollection) | |
173 { | |
174 return; | |
175 } | |
176 | |
177 CComVariant vIndex(0); | |
178 CComPtr<IDispatch> pBodyDispatch; | |
179 if (FAILED(pBodyCollection->item(vIndex, vIndex, &pBodyDispatch)) || !pBodyD
ispatch) | |
180 { | |
181 return; | |
182 } | |
183 | |
184 if (FAILED(pBodyDispatch->QueryInterface(&pBodyEl)) || !pBodyEl) | |
185 { | |
186 return; | |
187 } | |
188 } | 144 } |
189 | 145 |
190 // Clear cache (if eg. refreshing) ??? | 146 CComVariant vIndex(0); |
191 if (isMainDoc) | 147 CComPtr<IDispatch> pBodyDispatch; |
| 148 if (FAILED(pBodyCollection->item(vIndex, vIndex, &pBodyDispatch)) || !pBodyDis
patch) |
192 { | 149 { |
193 CComVariant vCacheIndex; | 150 return; |
| 151 } |
194 | 152 |
195 if (FAILED(pBodyEl->getAttribute(ATL::CComBSTR(L"abp"), 0, &vCacheIndex)) ||
vCacheIndex.vt == VT_NULL) | 153 if (FAILED(pBodyDispatch->QueryInterface(&pBodyEl)) || !pBodyEl) |
196 { | 154 { |
197 ClearCache(); | 155 return; |
198 } | |
199 } | 156 } |
200 | 157 |
201 // Hide elements in body part | 158 // Hide elements in body part |
202 TraverseChild(pBodyEl, pBrowser, indent); | 159 TraverseChild(pBodyEl, pBrowser, indent); |
203 | 160 |
204 // Check frames and iframes | 161 // Check frames and iframes |
205 bool hasFrames = false; | 162 bool hasFrames = false; |
206 bool hasIframes = false; | 163 bool hasIframes = false; |
207 | 164 |
208 m_criticalSection.Lock(); | 165 m_criticalSection.Lock(); |
(...skipping 27 matching lines...) Expand all Loading... |
236 if (pFrameBrowser) | 193 if (pFrameBrowser) |
237 { | 194 { |
238 std::wstring src; | 195 std::wstring src; |
239 CComBSTR bstrSrc; | 196 CComBSTR bstrSrc; |
240 if (SUCCEEDED(pFrameBrowser->get_LocationURL(&bstrSrc))) | 197 if (SUCCEEDED(pFrameBrowser->get_LocationURL(&bstrSrc))) |
241 { | 198 { |
242 src = ToWstring(bstrSrc); | 199 src = ToWstring(bstrSrc); |
243 } | 200 } |
244 if (!src.empty()) | 201 if (!src.empty()) |
245 { | 202 { |
246 TraverseDocument(pFrameBrowser, false, indent); | 203 TraverseDocument(pFrameBrowser, indent); |
247 } | 204 } |
248 } | 205 } |
249 } | 206 } |
250 } | 207 } |
251 } | 208 } |
252 | 209 |
253 // Iframes | 210 // Iframes |
254 if (hasIframes) | 211 if (hasIframes) |
255 { | 212 { |
256 long frameCount = 0; | 213 long frameCount = 0; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 { | 247 { |
291 src = L"http://" + m_domain + src; | 248 src = L"http://" + m_domain + src; |
292 } | 249 } |
293 | 250 |
294 // Check if Iframe should be traversed | 251 // Check if Iframe should be traversed |
295 if (OnIFrame(pFrameEl, src, indent)) | 252 if (OnIFrame(pFrameEl, src, indent)) |
296 { | 253 { |
297 CComQIPtr<IWebBrowser2> pFrameBrowser = pFrameDispatch; | 254 CComQIPtr<IWebBrowser2> pFrameBrowser = pFrameDispatch; |
298 if (pFrameBrowser) | 255 if (pFrameBrowser) |
299 { | 256 { |
300 TraverseDocument(pFrameBrowser, false, indent); | 257 TraverseDocument(pFrameBrowser, indent); |
301 } | 258 } |
302 } | 259 } |
303 } | 260 } |
304 } | 261 } |
305 } | 262 } |
306 } | 263 } |
307 } | 264 } |
308 } | 265 } |
309 } | 266 } |
310 | 267 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 TraverseChild(pChildEl, pBrowser, indent + L" ", isCached); | 399 TraverseChild(pChildEl, pBrowser, indent + L" ", isCached); |
443 } | 400 } |
444 } | 401 } |
445 } | 402 } |
446 } | 403 } |
447 } | 404 } |
448 } | 405 } |
449 } | 406 } |
450 | 407 |
451 | 408 |
452 template <class T> | |
453 void CPluginDomTraverserBase<T>::ClearCache() | |
454 { | |
455 m_criticalSection.Lock(); | |
456 { | |
457 m_cacheIndexLast = 0; | |
458 m_cacheDocumentHasFrames.clear(); | |
459 m_cacheDocumentHasIframes.clear(); | |
460 } | |
461 m_criticalSection.Unlock(); | |
462 } | |
463 | |
464 | |
465 #endif // _PLUGIN_DOM_TRAVERSER_BASE_H_ | 409 #endif // _PLUGIN_DOM_TRAVERSER_BASE_H_ |
OLD | NEW |