/*******************************************************************************
**
** smolbote: yet another qute browser
** Copyright (C) 2017 Xian Nox
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see .
**
******************************************************************************/
#include "filter.h"
Filter::Filter(const QString &domain, const QString &request, ResourceRules rules, bool shouldBlock, Filter *parentItem)
{
m_parentItem = parentItem;
m_domainUrl.setPattern(domain);
m_requestUrl.setPattern(request);
m_rules = rules;
m_shouldBlock = shouldBlock;
m_valid = false;
}
Filter::~Filter()
{
qDeleteAll(m_children);
}
void Filter::enable()
{
m_valid = true;
}
Filter *Filter::parentItem()
{
return m_parentItem;
}
void Filter::appendChild(Filter *child)
{
m_children.append(child);
}
Filter *Filter::child(int row)
{
return m_children.value(row);
}
int Filter::childCount() const
{
return m_children.count();
}
QString Filter::domain() const
{
return m_domainUrl.pattern();
}
QString Filter::request() const
{
return m_requestUrl.pattern();
}
bool Filter::isBlocking()
{
return m_shouldBlock;
}
QString Filter::allowedTypes() const
{
return flags(m_rules.allowed);
}
QString Filter::blockedTypes() const
{
return flags(m_rules.blocked);
}
int Filter::row() const
{
if(m_parentItem) {
return m_parentItem->m_children.indexOf(const_cast(this));
}
return 0;
}
bool Filter::hasDomainMatch(const QString &string)
{
return m_domainUrl.hasMatch(string);
}
bool Filter::hasRuleMatch(const QString &string)
{
return m_requestUrl.hasMatch(string);
}
bool Filter::shouldBlock(const QWebEngineUrlRequestInfo &info)
{
if(!m_valid) {
return false;
}
// Check options
switch (info.resourceType()) {
case QWebEngineUrlRequestInfo::ResourceTypeMainFrame:
return testFlag(MainFrame);
case QWebEngineUrlRequestInfo::ResourceTypeSubFrame:
return testFlag(SubFrame);
case QWebEngineUrlRequestInfo::ResourceTypeStylesheet:
return testFlag(Stylesheet);
case QWebEngineUrlRequestInfo::ResourceTypeScript:
return testFlag(Script);
case QWebEngineUrlRequestInfo::ResourceTypeImage:
return testFlag(Image);
case QWebEngineUrlRequestInfo::ResourceTypeFontResource:
return testFlag(Font);
case QWebEngineUrlRequestInfo::ResourceTypeSubResource:
return testFlag(SubResource);
case QWebEngineUrlRequestInfo::ResourceTypeObject:
return testFlag(Object);
case QWebEngineUrlRequestInfo::ResourceTypeMedia:
return testFlag(Media);
case QWebEngineUrlRequestInfo::ResourceTypeWorker:
return testFlag(Worker);
case QWebEngineUrlRequestInfo::ResourceTypeSharedWorker:
return testFlag(SharedWorker);
case QWebEngineUrlRequestInfo::ResourceTypePrefetch:
return testFlag(Prefetch);
case QWebEngineUrlRequestInfo::ResourceTypeFavicon:
return testFlag(Favicon);
case QWebEngineUrlRequestInfo::ResourceTypeXhr:
return testFlag(Xhr);
case QWebEngineUrlRequestInfo::ResourceTypePing:
return testFlag(Ping);
case QWebEngineUrlRequestInfo::ResourceTypeServiceWorker:
return testFlag(ServiceWorker);
case QWebEngineUrlRequestInfo::ResourceTypeCspReport:
return testFlag(CspReport);
case QWebEngineUrlRequestInfo::ResourceTypePluginResource:
return testFlag(PluginResource);
case QWebEngineUrlRequestInfo::ResourceTypeUnknown:
break;
}
return m_shouldBlock;
}
bool Filter::isValid()
{
return m_valid;
}
bool Filter::testFlag(ResourceType flag)
{
if(m_rules.allowed.testFlag(flag)) {
return false;
} else if(m_rules.blocked.testFlag(flag)) {
return true;
} else {
return m_shouldBlock;
}
}
QString Filter::flags(Resources f) const
{
QStringList r;
if(f.testFlag(MainFrame)) {
r << QObject::tr("Main Frame");
}
if(f.testFlag(SubFrame)) {
r << QObject::tr("Sub Frame");
}
if(f.testFlag(Stylesheet)) {
r << QObject::tr("Stylesheet");
}
if(f.testFlag(Script)) {
r << QObject::tr("Script");
}
if(f.testFlag(Image)) {
r << QObject::tr("Image");
}
if(f.testFlag(Font)) {
r << QObject::tr("Font");
}
if(f.testFlag(SubResource)) {
r << QObject::tr("Sub Resource");
}
if(f.testFlag(Object)) {
r << QObject::tr("Object");
}
if(f.testFlag(Media)) {
r << QObject::tr("Media");
}
if(f.testFlag(Worker)) {
r << QObject::tr("Worker");
}
if(f.testFlag(SharedWorker)) {
r << QObject::tr("Shared Worker");
}
if(f.testFlag(Prefetch)) {
r << QObject::tr("Prefetch");
}
if(f.testFlag(Favicon)) {
r << QObject::tr("Favicon");
}
if(f.testFlag(Xhr)) {
r << QObject::tr("Xhr");
}
if(f.testFlag(Ping)) {
r << QObject::tr("Ping");
}
if(f.testFlag(ServiceWorker)) {
r << QObject::tr("Service Worker");
}
if(f.testFlag(CspReport)) {
r << QObject::tr("Csp Report");
}
if(f.testFlag(PluginResource)) {
r << QObject::tr("Plugin Resource");
}
if(f.testFlag(Unknown)) {
r << QObject::tr("Unknown");
}
return r.join(',');
}