Ensure correct thread locale QgsNetworkAccessManager is used during

o2 requests

Otherwise the main thread manager is always used, resulting in
lots of warnings and potential crashes
This commit is contained in:
Nyall Dawson 2019-05-09 16:24:22 +10:00
parent ba8f8e3f07
commit 9cde65457b
4 changed files with 21 additions and 5 deletions

View File

@ -219,7 +219,7 @@ void O2::link() {
QUrl url(tokenUrl_);
QNetworkRequest tokenRequest(url);
tokenRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply *tokenReply = manager_->post(tokenRequest, payload);
QNetworkReply *tokenReply = getManager()->post(tokenRequest, payload);
connect(tokenReply, SIGNAL(finished()), this, SLOT(onTokenReplyFinished()), Qt::QueuedConnection);
connect(tokenReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onTokenReplyError(QNetworkReply::NetworkError)), Qt::QueuedConnection);
@ -268,7 +268,7 @@ void O2::onVerificationReceived(const QMap<QString, QString> response) {
qDebug() << QString("O2::onVerificationReceived: Exchange access code data:\n%1").arg(QString(data));
QNetworkReply *tokenReply = manager_->post(tokenRequest, data);
QNetworkReply *tokenReply = getManager()->post(tokenRequest, data);
timedReplies_.add(tokenReply);
connect(tokenReply, SIGNAL(finished()), this, SLOT(onTokenReplyFinished()), Qt::QueuedConnection);
connect(tokenReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onTokenReplyError(QNetworkReply::NetworkError)), Qt::QueuedConnection);
@ -390,6 +390,11 @@ void O2::setExpires(int v) {
store_->setValue(key, QString::number(v));
}
QNetworkAccessManager *O2::getManager()
{
return manager_;
}
QString O2::refreshToken() {
QString key = QString(O2_KEY_REFRESH_TOKEN).arg(clientId_);
return store_->value(key);
@ -424,7 +429,7 @@ void O2::refresh() {
parameters.insert(O2_OAUTH2_GRANT_TYPE, O2_OAUTH2_REFRESH_TOKEN);
QByteArray data = buildRequestBody(parameters);
QNetworkReply *refreshReply = manager_->post(refreshRequest, data);
QNetworkReply *refreshReply = getManager()->post(refreshRequest, data);
timedReplies_.add(refreshReply);
connect(refreshReply, SIGNAL(finished()), this, SLOT(onRefreshFinished()), Qt::QueuedConnection);
connect(refreshReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onRefreshError(QNetworkReply::NetworkError)), Qt::QueuedConnection);

View File

@ -161,6 +161,8 @@ protected:
/// Set token expiration time.
void setExpires(int v);
virtual QNetworkAccessManager *getManager();
protected:
QString username_;
QString password_;

View File

@ -233,7 +233,7 @@ void QgsO2::link()
QNetworkRequest tokenRequest( url );
QgsSetRequestInitiatorClass( tokenRequest, QStringLiteral( "QgsO2" ) );
tokenRequest.setHeader( QNetworkRequest::ContentTypeHeader, QLatin1Literal( "application/x-www-form-urlencoded" ) );
QNetworkReply *tokenReply = manager_->post( tokenRequest, payload );
QNetworkReply *tokenReply = getManager()->post( tokenRequest, payload );
connect( tokenReply, SIGNAL( finished() ), this, SLOT( onTokenReplyFinished() ), Qt::QueuedConnection );
connect( tokenReply, SIGNAL( error( QNetworkReply::NetworkError ) ), this, SLOT( onTokenReplyError( QNetworkReply::NetworkError ) ), Qt::QueuedConnection );
@ -302,7 +302,7 @@ void QgsO2::onVerificationReceived( QMap<QString, QString> response )
parameters.insert( O2_OAUTH2_REDIRECT_URI, redirectUri_ );
parameters.insert( O2_OAUTH2_GRANT_TYPE, O2_AUTHORIZATION_CODE );
QByteArray data = buildRequestBody( parameters );
QNetworkReply *tokenReply = manager_->post( tokenRequest, data );
QNetworkReply *tokenReply = getManager()->post( tokenRequest, data );
timedReplies_.add( tokenReply );
connect( tokenReply, &QNetworkReply::finished, this, &QgsO2::onTokenReplyFinished, Qt::QueuedConnection );
connect( tokenReply, qgis::overload<QNetworkReply::NetworkError>::of( &QNetworkReply::error ), this, &QgsO2::onTokenReplyError, Qt::QueuedConnection );
@ -339,3 +339,8 @@ void QgsO2::onVerificationReceived( QMap<QString, QString> response )
setRefreshToken( response.value( O2_OAUTH2_REFRESH_TOKEN ) );
}
}
QNetworkAccessManager *QgsO2::getManager()
{
return QgsNetworkAccessManager::instance();
}

View File

@ -74,6 +74,10 @@ class QgsO2: public O2
//! Handle verification response.
void onVerificationReceived( QMap<QString, QString> response ) override;
protected:
QNetworkAccessManager* getManager() override;
signals:
//! Emitted when the state has changed