From 88c5b271e235e077d975a23968571d8cd86ec7b4 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Mon, 5 Oct 2015 20:43:05 +0200 Subject: [PATCH] AdBlockRule: Add support for script, stylesheet and object-subrequest --- src/lib/adblock/adblockrule.cpp | 51 +++++++++++++++++++++++++++++++++ src/lib/adblock/adblockrule.h | 10 +++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/lib/adblock/adblockrule.cpp b/src/lib/adblock/adblockrule.cpp index cb8814d98..022dc47a0 100644 --- a/src/lib/adblock/adblockrule.cpp +++ b/src/lib/adblock/adblockrule.cpp @@ -248,6 +248,21 @@ bool AdBlockRule::networkMatch(const QWebEngineUrlRequestInfo &request, const QS 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; + } } return matched; @@ -345,6 +360,27 @@ bool AdBlockRule::matchImage(const QWebEngineUrlRequestInfo &request) const 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::ResourceTypeSubResource; + + return hasException(ObjectSubrequestOption) ? !match : match; +} + void AdBlockRule::parseFilter() { QString parsedLine = m_filter; @@ -423,6 +459,21 @@ void AdBlockRule::parseFilter() setException(ImageOption, option.startsWith(QL1C('~'))); ++handledOptions; } + else if (option.endsWith(QL1S("script"))) { + setOption(ScriptOption); + setException(ScriptOption, option.startsWith(QL1C('~'))); + ++handledOptions; + } + else if (option.endsWith(QL1S("stylesheet"))) { + setOption(StyleSheetOption); + setException(StyleSheetOption, option.startsWith(QL1C('~'))); + ++handledOptions; + } + else if (option.endsWith(QL1S("object-subrequest"))) { + setOption(ObjectSubrequestOption); + setException(ObjectSubrequestOption, option.startsWith(QL1C('~'))); + ++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 5ec268bbc..84be0814a 100644 --- a/src/lib/adblock/adblockrule.h +++ b/src/lib/adblock/adblockrule.h @@ -99,6 +99,9 @@ public: 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; protected: bool stringMatch(const QString &domain, const QString &encodedUrl) const; @@ -123,10 +126,13 @@ private: SubdocumentOption = 8, XMLHttpRequestOption = 16, ImageOption = 32, + ScriptOption = 64, + StyleSheetOption = 128, + ObjectSubrequestOption = 256, // Exception only options - DocumentOption = 64, - ElementHideOption = 128 + DocumentOption = 1024, + ElementHideOption = 2048 }; Q_DECLARE_FLAGS(RuleOptions, RuleOption)