This commit is contained in:
Martin Rotter 2022-01-05 09:45:43 +01:00
parent 94c9dd71c4
commit 1811a03414
3 changed files with 76 additions and 6 deletions

View File

@ -26,7 +26,7 @@
<url type="donation">https://github.com/sponsors/martinrotter</url>
<content_rating type="oars-1.1" />
<releases>
<release version="4.0.4" date="2022-01-04"/>
<release version="4.0.4" date="2022-01-05"/>
</releases>
<content_rating type="oars-1.0">
<content_attribute id="violence-cartoon">none</content_attribute>

View File

@ -13,7 +13,26 @@
#include <QNetworkCookie>
#include <QSettings>
#if defined(USE_WEBENGINE)
#include <QWebEngineCookieStore>
#include <QWebEngineProfile>
#endif
CookieJar::CookieJar(QObject* parent) : QNetworkCookieJar(parent) {
#if defined(USE_WEBENGINE)
QWebEngineProfile::defaultProfile()->setPersistentCookiesPolicy(QWebEngineProfile::PersistentCookiesPolicy::NoPersistentCookies);
m_webEngineCookies = QWebEngineProfile::defaultProfile()->cookieStore();
// When cookies change in WebEngine, then change in main cookie jar too.
connect(m_webEngineCookies, &QWebEngineCookieStore::cookieAdded, this, [=](const QNetworkCookie& cookie) {
insertCookieInternal(cookie, false, true);
});
connect(m_webEngineCookies, &QWebEngineCookieStore::cookieRemoved, this, [=](const QNetworkCookie& cookie) {
deleteCookieInternal(cookie, false);
});
#endif
loadCookies();
}
@ -56,7 +75,7 @@ void CookieJar::loadCookies() {
auto cookie = QNetworkCookie::parseCookies(encoded);
if (!cookie.isEmpty()) {
if (!QNetworkCookieJar::insertCookie(cookie.at(0))) {
if (!insertCookieInternal(cookie.at(0), true, false)) {
qCriticalNN << LOGSEC_NETWORK
<< "Failed to load cookie"
<< QUOTE_W_SPACE(cookie_key)
@ -95,32 +114,70 @@ bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie>& cookie_list, cons
return QNetworkCookieJar::setCookiesFromUrl(cookie_list, url);
}
bool CookieJar::insertCookie(const QNetworkCookie& cookie) {
bool CookieJar::insertCookieInternal(const QNetworkCookie& cookie, bool notify_others, bool should_save) {
auto result = QNetworkCookieJar::insertCookie(cookie);
if (result) {
saveCookies();
if (should_save) {
saveCookies();
}
#if defined(USE_WEBENGINE)
if (notify_others) {
m_webEngineCookies->setCookie(cookie);
}
#else
Q_UNUSED(notify_others)
#endif
}
return result;
}
bool CookieJar::updateCookie(const QNetworkCookie& cookie) {
bool CookieJar::updateCookieInternal(const QNetworkCookie& cookie, bool notify_others) {
auto result = QNetworkCookieJar::updateCookie(cookie);
if (result) {
saveCookies();
#if defined(USE_WEBENGINE)
if (notify_others) {
m_webEngineCookies->setCookie(cookie);
}
#else
Q_UNUSED(notify_others)
#endif
}
return result;
}
bool CookieJar::deleteCookie(const QNetworkCookie& cookie) {
bool CookieJar::deleteCookieInternal(const QNetworkCookie& cookie, bool notify_others) {
auto result = QNetworkCookieJar::deleteCookie(cookie);
if (result) {
saveCookies();
#if defined(USE_WEBENGINE)
if (notify_others) {
m_webEngineCookies->deleteCookie(cookie);
}
#else
Q_UNUSED(notify_others)
#endif
}
return result;
}
bool CookieJar::insertCookie(const QNetworkCookie& cookie) {
return insertCookieInternal(cookie, true, true);
}
bool CookieJar::deleteCookie(const QNetworkCookie& cookie) {
return deleteCookieInternal(cookie, true);
}
bool CookieJar::updateCookie(const QNetworkCookie& cookie) {
return updateCookieInternal(cookie, true);
}

View File

@ -5,6 +5,10 @@
#include <QNetworkCookieJar>
#if defined(USE_WEBENGINE)
class QWebEngineCookieStore;
#endif
class CookieJar : public QNetworkCookieJar {
public:
explicit CookieJar(QObject* parent = nullptr);
@ -19,8 +23,17 @@ class CookieJar : public QNetworkCookieJar {
static QList<QNetworkCookie> extractCookiesFromUrl(const QString& url);
private:
bool insertCookieInternal(const QNetworkCookie& cookie, bool notify_others, bool should_save);
bool updateCookieInternal(const QNetworkCookie& cookie, bool notify_others);
bool deleteCookieInternal(const QNetworkCookie& cookie, bool notify_others);
void loadCookies();
void saveCookies();
private:
#if defined(USE_WEBENGINE)
QWebEngineCookieStore * m_webEngineCookies;
#endif
};
#endif // COOKIEJAR_H