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

Delta Between Two Patch Sets: src/plugin/PluginFilter.cpp

Issue 6433575100481536: Issue 1148 - ABP on chrome blocks ads but IE doesnt (Closed)
Left Patch Set: Created Nov. 10, 2014, 12:26 p.m.
Right Patch Set: fix according to the discussion Created Nov. 27, 2014, 1:10 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | src/plugin/PluginUtil.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 #include "..\shared\Utils.h"
15 15
16 16
17 // The filters are described at http://adblockplus.org/en/filters 17 // The filters are described at http://adblockplus.org/en/filters
18 18
19 static CriticalSection s_criticalSectionFilterMap; 19 static CriticalSection s_criticalSectionFilterMap;
20 20
21 namespace 21 namespace
22 { 22 {
23 // Returns attribute value and the bool flag which indicates whether the attri bute is found. 23 struct GetHtmlElementAttributeResult
Felix Dahlke 2014/11/27 13:58:46 How about "HtmlAttributeMatch" or something in tha
sergei 2014/11/27 14:47:25 I also don't feel it to be a good name, but I find
Felix Dahlke 2014/11/27 14:57:27 Fair enough, yeah. Let's go with GetHtmlElementAtt
24 // For the cases like <some-tag on-some-event="do();"/> IE returns the value o f 'on-some-event' 24 {
25 // attribute as 'function(event){\ndo();\n}'. Our filters are not designed for such 25 GetHtmlElementAttributeResult() : isAttributeFound(false)
26 // transformations, so this method retrives the value of (body) of /^.*?\{(bod y)\}$/ as well. 26 {
27 std::pair<std::wstring, bool> GetHtmlElementAttribute(IHTMLElement& htmlElemen t, const ATL::CComBSTR& attributeName) 27 }
28 { 28 std::wstring attributeValue;
29 std::pair<std::wstring, bool> retResult; 29 bool isAttributeFound;
30 retResult.second = false; 30 };
31
32 GetHtmlElementAttributeResult GetHtmlElementAttribute(IHTMLElement& htmlElemen t,
33 const ATL::CComBSTR& attributeName)
34 {
35 GetHtmlElementAttributeResult retValue;
31 ATL::CComVariant vAttr; 36 ATL::CComVariant vAttr;
32 // Performs a property search that is not case-sensitive, 37 ATL::CComPtr<IHTMLElement4> htmlElement4;
33 // and returns an interpolated value if the property is found. 38 if (FAILED(htmlElement.QueryInterface(&htmlElement4)) || !htmlElement4)
34 LONG flags = 0; 39 {
35 if (FAILED(htmlElement.getAttribute(attributeName, flags, &vAttr))) 40 return retValue;
36 { 41 }
37 return retResult; 42 ATL::CComPtr<IHTMLDOMAttribute> attributeNode;
43 if (FAILED(htmlElement4->getAttributeNode(attributeName, &attributeNode)) || !attributeNode)
44 {
45 return retValue;
38 } 46 }
39 // we set that attribute found but it's not necessary that we can retrieve i ts value 47 // we set that attribute found but it's not necessary that we can retrieve i ts value
40 retResult.second = true; 48 retValue.isAttributeFound = true;
49 if (FAILED(attributeNode->get_nodeValue(&vAttr)))
50 {
51 return retValue;
52 }
41 if (vAttr.vt == VT_BSTR && vAttr.bstrVal) 53 if (vAttr.vt == VT_BSTR && vAttr.bstrVal)
42 { 54 {
43 retResult.first.assign(vAttr.bstrVal); 55 retValue.attributeValue = vAttr.bstrVal;
44 } 56 }
45 else if (vAttr.vt == VT_I4) 57 else if (vAttr.vt == VT_I4)
46 { 58 {
47 retResult.first = std::to_wstring(vAttr.iVal); 59 retValue.attributeValue = std::to_wstring(vAttr.iVal);
48 } 60 }
49 else if (vAttr.vt == VT_DISPATCH) 61 return retValue;
50 {
51 wchar_t* toStringMethod = L"toString";
52 DISPID methodId = 0;
53 if (FAILED(vAttr.pdispVal->GetIDsOfNames(/*must be null iid*/IID_NULL, &to StringMethod,
54 /*cNames*/1, LOCALE_SYSTEM_DEFAULT, &methodId)))
55 {
56 return retResult;
57 }
58 ATL::CComVariant variantFunctionAsString; // result of IDispatch::Invoke
59 DISPID dispidNamed = DISPATCH_METHOD | DISPATCH_PROPERTYGET;
60 DISPPARAMS dispparams;
61 dispparams.rgvarg = &variantFunctionAsString;
62 dispparams.cArgs = 1;
63 dispparams.cNamedArgs = 1;
64 dispparams.rgdispidNamedArgs = &dispidNamed;
65 UINT ArgErr = 0;
66 if (FAILED(vAttr.pdispVal->Invoke(methodId, /*must be null iid*/IID_NULL,
67 LOCALE_SYSTEM_DEFAULT, dispidNamed, &dispparams, &variantFunctionAsStrin g, /*exception info*/nullptr,
68 &ArgErr)))
69 {
70 return retResult;
71 }
72 if (variantFunctionAsString.vt != VT_BSTR)
73 {
74 return retResult;
75 }
76 std::wstring functionAsString = variantFunctionAsString.bstrVal;
77 auto bodyBeginsAt = functionAsString.find(L'{');
78 if (bodyBeginsAt == std::wstring::npos)
79 {
80 return retResult;
81 }
82 // eat spaces
83 while (::iswspace(functionAsString[++bodyBeginsAt]));
84 auto bodyEndsAt = functionAsString.rfind(L'}');
85 if (bodyEndsAt == std::wstring::npos || bodyEndsAt < bodyBeginsAt)
86 {
87 return retResult;
88 }
89 // eat spaces
90 while(::iswspace(functionAsString[--bodyEndsAt]));
91 retResult.first = functionAsString.substr(bodyBeginsAt, bodyEndsAt - bodyB eginsAt + 1);
92 }
93 return retResult;
94 } 62 }
95 } 63 }
96 64
97 // ============================================================================ 65 // ============================================================================
98 // CFilterElementHideAttrSelector 66 // CFilterElementHideAttrSelector
99 // ============================================================================ 67 // ============================================================================
100 68
101 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N ONE), m_pos(POS_NONE), m_bstrAttr(NULL) 69 CFilterElementHideAttrSelector::CFilterElementHideAttrSelector() : m_type(TYPE_N ONE), m_pos(POS_NONE), m_bstrAttr(NULL)
102 { 70 {
103 } 71 }
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 if ((hr != S_OK) || (tagName != CComBSTR(m_tag))) 322 if ((hr != S_OK) || (tagName != CComBSTR(m_tag)))
355 { 323 {
356 return false; 324 return false;
357 } 325 }
358 } 326 }
359 327
360 // Check attributes 328 // Check attributes
361 for (std::vector<CFilterElementHideAttrSelector>::const_iterator attrIt = m_at tributeSelectors.begin(); 329 for (std::vector<CFilterElementHideAttrSelector>::const_iterator attrIt = m_at tributeSelectors.begin();
362 attrIt != m_attributeSelectors.end(); ++ attrIt) 330 attrIt != m_attributeSelectors.end(); ++ attrIt)
363 { 331 {
364 ATL::CString value; 332 ATL::CString value;
Felix Dahlke 2014/11/27 13:58:46 This change seems unrelated, hm? Plus we're still
sergei 2014/11/27 14:47:25 Yes, it's not important here, just was irritating
Felix Dahlke 2014/11/27 14:57:27 Yes I agree. It's just that we should avoid unrela
365 bool attrFound = false; 333 bool attrFound = false;
366 if (attrIt->m_type == CFilterElementHideAttrType::STYLE) 334 if (attrIt->m_type == CFilterElementHideAttrType::STYLE)
367 { 335 {
368 CComPtr<IHTMLStyle> pStyle; 336 CComPtr<IHTMLStyle> pStyle;
369 if (SUCCEEDED(pEl->get_style(&pStyle)) && pStyle) 337 if (SUCCEEDED(pEl->get_style(&pStyle)) && pStyle)
370 { 338 {
371 CComBSTR bstrStyle; 339 CComBSTR bstrStyle;
372 340
373 if (SUCCEEDED(pStyle->get_cssText(&bstrStyle)) && bstrStyle) 341 if (SUCCEEDED(pStyle->get_cssText(&bstrStyle)) && bstrStyle)
374 { 342 {
(...skipping 16 matching lines...) Expand all
391 { 359 {
392 CComBSTR bstrId; 360 CComBSTR bstrId;
393 if (SUCCEEDED(pEl->get_id(&bstrId)) && bstrId) 361 if (SUCCEEDED(pEl->get_id(&bstrId)) && bstrId)
394 { 362 {
395 value = bstrId; 363 value = bstrId;
396 attrFound = true; 364 attrFound = true;
397 } 365 }
398 } 366 }
399 else 367 else
400 { 368 {
401 auto attribute = GetHtmlElementAttribute(*pEl, attrIt->m_bstrAttr); 369 auto attributeValue = GetHtmlElementAttribute(*pEl, attrIt->m_bstrAttr);
402 if (attrFound = attribute.second) 370 if (attrFound = attributeValue.isAttributeFound)
403 { 371 {
404 value = ToCString(attribute.first); 372 value = ToCString(attributeValue.attributeValue);
405 } 373 }
406 } 374 }
407 375
408 if (attrFound) 376 if (attrFound)
409 { 377 {
410 if (attrIt->m_pos == CFilterElementHideAttrPos::EXACT) 378 if (attrIt->m_pos == CFilterElementHideAttrPos::EXACT)
411 { 379 {
412 // TODO: IE rearranges the style attribute completely. Figure out if any thing can be done about it. 380 // TODO: IE rearranges the style attribute completely. Figure out if any thing can be done about it.
413 if (value != attrIt->m_value) 381 if (value != attrIt->m_value)
414 return false; 382 return false;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 450
483 451
484 // ============================================================================ 452 // ============================================================================
485 // CPluginFilter 453 // CPluginFilter
486 // ============================================================================ 454 // ============================================================================
487 455
488 CPluginFilter::CPluginFilter(const CString& dataPath) : m_dataPath(dataPath) 456 CPluginFilter::CPluginFilter(const CString& dataPath) : m_dataPath(dataPath)
489 { 457 {
490 m_contentMapText[CFilter::contentTypeDocument] = "DOCUMENT"; 458 m_contentMapText[CFilter::contentTypeDocument] = "DOCUMENT";
491 m_contentMapText[CFilter::contentTypeObject] = "OBJECT"; 459 m_contentMapText[CFilter::contentTypeObject] = "OBJECT";
492 m_contentMapText[CFilter::contentTypeObjectSubrequest] = "OBJECT-SUBREQUEST"; 460 m_contentMapText[CFilter::contentTypeObjectSubrequest] = "OBJECT_SUBREQUEST";
493 m_contentMapText[CFilter::contentTypeImage] = "IMAGE"; 461 m_contentMapText[CFilter::contentTypeImage] = "IMAGE";
494 m_contentMapText[CFilter::contentTypeScript] = "SCRIPT"; 462 m_contentMapText[CFilter::contentTypeScript] = "SCRIPT";
495 m_contentMapText[CFilter::contentTypeOther] = "OTHER"; 463 m_contentMapText[CFilter::contentTypeOther] = "OTHER";
496 m_contentMapText[CFilter::contentTypeUnknown] = "OTHER"; 464 m_contentMapText[CFilter::contentTypeUnknown] = "OTHER";
497 m_contentMapText[CFilter::contentTypeSubdocument] = "SUBDOCUMENT"; 465 m_contentMapText[CFilter::contentTypeSubdocument] = "SUBDOCUMENT";
498 m_contentMapText[CFilter::contentTypeStyleSheet] = "STYLESHEET"; 466 m_contentMapText[CFilter::contentTypeStyleSheet] = "STYLESHEET";
499 m_contentMapText[CFilter::contentTypeXmlHttpRequest] = "XMLHTTPREQUEST"; 467 m_contentMapText[CFilter::contentTypeXmlHttpRequest] = "XMLHTTPREQUEST";
500 468
501 ClearFilters(); 469 ClearFilters();
502 } 470 }
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 CPluginDebug::DebugResultBlocking(type, srcCString, domain); 739 CPluginDebug::DebugResultBlocking(type, srcCString, domain);
772 #endif 740 #endif
773 } 741 }
774 return true; 742 return true;
775 } 743 }
776 #ifdef ENABLE_DEBUG_RESULT 744 #ifdef ENABLE_DEBUG_RESULT
777 CPluginDebug::DebugResultIgnoring(type, srcCString, domain); 745 CPluginDebug::DebugResultIgnoring(type, srcCString, domain);
778 #endif 746 #endif
779 return false; 747 return false;
780 } 748 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld