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 <lars.knoll@nokia.com>
Reviewed-by: Martin Petersson <Martin.Petersson@nokia.com>
This commit is contained in:
Shane Kearns 2012-04-04 18:37:54 +01:00 committed by Qt by Nokia
parent 6957b8d1ed
commit b0c7f34c90
2 changed files with 26 additions and 5 deletions

View File

@ -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<QNetworkSession> 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.

View File

@ -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<QNetworkSession> 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 {