Skip to content

Commit 997ad30

Browse files
Peter Hartmannphartmann
authored andcommitted
[BB10-internal] QNetworkAccessManager: track online / accesible state without session
In particular, set online state right upon construction of the QNetworkAccessManager instance. Before, this would only work properly if a network session was created. Now, networkAccessible() returns the correct status. Change-Id: I7ff9ccfd18ad376a131fc5977843b55bf185fba0 Signed-off-by: Peter Hartmann <[email protected]>
1 parent 37135fd commit 997ad30

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

src/network/access/qnetworkaccessmanager.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,22 @@ static void ensureInitialized()
357357
QNetworkAccessManager::QNetworkAccessManager(QObject *parent)
358358
: QObject(*new QNetworkAccessManagerPrivate, parent)
359359
{
360+
Q_D(QNetworkAccessManager);
360361
ensureInitialized();
361362

363+
#ifndef QT_NO_BEARERMANAGEMENT
364+
if (!d->networkSessionRequired) {
365+
// if a session is required, we track online state through
366+
// the QNetworkSession's signals
367+
connect(&d->networkConfigurationManager, SIGNAL(onlineStateChanged(bool)),
368+
SLOT(_q_onlineStateChanged(bool)));
369+
// we would need all configurations to check for
370+
// d->networkConfigurationManager.isOnline(), which is asynchronous
371+
// and potentially expensive. We can just check here
372+
d->online = (d->networkConfiguration.state() & QNetworkConfiguration::Active);
373+
}
374+
#endif
375+
362376
qRegisterMetaType<QNetworkReply::NetworkError>("QNetworkReply::NetworkError");
363377
}
364378

@@ -759,9 +773,8 @@ void QNetworkAccessManager::setConfiguration(const QNetworkConfiguration &config
759773
{
760774
Q_D(QNetworkAccessManager);
761775
d->networkConfiguration = config;
762-
QNetworkConfigurationManager manager;
763776
d->customNetworkConfiguration = true;
764-
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired)
777+
if (d->networkConfigurationManager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired)
765778
d->createSession(config);
766779
}
767780

@@ -844,16 +857,23 @@ QNetworkAccessManager::NetworkAccessibility QNetworkAccessManager::networkAccess
844857
{
845858
Q_D(const QNetworkAccessManager);
846859

847-
QSharedPointer<QNetworkSession> networkSession(d->getNetworkSession());
848-
if (networkSession) {
849-
// d->online holds online/offline state of this network session.
860+
if (d->networkSessionRequired) {
861+
QSharedPointer<QNetworkSession> networkSession(d->getNetworkSession());
862+
if (networkSession) {
863+
// d->online holds online/offline state of this network session.
864+
if (d->online)
865+
return d->networkAccessible;
866+
else
867+
return NotAccessible;
868+
} else {
869+
// Network accessibility is either disabled or unknown.
870+
return (d->networkAccessible == NotAccessible) ? NotAccessible : UnknownAccessibility;
871+
}
872+
} else {
850873
if (d->online)
851874
return d->networkAccessible;
852875
else
853876
return NotAccessible;
854-
} else {
855-
// Network accessibility is either disabled or unknown.
856-
return (d->networkAccessible == NotAccessible) ? NotAccessible : UnknownAccessibility;
857877
}
858878
}
859879

@@ -1276,6 +1296,12 @@ void QNetworkAccessManagerPrivate::_q_networkSessionStateChanged(QNetworkSession
12761296
}
12771297
}
12781298
}
1299+
1300+
void QNetworkAccessManagerPrivate::_q_onlineStateChanged(bool isOnline)
1301+
{
1302+
online = isOnline;
1303+
}
1304+
12791305
#endif // QT_NO_BEARERMANAGEMENT
12801306

12811307
QNetworkRequest QNetworkAccessManagerPrivate::prepareMultipart(const QNetworkRequest &request, QHttpMultiPart *multiPart)

src/network/access/qnetworkaccessmanager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class Q_NETWORK_EXPORT QNetworkAccessManager: public QObject
167167
#if !defined(QT_NO_BEARERMANAGEMENT) && !defined(QT_MOBILITY_BEARER)
168168
Q_PRIVATE_SLOT(d_func(), void _q_networkSessionClosed())
169169
Q_PRIVATE_SLOT(d_func(), void _q_networkSessionStateChanged(QNetworkSession::State))
170+
Q_PRIVATE_SLOT(d_func(), void _q_onlineStateChanged(bool))
170171
#endif
171172
};
172173

src/network/access/qnetworkaccessmanager_p.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
#include "QtNetwork/qnetworkproxy.h"
6161
#include "QtNetwork/qnetworksession.h"
6262
#include "qnetworkaccessauthenticationmanager_p.h"
63+
#ifndef QT_NO_BEARERMANAGEMENT
64+
#include "QtNetwork/qnetworkconfigmanager.h"
65+
#endif
6366

6467
QT_BEGIN_NAMESPACE
6568

