Skip to content

Commit 8308ce1

Browse files
committedMay 7, 2018
avoid race condition
1 parent 0d6dcb2 commit 8308ce1

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed
 

‎python/core/qgsnetworkcontentfetcherregistry.sip.in

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,25 @@ Return the potential error of the download
6565
:param redownload: if set to true, it will restart any achieved or pending download.
6666
%End
6767

68+
void cancel();
69+
%Docstring
70+
Cancel the download operation
71+
%End
72+
6873
signals:
6974
void fetched();
7075
%Docstring
71-
Sent when the file is fetched and accessible
76+
Emitted when the file is fetched and accessible
7277
%End
7378

7479
void downloadStarted( const bool redownload );
7580
%Docstring
76-
Sent went the download actually starts
81+
Emitted when the download actually starts
82+
%End
83+
84+
void cancelTriggered();
85+
%Docstring
86+
Emitted when download is canceled.
7787
%End
7888

7989
};

‎src/core/qgsnetworkcontentfetcherregistry.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,53 @@ QgsNetworkContentFetcherRegistry::~QgsNetworkContentFetcherRegistry()
3939

4040
const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch( const QUrl &url, const FetchingMode &fetchingMode )
4141
{
42+
QMutexLocker locker( &mMutex );
4243
if ( mFileRegistry.contains( url ) )
4344
{
4445
return mFileRegistry.value( url );
4546
}
4647

4748
QgsFetchedContent *content = new QgsFetchedContent( nullptr, QgsFetchedContent::NotStarted );
48-
QgsNetworkContentFetcherTask *fetcher = new QgsNetworkContentFetcherTask( url );
49+
content->mFetchingTask = new QgsNetworkContentFetcherTask( url );
4950

51+
// start
5052
QObject::connect( content, &QgsFetchedContent::downloadStarted, this, [ = ]( const bool redownload )
5153
{
5254
QMutexLocker locker( &mMutex );
5355
if ( mFileRegistry.contains( url ) && redownload )
5456
{
55-
const QgsFetchedContent *content = mFileRegistry[url];
56-
if ( mFileRegistry.value( url )->status() == QgsFetchedContent::Downloading && content->mFetchingTask )
57+
QgsFetchedContent *content = mFileRegistry[url];
58+
if ( mFileRegistry.value( url )->status() == QgsFetchedContent::Downloading )
5759
{
58-
content->mFetchingTask->cancel();
59-
}
60-
if ( content->mFile )
61-
{
62-
content->mFile->deleteLater();
63-
mFileRegistry[url]->setFilePath( QStringLiteral() );
60+
content->cancel();
6461
}
6562
}
6663
if ( ( mFileRegistry.contains( url ) && mFileRegistry.value( url )->status() == QgsFetchedContent::NotStarted ) || redownload )
6764
{
68-
QgsApplication::instance()->taskManager()->addTask( fetcher );
65+
QgsApplication::instance()->taskManager()->addTask( content->mFetchingTask );
6966
}
7067
} );
7168

72-
QObject::connect( fetcher, &QgsNetworkContentFetcherTask::fetched, this, [ = ]()
69+
// cancel
70+
QObject::connect( content, &QgsFetchedContent::cancelTriggered, this, [ = ]()
7371
{
7472
QMutexLocker locker( &mMutex );
75-
QNetworkReply *reply = fetcher->reply();
73+
if ( content->mFetchingTask )
74+
{
75+
content->mFetchingTask->cancel();
76+
}
77+
if ( content->mFile )
78+
{
79+
content->mFile->deleteLater();
80+
mFileRegistry[url]->setFilePath( QStringLiteral() );
81+
}
82+
} );
83+
84+
// finished
85+
QObject::connect( content->mFetchingTask, &QgsNetworkContentFetcherTask::fetched, this, [ = ]()
86+
{
87+
QMutexLocker locker( &mMutex );
88+
QNetworkReply *reply = content->mFetchingTask->reply();
7689
QgsFetchedContent *content = mFileRegistry.value( url );
7790
if ( reply->error() == QNetworkReply::NoError )
7891
{
@@ -96,6 +109,7 @@ const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch( const QUrl &ur
96109

97110
if ( fetchingMode == DownloadImmediately )
98111
content->download();
112+
99113
mFileRegistry.insert( url, content );
100114

101115
return content;

‎src/core/qgsnetworkcontentfetcherregistry.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,21 @@ class CORE_EXPORT QgsFetchedContent : public QObject
7979
*/
8080
void download( bool redownload = false ) {emit downloadStarted( redownload );}
8181

82+
/**
83+
* @brief Cancel the download operation
84+
*/
85+
void cancel() {emit cancelTriggered();}
86+
8287
signals:
83-
//! Sent when the file is fetched and accessible
88+
//! Emitted when the file is fetched and accessible
8489
void fetched();
8590

86-
//! Sent went the download actually starts
91+
//! Emitted when the download actually starts
8792
void downloadStarted( const bool redownload );
8893

94+
//! Emitted when download is canceled.
95+
void cancelTriggered();
96+
8997
private:
9098
void setFile( QTemporaryFile *file ) {mFile = file;}
9199
void setStatus( ContentStatus status ) {mStatus = status;}

0 commit comments

Comments
 (0)
Please sign in to comment.