| OLD | NEW |
| 1 #include "PluginStdAfx.h" | 1 #include "PluginStdAfx.h" |
| 2 | 2 |
| 3 #include "PluginFilter.h" | 3 #include "PluginFilter.h" |
| 4 | 4 |
| 5 #if (defined PRODUCT_ADBLOCKPLUS) | 5 #if (defined PRODUCT_ADBLOCKPLUS) |
| 6 #include "PluginSettings.h" | 6 #include "PluginSettings.h" |
| 7 #include "PluginClient.h" | 7 #include "PluginClient.h" |
| 8 #include "PluginClientFactory.h" | 8 #include "PluginClientFactory.h" |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include "PluginMutex.h" | 11 #include "PluginMutex.h" |
| 12 #include "PluginHttpRequest.h" | 12 #include "PluginHttpRequest.h" |
| 13 #include "PluginSettings.h" | 13 #include "PluginSettings.h" |
| 14 #include "PluginSystem.h" | 14 #include "PluginSystem.h" |
| 15 #include "PluginClass.h" | 15 #include "PluginClass.h" |
| 16 #include "mlang.h" | 16 #include "mlang.h" |
| 17 | 17 |
| 18 | 18 |
| 19 // The filters are described at http://adblockplus.org/en/filters | 19 // The filters are described at http://adblockplus.org/en/filters |
| 20 | 20 |
| 21 CComAutoCriticalSection CPluginFilter::s_criticalSectionFilterMap; | 21 static CriticalSection s_criticalSectionFilterMap; |
| 22 | |
| 23 | 22 |
| 24 // ============================================================================ | 23 // ============================================================================ |
| 25 // CFilterElementHideAttrSelector | 24 // CFilterElementHideAttrSelector |
| 26 // ============================================================================ | 25 // ============================================================================ |
| 27 | 26 |
| 28 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N
ONE), m_pos(POS_NONE), m_bstrAttr(NULL) | 27 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N
ONE), m_pos(POS_NONE), m_bstrAttr(NULL) |
| 29 { | 28 { |
| 30 } | 29 } |
| 31 | 30 |
| 32 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector(const CFilterElem
entHideAttrSelector& filter) | 31 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector(const CFilterElem
entHideAttrSelector& filter) |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 ClearFilters(); | 438 ClearFilters(); |
| 440 } | 439 } |
| 441 | 440 |
| 442 | 441 |
| 443 bool CPluginFilter::AddFilterElementHide(CString filterText) | 442 bool CPluginFilter::AddFilterElementHide(CString filterText) |
| 444 { | 443 { |
| 445 | 444 |
| 446 | 445 |
| 447 DEBUG_FILTER("Input: " + filterText + " filterFile" + filterFile); | 446 DEBUG_FILTER("Input: " + filterText + " filterFile" + filterFile); |
| 448 | 447 |
| 449 s_criticalSectionFilterMap.Lock(); | 448 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); |
| 450 { | 449 { |
| 451 | 450 |
| 452 CString filterString = filterText; | 451 CString filterString = filterText; |
| 453 // Create filter descriptor | 452 // Create filter descriptor |
| 454 std::auto_ptr<CFilterElementHide> filter; | 453 std::auto_ptr<CFilterElementHide> filter; |
| 455 | 454 |
| 456 CString wholeFilterString = filterString; | 455 CString wholeFilterString = filterString; |
| 457 TCHAR separatorChar; | 456 TCHAR separatorChar; |
| 458 do | 457 do |
| 459 { | 458 { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 m_elementHideTagsClass.insert(std::make_pair(std::make_pair(filter->m_
tag, filter->m_tagClassName), *filter)); | 496 m_elementHideTagsClass.insert(std::make_pair(std::make_pair(filter->m_
tag, filter->m_tagClassName), *filter)); |
| 498 } | 497 } |
| 499 else | 498 else |
| 500 { | 499 { |
| 501 std::pair<CString, CFilterElementHide> pair = std::make_pair(filter->m
_tag, *filter); | 500 std::pair<CString, CFilterElementHide> pair = std::make_pair(filter->m
_tag, *filter); |
| 502 m_elementHideTags.insert(pair); | 501 m_elementHideTags.insert(pair); |
| 503 } | 502 } |
| 504 } | 503 } |
| 505 } while (separatorChar != '\0'); | 504 } while (separatorChar != '\0'); |
| 506 } | 505 } |
| 507 s_criticalSectionFilterMap.Unlock(); | |
| 508 | 506 |
| 509 return true; | 507 return true; |
| 510 } | 508 } |
| 511 | 509 |
| 512 bool CPluginFilter::IsElementHidden(const CString& tag, IHTMLElement* pEl, const
CString& domain, const CString& indent) const | 510 bool CPluginFilter::IsElementHidden(const CString& tag, IHTMLElement* pEl, const
CString& domain, const CString& indent) const |
| 513 { | 511 { |
| 514 CString id; | 512 CString id; |
| 515 CComBSTR bstrId; | 513 CComBSTR bstrId; |
| 516 if (SUCCEEDED(pEl->get_id(&bstrId)) && bstrId) | 514 if (SUCCEEDED(pEl->get_id(&bstrId)) && bstrId) |
| 517 { | 515 { |
| 518 id = bstrId; | 516 id = bstrId; |
| 519 } | 517 } |
| 520 | 518 |
| 521 CString classNames; | 519 CString classNames; |
| 522 CComBSTR bstrClassNames; | 520 CComBSTR bstrClassNames; |
| 523 if (SUCCEEDED(pEl->get_className(&bstrClassNames)) && bstrClassNames) | 521 if (SUCCEEDED(pEl->get_className(&bstrClassNames)) && bstrClassNames) |
| 524 { | 522 { |
| 525 classNames = bstrClassNames; | 523 classNames = bstrClassNames; |
| 526 } | 524 } |
| 527 | 525 |
| 528 s_criticalSectionFilterMap.Lock(); | 526 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); |
| 529 { | 527 { |
| 530 CString domainTest = domain; | 528 CString domainTest = domain; |
| 531 | 529 |
| 532 // Search tag/id filters | 530 // Search tag/id filters |
| 533 if (!id.IsEmpty()) | 531 if (!id.IsEmpty()) |
| 534 { | 532 { |
| 535 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHideT
agsNamed::const_iterator> idItEnum = | 533 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHideT
agsNamed::const_iterator> idItEnum = |
| 536 m_elementHideTagsId.equal_range(std::make_pair(tag, id)); | 534 m_elementHideTagsId.equal_range(std::make_pair(tag, id)); |
| 537 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id
It != idItEnum.second; idIt ++) | 535 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id
It != idItEnum.second; idIt ++) |
| 538 { | 536 { |
| 539 if (idIt->second.IsMatchFilterElementHide(pEl)) | 537 if (idIt->second.IsMatchFilterElementHide(pEl)) |
| 540 { | 538 { |
| 541 #ifdef ENABLE_DEBUG_RESULT | 539 #ifdef ENABLE_DEBUG_RESULT |
| 542 DEBUG_HIDE_EL(indent + "HideEl::Found (tag/id) filter:" + idIt->second
.m_filterText) | 540 DEBUG_HIDE_EL(indent + "HideEl::Found (tag/id) filter:" + idIt->second
.m_filterText) |
| 543 CPluginDebug::DebugResultHiding(tag, "id:" + id, idIt->second.m_filt
erText); | 541 CPluginDebug::DebugResultHiding(tag, "id:" + id, idIt->second.m_filt
erText); |
| 544 #endif | 542 #endif |
| 545 s_criticalSectionFilterMap.Unlock(); | |
| 546 return true; | 543 return true; |
| 547 } | 544 } |
| 548 } | 545 } |
| 549 | 546 |
| 550 // Search general id | 547 // Search general id |
| 551 idItEnum = m_elementHideTagsId.equal_range(std::make_pair("", id)); | 548 idItEnum = m_elementHideTagsId.equal_range(std::make_pair("", id)); |
| 552 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id
It != idItEnum.second; idIt ++) | 549 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id
It != idItEnum.second; idIt ++) |
| 553 { | 550 { |
| 554 if (idIt->second.IsMatchFilterElementHide(pEl)) | 551 if (idIt->second.IsMatchFilterElementHide(pEl)) |
| 555 { | 552 { |
| 556 #ifdef ENABLE_DEBUG_RESULT | 553 #ifdef ENABLE_DEBUG_RESULT |
| 557 DEBUG_HIDE_EL(indent + "HideEl::Found (?/id) filter:" + idIt->second.m
_filterText) | 554 DEBUG_HIDE_EL(indent + "HideEl::Found (?/id) filter:" + idIt->second.m
_filterText) |
| 558 CPluginDebug::DebugResultHiding(tag, "id:" + id, idIt->second.m_filt
erText); | 555 CPluginDebug::DebugResultHiding(tag, "id:" + id, idIt->second.m_filt
erText); |
| 559 #endif | 556 #endif |
| 560 s_criticalSectionFilterMap.Unlock(); | |
| 561 return true; | 557 return true; |
| 562 } | 558 } |
| 563 } | 559 } |
| 564 } | 560 } |
| 565 | 561 |
| 566 // Search tag/className filters | 562 // Search tag/className filters |
| 567 if (!classNames.IsEmpty()) | 563 if (!classNames.IsEmpty()) |
| 568 { | 564 { |
| 569 int pos = 0; | 565 int pos = 0; |
| 570 CString className = classNames.Tokenize(L" \t\n\r", pos); | 566 CString className = classNames.Tokenize(L" \t\n\r", pos); |
| 571 while (pos >= 0) | 567 while (pos >= 0) |
| 572 { | 568 { |
| 573 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHid
eTagsNamed::const_iterator> classItEnum = | 569 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHid
eTagsNamed::const_iterator> classItEnum = |
| 574 m_elementHideTagsClass.equal_range(std::make_pair(tag, className)); | 570 m_elementHideTagsClass.equal_range(std::make_pair(tag, className)); |
| 575 | 571 |
| 576 for (TFilterElementHideTagsNamed::const_iterator classIt = classItEnum.f
irst; classIt != classItEnum.second; ++classIt) | 572 for (TFilterElementHideTagsNamed::const_iterator classIt = classItEnum.f
irst; classIt != classItEnum.second; ++classIt) |
| 577 { | 573 { |
| 578 if (classIt->second.IsMatchFilterElementHide(pEl)) | 574 if (classIt->second.IsMatchFilterElementHide(pEl)) |
| 579 { | 575 { |
| 580 #ifdef ENABLE_DEBUG_RESULT | 576 #ifdef ENABLE_DEBUG_RESULT |
| 581 DEBUG_HIDE_EL(indent + "HideEl::Found (tag/class) filter:" + classIt
->second.m_filterText) | 577 DEBUG_HIDE_EL(indent + "HideEl::Found (tag/class) filter:" + classIt
->second.m_filterText) |
| 582 CPluginDebug::DebugResultHiding(tag, "class:" + className, classIt
->second.m_filterText); | 578 CPluginDebug::DebugResultHiding(tag, "class:" + className, classIt
->second.m_filterText); |
| 583 #endif | 579 #endif |
| 584 s_criticalSectionFilterMap.Unlock(); | |
| 585 return true; | 580 return true; |
| 586 } | 581 } |
| 587 } | 582 } |
| 588 | 583 |
| 589 // Search general class name | 584 // Search general class name |
| 590 classItEnum = m_elementHideTagsClass.equal_range(std::make_pair("", clas
sName)); | 585 classItEnum = m_elementHideTagsClass.equal_range(std::make_pair("", clas
sName)); |
| 591 for (TFilterElementHideTagsNamed::const_iterator classIt = classItEnum.f
irst; classIt != classItEnum.second; ++ classIt) | 586 for (TFilterElementHideTagsNamed::const_iterator classIt = classItEnum.f
irst; classIt != classItEnum.second; ++ classIt) |
| 592 { | 587 { |
| 593 if (classIt->second.IsMatchFilterElementHide(pEl)) | 588 if (classIt->second.IsMatchFilterElementHide(pEl)) |
| 594 { | 589 { |
| 595 #ifdef ENABLE_DEBUG_RESULT | 590 #ifdef ENABLE_DEBUG_RESULT |
| 596 DEBUG_HIDE_EL(indent + "HideEl::Found (?/class) filter:" + classIt->
second.m_filterText) | 591 DEBUG_HIDE_EL(indent + "HideEl::Found (?/class) filter:" + classIt->
second.m_filterText) |
| 597 CPluginDebug::DebugResultHiding(tag, "class:" + className, classIt
->second.m_filterText); | 592 CPluginDebug::DebugResultHiding(tag, "class:" + className, classIt
->second.m_filterText); |
| 598 #endif | 593 #endif |
| 599 s_criticalSectionFilterMap.Unlock(); | |
| 600 return true; | 594 return true; |
| 601 } | 595 } |
| 602 } | 596 } |
| 603 | 597 |
| 604 // Next class name | 598 // Next class name |
| 605 className = classNames.Tokenize(L" \t\n\r", pos); | 599 className = classNames.Tokenize(L" \t\n\r", pos); |
| 606 } | 600 } |
| 607 } | 601 } |
| 608 | 602 |
| 609 // Search tag filters | 603 // Search tag filters |
| 610 std::pair<TFilterElementHideTags::const_iterator, TFilterElementHideTags::co
nst_iterator> tagItEnum | 604 std::pair<TFilterElementHideTags::const_iterator, TFilterElementHideTags::co
nst_iterator> tagItEnum |
| 611 = m_elementHideTags.equal_range(tag); | 605 = m_elementHideTags.equal_range(tag); |
| 612 for (TFilterElementHideTags::const_iterator tagIt = tagItEnum.first; tagIt !
= tagItEnum.second; ++ tagIt) | 606 for (TFilterElementHideTags::const_iterator tagIt = tagItEnum.first; tagIt !
= tagItEnum.second; ++ tagIt) |
| 613 { | 607 { |
| 614 if (tagIt->second.IsMatchFilterElementHide(pEl)) | 608 if (tagIt->second.IsMatchFilterElementHide(pEl)) |
| 615 { | 609 { |
| 616 #ifdef ENABLE_DEBUG_RESULT | 610 #ifdef ENABLE_DEBUG_RESULT |
| 617 DEBUG_HIDE_EL(indent + "HideEl::Found (tag) filter:" + tagIt->second.m_f
ilterText) | 611 DEBUG_HIDE_EL(indent + "HideEl::Found (tag) filter:" + tagIt->second.m_f
ilterText) |
| 618 CPluginDebug::DebugResultHiding(tag, "-", tagIt->second.m_filterText); | 612 CPluginDebug::DebugResultHiding(tag, "-", tagIt->second.m_filterText); |
| 619 #endif | 613 #endif |
| 620 s_criticalSectionFilterMap.Unlock(); | |
| 621 return true; | 614 return true; |
| 622 } | 615 } |
| 623 } | 616 } |
| 624 } | 617 } |
| 625 s_criticalSectionFilterMap.Unlock(); | |
| 626 | 618 |
| 627 return false; | 619 return false; |
| 628 } | 620 } |
| 629 | 621 |
| 630 bool CPluginFilter::LoadHideFilters(std::vector<std::string> filters) | 622 bool CPluginFilter::LoadHideFilters(std::vector<std::string> filters) |
| 631 { | 623 { |
| 632 | 624 |
| 633 ClearFilters(); | 625 ClearFilters(); |
| 634 | 626 |
| 635 bool isRead = false; | 627 bool isRead = false; |
| 636 | 628 |
| 637 #ifdef PRODUCT_ADBLOCKPLUS | 629 #ifdef PRODUCT_ADBLOCKPLUS |
| 638 CPluginClient* client = CPluginClient::GetInstance(); | 630 CPluginClient* client = CPluginClient::GetInstance(); |
| 639 #endif | 631 #endif |
| 640 | 632 |
| 641 // Parse hide string | 633 // Parse hide string |
| 642 int pos = 0; | 634 int pos = 0; |
| 643 | 635 |
| 644 s_criticalSectionFilterMap.Lock(); | 636 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); |
| 645 { | 637 { |
| 646 for (std::vector<std::string>::iterator it = filters.begin(); it < filters.e
nd(); ++it) | 638 for (std::vector<std::string>::iterator it = filters.begin(); it < filters.e
nd(); ++it) |
| 647 { | 639 { |
| 648 CString filter((*it).c_str()); | 640 CString filter((*it).c_str()); |
| 649 // If the line is not commented out | 641 // If the line is not commented out |
| 650 if (!filter.Trim().IsEmpty() && filter.GetAt(0) != '!' && filter.GetAt(0)
!= '[') | 642 if (!filter.Trim().IsEmpty() && filter.GetAt(0) != '!' && filter.GetAt(0)
!= '[') |
| 651 { | 643 { |
| 652 int filterType = 0; | 644 int filterType = 0; |
| 653 | 645 |
| 654 // See http://adblockplus.org/en/filters for further documentation | 646 // See http://adblockplus.org/en/filters for further documentation |
| 655 | 647 |
| 656 try | 648 try |
| 657 { | 649 { |
| 658 AddFilterElementHide(filter); | 650 AddFilterElementHide(filter); |
| 659 } | 651 } |
| 660 catch(...) | 652 catch(...) |
| 661 { | 653 { |
| 662 #ifdef ENABLE_DEBUG_RESULT | 654 #ifdef ENABLE_DEBUG_RESULT |
| 663 CPluginDebug::DebugResult(L"Error loading hide filter: " + filter); | 655 CPluginDebug::DebugResult(L"Error loading hide filter: " + filter); |
| 664 #endif | 656 #endif |
| 665 } | 657 } |
| 666 } | 658 } |
| 667 } | 659 } |
| 668 } | 660 } |
| 669 s_criticalSectionFilterMap.Unlock(); | |
| 670 | 661 |
| 671 return isRead; | 662 return isRead; |
| 672 } | 663 } |
| 673 | 664 |
| 674 void CPluginFilter::ClearFilters() | 665 void CPluginFilter::ClearFilters() |
| 675 { | 666 { |
| 676 // Clear filter maps | 667 // Clear filter maps |
| 677 s_criticalSectionFilterMap.Lock(); | 668 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); |
| 678 { | 669 { |
| 679 for (int i = 0; i < 2; i++) | 670 for (int i = 0; i < 2; i++) |
| 680 { | 671 { |
| 681 for (int j = 0; j < 2; j++) | 672 for (int j = 0; j < 2; j++) |
| 682 { | 673 { |
| 683 m_filterMap[i][j].clear(); | 674 m_filterMap[i][j].clear(); |
| 684 } | 675 } |
| 685 m_filterMapDefault[i].clear(); | 676 m_filterMapDefault[i].clear(); |
| 686 } | 677 } |
| 687 | 678 |
| 688 m_elementHideTags.clear(); | 679 m_elementHideTags.clear(); |
| 689 m_elementHideTagsId.clear(); | 680 m_elementHideTagsId.clear(); |
| 690 m_elementHideTagsClass.clear(); | 681 m_elementHideTagsClass.clear(); |
| 691 } | 682 } |
| 692 s_criticalSectionFilterMap.Unlock(); | |
| 693 } | 683 } |
| 694 | 684 |
| 695 | 685 |
| 696 bool CPluginFilter::ShouldWhiteList(CString src) const | 686 bool CPluginFilter::ShouldWhiteList(CString src) const |
| 697 { | 687 { |
| 698 // We should not block the empty string, so all filtering does not make sense | 688 // We should not block the empty string, so all filtering does not make sense |
| 699 // Therefore we just return | 689 // Therefore we just return |
| 700 if (src.Trim().IsEmpty()) | 690 if (src.Trim().IsEmpty()) |
| 701 { | 691 { |
| 702 return false; | 692 return false; |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 898 if (pos > 0 && domain.GetLength() + pos == subdomain.GetLength()) | 888 if (pos > 0 && domain.GetLength() + pos == subdomain.GetLength()) |
| 899 { | 889 { |
| 900 if (subdomain.GetAt(pos - 1) == '.') | 890 if (subdomain.GetAt(pos - 1) == '.') |
| 901 { | 891 { |
| 902 return true; | 892 return true; |
| 903 } | 893 } |
| 904 } | 894 } |
| 905 | 895 |
| 906 return false; | 896 return false; |
| 907 } | 897 } |
| OLD | NEW |