refs #502, cookie manager

* use authenticated user data objects in databaseauthentication
* shared threadsafe cookie manager across the QNetworkAccessManagers
* use cookie manager in existing readers
This commit is contained in:
Klaus Basan
2015-11-04 03:06:55 +01:00
committed by Mathew Sutcliffe
parent 98b86b6f27
commit dda64d0879
7 changed files with 212 additions and 22 deletions

View File

@@ -0,0 +1,91 @@
/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackcore/cookiemanager.h"
#include "blackmisc/threadutilities.h"
#include <QCoreApplication>
#include <QNetworkCookieJar>
#include <QReadLocker>
#include <QWriteLocker>
using namespace BlackMisc;
namespace BlackCore
{
CCookieManager *CCookieManager::instance()
{
static CCookieManager *manager = nullptr;
if (!manager)
{
Q_ASSERT_X(CThreadUtils::isCurrentThreadApplicationThread(), Q_FUNC_INFO, "Supposed to run in application thread");
manager = new CCookieManager(QCoreApplication::instance());
}
return manager;
}
void CCookieManager::resetParent()
{
instance()->setParent(nullptr);
}
void CCookieManager::setToAccessManager(QNetworkAccessManager *manager)
{
if (!manager) { return; }
manager->setCookieJar(instance());
resetParent();
}
CCookieManager::CCookieManager(QObject *parent) : QNetworkCookieJar(parent)
{
// code
}
bool CCookieManager::setCookiesFromUrl(const QList<QNetworkCookie> &cookies, const QUrl &url)
{
QWriteLocker l(&m_lock);
return QNetworkCookieJar::setCookiesFromUrl(cookies, url);
}
QList<QNetworkCookie> CCookieManager::cookiesForUrl(const QUrl &url) const
{
QReadLocker l(&m_lock);
const QList<QNetworkCookie> cookies(QNetworkCookieJar::cookiesForUrl(url));
return cookies;
}
QList<QNetworkCookie> CCookieManager::cookiesForRequest(const QNetworkRequest &request) const
{
return cookiesForUrl(request.url());
}
bool CCookieManager::deleteCookie(const QNetworkCookie &cookie)
{
QWriteLocker l(&m_lock);
return QNetworkCookieJar::deleteCookie(cookie);
}
bool CCookieManager::insertCookie(const QNetworkCookie &cookie)
{
QWriteLocker l(&m_lock);
return QNetworkCookieJar::insertCookie(cookie);
}
bool CCookieManager::updateCookie(const QNetworkCookie &cookie)
{
QWriteLocker l(&m_lock);
return QNetworkCookieJar::updateCookie(cookie);
}
void CCookieManager::deleteAllCookies()
{
QWriteLocker l(&m_lock);
this->setAllCookies(QList<QNetworkCookie>());
}
} // ns