From b0c7f34c90b57efd6a5714cd492b19f9a7eb6b5a Mon Sep 17 00:00:00 2001 From: Shane Kearns Date: Wed, 4 Apr 2012 18:37:54 +0100 Subject: [PATCH] Check background requests are allowed before starting If background requests are not allowed, don't even attempt to start the network session, just fail the request immediately. After this change, a QNAM with mixed requests queued should behave as follows when background requests are disabled: - background requests at head of the queue fail - first foreground request starts the session and succeeds - remaining background requests fail - remaining foreground requests succeed If policy is changed on the fly, then running background requests are not aborted. However queued background requests won't be started. Change-Id: Ic9aba1eb59ca41b166a08d2ed09418e1b6af6b60 Reviewed-by: Lars Knoll Reviewed-by: Martin Petersson --- src/network/access/qnetworkreplyhttpimpl.cpp | 12 ++++++++++++ src/network/access/qnetworkreplyimpl.cpp | 19 ++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/network/access/qnetworkreplyhttpimpl.cpp b/src/network/access/qnetworkreplyhttpimpl.cpp index 54b98ceb78..9aabe4f318 100644 --- a/src/network/access/qnetworkreplyhttpimpl.cpp +++ b/src/network/access/qnetworkreplyhttpimpl.cpp @@ -1536,6 +1536,18 @@ void QNetworkReplyHttpImplPrivate::_q_startOperation() } state = Working; +#ifndef QT_NO_BEARERMANAGEMENT + // Do not start background requests if they are not allowed by session policy + QSharedPointer session(manager->d_func()->networkSession); + QVariant isBackground = request.attribute(QNetworkRequest::BackgroundRequestAttribute, QVariant::fromValue(false)); + if (isBackground.toBool() && session && session->usagePolicies().testFlag(QNetworkSession::NoBackgroundTrafficPolicy)) { + error(QNetworkReply::BackgroundRequestNotAllowedError, + QCoreApplication::translate("QNetworkReply", "Background request not allowed.")); + finished(); + return; + } +#endif + if (!start()) { #ifndef QT_NO_BEARERMANAGEMENT // backend failed to start because the session state is not Connected. diff --git a/src/network/access/qnetworkreplyimpl.cpp b/src/network/access/qnetworkreplyimpl.cpp index 8a66539444..79e922387c 100644 --- a/src/network/access/qnetworkreplyimpl.cpp +++ b/src/network/access/qnetworkreplyimpl.cpp @@ -90,6 +90,18 @@ void QNetworkReplyImplPrivate::_q_startOperation() return; } +#ifndef QT_NO_BEARERMANAGEMENT + // Do not start background requests if they are not allowed by session policy + QSharedPointer session(manager->d_func()->networkSession); + QVariant isBackground = backend->request().attribute(QNetworkRequest::BackgroundRequestAttribute, QVariant::fromValue(false)); + if (isBackground.toBool() && session && session->usagePolicies().testFlag(QNetworkSession::NoBackgroundTrafficPolicy)) { + error(QNetworkReply::BackgroundRequestNotAllowedError, + QCoreApplication::translate("QNetworkReply", "Background request not allowed.")); + finished(); + return; + } +#endif + if (!backend->start()) { #ifndef QT_NO_BEARERMANAGEMENT // backend failed to start because the session state is not Connected. @@ -97,17 +109,14 @@ void QNetworkReplyImplPrivate::_q_startOperation() // state changes. state = WaitingForSession; - QNetworkSession *session = manager->d_func()->networkSession.data(); - if (session) { Q_Q(QNetworkReplyImpl); - QObject::connect(session, SIGNAL(error(QNetworkSession::SessionError)), + QObject::connect(session.data(), SIGNAL(error(QNetworkSession::SessionError)), q, SLOT(_q_networkSessionFailed())); if (!session->isOpen()) { - session->setSessionProperty(QStringLiteral("ConnectInBackground"), - backend->request().attribute(QNetworkRequest::BackgroundRequestAttribute, QVariant::fromValue(false))); + session->setSessionProperty(QStringLiteral("ConnectInBackground"), isBackground); session->open(); } } else {