@@ -80,6 +83,9 @@ class QNetworkAccessManagerPrivate: public QObjectPrivate
8083
#ifndef QT_NO_BEARERMANAGEMENT
8184
lastSessionState(QNetworkSession::Invalid),
8285
customNetworkConfiguration(false),
86+
networkConfiguration(networkConfigurationManager.defaultConfiguration()),
87+
networkSessionRequired(networkConfigurationManager.capabilities()
88+
& QNetworkConfigurationManager::NetworkSessionRequired),
8389
networkAccessible(QNetworkAccessManager::Accessible),
8490
activeReplyCount(0),
8591
online(false),
@@ -120,6 +126,7 @@ class QNetworkAccessManagerPrivate: public QObjectPrivate
120126
void _q_networkSessionPreferredConfigurationChanged(const QNetworkConfiguration &config,
121127
bool isSeamless);
122128
void _q_networkSessionStateChanged(QNetworkSession::State state);
129+
void _q_onlineStateChanged(bool isOnline);
123130
#endif
124131

125132
QNetworkRequest prepareMultipart(const QNetworkRequest &request, QHttpMultiPart *multiPart);
@@ -141,10 +148,12 @@ class QNetworkAccessManagerPrivate: public QObjectPrivate
141148
QSharedPointer<QNetworkSession> networkSessionStrongRef;
142149
QWeakPointer<QNetworkSession> networkSessionWeakRef;
143150
QNetworkSession::State lastSessionState;
151+
QNetworkConfigurationManager networkConfigurationManager;
144152
QNetworkConfiguration networkConfiguration;
145153
// we need to track whether the user set a config or not,
146154
// because the default config might change
147155
bool customNetworkConfiguration;
156+
bool networkSessionRequired;
148157
QNetworkAccessManager::NetworkAccessibility networkAccessible;
149158
int activeReplyCount;
150159
bool online;

tests/auto/qnetworkaccessmanager/tst_qnetworkaccessmanager.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,43 @@ void tst_QNetworkAccessManager::networkAccessible()
7272
QSignalSpy spy(&manager,
7373
SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)));
7474

75-
QCOMPARE(manager.networkAccessible(), QNetworkAccessManager::UnknownAccessibility);
75+
// if there is no session, we cannot know in which state we are in
76+
QNetworkAccessManager::NetworkAccessibility initialAccessibility =
77+
manager.networkAccessible();
78+
QCOMPARE(manager.networkAccessible(), initialAccessibility);
7679

7780
manager.setNetworkAccessible(QNetworkAccessManager::NotAccessible);
7881

79-
QCOMPARE(spy.count(), 1);
80-
QCOMPARE(spy.takeFirst().at(0).value<QNetworkAccessManager::NetworkAccessibility>(),
81-
QNetworkAccessManager::NotAccessible);
82+
int expectedCount = (initialAccessibility == QNetworkAccessManager::Accessible) ? 1 : 0;
83+
QCOMPARE(spy.count(), expectedCount);
84+
if (expectedCount > 0)
85+
QCOMPARE(spy.takeFirst().at(0).value<QNetworkAccessManager::NetworkAccessibility>(),
86+
QNetworkAccessManager::NotAccessible);
8287
QCOMPARE(manager.networkAccessible(), QNetworkAccessManager::NotAccessible);
8388

8489
manager.setNetworkAccessible(QNetworkAccessManager::Accessible);
8590

86-
QCOMPARE(spy.count(), 1);
87-
QCOMPARE(spy.takeFirst().at(0).value<QNetworkAccessManager::NetworkAccessibility>(),
88-
QNetworkAccessManager::UnknownAccessibility);
89-
QCOMPARE(manager.networkAccessible(), QNetworkAccessManager::UnknownAccessibility);
91+
QCOMPARE(spy.count(), expectedCount);
92+
if (expectedCount > 0)
93+
QCOMPARE(spy.takeFirst().at(0).value<QNetworkAccessManager::NetworkAccessibility>(),
94+
initialAccessibility);
95+
QCOMPARE(manager.networkAccessible(), initialAccessibility);
9096

9197
QNetworkConfigurationManager configManager;
98+
bool sessionRequired = (configManager.capabilities()
99+
& QNetworkConfigurationManager::NetworkSessionRequired);
92100
QNetworkConfiguration defaultConfig = configManager.defaultConfiguration();
93101
if (defaultConfig.isValid()) {
94102
manager.setConfiguration(defaultConfig);
95103

96-
QCOMPARE(spy.count(), 1);
97-
QCOMPARE(spy.takeFirst().at(0).value<QNetworkAccessManager::NetworkAccessibility>(),
98-
QNetworkAccessManager::Accessible);
104+
// the accessibility has not changed if no session is required
105+
if (sessionRequired) {
106+
QCOMPARE(spy.count(), 1);
107+
QCOMPARE(spy.takeFirst().at(0).value<QNetworkAccessManager::NetworkAccessibility>(),
108+
QNetworkAccessManager::Accessible);
109+
} else {
110+
QCOMPARE(spy.count(), 0);
111+
}
99112
QCOMPARE(manager.networkAccessible(), QNetworkAccessManager::Accessible);
100113

101114
manager.setNetworkAccessible(QNetworkAccessManager::NotAccessible);

0 commit comments

Comments
 (0)