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

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

Issue 6433575100481536: Issue 1148 - ABP on chrome blocks ads but IE doesnt (Closed)
Patch Set: merge GetAttributeValueAsString into the caller Created Nov. 19, 2014, 11:52 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
« no previous file with comments | « no previous file | src/plugin/PluginUtil.h » ('j') | 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 #include "PluginSettings.h" 4 #include "PluginSettings.h"
5 #include "PluginClient.h" 5 #include "PluginClient.h"
6 #include "PluginClientFactory.h" 6 #include "PluginClientFactory.h"
7 #include "PluginMutex.h" 7 #include "PluginMutex.h"
8 #include "PluginSettings.h" 8 #include "PluginSettings.h"
9 #include "PluginSystem.h" 9 #include "PluginSystem.h"
10 #include "PluginClass.h" 10 #include "PluginClass.h"
11 #include "mlang.h" 11 #include "mlang.h"
12 12
13 #include "..\shared\CriticalSection.h" 13 #include "..\shared\CriticalSection.h"
14 #include "..\shared\Utils.h"
14 15
15 16
16 // The filters are described at http://adblockplus.org/en/filters 17 // The filters are described at http://adblockplus.org/en/filters
17 18
18 static CriticalSection s_criticalSectionFilterMap; 19 static CriticalSection s_criticalSectionFilterMap;
19 20
21 namespace
22 {
23 std::pair<std::wstring, bool> GetHtmlElementAttribute(IHTMLElement& htmlElemen t, const ATL::CComBSTR& attributeName)
24 {
25 std::pair<std::wstring, bool> retResult;
26 retResult.second = false;
27 ATL::CComVariant vAttr;
28 ATL::CComQIPtr<IHTMLElement4> htmlElement4 = &htmlElement;
29 if (!htmlElement4)
30 {
31 return retResult;
32 }
33 ATL::CComPtr<IHTMLDOMAttribute> attributeNode;
34 if (FAILED(htmlElement4->getAttributeNode(attributeName, &attributeNode)) || !attributeNode)
35 {
36 return retResult;
37 }
38 // we set that attribute found but it's not necessary that we can retrieve i ts value
39 retResult.second = true;
40 if (FAILED(attributeNode->get_nodeValue(&vAttr)))
41 {
42 return retResult;
43 }
44 if (vAttr.vt == VT_BSTR && vAttr.bstrVal)
45 {
46 retResult.first = vAttr.bstrVal;
47 }
48 else if (vAttr.vt == VT_I4)
49 {
50 retResult.first = std::to_wstring(vAttr.iVal);
51 }
52 return retResult;
53 }
54 }
55
20 // ============================================================================ 56 // ============================================================================
21 // CFilterElementHideAttrSelector 57 // CFilterElementHideAttrSelector
22 // ============================================================================ 58 // ============================================================================
23 59
24 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N ONE), m_pos(POS_NONE), m_bstrAttr(NULL) 60 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N ONE), m_pos(POS_NONE), m_bstrAttr(NULL)
25 { 61 {
26 } 62 }
27 63
28 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector(const CFilterElem entHideAttrSelector& filter) 64 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector(const CFilterElem entHideAttrSelector& filter)
29 { 65 {
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 } 301 }
266 if (!foundMatch) 302 if (!foundMatch)
267 { 303 {
268 return false; 304 return false;
269 } 305 }
270 } 306 }
271 } 307 }
272 if (!m_tag.IsEmpty()) 308 if (!m_tag.IsEmpty())
273 { 309 {
274 CComBSTR tagName; 310 CComBSTR tagName;
311 hr = pEl->get_tagName(&tagName);
275 tagName.ToLower(); 312 tagName.ToLower();
276 hr = pEl->get_tagName(&tagName);
277 if ((hr != S_OK) || (tagName != CComBSTR(m_tag))) 313 if ((hr != S_OK) || (tagName != CComBSTR(m_tag)))
278 { 314 {
279 return false; 315 return false;
280 } 316 }
281 } 317 }
282 318
283 // Check attributes 319 // Check attributes
284 for (std::vector<CFilterElementHideAttrSelector>::const_iterator attrIt = m_at tributeSelectors.begin(); 320 for (std::vector<CFilterElementHideAttrSelector>::const_iterator attrIt = m_at tributeSelectors.begin();
285 attrIt != m_attributeSelectors.end(); ++ attrIt) 321 attrIt != m_attributeSelectors.end(); ++ attrIt)
286 { 322 {
287 CString value; 323 ATL::CString value;
288 bool attrFound = false; 324 bool attrFound = false;
289 if (attrIt->m_type == CFilterElementHideAttrType::STYLE) 325 if (attrIt->m_type == CFilterElementHideAttrType::STYLE)
290 { 326 {
291 CComPtr<IHTMLStyle> pStyle; 327 CComPtr<IHTMLStyle> pStyle;
292 if (SUCCEEDED(pEl->get_style(&pStyle)) && pStyle) 328 if (SUCCEEDED(pEl->get_style(&pStyle)) && pStyle)
293 { 329 {
294 CComBSTR bstrStyle; 330 CComBSTR bstrStyle;
295 331
296 if (SUCCEEDED(pStyle->get_cssText(&bstrStyle)) && bstrStyle) 332 if (SUCCEEDED(pStyle->get_cssText(&bstrStyle)) && bstrStyle)
297 { 333 {
(...skipping 14 matching lines...) Expand all
312 } 348 }
313 else if (attrIt->m_type == CFilterElementHideAttrType::ID) 349 else if (attrIt->m_type == CFilterElementHideAttrType::ID)
314 { 350 {
315 CComBSTR bstrId; 351 CComBSTR bstrId;
316 if (SUCCEEDED(pEl->get_id(&bstrId)) && bstrId) 352 if (SUCCEEDED(pEl->get_id(&bstrId)) && bstrId)
317 { 353 {
318 value = bstrId; 354 value = bstrId;
319 attrFound = true; 355 attrFound = true;
320 } 356 }
321 } 357 }
322 else 358 else
323 { 359 {
324 CComVariant vAttr; 360 auto attribute = GetHtmlElementAttribute(*pEl, attrIt->m_bstrAttr);
325 if (SUCCEEDED(pEl->getAttribute(attrIt->m_bstrAttr, 0, &vAttr))) 361 if (attrFound = attribute.second)
326 { 362 {
327 attrFound = true; 363 value = ToCString(attribute.first);
328 if (vAttr.vt == VT_BSTR)
329 {
330 value = vAttr.bstrVal;
331 }
332 else if (vAttr.vt == VT_I4)
333 {
334 value.Format(L"%u", vAttr.iVal);
335 }
336 } 364 }
337 } 365 }
338 366
339 if (attrFound) 367 if (attrFound)
340 { 368 {
341 if (attrIt->m_pos == CFilterElementHideAttrPos::EXACT) 369 if (attrIt->m_pos == CFilterElementHideAttrPos::EXACT)
342 { 370 {
343 // TODO: IE rearranges the style attribute completely. Figure out if any thing can be done about it. 371 // TODO: IE rearranges the style attribute completely. Figure out if any thing can be done about it.
344 if (value != attrIt->m_value) 372 if (value != attrIt->m_value)
345 return false; 373 return false;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 m_contentMapText[CFilter::contentTypeSubdocument] = "SUBDOCUMENT"; 456 m_contentMapText[CFilter::contentTypeSubdocument] = "SUBDOCUMENT";
429 m_contentMapText[CFilter::contentTypeStyleSheet] = "STYLESHEET"; 457 m_contentMapText[CFilter::contentTypeStyleSheet] = "STYLESHEET";
430 m_contentMapText[CFilter::contentTypeXmlHttpRequest] = "XMLHTTPREQUEST"; 458 m_contentMapText[CFilter::contentTypeXmlHttpRequest] = "XMLHTTPREQUEST";
431 459
432 ClearFilters(); 460 ClearFilters();
433 } 461 }
434 462
435 463
436 bool CPluginFilter::AddFilterElementHide(CString filterText) 464 bool CPluginFilter::AddFilterElementHide(CString filterText)
437 { 465 {
438
439
440 DEBUG_FILTER("Input: " + filterText + " filterFile" + filterFile); 466 DEBUG_FILTER("Input: " + filterText + " filterFile" + filterFile);
441 467 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap);
442 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap);
443 { 468 {
444
445 CString filterString = filterText; 469 CString filterString = filterText;
446 // Create filter descriptor 470 // Create filter descriptor
447 std::auto_ptr<CFilterElementHide> filter; 471 std::auto_ptr<CFilterElementHide> filter;
448 472
449 CString wholeFilterString = filterString; 473 CString wholeFilterString = filterString;
450 wchar_t separatorChar; 474 wchar_t separatorChar;
451 do 475 do
452 { 476 {
453 int chunkEnd = filterText.FindOneOf(L"+>"); 477 int chunkEnd = filterText.FindOneOf(L"+>");
454 if (chunkEnd > 0) 478 if (chunkEnd > 0)
455 { 479 {
456 separatorChar = filterText.GetAt(chunkEnd); 480 separatorChar = filterText.GetAt(chunkEnd);
457 } 481 }
458 else 482 else
459 { 483 {
460 chunkEnd = filterText.GetLength(); 484 chunkEnd = filterText.GetLength();
461 separatorChar = L'\0'; 485 separatorChar = L'\0';
462 } 486 }
463 487
464 CString filterChunk = filterText.Left(chunkEnd).TrimRight(); 488 CString filterChunk = filterText.Left(chunkEnd).TrimRight();
465 std::auto_ptr<CFilterElementHide> filterParent(filter); 489 std::auto_ptr<CFilterElementHide> filterParent(filter);
466 490
467 filter.reset(new CFilterElementHide(filterChunk)); 491 filter.reset(new CFilterElementHide(filterChunk));
468 492
469 if (filterParent.get() != 0) 493 if (filterParent.get() != 0)
470 { 494 {
471 filter->m_predecessor.reset(filterParent.release()); 495 filter->m_predecessor.reset(filterParent.release());
472 } 496 }
473 497
474 if (separatorChar != L'\0') // complex selector 498 if (separatorChar != L'\0') // complex selector
475 { 499 {
476 filterText = filterText.Mid(chunkEnd + 1).TrimLeft(); 500 filterText = filterText.Mid(chunkEnd + 1).TrimLeft();
477 if (separatorChar == '+') 501 if (separatorChar == '+')
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 id = bstrId; 536 id = bstrId;
513 } 537 }
514 538
515 CString classNames; 539 CString classNames;
516 CComBSTR bstrClassNames; 540 CComBSTR bstrClassNames;
517 if (SUCCEEDED(pEl->get_className(&bstrClassNames)) && bstrClassNames) 541 if (SUCCEEDED(pEl->get_className(&bstrClassNames)) && bstrClassNames)
518 { 542 {
519 classNames = bstrClassNames; 543 classNames = bstrClassNames;
520 } 544 }
521 545
522 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); 546 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap);
523 { 547 {
524 // Search tag/id filters 548 // Search tag/id filters
525 if (!id.IsEmpty()) 549 if (!id.IsEmpty())
526 { 550 {
527 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHideT agsNamed::const_iterator> idItEnum = 551 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHideT agsNamed::const_iterator> idItEnum =
528 m_elementHideTagsId.equal_range(std::make_pair(tagCString, id)); 552 m_elementHideTagsId.equal_range(std::make_pair(tagCString, id));
529 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id It != idItEnum.second; idIt ++) 553 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id It != idItEnum.second; idIt ++)
530 { 554 {
531 if (idIt->second.IsMatchFilterElementHide(pEl)) 555 if (idIt->second.IsMatchFilterElementHide(pEl))
532 { 556 {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 } 638 }
615 639
616 bool CPluginFilter::LoadHideFilters(std::vector<std::wstring> filters) 640 bool CPluginFilter::LoadHideFilters(std::vector<std::wstring> filters)
617 { 641 {
618 ClearFilters(); 642 ClearFilters();
619 bool isRead = false; 643 bool isRead = false;
620 CPluginClient* client = CPluginClient::GetInstance(); 644 CPluginClient* client = CPluginClient::GetInstance();
621 645
622 // Parse hide string 646 // Parse hide string
623 int pos = 0; 647 int pos = 0;
624 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); 648 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap);
625 { 649 {
626 for (std::vector<std::wstring>::iterator it = filters.begin(); it < filters. end(); ++it) 650 for (std::vector<std::wstring>::iterator it = filters.begin(); it < filters. end(); ++it)
627 { 651 {
628 CString filter((*it).c_str()); 652 CString filter((*it).c_str());
629 // If the line is not commented out 653 // If the line is not commented out
630 if (!filter.Trim().IsEmpty() && filter.GetAt(0) != '!' && filter.GetAt(0) != '[') 654 if (!filter.Trim().IsEmpty() && filter.GetAt(0) != '!' && filter.GetAt(0) != '[')
631 { 655 {
632 int filterType = 0; 656 int filterType = 0;
633 657
634 // See http://adblockplus.org/en/filters for further documentation 658 // See http://adblockplus.org/en/filters for further documentation
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 CPluginDebug::DebugResultBlocking(type, srcCString, domain); 730 CPluginDebug::DebugResultBlocking(type, srcCString, domain);
707 #endif 731 #endif
708 } 732 }
709 return true; 733 return true;
710 } 734 }
711 #ifdef ENABLE_DEBUG_RESULT 735 #ifdef ENABLE_DEBUG_RESULT
712 CPluginDebug::DebugResultIgnoring(type, srcCString, domain); 736 CPluginDebug::DebugResultIgnoring(type, srcCString, domain);
713 #endif 737 #endif
714 return false; 738 return false;
715 } 739 }
OLDNEW
« no previous file with comments | « no previous file | src/plugin/PluginUtil.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld