1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

Add preliminary Cookie support

Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
Juraj Oravec 2022-09-26 23:36:23 +02:00
parent 90ec7bcf99
commit 3b259711f4
Signed by: SGOrava
GPG Key ID: 13660A3F1D9F093B
4 changed files with 59 additions and 31 deletions

View File

@ -21,6 +21,7 @@
#include "autosaver.h" #include "autosaver.h"
#include "settings.h" #include "settings.h"
#include "qztools.h" #include "qztools.h"
#include "sitesettingsmanager.h"
#include <QNetworkCookie> #include <QNetworkCookie>
#include <QWebEngineProfile> #include <QWebEngineProfile>
@ -134,25 +135,21 @@ void CookieJar::slotCookieRemoved(const QNetworkCookie &cookie)
bool CookieJar::cookieFilter(const QWebEngineCookieStore::FilterRequest &request) const bool CookieJar::cookieFilter(const QWebEngineCookieStore::FilterRequest &request) const
{ {
if (!m_allowCookies) { auto result = mApp->siteSettingsManager()->getPermission(SiteSettingsManager::poAllowCookies, request.origin);
bool result = listMatchesDomain(m_whitelist, request.origin.host());
if (!result) { if (!m_allowCookies && (result != SiteSettingsManager::Allow)) {
#ifdef COOKIE_DEBUG #ifdef COOKIE_DEBUG
qDebug() << "not in whitelist" << request.origin; qDebug() << "Cookies not allowed" << request.origin;
#endif #endif
return false; return false;
} }
}
if (m_allowCookies) { if (m_allowCookies && (result == SiteSettingsManager::Deny)) {
bool result = listMatchesDomain(m_blacklist, request.origin.host());
if (result) {
#ifdef COOKIE_DEBUG #ifdef COOKIE_DEBUG
qDebug() << "found in blacklist" << request.origin.host(); qDebug() << "Cookies denied" << request.origin;
#endif #endif
return false; return false;
} }
}
if (m_filterThirdParty && request.thirdParty) { if (m_filterThirdParty && request.thirdParty) {
#ifdef COOKIE_DEBUG #ifdef COOKIE_DEBUG
@ -168,24 +165,20 @@ bool CookieJar::rejectCookie(const QString &domain, const QNetworkCookie &cookie
{ {
Q_UNUSED(domain) Q_UNUSED(domain)
if (!m_allowCookies) { auto result = mApp->siteSettingsManager()->getPermission(SiteSettingsManager::poAllowCookies, cookieDomain);
bool result = listMatchesDomain(m_whitelist, cookieDomain);
if (!result) { if (!m_allowCookies && (result != SiteSettingsManager::Allow)) {
#ifdef COOKIE_DEBUG #ifdef COOKIE_DEBUG
qDebug() << "not in whitelist" << cookie; qDebug() << "Cookies not allowed" << cookie;
#endif #endif
return true; return false;
}
} }
if (m_allowCookies) { if (m_allowCookies && (result == SiteSettingsManager::Deny)) {
bool result = listMatchesDomain(m_blacklist, cookieDomain);
if (result) {
#ifdef COOKIE_DEBUG #ifdef COOKIE_DEBUG
qDebug() << "found in blacklist" << cookie; qDebug() << "Cookies denied" << cookie;
#endif #endif
return true; return false;
}
} }
#ifdef QTWEBENGINE_DISABLED #ifdef QTWEBENGINE_DISABLED

View File

@ -353,6 +353,8 @@ void SiteInfo::addSiteSettings()
SiteInfoPermissionItem *item = addPermissionOption(siteSettings.features[feature]); SiteInfoPermissionItem *item = addPermissionOption(siteSettings.features[feature]);
item->setFeature(feature); item->setFeature(feature);
} }
SiteInfoPermissionItem *item = addPermissionOption(siteSettings.AllowCookies);
item->setOption(SiteSettingsManager::poAllowCookies);
} }
void SiteInfo::saveSiteSettings() void SiteInfo::saveSiteSettings()
@ -370,7 +372,8 @@ void SiteInfo::saveSiteSettings()
auto* item = static_cast<SiteInfoPermissionItem*>(ui->listPermissions->itemWidget(ui->listPermissions->item(index))); auto* item = static_cast<SiteInfoPermissionItem*>(ui->listPermissions->itemWidget(ui->listPermissions->item(index)));
siteSettings.features[supportedFeatures[i]] = item->permission(); siteSettings.features[supportedFeatures[i]] = item->permission();
} }
siteSettings.AllowCookies = SiteSettingsManager::Default; auto* item = static_cast<SiteInfoPermissionItem*>(ui->listPermissions->itemWidget(ui->listPermissions->item(index++)));
siteSettings.AllowCookies = item->permission();
siteSettings.ZoomLevel = -1; siteSettings.ZoomLevel = -1;
siteSettings.server = m_baseUrl.host(); siteSettings.server = m_baseUrl.host();

View File

@ -273,7 +273,7 @@ void SiteSettingsManager::setOption(const QWebEngineSettings::WebAttribute& attr
setOption(webAttributeToSqlColumn(attribute), url, value); setOption(webAttributeToSqlColumn(attribute), url, value);
} }
SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QString &column, const QUrl& url) SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QString& column, const QString& host)
{ {
if (column.isEmpty()) { if (column.isEmpty()) {
return Deny; return Deny;
@ -281,7 +281,7 @@ SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QString
QSqlQuery query(SqlDatabase::instance()->database()); QSqlQuery query(SqlDatabase::instance()->database());
query.prepare(QSL("SELECT %1 FROM %2 WHERE server=?").arg(column, sqlTable())); query.prepare(QSL("SELECT %1 FROM %2 WHERE server=?").arg(column, sqlTable()));
query.addBindValue(url.host()); query.addBindValue(host);
query.exec(); query.exec();
if (query.next()) { if (query.next()) {
@ -293,19 +293,39 @@ SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QString
return Default; return Default;
} }
SiteSettingsManager::Permission SiteSettingsManager::getPermission(const SiteSettingsManager::PageOptions option, const QString& host)
{
return getPermission(optionToSqlColumn(option), host);
}
SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QWebEnginePage::Feature& feature, const QString& host)
{
return getPermission(featureToSqlColumn(feature), host);
}
SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QWebEngineSettings::WebAttribute& attribute, const QString& host)
{
return getPermission(webAttributeToSqlColumn(attribute), host);
}
SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QString &column, const QUrl& url)
{
return getPermission(column, url.host());
}
SiteSettingsManager::Permission SiteSettingsManager::getPermission(const SiteSettingsManager::PageOptions option, const QUrl& url) SiteSettingsManager::Permission SiteSettingsManager::getPermission(const SiteSettingsManager::PageOptions option, const QUrl& url)
{ {
return getPermission(optionToSqlColumn(option), url); return getPermission(optionToSqlColumn(option), url.host());
} }
SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QWebEnginePage::Feature& feature, const QUrl& url) SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QWebEnginePage::Feature& feature, const QUrl& url)
{ {
return getPermission(featureToSqlColumn(feature), url); return getPermission(featureToSqlColumn(feature), url.host());
} }
SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QWebEngineSettings::WebAttribute& attribute, const QUrl& url) SiteSettingsManager::Permission SiteSettingsManager::getPermission(const QWebEngineSettings::WebAttribute& attribute, const QUrl& url)
{ {
return getPermission(webAttributeToSqlColumn(attribute), url); return getPermission(webAttributeToSqlColumn(attribute), url.host());
} }
QString SiteSettingsManager::optionToSqlColumn(const SiteSettingsManager::PageOptions &option) QString SiteSettingsManager::optionToSqlColumn(const SiteSettingsManager::PageOptions &option)
@ -324,9 +344,16 @@ QString SiteSettingsManager::optionToSqlColumn(const SiteSettingsManager::PageOp
SiteSettingsManager::Permission SiteSettingsManager::getDefaultPermission(const SiteSettingsManager::PageOptions& option) SiteSettingsManager::Permission SiteSettingsManager::getDefaultPermission(const SiteSettingsManager::PageOptions& option)
{ {
switch (option) { switch (option) {
case poAllowCookies: {
Settings settings;
settings.beginGroup("Cookie-Settings");
auto defaultCookies = settings.value("allowCookies", true).toBool() ? Allow : Deny;
settings.endGroup();
return defaultCookies;
}
// so far not implemented // so far not implemented
case poZoomLevel: case poZoomLevel:
case poAllowCookies:
default: default:
qWarning() << "Unknown option:" << option; qWarning() << "Unknown option:" << option;
return Deny; return Deny;

View File

@ -84,6 +84,11 @@ public:
void setJavascript(const QUrl &url, const int value); void setJavascript(const QUrl &url, const int value);
void setImages(const QUrl &url, const int value); void setImages(const QUrl &url, const int value);
Permission getPermission(const QString &column, const QString &host);
Permission getPermission(const PageOptions option, const QString &host);
Permission getPermission(const QWebEnginePage::Feature &feature, const QString &host);
Permission getPermission(const QWebEngineSettings::WebAttribute &attribute, const QString &host);
Permission getPermission(const QString &column, const QUrl &url); Permission getPermission(const QString &column, const QUrl &url);
Permission getPermission(const PageOptions option, const QUrl &url); Permission getPermission(const PageOptions option, const QUrl &url);
Permission getPermission(const QWebEnginePage::Feature &feature, const QUrl &url); Permission getPermission(const QWebEnginePage::Feature &feature, const QUrl &url);