Check background requests are allowed before starting
authorShane Kearns <ext-shane.2.kearns@nokia.com>
Wed, 4 Apr 2012 17:37:54 +0000 (18:37 +0100)
committerQt by Nokia <qt-info@nokia.com>
Wed, 11 Apr 2012 20:19:35 +0000 (22:19 +0200)
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>
src/network/access/qnetworkreplyhttpimpl.cpp
src/network/access/qnetworkreplyimpl.cpp

index 54b98ce..9aabe4f 100644 (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.
index 8a66539..79e9223 100644 (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 {