diff --git a/src/lib/adblock/adblockrule.cpp b/src/lib/adblock/adblockrule.cpp index 150415099..3ac5d8630 100644 --- a/src/lib/adblock/adblockrule.cpp +++ b/src/lib/adblock/adblockrule.cpp @@ -225,55 +225,9 @@ bool AdBlockRule::networkMatch(const QWebEngineUrlRequestInfo &request, const QS return false; } - // Check object restrictions - if (hasOption(ObjectOption) && !matchObject(request)) { + // Check type restrictions + if (((m_exceptions | m_options) & TypeOptions) && !matchType(request)) return false; - } - - // Check subdocument restriction - if (hasOption(SubdocumentOption) && !matchSubdocument(request)) { - return false; - } - - // Check xmlhttprequest restriction - if (hasOption(XMLHttpRequestOption) && !matchXmlHttpRequest(request)) { - return false; - } - - // Check image restriction - if (hasOption(ImageOption) && !matchImage(request)) { - return false; - } - - // Check script restriction - if (hasOption(ScriptOption) && !matchScript(request)) { - return false; - } - - // Check stylesheet restriction - if (hasOption(StyleSheetOption) && !matchStyleSheet(request)) { - return false; - } - - // Check object-subrequest restriction - if (hasOption(ObjectSubrequestOption) && !matchObjectSubrequest(request)) { - return false; - } - - // Check ping restriction - if (hasOption(PingOption) && !matchPing(request)) { - return false; - } - - // Check media restriction - if (hasOption(MediaOption) && !matchMedia(request)) { - return false; - } - - // Check font restriction - if (hasOption(FontOption) && !matchFont(request)) { - return false; - } } return matched; @@ -332,88 +286,60 @@ bool AdBlockRule::matchThirdParty(const QWebEngineUrlRequestInfo &request) const return hasException(ThirdPartyOption) ? !match : match; } -bool AdBlockRule::matchObject(const QWebEngineUrlRequestInfo &request) const +bool AdBlockRule::matchType(const QWebEngineUrlRequestInfo &request) const { - bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeObject; - - return hasException(ObjectOption) ? !match : match; -} - -bool AdBlockRule::matchSubdocument(const QWebEngineUrlRequestInfo &request) const -{ - bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeSubFrame; - - return hasException(SubdocumentOption) ? !match : match; -} - -bool AdBlockRule::matchXmlHttpRequest(const QWebEngineUrlRequestInfo &request) const -{ - bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeXhr; - - return hasException(XMLHttpRequestOption) ? !match : match; -} - -bool AdBlockRule::matchImage(const QWebEngineUrlRequestInfo &request) const -{ - bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage; - - return hasException(ImageOption) ? !match : match; -} - -bool AdBlockRule::matchScript(const QWebEngineUrlRequestInfo &request) const -{ - bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeScript; - - return hasException(ScriptOption) ? !match : match; -} - -bool AdBlockRule::matchStyleSheet(const QWebEngineUrlRequestInfo &request) const -{ - bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeStylesheet; - - return hasException(StyleSheetOption) ? !match : match; -} - -bool AdBlockRule::matchObjectSubrequest(const QWebEngineUrlRequestInfo &request) const -{ - bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypePluginResource; - - return hasException(ObjectSubrequestOption) ? !match : match; -} - -bool AdBlockRule::matchPing(const QWebEngineUrlRequestInfo &request) const -{ - bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypePing; - - return hasException(PingOption) ? !match : match; -} - -bool AdBlockRule::matchMedia(const QWebEngineUrlRequestInfo &request) const -{ - bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMedia; - - return hasException(MediaOption) ? !match : match; -} - -bool AdBlockRule::matchFont(const QWebEngineUrlRequestInfo &request) const -{ - bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeFontResource; - - return hasException(FontOption) ? !match : match; -} - -bool AdBlockRule::matchOther(const QWebEngineUrlRequestInfo &request) const -{ - bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeFontResource - || request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeSubResource - || request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeWorker - || request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeSharedWorker - || request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypePrefetch - || request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeFavicon - || request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeServiceWorker - || request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeUnknown; - - return hasException(MediaOption) ? !match : match; + RuleOption type; + switch (request.resourceType()) { + case QWebEngineUrlRequestInfo::ResourceTypeMainFrame: + type = DocumentOption; + break; + case QWebEngineUrlRequestInfo::ResourceTypeSubFrame: + type = SubdocumentOption; + break; + case QWebEngineUrlRequestInfo::ResourceTypeStylesheet: + type = StyleSheetOption; + break; + case QWebEngineUrlRequestInfo::ResourceTypeScript: + type = ScriptOption; + break; + case QWebEngineUrlRequestInfo::ResourceTypeImage: + type = ImageOption; + break; + case QWebEngineUrlRequestInfo::ResourceTypeFontResource: + type = FontOption; + break; + case QWebEngineUrlRequestInfo::ResourceTypeObject: + type = ObjectOption; + break; + case QWebEngineUrlRequestInfo::ResourceTypeMedia: + type = MediaOption; + break; + case QWebEngineUrlRequestInfo::ResourceTypeXhr: + type = XMLHttpRequestOption; + break; + case QWebEngineUrlRequestInfo::ResourceTypePing: + type = PingOption; + break; + case QWebEngineUrlRequestInfo::ResourceTypePluginResource: + type = ObjectSubrequestOption; + break; + case QWebEngineUrlRequestInfo::ResourceTypeSubResource: + case QWebEngineUrlRequestInfo::ResourceTypeWorker: + case QWebEngineUrlRequestInfo::ResourceTypeSharedWorker: + case QWebEngineUrlRequestInfo::ResourceTypePrefetch: + case QWebEngineUrlRequestInfo::ResourceTypeFavicon: + case QWebEngineUrlRequestInfo::ResourceTypeServiceWorker: + case QWebEngineUrlRequestInfo::ResourceTypeCspReport: + case QWebEngineUrlRequestInfo::ResourceTypeNavigationPreloadMainFrame: + case QWebEngineUrlRequestInfo::ResourceTypeNavigationPreloadSubFrame: + case QWebEngineUrlRequestInfo::ResourceTypeUnknown: + default: + type = OtherOption; + break; + } + if (!m_exceptions) + return m_options.testFlag(type); + return !m_exceptions.testFlag(type); } void AdBlockRule::parseFilter() @@ -529,6 +455,11 @@ void AdBlockRule::parseFilter() setException(OtherOption, option.startsWith(QL1C('~'))); ++handledOptions; } + else if (option == QL1S("popup")) { + // doesn't do anything yet + setOption(PopupOption); + ++handledOptions; + } else if (option == QL1S("document") && m_isException) { setOption(DocumentOption); ++handledOptions; diff --git a/src/lib/adblock/adblockrule.h b/src/lib/adblock/adblockrule.h index 921795538..adf6d0269 100644 --- a/src/lib/adblock/adblockrule.h +++ b/src/lib/adblock/adblockrule.h @@ -95,17 +95,8 @@ public: bool matchDomain(const QString &domain) const; bool matchThirdParty(const QWebEngineUrlRequestInfo &request) const; - bool matchObject(const QWebEngineUrlRequestInfo &request) const; - bool matchSubdocument(const QWebEngineUrlRequestInfo &request) const; - bool matchXmlHttpRequest(const QWebEngineUrlRequestInfo &request) const; - bool matchImage(const QWebEngineUrlRequestInfo &request) const; - bool matchScript(const QWebEngineUrlRequestInfo &request) const; - bool matchStyleSheet(const QWebEngineUrlRequestInfo &request) const; - bool matchObjectSubrequest(const QWebEngineUrlRequestInfo &request) const; - bool matchPing(const QWebEngineUrlRequestInfo &request) const; - bool matchMedia(const QWebEngineUrlRequestInfo &request) const; - bool matchFont(const QWebEngineUrlRequestInfo &request) const; - bool matchOther(const QWebEngineUrlRequestInfo &request) const; + + bool matchType(const QWebEngineUrlRequestInfo &request) const; protected: bool stringMatch(const QString &domain, const QString &encodedUrl) const; @@ -125,24 +116,38 @@ private: }; enum RuleOption { - NoOption = 0, - DomainRestrictedOption = 1, - ThirdPartyOption = 2, - ObjectOption = 4, - SubdocumentOption = 8, - XMLHttpRequestOption = 16, - ImageOption = 32, - ScriptOption = 64, - StyleSheetOption = 128, - ObjectSubrequestOption = 256, - PingOption = 512, - MediaOption = 1024, - FontOption = 2048, - OtherOption = 4096, + NoOption = 0, + DomainRestrictedOption = 1, + ThirdPartyOption = 1 << 1, + + ObjectOption = 1 << 2, + SubdocumentOption = 1 << 3, + XMLHttpRequestOption = 1 << 4, + ImageOption = 1 << 5, + ScriptOption = 1 << 6, + StyleSheetOption = 1 << 7, + ObjectSubrequestOption = 1 << 8, + PingOption = 1 << 9, + MediaOption = 1 << 10, + FontOption = 1 << 11, + OtherOption = 1 << 12, + TypeOptions = ObjectOption + | SubdocumentOption + | XMLHttpRequestOption + | ImageOption + | ScriptOption + | StyleSheetOption + | ObjectSubrequestOption + | PingOption + | MediaOption + | FontOption + | OtherOption, + + PopupOption = 1 << 13, // Exception only options - DocumentOption = 8192, - ElementHideOption = 16384 + DocumentOption = 1 << 20, + ElementHideOption = 1 << 21, }; Q_DECLARE_FLAGS(RuleOptions, RuleOption)