QNAM: Make QNetworkConnectionMonitor able to move to another thread

Requires moving construction and destruction of the IConnectionPoint
(contained in QNetworkListManagerEvents contained in
QNetworkStatusMonitorPrivate) and the calls to CoInitialize and
CoUninitialize to start()/stop() so it can be taken down
and brought up in a reasonable way.

Partial squashed cherry pick of bde6b27882
and the following fixup patch 84be436ef0.

Task-number: QTBUG-84031
Change-Id: I9d644261c6c730ea511e28d75da91bd34504bd03
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
(cherry picked from commit bde6b27882)
(cherry picked from commit 84be436ef0)
This commit is contained in:
Mårten Nordheim 2020-06-15 17:07:04 +02:00
parent c2bf56fc3a
commit f1f0aa4a3a
4 changed files with 51 additions and 14 deletions

View File

@ -310,8 +310,8 @@ public:
bool isOnlineIpv6 = false;
};
QNetworkStatusMonitor::QNetworkStatusMonitor()
: QObject(*new QNetworkStatusMonitorPrivate)
QNetworkStatusMonitor::QNetworkStatusMonitor(QObject *parent)
: QObject(*new QNetworkStatusMonitorPrivate, parent)
{
Q_D(QNetworkStatusMonitor);
@ -385,6 +385,11 @@ bool QNetworkStatusMonitor::isNetworkAccessible()
return d->isOnlineIpv4 || d->isOnlineIpv6;
}
bool QNetworkStatusMonitor::event(QEvent *event)
{
return QObject::event(event);
}
bool QNetworkStatusMonitor::isEnabled()
{
return true;

View File

@ -96,7 +96,7 @@ class Q_AUTOTEST_EXPORT QNetworkStatusMonitor : public QObject
Q_OBJECT
public:
QNetworkStatusMonitor();
QNetworkStatusMonitor(QObject *parent);
~QNetworkStatusMonitor();
bool isNetworkAccessible();
@ -105,6 +105,8 @@ public:
void stop();
bool isMonitoring() const;
bool event(QEvent *event) override;
static bool isEnabled();
Q_SIGNALS:

View File

@ -100,8 +100,8 @@ class QNetworkStatusMonitorPrivate : public QObjectPrivate
{
};
QNetworkStatusMonitor::QNetworkStatusMonitor()
: QObject(*new QNetworkStatusMonitorPrivate)
QNetworkStatusMonitor::QNetworkStatusMonitor(QObject *parent)
: QObject(*new QNetworkStatusMonitorPrivate, parent)
{
}
@ -128,6 +128,11 @@ bool QNetworkStatusMonitor::isNetworkAccessible()
return false;
}
bool QNetworkStatusMonitor::event(QEvent *event)
{
return QObject::event(event);
}
bool QNetworkStatusMonitor::isEnabled()
{
return false;

View File

@ -512,7 +512,6 @@ public:
Q_REQUIRED_RESULT
bool start();
Q_REQUIRED_RESULT
bool stop();
private:
@ -659,8 +658,6 @@ QNetworkStatusMonitorPrivate::~QNetworkStatusMonitorPrivate()
return;
if (monitoring)
stop();
managerEvents.Reset();
CoUninitialize();
}
void QNetworkStatusMonitorPrivate::setConnectivity(NLM_CONNECTIVITY newConnectivity)
@ -676,10 +673,20 @@ void QNetworkStatusMonitorPrivate::setConnectivity(NLM_CONNECTIVITY newConnectiv
bool QNetworkStatusMonitorPrivate::start()
{
if (comInitFailed)
return false;
Q_ASSERT(managerEvents);
Q_ASSERT(!monitoring);
if (comInitFailed) {
auto hr = CoInitialize(nullptr);
if (FAILED(hr)) {
qCWarning(lcNetMon) << "Failed to initialize COM:" << errorStringFromHResult(hr);
comInitFailed = true;
return false;
}
comInitFailed = false;
}
if (!managerEvents)
managerEvents = new QNetworkListManagerEvents(this);
if (managerEvents->start())
monitoring = true;
return monitoring;
@ -689,11 +696,19 @@ void QNetworkStatusMonitorPrivate::stop()
{
Q_ASSERT(managerEvents);
Q_ASSERT(monitoring);
if (managerEvents->stop())
monitoring = false;
// Can return false but realistically shouldn't since that would break everything:
managerEvents->stop();
monitoring = false;
managerEvents.Reset();
CoUninitialize();
comInitFailed = true; // we check this value in start() to see if we need to re-initialize
}
QNetworkStatusMonitor::QNetworkStatusMonitor() : QObject(*new QNetworkStatusMonitorPrivate) {}
QNetworkStatusMonitor::QNetworkStatusMonitor(QObject *parent)
: QObject(*new QNetworkStatusMonitorPrivate, parent)
{
}
QNetworkStatusMonitor::~QNetworkStatusMonitor() {}
@ -730,6 +745,16 @@ bool QNetworkStatusMonitor::isNetworkAccessible()
| NLM_CONNECTIVITY_IPV4_LOCALNETWORK | NLM_CONNECTIVITY_IPV6_LOCALNETWORK);
}
bool QNetworkStatusMonitor::event(QEvent *event)
{
if (event->type() == QEvent::ThreadChange && isMonitoring()) {
stop();
QMetaObject::invokeMethod(this, &QNetworkStatusMonitor::start, Qt::QueuedConnection);
}
return QObject::event(event);
}
bool QNetworkStatusMonitor::isEnabled()
{
return true;