From df0c4c82089a64d7cb58ab55f3e6c01ef23f15e1 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 20 Jun 2023 21:51:28 -0500 Subject: [PATCH] Delete the QNetworkAccessManager when we're done with it Apparently having this object around can lead to background network scans happening that cause WiFi perf degradation. --- app/backend/autoupdatechecker.cpp | 25 ++++++++++++++++++------- app/backend/autoupdatechecker.h | 2 +- app/settings/compatfetcher.cpp | 25 ++++++++++++++++++------- app/settings/compatfetcher.h | 2 +- app/settings/mappingfetcher.cpp | 25 ++++++++++++++++++------- app/settings/mappingfetcher.h | 2 +- 6 files changed, 57 insertions(+), 24 deletions(-) diff --git a/app/backend/autoupdatechecker.cpp b/app/backend/autoupdatechecker.cpp index b1a1d60f..7b42ba8d 100644 --- a/app/backend/autoupdatechecker.cpp +++ b/app/backend/autoupdatechecker.cpp @@ -6,16 +6,17 @@ #include AutoUpdateChecker::AutoUpdateChecker(QObject *parent) : - QObject(parent), - m_Nam(this) + QObject(parent) { + m_Nam = new QNetworkAccessManager(this); + // Never communicate over HTTP - m_Nam.setStrictTransportSecurityEnabled(true); + m_Nam->setStrictTransportSecurityEnabled(true); // Allow HTTP redirects - m_Nam.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); + m_Nam->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); - connect(&m_Nam, &QNetworkAccessManager::finished, + connect(m_Nam, &QNetworkAccessManager::finished, this, &AutoUpdateChecker::handleUpdateCheckRequestFinished); QString currentVersion(VERSION_STR); @@ -28,12 +29,17 @@ AutoUpdateChecker::AutoUpdateChecker(QObject *parent) : void AutoUpdateChecker::start() { + if (!m_Nam) { + Q_ASSERT(m_Nam); + return; + } + #if defined(Q_OS_WIN32) || defined(Q_OS_DARWIN) || defined(STEAM_LINK) || defined(APP_IMAGE) // Only run update checker on platforms without auto-update #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) && QT_VERSION < QT_VERSION_CHECK(5, 15, 1) && !defined(QT_NO_BEARERMANAGEMENT) // HACK: Set network accessibility to work around QTBUG-80947 (introduced in Qt 5.14.0 and fixed in Qt 5.15.1) QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED - m_Nam.setNetworkAccessible(QNetworkAccessManager::Accessible); + m_Nam->setNetworkAccessible(QNetworkAccessManager::Accessible); QT_WARNING_POP #endif @@ -45,7 +51,7 @@ void AutoUpdateChecker::start() #else request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true); #endif - m_Nam.get(request); + m_Nam->get(request); #endif } @@ -102,6 +108,11 @@ void AutoUpdateChecker::handleUpdateCheckRequestFinished(QNetworkReply* reply) { Q_ASSERT(reply->isFinished()); + // Delete the QNetworkAccessManager to free resources and + // prevent the bearer plugin from polling in the background. + m_Nam->deleteLater(); + m_Nam = nullptr; + if (reply->error() == QNetworkReply::NoError) { QTextStream stream(reply); diff --git a/app/backend/autoupdatechecker.h b/app/backend/autoupdatechecker.h index e3b19c21..7c532153 100644 --- a/app/backend/autoupdatechecker.h +++ b/app/backend/autoupdatechecker.h @@ -25,5 +25,5 @@ private: QString getPlatform(); QVector m_CurrentVersionQuad; - QNetworkAccessManager m_Nam; + QNetworkAccessManager* m_Nam; }; diff --git a/app/settings/compatfetcher.cpp b/app/settings/compatfetcher.cpp index 5817a40d..9882d708 100644 --- a/app/settings/compatfetcher.cpp +++ b/app/settings/compatfetcher.cpp @@ -8,26 +8,32 @@ #define COMPAT_KEY "latestsupportedversion-" CompatFetcher::CompatFetcher(QObject *parent) : - QObject(parent), - m_Nam(this) + QObject(parent) { + m_Nam = new QNetworkAccessManager(this); + // Never communicate over HTTP - m_Nam.setStrictTransportSecurityEnabled(true); + m_Nam->setStrictTransportSecurityEnabled(true); // Allow HTTP redirects - m_Nam.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); + m_Nam->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); - connect(&m_Nam, &QNetworkAccessManager::finished, + connect(m_Nam, &QNetworkAccessManager::finished, this, &CompatFetcher::handleCompatInfoFetched); } void CompatFetcher::start() { + if (!m_Nam) { + Q_ASSERT(m_Nam); + return; + } + #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) && QT_VERSION < QT_VERSION_CHECK(5, 15, 1) && !defined(QT_NO_BEARERMANAGEMENT) // HACK: Set network accessibility to work around QTBUG-80947 (introduced in Qt 5.14.0 and fixed in Qt 5.15.1) QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED - m_Nam.setNetworkAccessible(QNetworkAccessManager::Accessible); + m_Nam->setNetworkAccessible(QNetworkAccessManager::Accessible); QT_WARNING_POP #endif @@ -41,7 +47,7 @@ void CompatFetcher::start() #endif // We'll get a callback when this is finished - m_Nam.get(request); + m_Nam->get(request); } bool CompatFetcher::isGfeVersionSupported(QString gfeVersion) @@ -119,6 +125,11 @@ void CompatFetcher::handleCompatInfoFetched(QNetworkReply* reply) { Q_ASSERT(reply->isFinished()); + // Delete the QNetworkAccessManager to free resources and + // prevent the bearer plugin from polling in the background. + m_Nam->deleteLater(); + m_Nam = nullptr; + if (reply->error() == QNetworkReply::NoError) { // Queue the reply for deletion reply->deleteLater(); diff --git a/app/settings/compatfetcher.h b/app/settings/compatfetcher.h index 3ff661cd..b548e7d4 100644 --- a/app/settings/compatfetcher.h +++ b/app/settings/compatfetcher.h @@ -18,5 +18,5 @@ private slots: void handleCompatInfoFetched(QNetworkReply* reply); private: - QNetworkAccessManager m_Nam; + QNetworkAccessManager* m_Nam; }; diff --git a/app/settings/mappingfetcher.cpp b/app/settings/mappingfetcher.cpp index 8e59fcae..4fa5756d 100644 --- a/app/settings/mappingfetcher.cpp +++ b/app/settings/mappingfetcher.cpp @@ -4,26 +4,32 @@ #include MappingFetcher::MappingFetcher(QObject *parent) : - QObject(parent), - m_Nam(this) + QObject(parent) { + m_Nam = new QNetworkAccessManager(this); + // Never communicate over HTTP - m_Nam.setStrictTransportSecurityEnabled(true); + m_Nam->setStrictTransportSecurityEnabled(true); // Allow HTTP redirects - m_Nam.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); + m_Nam->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); - connect(&m_Nam, &QNetworkAccessManager::finished, + connect(m_Nam, &QNetworkAccessManager::finished, this, &MappingFetcher::handleMappingListFetched); } void MappingFetcher::start() { + if (!m_Nam) { + Q_ASSERT(m_Nam); + return; + } + #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) && QT_VERSION < QT_VERSION_CHECK(5, 15, 1) && !defined(QT_NO_BEARERMANAGEMENT) // HACK: Set network accessibility to work around QTBUG-80947 (introduced in Qt 5.14.0 and fixed in Qt 5.15.1) QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED - m_Nam.setNetworkAccessible(QNetworkAccessManager::Accessible); + m_Nam->setNetworkAccessible(QNetworkAccessManager::Accessible); QT_WARNING_POP #endif @@ -57,13 +63,18 @@ void MappingFetcher::start() #endif // We'll get a callback when this is finished - m_Nam.get(request); + m_Nam->get(request); } void MappingFetcher::handleMappingListFetched(QNetworkReply* reply) { Q_ASSERT(reply->isFinished()); + // Delete the QNetworkAccessManager to free resources and + // prevent the bearer plugin from polling in the background. + m_Nam->deleteLater(); + m_Nam = nullptr; + if (reply->error() == QNetworkReply::NoError) { // Queue the reply for deletion reply->deleteLater(); diff --git a/app/settings/mappingfetcher.h b/app/settings/mappingfetcher.h index 8a2097c1..c3bcfdfc 100644 --- a/app/settings/mappingfetcher.h +++ b/app/settings/mappingfetcher.h @@ -16,5 +16,5 @@ private slots: void handleMappingListFetched(QNetworkReply* reply); private: - QNetworkAccessManager m_Nam; + QNetworkAccessManager* m_Nam; };