Skip to content

Commit

Permalink
Merge branch 'jb34613' into 'mer-5.6'
Browse files Browse the repository at this point in the history
[mer][qtnetwork] Create new QNetworkSession from the new online QNetworkConfiguration. Fixes JB#34613

See merge request !30
  • Loading branch information
rainemak committed Oct 5, 2017
2 parents 1e76877 + 7b553df commit 70659f7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
61 changes: 53 additions & 8 deletions src/network/access/qnetworkaccessmanager.cpp
Expand Up @@ -51,6 +51,7 @@
#include "qnetworkreplyfileimpl_p.h"

#include "QtCore/qbuffer.h"
#include "QtCore/qloggingcategory.h"
#include "QtCore/qurl.h"
#include "QtCore/qvector.h"
#include "QtNetwork/private/qauthenticator_p.h"
Expand All @@ -65,6 +66,8 @@

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcNetworkAccess, "qt.network.access", QtWarningMsg)

Q_GLOBAL_STATIC(QNetworkAccessFileBackendFactory, fileBackend)
#ifndef QT_NO_FTP
Q_GLOBAL_STATIC(QNetworkAccessFtpBackendFactory, ftpBackend)
Expand Down Expand Up @@ -1009,6 +1012,37 @@ QSharedPointer<QNetworkSession> QNetworkAccessManagerPrivate::getNetworkSession(
return networkSessionWeakRef.toStrongRef();
}

bool QNetworkAccessManagerPrivate::networkSessionCreationAllowed(const QNetworkConfiguration &configuration) const
{
// Do not allow network session creation if changing to non Wlan when Wlan already active.
// cellular to WLAN -> ok
// cellular to cellular -> ok
// WLAN to cellular -> not ok
// WLAN to WLAN -> ok

bool wlanActive = false;
QList<QNetworkConfiguration> activeConfigurations = networkConfigurationManager.allConfigurations(QNetworkConfiguration::Active);
if (configuration.bearerTypeFamily() != QNetworkConfiguration::BearerWLAN) {
Q_FOREACH (const QNetworkConfiguration config, activeConfigurations) {
if (config.bearerType() == QNetworkConfiguration::BearerWLAN) {
wlanActive = true;
break;
}
}
}

QSharedPointer<QNetworkSession> session(getNetworkSession());
if (session) {
// Prevent moving from WLAN to mobile data.
return !((session->configuration().bearerTypeFamily() == QNetworkConfiguration::BearerWLAN && wlanActive) &&
(configuration.bearerTypeFamily() == QNetworkConfiguration::Bearer2G ||
configuration.bearerTypeFamily() == QNetworkConfiguration::Bearer3G ||
configuration.bearerTypeFamily() == QNetworkConfiguration::Bearer4G));
}

return false;
}

#endif // QT_NO_BEARERMANAGEMENT


Expand Down Expand Up @@ -1158,6 +1192,7 @@ QNetworkReply *QNetworkAccessManager::createRequest(QNetworkAccessManager::Opera
// Return a disabled network reply if network access is disabled.
// Except if the scheme is empty or file://.
if (d->networkAccessible == NotAccessible && !isLocalFile) {
qCWarning(lcNetworkAccess) << "QNAM: network not accessible, returning disabled network reply.";
return new QDisabledNetworkReply(this, req, op);
}

Expand Down Expand Up @@ -1545,8 +1580,10 @@ void QNetworkAccessManagerPrivate::createSession(const QNetworkConfiguration &co
networkSessionStrongRef = networkSessionWeakRef.toStrongRef();

QSharedPointer<QNetworkSession> newSession;
if (config.isValid())
if (config.isValid()) {
newSession = QSharedNetworkSessionManager::getSession(config);
qCDebug(lcNetworkAccess) << "QNAM: get network session from network configuration.";
}

if (networkSessionStrongRef) {
//do nothing if new and old session are the same
Expand Down Expand Up @@ -1579,11 +1616,16 @@ void QNetworkAccessManagerPrivate::createSession(const QNetworkConfiguration &co
QObject::connect(networkSessionStrongRef.data(), SIGNAL(opened()), q, SIGNAL(networkSessionConnected()), Qt::QueuedConnection);
//QueuedConnection is used to avoid deleting the networkSession inside its closed signal
QObject::connect(networkSessionStrongRef.data(), SIGNAL(closed()), q, SLOT(_q_networkSessionClosed()), Qt::QueuedConnection);
QObject::connect(networkSessionStrongRef.data(), SIGNAL(stateChanged(QNetworkSession::State)),
q, SLOT(_q_networkSessionStateChanged(QNetworkSession::State)), Qt::QueuedConnection);
QObject::connect(networkSessionStrongRef.data(), &QNetworkSession::stateChanged,
q, [this](QNetworkSession::State s) {
qCDebug(lcNetworkAccess) << "QNAM: network session state changed:" << s;
_q_networkSessionStateChanged(s);
}, Qt::QueuedConnection);
QObject::connect(networkSessionStrongRef.data(), SIGNAL(error(QNetworkSession::SessionError)),
q, SLOT(_q_networkSessionFailed(QNetworkSession::SessionError)));


qCDebug(lcNetworkAccess) << "QNAM: initial network session" << networkSessionStrongRef->configuration().name() << "state:" << networkSessionStrongRef->state();
_q_networkSessionStateChanged(networkSessionStrongRef->state());
}

Expand Down Expand Up @@ -1683,17 +1725,20 @@ void QNetworkAccessManagerPrivate::_q_configurationChanged(const QNetworkConfigu
const QString id = configuration.identifier();
if (configuration.state().testFlag(QNetworkConfiguration::Active)) {
if (!onlineConfigurations.contains(id)) {

QSharedPointer<QNetworkSession> session(getNetworkSession());
if (session) {
if (online && session->configuration().identifier()
!= networkConfigurationManager.defaultConfiguration().identifier()) {

bool canCreateNetworkSession = networkSessionCreationAllowed(configuration);
// Use the id of the configuration over here as the networkConfigurationManager.defaultConfiguration()
// points to previous default configuration i.e. not yet changed.
if (canCreateNetworkSession && online && session->configuration().identifier() != id) {
qCDebug(lcNetworkAccess) << "QNAM: network session changing from" << session->configuration().name() << "to" << configuration.name();
onlineConfigurations.insert(id);
//this one disconnected but another one is online,
// close and create new session
_q_networkSessionClosed();
createSession(networkConfigurationManager.defaultConfiguration());
createSession(configuration);
} else if (!canCreateNetworkSession) {
qCDebug(lcNetworkAccess) << "QNAM: network session changed prevented from" << session->configuration().name() << "to" << configuration.name() << "as WLAN access point already online.";
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/network/access/qnetworkaccessmanager_p.h
Expand Up @@ -137,6 +137,8 @@ class QNetworkAccessManagerPrivate: public QObjectPrivate
void createSession(const QNetworkConfiguration &config);
QSharedPointer<QNetworkSession> getNetworkSession() const;

bool networkSessionCreationAllowed(const QNetworkConfiguration &configuration) const;

void _q_networkSessionClosed();
void _q_networkSessionNewConfigurationActivated();
void _q_networkSessionPreferredConfigurationChanged(const QNetworkConfiguration &config,
Expand Down

0 comments on commit 70659f7

Please sign in to comment.