aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/blockerrule.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/webengine/blockerrule.cpp')
-rw-r--r--src/webengine/blockerrule.cpp105
1 files changed, 87 insertions, 18 deletions
diff --git a/src/webengine/blockerrule.cpp b/src/webengine/blockerrule.cpp
index 8ecc1af..4146e85 100644
--- a/src/webengine/blockerrule.cpp
+++ b/src/webengine/blockerrule.cpp
@@ -48,11 +48,41 @@ BlockerRule::BlockerRule(QString rule, QObject *parent) :
QString opts = pattern.mid(pattern.indexOf("$")+1);
for(QString opt : opts.split(',')) {
if(opt.endsWith("script")) {
- m_blacklistOptions.setFlag(RuleOption::script, true);
- m_whitelistOptions.setFlag(RuleOption::script, opt.startsWith("~"));
+ if(opt.startsWith("~")) {
+ m_whitelistOptions.setFlag(RuleOption::script, true);
+ } else {
+ m_blacklistOptions.setFlag(RuleOption::script, true);
+ }
} else if(opt.endsWith("image")) {
- m_blacklistOptions.setFlag(RuleOption::image, true);
- m_whitelistOptions.setFlag(RuleOption::image, opt.startsWith("~"));
+ if(opt.startsWith("~")) {
+ m_whitelistOptions.setFlag(RuleOption::image, true);
+ } else {
+ m_blacklistOptions.setFlag(RuleOption::image, true);
+ }
+ } else if(opt.endsWith("stylesheet")) {
+ if(opt.startsWith("~")) {
+ m_whitelistOptions.setFlag(RuleOption::stylesheet, true);
+ } else {
+ m_blacklistOptions.setFlag(RuleOption::stylesheet, true);
+ }
+ } else if(opt.endsWith("object")) {
+ if(opt.startsWith("~")) {
+ m_whitelistOptions.setFlag(RuleOption::object, true);
+ } else {
+ m_blacklistOptions.setFlag(RuleOption::object, true);
+ }
+ } else if(opt.endsWith("object-subrequest")) {
+ if(opt.startsWith("~")) {
+ m_whitelistOptions.setFlag(RuleOption::objectsubrequest, true);
+ } else {
+ m_blacklistOptions.setFlag(RuleOption::objectsubrequest, true);
+ }
+ } else if(opt.endsWith("subdocument")) {
+ if(opt.startsWith("~")) {
+ m_whitelistOptions.setFlag(RuleOption::subdocument, true);
+ } else {
+ m_blacklistOptions.setFlag(RuleOption::subdocument, true);
+ }
}
}
@@ -89,7 +119,13 @@ bool BlockerRule::match(const QWebEngineUrlRequestInfo &info)
case DomainMatch:
if(info.requestUrl().host() == m_domain) {
- shouldBlock = true;
+ if(matchOptions(info, m_whitelistOptions)) {
+ shouldBlock = false;
+ } else {
+ if(matchOptions(info, m_blacklistOptions)) {
+ shouldBlock = true;
+ }
+ }
}
break;
@@ -99,19 +135,6 @@ bool BlockerRule::match(const QWebEngineUrlRequestInfo &info)
break;
}
- if(shouldBlock) {
- if(m_whitelistOptions.testFlag(RuleOption::image) && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage) {
- shouldBlock = false;
- }
- }
- if(shouldBlock) {
- if(m_blacklistOptions.testFlag(RuleOption::image) && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage) {
- shouldBlock = true;
- } else {
- shouldBlock = false;
- }
- }
-
return shouldBlock;
}
@@ -163,3 +186,49 @@ QString BlockerRule::fromWildcardMatch(const QString &pattern)
return parsed;
}
+
+bool BlockerRule::matchOptions(const QWebEngineUrlRequestInfo &info, const RuleOptions &options)
+{
+ // no options are defined
+ if(options == 0) {
+ return false;
+ }
+
+ bool ret = false;
+ switch (info.resourceType()) {
+ case QWebEngineUrlRequestInfo::ResourceTypeScript:
+ if(options.testFlag(RuleOption::script)) {
+ ret = true;
+ }
+ break;
+ case QWebEngineUrlRequestInfo::ResourceTypeImage:
+ if(options.testFlag(RuleOption::image)) {
+ ret = true;
+ }
+ break;
+ case QWebEngineUrlRequestInfo::ResourceTypeStylesheet:
+ if(options.testFlag(RuleOption::stylesheet)) {
+ ret = true;
+ }
+ break;
+ case QWebEngineUrlRequestInfo::ResourceTypeObject:
+ if(options.testFlag(RuleOption::object)) {
+ ret = true;
+ }
+ break;
+ case QWebEngineUrlRequestInfo::ResourceTypePluginResource:
+ if(options.testFlag(RuleOption::objectsubrequest)) {
+ ret = true;
+ }
+ break;
+ case QWebEngineUrlRequestInfo::ResourceTypeSubFrame:
+ if(options.testFlag(RuleOption::subdocument)) {
+ ret = true;
+ }
+ break;
+ default:
+ break;
+ }
+
+ return ret;
+}