Skip to content
Permalink
Browse files

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
  • Loading branch information
nyalldawson committed May 9, 2019
1 parent 92d0194 commit f2a513e3cb7b2dc408abd477fd4b3596c731b6f9
Showing with 21 additions and 5 deletions.
  1. +8 −3 external/o2/src/o2.cpp
  2. +2 −0 external/o2/src/o2.h
  3. +7 −2 src/auth/oauth2/qgso2.cpp
  4. +4 −0 src/auth/oauth2/qgso2.h
@@ -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);
@@ -161,6 +161,8 @@ protected Q_SLOTS:
/// Set token expiration time.
void setExpires(int v);

virtual QNetworkAccessManager *getManager();

protected:
QString username_;
QString password_;
@@ -231,7 +231,7 @@ void QgsO2::link()
QUrl url( tokenUrl_ );
QNetworkRequest tokenRequest( url );
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 );
@@ -299,7 +299,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 );
@@ -336,3 +336,8 @@ void QgsO2::onVerificationReceived( QMap<QString, QString> response )
setRefreshToken( response.value( O2_OAUTH2_REFRESH_TOKEN ) );
}
}

QNetworkAccessManager *QgsO2::getManager()
{
return QgsNetworkAccessManager::instance();
}
@@ -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

0 comments on commit f2a513e

Please sign in to comment.
You can’t perform that action at this time.