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

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

Issue 5140323101573120: Issue #276 - introduce classes BSTR_ParamArgument and BSTR_ParamResult (Closed)
Patch Set: Created Aug. 4, 2014, 2:41 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/plugin/PluginFilter.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include "PluginStdAfx.h" 1 #include "PluginStdAfx.h"
2 2
3 #include "PluginFilter.h" 3 #include "PluginFilter.h"
4 4
5 #include "COM_Value.h"
5 #if (defined PRODUCT_ADBLOCKPLUS) 6 #if (defined PRODUCT_ADBLOCKPLUS)
6 #include "PluginSettings.h" 7 #include "PluginSettings.h"
7 #include "PluginClient.h" 8 #include "PluginClient.h"
8 #include "PluginClientFactory.h" 9 #include "PluginClientFactory.h"
9 #endif 10 #endif
10 11
11 #include "PluginMutex.h" 12 #include "PluginMutex.h"
12 #include "PluginSettings.h" 13 #include "PluginSettings.h"
13 #include "PluginSystem.h" 14 #include "PluginSystem.h"
14 #include "PluginClass.h" 15 #include "PluginClass.h"
15 #include "mlang.h" 16 #include "mlang.h"
16 17
17 #include "..\shared\CriticalSection.h" 18 #include "..\shared\CriticalSection.h"
18 19
19
20 // The filters are described at http://adblockplus.org/en/filters 20 // The filters are described at http://adblockplus.org/en/filters
21 21
22 static CriticalSection s_criticalSectionFilterMap; 22 static CriticalSection s_criticalSectionFilterMap;
23 23
24 // ============================================================================ 24 // ============================================================================
25 // CFilterElementHideAttrSelector 25 // CFilterElementHideAttrSelector
26 // ============================================================================ 26 // ============================================================================
27 27
28 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N ONE), m_pos(POS_NONE), m_bstrAttr(NULL) 28 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N ONE), m_pos(POS_NONE)
29 { 29 {
30 } 30 }
31 31
32 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector(const CFilterElem entHideAttrSelector& filter) 32 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector(const CFilterElem entHideAttrSelector& filter)
33 { 33 {
34 m_type = filter.m_type; 34 m_type = filter.m_type;
35 m_pos = filter.m_pos; 35 m_pos = filter.m_pos;
36 m_bstrAttr = filter.m_bstrAttr; 36 m_attr = filter.m_attr;
37 37
38 m_value = filter.m_value; 38 m_value = filter.m_value;
39 } 39 }
40 40
41 CFilterElementHideAttrSelector::~CFilterElementHideAttrSelector() 41 CFilterElementHideAttrSelector::~CFilterElementHideAttrSelector()
42 { 42 {
43 } 43 }
44 44
45 45
46 // ============================================================================ 46 // ============================================================================
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 if (delimiterPos > 0) 143 if (delimiterPos > 0)
144 { 144 {
145 attrSelector.m_value = arg.Mid(delimiterPos + 1); 145 attrSelector.m_value = arg.Mid(delimiterPos + 1);
146 if (attrSelector.m_value.GetLength() >= 2 && attrSelector.m_value.GetAt(0) == '\"' && attrSelector.m_value.GetAt(attrSelector.m_value.GetLength() - 1) == '\"') 146 if (attrSelector.m_value.GetLength() >= 2 && attrSelector.m_value.GetAt(0) == '\"' && attrSelector.m_value.GetAt(attrSelector.m_value.GetLength() - 1) == '\"')
147 { 147 {
148 attrSelector.m_value = attrSelector.m_value.Mid(1, attrSelector.m_value. GetLength() - 2); 148 attrSelector.m_value = attrSelector.m_value.Mid(1, attrSelector.m_value. GetLength() - 2);
149 } 149 }
150 150
151 if (arg.GetAt(delimiterPos - 1) == '^') 151 if (arg.GetAt(delimiterPos - 1) == '^')
152 { 152 {
153 attrSelector.m_bstrAttr = arg.Left(delimiterPos - 1); 153 attrSelector.m_attr = arg.Left(delimiterPos - 1);
154 attrSelector.m_pos = CFilterElementHideAttrPos::STARTING; 154 attrSelector.m_pos = CFilterElementHideAttrPos::STARTING;
155 } 155 }
156 else if (arg.GetAt(delimiterPos - 1) == '*') 156 else if (arg.GetAt(delimiterPos - 1) == '*')
157 { 157 {
158 attrSelector.m_bstrAttr = arg.Left(delimiterPos - 1); 158 attrSelector.m_attr = arg.Left(delimiterPos - 1);
159 attrSelector.m_pos = CFilterElementHideAttrPos::ANYWHERE; 159 attrSelector.m_pos = CFilterElementHideAttrPos::ANYWHERE;
160 } 160 }
161 else if (arg.GetAt(delimiterPos - 1) == '$') 161 else if (arg.GetAt(delimiterPos - 1) == '$')
162 { 162 {
163 attrSelector.m_bstrAttr = arg.Left(delimiterPos - 1); 163 attrSelector.m_attr = arg.Left(delimiterPos - 1);
164 attrSelector.m_pos = CFilterElementHideAttrPos::ENDING; 164 attrSelector.m_pos = CFilterElementHideAttrPos::ENDING;
165 } 165 }
166 else 166 else
167 { 167 {
168 attrSelector.m_bstrAttr = arg.Left(delimiterPos); 168 attrSelector.m_attr = arg.Left(delimiterPos);
169 attrSelector.m_pos = CFilterElementHideAttrPos::EXACT; 169 attrSelector.m_pos = CFilterElementHideAttrPos::EXACT;
170 } 170 }
171 } 171 }
172 CString tag = attrSelector.m_bstrAttr; 172 CString tag = attrSelector.m_attr;
173 if (tag == "style") 173 if (tag == "style")
174 { 174 {
175 attrSelector.m_type = CFilterElementHideAttrType::STYLE; 175 attrSelector.m_type = CFilterElementHideAttrType::STYLE;
176 attrSelector.m_value.MakeLower(); 176 attrSelector.m_value.MakeLower();
177 } 177 }
178 else if (tag == "id") 178 else if (tag == "id")
179 { 179 {
180 attrSelector.m_type = CFilterElementHideAttrType::ID; 180 attrSelector.m_type = CFilterElementHideAttrType::ID;
181 } 181 }
182 else if (tag == "class") 182 else if (tag == "class")
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 { 235 {
236 } 236 }
237 237
238 238
239 bool CFilterElementHide::IsMatchFilterElementHide(IHTMLElement* pEl) const 239 bool CFilterElementHide::IsMatchFilterElementHide(IHTMLElement* pEl) const
240 { 240 {
241 HRESULT hr; 241 HRESULT hr;
242 242
243 if (!m_tagId.IsEmpty()) 243 if (!m_tagId.IsEmpty())
244 { 244 {
245 CComBSTR id; 245 AdblockPlus::COM::BSTR_ParamResult id;
246 hr = pEl->get_id(&id); 246 hr = pEl->get_id(&id);
247 if ((hr != S_OK) || (id != CComBSTR(m_tagId))) 247 if ((hr != S_OK) || (to_CString(id) != m_tagId))
248 { 248 {
249 return false; 249 return false;
250 } 250 }
251 } 251 }
252 if (!m_tagClassName.IsEmpty()) 252 if (!m_tagClassName.IsEmpty())
253 { 253 {
254 CComBSTR classNameBSTR; 254 AdblockPlus::COM::BSTR_ParamResult result;
255 hr = pEl->get_className(&classNameBSTR); 255 hr = pEl->get_className(&result);
256 if (hr == S_OK) 256 if (hr == S_OK)
257 { 257 {
258 CString className = classNameBSTR; 258 CString className = to_CString(result);
259 int start = 0; 259 int start = 0;
260 CString specificClass; 260 CString specificClass;
261 bool foundMatch = false; 261 bool foundMatch = false;
262 while ((specificClass = className.Tokenize(L" ", start)) != L"") 262 while ((specificClass = className.Tokenize(L" ", start)) != L"")
263 { 263 {
264 // TODO: Consider case of multiple classes. (m_tagClassName can be somet hing like "foo.bar") 264 // TODO: Consider case of multiple classes. (m_tagClassName can be somet hing like "foo.bar")
265 if (specificClass == m_tagClassName) 265 if (specificClass == m_tagClassName)
266 { 266 {
267 foundMatch = true; 267 foundMatch = true;
268 } 268 }
269 } 269 }
270 if (!foundMatch) 270 if (!foundMatch)
271 { 271 {
272 return false; 272 return false;
273 } 273 }
274 } 274 }
275 } 275 }
276 if (!m_tag.IsEmpty()) 276 if (!m_tag.IsEmpty())
277 { 277 {
278 CComBSTR tagName; 278 AdblockPlus::COM::BSTR_ParamResult result;
279 tagName.ToLower(); 279 hr = pEl->get_tagName(&result);
280 hr = pEl->get_tagName(&tagName); 280 CString tagName = to_CString(result);
281 if ((hr != S_OK) || (tagName != CComBSTR(m_tag))) 281 tagName.MakeLower();
282 if ((hr != S_OK) || tagName != m_tag)
282 { 283 {
283 return false; 284 return false;
284 } 285 }
285 } 286 }
286 287
287 // Check attributes 288 // Check attributes
288 for (std::vector<CFilterElementHideAttrSelector>::const_iterator attrIt = m_at tributeSelectors.begin(); 289 for (std::vector<CFilterElementHideAttrSelector>::const_iterator attrIt = m_at tributeSelectors.begin();
289 attrIt != m_attributeSelectors.end(); ++ attrIt) 290 attrIt != m_attributeSelectors.end(); ++ attrIt)
290 { 291 {
291 CString value; 292 CString value;
292 bool attrFound = false; 293 bool attrFound = false;
293 if (attrIt->m_type == CFilterElementHideAttrType::STYLE) 294 if (attrIt->m_type == CFilterElementHideAttrType::STYLE)
294 { 295 {
295 CComPtr<IHTMLStyle> pStyle; 296 CComPtr<IHTMLStyle> pStyle;
296 if (SUCCEEDED(pEl->get_style(&pStyle)) && pStyle) 297 if (SUCCEEDED(pEl->get_style(&pStyle)) && pStyle)
297 { 298 {
298 CComBSTR bstrStyle; 299 AdblockPlus::COM::BSTR_ParamResult cssText;
299 300 if (SUCCEEDED(pStyle->get_cssText(&cssText)))
300 if (SUCCEEDED(pStyle->get_cssText(&bstrStyle)) && bstrStyle)
301 { 301 {
302 value = bstrStyle; 302 value = to_CString(cssText);
303 value.MakeLower(); 303 if (!value.IsEmpty())
304 attrFound = true; 304 {
305 value.MakeLower();
306 attrFound = true;
307 }
305 } 308 }
306 } 309 }
307 } 310 }
308 else if (attrIt->m_type == CFilterElementHideAttrType::CLASS) 311 else if (attrIt->m_type == CFilterElementHideAttrType::CLASS)
309 { 312 {
310 CComBSTR bstrClassNames; 313 AdblockPlus::COM::BSTR_ParamResult classNames;
311 if (SUCCEEDED(pEl->get_className(&bstrClassNames)) && bstrClassNames) 314 if (SUCCEEDED(pEl->get_className(&classNames)))
312 { 315 {
313 value = bstrClassNames; 316 value = to_CString(classNames);
314 attrFound = true; 317 if (!value.IsEmpty())
318 {
319 attrFound = true;
320 }
315 } 321 }
316 } 322 }
317 else if (attrIt->m_type == CFilterElementHideAttrType::ID) 323 else if (attrIt->m_type == CFilterElementHideAttrType::ID)
318 { 324 {
319 CComBSTR bstrId; 325 AdblockPlus::COM::BSTR_ParamResult id;
320 if (SUCCEEDED(pEl->get_id(&bstrId)) && bstrId) 326 if (SUCCEEDED(pEl->get_id(&id)))
321 { 327 {
322 value = bstrId; 328 value = to_CString(id);
323 attrFound = true; 329 if (!value.IsEmpty())
330 {
331 attrFound = true;
332 }
324 } 333 }
325 } 334 }
326 else 335 else
327 { 336 {
328 CComVariant vAttr; 337 CComVariant vAttr;
329 if (SUCCEEDED(pEl->getAttribute(attrIt->m_bstrAttr, 0, &vAttr))) 338 AdblockPlus::COM::BSTR_ParamArgument attribute_name(to_wstring(attrIt->m_a ttr));
339 if (SUCCEEDED(pEl->getAttribute(attribute_name, 0, &vAttr)))
330 { 340 {
331 attrFound = true; 341 attrFound = true;
332 if (vAttr.vt == VT_BSTR) 342 if (vAttr.vt == VT_BSTR)
333 { 343 {
334 value = vAttr.bstrVal; 344 value = vAttr.bstrVal;
335 } 345 }
336 else if (vAttr.vt == VT_I4) 346 else if (vAttr.vt == VT_I4)
337 { 347 {
338 value.Format(L"%u", vAttr.iVal); 348 value.Format(L"%u", vAttr.iVal);
339 } 349 }
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 } 510 }
501 } while (separatorChar != '\0'); 511 } while (separatorChar != '\0');
502 } 512 }
503 513
504 return true; 514 return true;
505 } 515 }
506 516
507 bool CPluginFilter::IsElementHidden(const CString& tag, IHTMLElement* pEl, const CString& domain, const CString& indent) const 517 bool CPluginFilter::IsElementHidden(const CString& tag, IHTMLElement* pEl, const CString& domain, const CString& indent) const
508 { 518 {
509 CString id; 519 CString id;
510 CComBSTR bstrId; 520 AdblockPlus::COM::BSTR_ParamResult result;
511 if (SUCCEEDED(pEl->get_id(&bstrId)) && bstrId) 521 if (SUCCEEDED(pEl->get_id(&result)))
512 { 522 {
513 id = bstrId; 523 id = to_CString(result);
514 } 524 }
515 525
516 CString classNames; 526 CString classNames;
517 CComBSTR bstrClassNames; 527 // reuse of 'result' is not an error, since address-of operator permits it
518 if (SUCCEEDED(pEl->get_className(&bstrClassNames)) && bstrClassNames) 528 if (SUCCEEDED(pEl->get_className(&result)))
519 { 529 {
520 classNames = bstrClassNames; 530 classNames = to_CString(result);
521 } 531 }
522 532
523 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); 533 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap);
524 { 534 {
525 CString domainTest = domain; 535 CString domainTest = domain;
526 536
527 // Search tag/id filters 537 // Search tag/id filters
528 if (!id.IsEmpty()) 538 if (!id.IsEmpty())
529 { 539 {
530 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHideT agsNamed::const_iterator> idItEnum = 540 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHideT agsNamed::const_iterator> idItEnum =
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 DEBUG_FILTER("Filter::ShouldBlock " + type + " YES") 720 DEBUG_FILTER("Filter::ShouldBlock " + type + " YES")
711 721
712 #ifdef ENABLE_DEBUG_RESULT 722 #ifdef ENABLE_DEBUG_RESULT
713 CPluginDebug::DebugResultBlocking(type, src, domain); 723 CPluginDebug::DebugResultBlocking(type, src, domain);
714 #endif 724 #endif
715 } 725 }
716 return true; 726 return true;
717 } 727 }
718 return false; 728 return false;
719 } 729 }
OLDNEW
« no previous file with comments | « src/plugin/PluginFilter.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld