Skip to content

Commit

Permalink
Ensure correct thread locale QgsNetworkAccessManager is used during
Browse files Browse the repository at this point in the history
o2 requests

Otherwise the main thread manager is always used, resulting in
lots of warnings and potential crashes
  • Loading branch information
nyalldawson committed May 12, 2019
1 parent ba8f8e3 commit 9cde654
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
11 changes: 8 additions & 3 deletions external/o2/src/o2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions external/o2/src/o2.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ protected Q_SLOTS:
/// Set token expiration time.
void setExpires(int v);

virtual QNetworkAccessManager *getManager();

protected:
QString username_;
QString password_;
Expand Down
9 changes: 7 additions & 2 deletions src/auth/oauth2/qgso2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -339,3 +339,8 @@ void QgsO2::onVerificationReceived( QMap<QString, QString> response )
setRefreshToken( response.value( O2_OAUTH2_REFRESH_TOKEN ) );
}
}

QNetworkAccessManager *QgsO2::getManager()
{
return QgsNetworkAccessManager::instance();
}
4 changes: 4 additions & 0 deletions src/auth/oauth2/qgso2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9cde654

Please sign in to comment.