Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/plugin/AdblockPlusDomTraverser.cpp

Issue 29332057: Issue #1234 - Replace CString in the traverser (Closed)
Patch Set: rebase Created Dec. 14, 2015, 11:56 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 #include "PluginStdAfx.h" 18 #include "PluginStdAfx.h"
19 #include "AdblockPlusDomTraverser.h" 19 #include "AdblockPlusDomTraverser.h"
20 #include "AdblockPlusClient.h" 20 #include "AdblockPlusClient.h"
21 #include "PluginFilter.h" 21 #include "PluginFilter.h"
22 #include "PluginSettings.h" 22 #include "PluginSettings.h"
23 #include "..\shared\Utils.h"
23 24
24 25
25 CPluginDomTraverser::CPluginDomTraverser(CPluginTab* tab) : CPluginDomTraverserB ase(tab) 26 CPluginDomTraverser::CPluginDomTraverser(CPluginTab* tab) : CPluginDomTraverserB ase(tab)
26 { 27 {
27 } 28 }
28 29
29 30
30 bool CPluginDomTraverser::OnIFrame(IHTMLElement* pEl, const std::wstring& url, C String& indent) 31 bool CPluginDomTraverser::OnIFrame(IHTMLElement* pEl, const std::wstring& url, c onst std::wstring& indent)
31 { 32 {
32 CPluginClient* client = CPluginClient::GetInstance(); 33 CPluginClient* client = CPluginClient::GetInstance();
33 34
34 // If src should be blocked, set style display:none on iframe 35 // If src should be blocked, set style display:none on iframe
35 bool isBlocked = client->ShouldBlock(url, 36 bool isBlocked = client->ShouldBlock(url,
36 AdblockPlus::FilterEngine::ContentType::CONTENT_TYPE_SUBDOCUMENT, m_domain); 37 AdblockPlus::FilterEngine::ContentType::CONTENT_TYPE_SUBDOCUMENT, m_domain);
37 if (isBlocked) 38 if (isBlocked)
38 { 39 {
39 HideElement(pEl, "iframe", url, true, indent); 40 HideElement(pEl, L"iframe", url, true, indent);
40 } 41 }
41 42
42 return !isBlocked; 43 return !isBlocked;
43 } 44 }
44 45
45 46
46 bool CPluginDomTraverser::OnElement(IHTMLElement* pEl, const CString& tag, CPlug inDomTraverserCache* cache, bool isDebug, CString& indent) 47 bool CPluginDomTraverser::OnElement(IHTMLElement* pEl, const std::wstring& tag, CPluginDomTraverserCache* cache, bool isDebug, const std::wstring& indent)
47 { 48 {
48 if (cache->m_isHidden) 49 if (cache->m_isHidden)
49 { 50 {
50 return false; 51 return false;
51 } 52 }
52 53
53 // Check if element is hidden 54 // Check if element is hidden
54 CPluginClient* client = CPluginClient::GetInstance(); 55 CPluginClient* client = CPluginClient::GetInstance();
55 56
56 cache->m_isHidden = client->IsElementHidden(ToWstring(tag), pEl, m_domain, ToW string(indent), m_tab->m_filter.get()); 57 cache->m_isHidden = client->IsElementHidden(tag, pEl, m_domain, indent, m_tab- >m_filter.get());
57 if (cache->m_isHidden) 58 if (cache->m_isHidden)
58 { 59 {
59 HideElement(pEl, tag, L"", false, indent); 60 HideElement(pEl, tag, L"", false, indent);
60 return false; 61 return false;
61 } 62 }
62 63
63 // Images 64 // Images
64 if (tag == "img") 65 if (tag == L"img")
65 { 66 {
66 CComVariant vAttr; 67 CComVariant vAttr;
67 68
68 if (SUCCEEDED(pEl->getAttribute(ATL::CComBSTR(L"src"), 0, &vAttr)) && vAttr. vt == VT_BSTR && ::SysStringLen(vAttr.bstrVal) > 0) 69 if (SUCCEEDED(pEl->getAttribute(ATL::CComBSTR(L"src"), 0, &vAttr)) && vAttr. vt == VT_BSTR && ::SysStringLen(vAttr.bstrVal) > 0)
69 { 70 {
70 std::wstring src(vAttr.bstrVal, SysStringLen(vAttr.bstrVal)); 71 std::wstring src(vAttr.bstrVal, SysStringLen(vAttr.bstrVal));
71 72
72 // If src should be blocked, set style display:none on image 73 // If src should be blocked, set style display:none on image
73 cache->m_isHidden = client->ShouldBlock(src, 74 cache->m_isHidden = client->ShouldBlock(src,
74 AdblockPlus::FilterEngine::ContentType::CONTENT_TYPE_IMAGE, m_domain); 75 AdblockPlus::FilterEngine::ContentType::CONTENT_TYPE_IMAGE, m_domain);
75 if (cache->m_isHidden) 76 if (cache->m_isHidden)
76 { 77 {
77 HideElement(pEl, "image", src, true, indent); 78 HideElement(pEl, L"image", src, true, indent);
78 return false; 79 return false;
79 } 80 }
80 } 81 }
81 } 82 }
82 // Objects 83 // Objects
83 else if (tag == "object") 84 else if (tag == L"object")
84 { 85 {
85 CComBSTR bstrInnerHtml; 86 CComBSTR bstrInnerHtml;
86
87 if (SUCCEEDED(pEl->get_innerHTML(&bstrInnerHtml)) && bstrInnerHtml) 87 if (SUCCEEDED(pEl->get_innerHTML(&bstrInnerHtml)) && bstrInnerHtml)
88 { 88 {
89 CString sObjectHtml = bstrInnerHtml; 89 const std::wstring objectInnerHtml(ToWstring(bstrInnerHtml));
90 CString src; 90 std::wstring::size_type posBegin = 0;
91 91 while (true)
92 int posBegin = sObjectHtml.Find(L"VALUE=\"");
93 int posEnd = posBegin >= 0 ? sObjectHtml.Find('\"', posBegin + 7) : -1;
94
95 while (posBegin >= 0 && posEnd >= 0)
96 { 92 {
97 posBegin += 7; 93 posBegin = objectInnerHtml.find(L"VALUE=\"", posBegin);
sergei 2015/12/14 12:17:55 It's not related to this issue, but shouldn't we p
Eric 2015/12/14 12:40:23 If we change it, we shouldn't be parsing the inner
Oleksandr 2015/12/14 12:45:34 It wouldn't hurt, but it doesn't depend on how it
sergei 2015/12/14 13:28:02 Yes, I thought about it too. The current approach
sergei 2015/12/14 13:28:02 Could you provide with a reference for it? I've tr
98 94 if (posBegin == std::wstring::npos)
99 src = sObjectHtml.Mid(posBegin, posEnd - posBegin); 95 {
100 96 // No more "value" attributes to scan
97 break;
98 }
99 auto posPostInitialQuote = posBegin + 7;
100 auto posFinalQuote = objectInnerHtml.find(L'\"', posPostInitialQuote);
101 if (posFinalQuote == std::wstring::npos)
102 {
103 // We have an initial quotation mark but no final one.
104 // Ignore this tag because it has an HTML syntax error.
105 break;
106 }
107 auto src = objectInnerHtml.substr(posPostInitialQuote, posFinalQuote - p osPostInitialQuote);
101 // eg. http://w3schools.com/html/html_examples.asp 108 // eg. http://w3schools.com/html/html_examples.asp
102 if (src.Left(2) == "//") 109 if (BeginsWith(src, L"//"))
103 { 110 {
104 src = "http:" + src; 111 src = L"http:" + src;
105 } 112 }
106 113 if (!src.empty() && cache->m_isHidden)
107 if (!src.IsEmpty())
108 { 114 {
109 if (cache->m_isHidden) 115 HideElement(pEl, L"object", src, true, indent);
110 { 116 return false;
111 HideElement(pEl, "object", ToWstring(src), true, indent);
112 return false;
113 }
114 } 117 }
115 118 posBegin = posFinalQuote; // Don't scan content of quoted string
116 posBegin = sObjectHtml.Find(L"VALUE=\"", posBegin);
117 posEnd = posBegin >= 0 ? sObjectHtml.Find(L"\"", posBegin + 7) : -1;
118 } 119 }
119 } 120 }
120 } 121 }
121 122
122 return true; 123 return true;
123 } 124 }
124 125
125 126
126 bool CPluginDomTraverser::IsEnabled() 127 bool CPluginDomTraverser::IsEnabled()
127 { 128 {
128 CPluginClient* client = CPluginClient::GetInstance(); 129 CPluginClient* client = CPluginClient::GetInstance();
129 return client && CPluginSettings::GetInstance()->IsPluginEnabled() && !client- >IsWhitelistedUrl(m_domain); 130 return client && CPluginSettings::GetInstance()->IsPluginEnabled() && !client- >IsWhitelistedUrl(m_domain);
130 } 131 }
131 132
132 133
133 void CPluginDomTraverser::HideElement(IHTMLElement* pEl, const CString& type, co nst std::wstring& url, bool isDebug, CString& indent) 134 void CPluginDomTraverser::HideElement(IHTMLElement* pEl, const std::wstring& typ e, const std::wstring& url, bool isDebug, const std::wstring& indent)
134 { 135 {
135 CComPtr<IHTMLStyle> pStyle; 136 CComPtr<IHTMLStyle> pStyle;
136 137
137 if (SUCCEEDED(pEl->get_style(&pStyle)) && pStyle) 138 if (SUCCEEDED(pEl->get_style(&pStyle)) && pStyle)
138 { 139 {
139 CComBSTR bstrDisplay; 140 CComBSTR bstrDisplay;
140 141 if (SUCCEEDED(pStyle->get_display(&bstrDisplay)) && bstrDisplay && ToWstring (bstrDisplay) == L"none")
141 if (SUCCEEDED(pStyle->get_display(&bstrDisplay)) && bstrDisplay && CString(b strDisplay) == L"none")
142 { 142 {
143 return; 143 return;
144 } 144 }
145 145
146 static const CComBSTR sbstrNone(L"none"); 146 static const CComBSTR sbstrNone(L"none");
147 147
148 if (SUCCEEDED(pStyle->put_display(sbstrNone))) 148 if (SUCCEEDED(pStyle->put_display(sbstrNone)))
149 { 149 {
150 DEBUG_HIDE_EL(ToWstring(indent) + L"HideEl::Hiding " + ToWstring(type) + L " url:" + url) 150 DEBUG_HIDE_EL(ToWstring(indent) + L"HideEl::Hiding " + ToWstring(type) + L " url:" + url)
151 151
152 #ifdef ENABLE_DEBUG_RESULT 152 #ifdef ENABLE_DEBUG_RESULT
153 if (isDebug) 153 if (isDebug)
154 { 154 {
155 CPluginDebug::DebugResultHiding(ToWstring(type), url, L"-"); 155 CPluginDebug::DebugResultHiding(type, url, L"-");
156 } 156 }
157 #endif // ENABLE_DEBUG_RESULT 157 #endif // ENABLE_DEBUG_RESULT
158 } 158 }
159 } 159 }
160 } 160 }
OLDNEW

Powered by Google App Engine
This is Rietveld