OLD | NEW |
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 struct GetHtmlElementAttributeResult |
| 24 { |
| 25 GetHtmlElementAttributeResult() : isAttributeFound(false) |
| 26 { |
| 27 } |
| 28 std::wstring attributeValue; |
| 29 bool isAttributeFound; |
| 30 }; |
| 31 |
| 32 bool GetHtmlElementAttribute(IHTMLElement* htmlElement, |
| 33 const ATL::CComBSTR& attributeName, GetHtmlElementAttributeResult& retValue) |
| 34 { |
| 35 if (!htmlElement) |
| 36 { |
| 37 return false; |
| 38 } |
| 39 ATL::CComVariant vAttr; |
| 40 ATL::CComPtr<IHTMLElement4> htmlElement4; |
| 41 if (FAILED(htmlElement->QueryInterface(&htmlElement4)) || !htmlElement4) |
| 42 { |
| 43 return false; |
| 44 } |
| 45 ATL::CComPtr<IHTMLDOMAttribute> attributeNode; |
| 46 if (FAILED(htmlElement4->getAttributeNode(attributeName, &attributeNode)) ||
!attributeNode) |
| 47 { |
| 48 return false; |
| 49 } |
| 50 // we set that attribute found but it's not necessary that we can retrieve i
ts value |
| 51 retValue.isAttributeFound = true; |
| 52 if (FAILED(attributeNode->get_nodeValue(&vAttr))) |
| 53 { |
| 54 return false; |
| 55 } |
| 56 if (vAttr.vt == VT_BSTR && vAttr.bstrVal) |
| 57 { |
| 58 retValue.attributeValue = vAttr.bstrVal; |
| 59 } |
| 60 else if (vAttr.vt == VT_I4) |
| 61 { |
| 62 retValue.attributeValue = std::to_wstring(vAttr.iVal); |
| 63 } |
| 64 return true; |
| 65 } |
| 66 } |
| 67 |
20 // ============================================================================ | 68 // ============================================================================ |
21 // CFilterElementHideAttrSelector | 69 // CFilterElementHideAttrSelector |
22 // ============================================================================ | 70 // ============================================================================ |
23 | 71 |
24 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N
ONE), m_pos(POS_NONE), m_bstrAttr(NULL) | 72 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N
ONE), m_pos(POS_NONE), m_bstrAttr(NULL) |
25 { | 73 { |
26 } | 74 } |
27 | 75 |
28 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector(const CFilterElem
entHideAttrSelector& filter) | 76 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector(const CFilterElem
entHideAttrSelector& filter) |
29 { | 77 { |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 } | 313 } |
266 if (!foundMatch) | 314 if (!foundMatch) |
267 { | 315 { |
268 return false; | 316 return false; |
269 } | 317 } |
270 } | 318 } |
271 } | 319 } |
272 if (!m_tag.IsEmpty()) | 320 if (!m_tag.IsEmpty()) |
273 { | 321 { |
274 CComBSTR tagName; | 322 CComBSTR tagName; |
| 323 hr = pEl->get_tagName(&tagName); |
275 tagName.ToLower(); | 324 tagName.ToLower(); |
276 hr = pEl->get_tagName(&tagName); | |
277 if ((hr != S_OK) || (tagName != CComBSTR(m_tag))) | 325 if ((hr != S_OK) || (tagName != CComBSTR(m_tag))) |
278 { | 326 { |
279 return false; | 327 return false; |
280 } | 328 } |
281 } | 329 } |
282 | 330 |
283 // Check attributes | 331 // Check attributes |
284 for (std::vector<CFilterElementHideAttrSelector>::const_iterator attrIt = m_at
tributeSelectors.begin(); | 332 for (std::vector<CFilterElementHideAttrSelector>::const_iterator attrIt = m_at
tributeSelectors.begin(); |
285 attrIt != m_attributeSelectors.end(); ++ attrIt) | 333 attrIt != m_attributeSelectors.end(); ++ attrIt) |
286 { | 334 { |
287 CString value; | 335 ATL::CString value; |
288 bool attrFound = false; | 336 bool attrFound = false; |
289 if (attrIt->m_type == CFilterElementHideAttrType::STYLE) | 337 if (attrIt->m_type == CFilterElementHideAttrType::STYLE) |
290 { | 338 { |
291 CComPtr<IHTMLStyle> pStyle; | 339 CComPtr<IHTMLStyle> pStyle; |
292 if (SUCCEEDED(pEl->get_style(&pStyle)) && pStyle) | 340 if (SUCCEEDED(pEl->get_style(&pStyle)) && pStyle) |
293 { | 341 { |
294 CComBSTR bstrStyle; | 342 CComBSTR bstrStyle; |
295 | 343 |
296 if (SUCCEEDED(pStyle->get_cssText(&bstrStyle)) && bstrStyle) | 344 if (SUCCEEDED(pStyle->get_cssText(&bstrStyle)) && bstrStyle) |
297 { | 345 { |
(...skipping 14 matching lines...) Expand all Loading... |
312 } | 360 } |
313 else if (attrIt->m_type == CFilterElementHideAttrType::ID) | 361 else if (attrIt->m_type == CFilterElementHideAttrType::ID) |
314 { | 362 { |
315 CComBSTR bstrId; | 363 CComBSTR bstrId; |
316 if (SUCCEEDED(pEl->get_id(&bstrId)) && bstrId) | 364 if (SUCCEEDED(pEl->get_id(&bstrId)) && bstrId) |
317 { | 365 { |
318 value = bstrId; | 366 value = bstrId; |
319 attrFound = true; | 367 attrFound = true; |
320 } | 368 } |
321 } | 369 } |
322 else | 370 else |
323 { | 371 { |
324 CComVariant vAttr; | 372 GetHtmlElementAttributeResult attributeValue; |
325 if (SUCCEEDED(pEl->getAttribute(attrIt->m_bstrAttr, 0, &vAttr))) | 373 bool rc = GetHtmlElementAttribute(pEl, attrIt->m_bstrAttr, attributeValue)
; |
| 374 if (rc && (attrFound = attributeValue.isAttributeFound)) |
326 { | 375 { |
327 attrFound = true; | 376 value = ToCString(attributeValue.attributeValue); |
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 } | 377 } |
337 } | 378 } |
338 | 379 |
339 if (attrFound) | 380 if (attrFound) |
340 { | 381 { |
341 if (attrIt->m_pos == CFilterElementHideAttrPos::EXACT) | 382 if (attrIt->m_pos == CFilterElementHideAttrPos::EXACT) |
342 { | 383 { |
343 // TODO: IE rearranges the style attribute completely. Figure out if any
thing can be done about it. | 384 // TODO: IE rearranges the style attribute completely. Figure out if any
thing can be done about it. |
344 if (value != attrIt->m_value) | 385 if (value != attrIt->m_value) |
345 return false; | 386 return false; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 m_contentMapText[CFilter::contentTypeSubdocument] = "SUBDOCUMENT"; | 469 m_contentMapText[CFilter::contentTypeSubdocument] = "SUBDOCUMENT"; |
429 m_contentMapText[CFilter::contentTypeStyleSheet] = "STYLESHEET"; | 470 m_contentMapText[CFilter::contentTypeStyleSheet] = "STYLESHEET"; |
430 m_contentMapText[CFilter::contentTypeXmlHttpRequest] = "XMLHTTPREQUEST"; | 471 m_contentMapText[CFilter::contentTypeXmlHttpRequest] = "XMLHTTPREQUEST"; |
431 | 472 |
432 ClearFilters(); | 473 ClearFilters(); |
433 } | 474 } |
434 | 475 |
435 | 476 |
436 bool CPluginFilter::AddFilterElementHide(CString filterText) | 477 bool CPluginFilter::AddFilterElementHide(CString filterText) |
437 { | 478 { |
438 | |
439 | |
440 DEBUG_FILTER("Input: " + filterText + " filterFile" + filterFile); | 479 DEBUG_FILTER("Input: " + filterText + " filterFile" + filterFile); |
441 | 480 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); |
442 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); | |
443 { | 481 { |
444 | |
445 CString filterString = filterText; | 482 CString filterString = filterText; |
446 // Create filter descriptor | 483 // Create filter descriptor |
447 std::auto_ptr<CFilterElementHide> filter; | 484 std::auto_ptr<CFilterElementHide> filter; |
448 | 485 |
449 CString wholeFilterString = filterString; | 486 CString wholeFilterString = filterString; |
450 wchar_t separatorChar; | 487 wchar_t separatorChar; |
451 do | 488 do |
452 { | 489 { |
453 int chunkEnd = filterText.FindOneOf(L"+>"); | 490 int chunkEnd = filterText.FindOneOf(L"+>"); |
454 if (chunkEnd > 0) | 491 if (chunkEnd > 0) |
455 { | 492 { |
456 separatorChar = filterText.GetAt(chunkEnd); | 493 separatorChar = filterText.GetAt(chunkEnd); |
457 } | 494 } |
458 else | 495 else |
459 { | 496 { |
460 chunkEnd = filterText.GetLength(); | 497 chunkEnd = filterText.GetLength(); |
461 separatorChar = L'\0'; | 498 separatorChar = L'\0'; |
462 } | 499 } |
463 | 500 |
464 CString filterChunk = filterText.Left(chunkEnd).TrimRight(); | 501 CString filterChunk = filterText.Left(chunkEnd).TrimRight(); |
465 std::auto_ptr<CFilterElementHide> filterParent(filter); | 502 std::auto_ptr<CFilterElementHide> filterParent(filter); |
466 | 503 |
467 filter.reset(new CFilterElementHide(filterChunk)); | 504 filter.reset(new CFilterElementHide(filterChunk)); |
468 | 505 |
469 if (filterParent.get() != 0) | 506 if (filterParent.get() != 0) |
470 { | 507 { |
471 filter->m_predecessor.reset(filterParent.release()); | 508 filter->m_predecessor.reset(filterParent.release()); |
472 } | 509 } |
473 | 510 |
474 if (separatorChar != L'\0') // complex selector | 511 if (separatorChar != L'\0') // complex selector |
475 { | 512 { |
476 filterText = filterText.Mid(chunkEnd + 1).TrimLeft(); | 513 filterText = filterText.Mid(chunkEnd + 1).TrimLeft(); |
477 if (separatorChar == '+') | 514 if (separatorChar == '+') |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 id = bstrId; | 549 id = bstrId; |
513 } | 550 } |
514 | 551 |
515 CString classNames; | 552 CString classNames; |
516 CComBSTR bstrClassNames; | 553 CComBSTR bstrClassNames; |
517 if (SUCCEEDED(pEl->get_className(&bstrClassNames)) && bstrClassNames) | 554 if (SUCCEEDED(pEl->get_className(&bstrClassNames)) && bstrClassNames) |
518 { | 555 { |
519 classNames = bstrClassNames; | 556 classNames = bstrClassNames; |
520 } | 557 } |
521 | 558 |
522 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); | 559 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); |
523 { | 560 { |
524 // Search tag/id filters | 561 // Search tag/id filters |
525 if (!id.IsEmpty()) | 562 if (!id.IsEmpty()) |
526 { | 563 { |
527 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHideT
agsNamed::const_iterator> idItEnum = | 564 std::pair<TFilterElementHideTagsNamed::const_iterator, TFilterElementHideT
agsNamed::const_iterator> idItEnum = |
528 m_elementHideTagsId.equal_range(std::make_pair(tagCString, id)); | 565 m_elementHideTagsId.equal_range(std::make_pair(tagCString, id)); |
529 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id
It != idItEnum.second; idIt ++) | 566 for (TFilterElementHideTagsNamed::const_iterator idIt = idItEnum.first; id
It != idItEnum.second; idIt ++) |
530 { | 567 { |
531 if (idIt->second.IsMatchFilterElementHide(pEl)) | 568 if (idIt->second.IsMatchFilterElementHide(pEl)) |
532 { | 569 { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 } | 651 } |
615 | 652 |
616 bool CPluginFilter::LoadHideFilters(std::vector<std::wstring> filters) | 653 bool CPluginFilter::LoadHideFilters(std::vector<std::wstring> filters) |
617 { | 654 { |
618 ClearFilters(); | 655 ClearFilters(); |
619 bool isRead = false; | 656 bool isRead = false; |
620 CPluginClient* client = CPluginClient::GetInstance(); | 657 CPluginClient* client = CPluginClient::GetInstance(); |
621 | 658 |
622 // Parse hide string | 659 // Parse hide string |
623 int pos = 0; | 660 int pos = 0; |
624 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); | 661 CriticalSection::Lock filterEngineLock(s_criticalSectionFilterMap); |
625 { | 662 { |
626 for (std::vector<std::wstring>::iterator it = filters.begin(); it < filters.
end(); ++it) | 663 for (std::vector<std::wstring>::iterator it = filters.begin(); it < filters.
end(); ++it) |
627 { | 664 { |
628 CString filter((*it).c_str()); | 665 CString filter((*it).c_str()); |
629 // If the line is not commented out | 666 // If the line is not commented out |
630 if (!filter.Trim().IsEmpty() && filter.GetAt(0) != '!' && filter.GetAt(0)
!= '[') | 667 if (!filter.Trim().IsEmpty() && filter.GetAt(0) != '!' && filter.GetAt(0)
!= '[') |
631 { | 668 { |
632 int filterType = 0; | 669 int filterType = 0; |
633 | 670 |
634 // See http://adblockplus.org/en/filters for further documentation | 671 // See http://adblockplus.org/en/filters for further documentation |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 CPluginDebug::DebugResultBlocking(type, srcCString, domain); | 743 CPluginDebug::DebugResultBlocking(type, srcCString, domain); |
707 #endif | 744 #endif |
708 } | 745 } |
709 return true; | 746 return true; |
710 } | 747 } |
711 #ifdef ENABLE_DEBUG_RESULT | 748 #ifdef ENABLE_DEBUG_RESULT |
712 CPluginDebug::DebugResultIgnoring(type, srcCString, domain); | 749 CPluginDebug::DebugResultIgnoring(type, srcCString, domain); |
713 #endif | 750 #endif |
714 return false; | 751 return false; |
715 } | 752 } |
OLD | NEW |