diff options
-rw-r--r-- | src/core/download_manager_delegate_qt.cpp | 10 | ||||
-rw-r--r-- | src/core/profile_adapter_client.h | 1 | ||||
-rw-r--r-- | src/webengine/api/qquickwebenginedownloaditem.cpp | 12 | ||||
-rw-r--r-- | src/webengine/api/qquickwebenginedownloaditem_p.h | 2 | ||||
-rw-r--r-- | src/webengine/api/qquickwebenginedownloaditem_p_p.h | 1 | ||||
-rw-r--r-- | src/webengine/api/qquickwebengineprofile.cpp | 1 | ||||
-rw-r--r-- | src/webenginewidgets/api/qwebenginedownloaditem.cpp | 11 | ||||
-rw-r--r-- | src/webenginewidgets/api/qwebenginedownloaditem.h | 1 | ||||
-rw-r--r-- | src/webenginewidgets/api/qwebenginedownloaditem_p.h | 1 | ||||
-rw-r--r-- | src/webenginewidgets/api/qwebengineprofile.cpp | 1 | ||||
-rw-r--r-- | tests/auto/quick/publicapi/tst_publicapi.cpp | 1 | ||||
-rw-r--r-- | tests/auto/quick/qmltests/data/tst_download.qml | 6 | ||||
-rw-r--r-- | tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp | 13 |
13 files changed, 58 insertions, 3 deletions
diff --git a/src/core/download_manager_delegate_qt.cpp b/src/core/download_manager_delegate_qt.cpp index 2af958068..ba506601f 100644 --- a/src/core/download_manager_delegate_qt.cpp +++ b/src/core/download_manager_delegate_qt.cpp @@ -208,7 +208,8 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(download::DownloadItem* false /* done */, downloadType, item->GetLastReason(), - adapterClient + adapterClient, + suggestedFilename }; for (ProfileAdapterClient *client : qAsConst(clients)) { @@ -309,7 +310,9 @@ void DownloadManagerDelegateQt::ChooseSavePath(content::WebContents *web_content false, /* done */ ProfileAdapterClient::SavePage, ProfileAdapterClient::NoReason, - adapterClient + adapterClient, + QFileInfo(suggestedFilePath).fileName() + }; for (ProfileAdapterClient *client : qAsConst(clients)) { @@ -375,7 +378,8 @@ void DownloadManagerDelegateQt::OnDownloadUpdated(download::DownloadItem *downlo download->IsDone(), 0 /* downloadType (unused) */, download->GetLastReason(), - adapterClient + adapterClient, + toQt(download->GetSuggestedFilename()) }; for (ProfileAdapterClient *client : qAsConst(clients)) { diff --git a/src/core/profile_adapter_client.h b/src/core/profile_adapter_client.h index b463043da..3589f6cd7 100644 --- a/src/core/profile_adapter_client.h +++ b/src/core/profile_adapter_client.h @@ -138,6 +138,7 @@ public: int downloadType; int downloadInterruptReason; WebContentsAdapterClient *page; + QString suggestedFileName; }; virtual ~ProfileAdapterClient() { } diff --git a/src/webengine/api/qquickwebenginedownloaditem.cpp b/src/webengine/api/qquickwebenginedownloaditem.cpp index cbd0e994a..d5f6a3ef1 100644 --- a/src/webengine/api/qquickwebenginedownloaditem.cpp +++ b/src/webengine/api/qquickwebenginedownloaditem.cpp @@ -446,6 +446,18 @@ void QQuickWebEngineDownloadItem::setPath(QString path) Q_EMIT pathChanged(); } } +/*! + \qmlproperty string WebEngineDownloadItem::suggestedFileName + \since QtWebEngine 1.10 + + Returns the suggested file name. +*/ + +QString QQuickWebEngineDownloadItem::suggestedFileName() const +{ + Q_D(const QQuickWebEngineDownloadItem); + return d->suggestedFileName; +} /*! \qmlproperty enumeration WebEngineDownloadItem::savePageFormat diff --git a/src/webengine/api/qquickwebenginedownloaditem_p.h b/src/webengine/api/qquickwebenginedownloaditem_p.h index 1b17f71aa..613b0173d 100644 --- a/src/webengine/api/qquickwebenginedownloaditem_p.h +++ b/src/webengine/api/qquickwebenginedownloaditem_p.h @@ -138,6 +138,7 @@ public: Q_PROPERTY(bool isSavePageDownload READ isSavePageDownload CONSTANT REVISION 6 FINAL) Q_PROPERTY(QQuickWebEngineView *view READ view CONSTANT REVISION 7 FINAL) Q_PROPERTY(QUrl url READ url CONSTANT REVISION 8 FINAL) + Q_PROPERTY(QString suggestedFileName READ suggestedFileName CONSTANT REVISION 8 FINAL) Q_INVOKABLE void accept(); Q_INVOKABLE void cancel(); @@ -161,6 +162,7 @@ public: bool isSavePageDownload() const; QQuickWebEngineView *view() const; QUrl url() const; + QString suggestedFileName() const; Q_SIGNALS: void stateChanged(); diff --git a/src/webengine/api/qquickwebenginedownloaditem_p_p.h b/src/webengine/api/qquickwebenginedownloaditem_p_p.h index 955a6fd6a..e4d90d8ef 100644 --- a/src/webengine/api/qquickwebenginedownloaditem_p_p.h +++ b/src/webengine/api/qquickwebenginedownloaditem_p_p.h @@ -83,6 +83,7 @@ public: bool downloadPaused; QQuickWebEngineView *view; QUrl downloadUrl; + QString suggestedFileName; void update(const QtWebEngineCore::ProfileAdapterClient::DownloadItemInfo &info); void updateState(QQuickWebEngineDownloadItem::DownloadState newState); diff --git a/src/webengine/api/qquickwebengineprofile.cpp b/src/webengine/api/qquickwebengineprofile.cpp index 911e4cbce..2dbd0e94b 100644 --- a/src/webengine/api/qquickwebengineprofile.cpp +++ b/src/webengine/api/qquickwebengineprofile.cpp @@ -243,6 +243,7 @@ void QQuickWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) itemPrivate->totalBytes = info.totalBytes; itemPrivate->mimeType = info.mimeType; itemPrivate->downloadPath = info.path; + itemPrivate->suggestedFileName = info.suggestedFileName; itemPrivate->savePageFormat = static_cast<QQuickWebEngineDownloadItem::SavePageFormat>( info.savePageFormat); itemPrivate->type = static_cast<QQuickWebEngineDownloadItem::DownloadType>(info.downloadType); diff --git a/src/webenginewidgets/api/qwebenginedownloaditem.cpp b/src/webenginewidgets/api/qwebenginedownloaditem.cpp index 4575f2929..ec088762e 100644 --- a/src/webenginewidgets/api/qwebenginedownloaditem.cpp +++ b/src/webenginewidgets/api/qwebenginedownloaditem.cpp @@ -536,6 +536,17 @@ void QWebEngineDownloadItem::setPath(QString path) d->downloadPath = path; } +/*! + \since 5.14 + + Returns the suggested file name. +*/ + +QString QWebEngineDownloadItem::suggestedFileName() const +{ + Q_D(const QWebEngineDownloadItem); + return d->suggestedFileName; +} /*! Returns whether this download is finished (completed, cancelled, or non-resumable interrupted state). diff --git a/src/webenginewidgets/api/qwebenginedownloaditem.h b/src/webenginewidgets/api/qwebenginedownloaditem.h index 981a3c374..0ae6b2575 100644 --- a/src/webenginewidgets/api/qwebenginedownloaditem.h +++ b/src/webenginewidgets/api/qwebenginedownloaditem.h @@ -128,6 +128,7 @@ public: DownloadInterruptReason interruptReason() const; QString interruptReasonString() const; bool isSavePageDownload() const; + QString suggestedFileName() const; QWebEnginePage *page() const; diff --git a/src/webenginewidgets/api/qwebenginedownloaditem_p.h b/src/webenginewidgets/api/qwebenginedownloaditem_p.h index b3bc8a3fe..ac5db7dcd 100644 --- a/src/webenginewidgets/api/qwebenginedownloaditem_p.h +++ b/src/webenginewidgets/api/qwebenginedownloaditem_p.h @@ -78,6 +78,7 @@ public: const QUrl downloadUrl; QString mimeType; bool downloadPaused; + QString suggestedFileName; qint64 totalBytes; qint64 receivedBytes; diff --git a/src/webenginewidgets/api/qwebengineprofile.cpp b/src/webenginewidgets/api/qwebengineprofile.cpp index 237d68473..6e7191355 100644 --- a/src/webenginewidgets/api/qwebengineprofile.cpp +++ b/src/webenginewidgets/api/qwebengineprofile.cpp @@ -227,6 +227,7 @@ void QWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) itemPrivate->downloadState = info.accepted ? QWebEngineDownloadItem::DownloadInProgress : QWebEngineDownloadItem::DownloadRequested; itemPrivate->downloadPath = info.path; + itemPrivate->suggestedFileName = info.suggestedFileName; itemPrivate->mimeType = info.mimeType; itemPrivate->savePageFormat = static_cast<QWebEngineDownloadItem::SavePageFormat>(info.savePageFormat); itemPrivate->type = static_cast<QWebEngineDownloadItem::DownloadType>(info.downloadType); diff --git a/tests/auto/quick/publicapi/tst_publicapi.cpp b/tests/auto/quick/publicapi/tst_publicapi.cpp index 096c3d2ca..3d4506b38 100644 --- a/tests/auto/quick/publicapi/tst_publicapi.cpp +++ b/tests/auto/quick/publicapi/tst_publicapi.cpp @@ -261,6 +261,7 @@ static const QStringList expectedAPI = QStringList() << "QQuickWebEngineDownloadItem.typeChanged() --> void" << "QQuickWebEngineDownloadItem.view --> QQuickWebEngineView*" << "QQuickWebEngineDownloadItem.url --> QUrl" + << "QQuickWebEngineDownloadItem.suggestedFileName --> QString" << "QQuickWebEngineFileDialogRequest.FileModeOpen --> FileMode" << "QQuickWebEngineFileDialogRequest.FileModeOpenMultiple --> FileMode" << "QQuickWebEngineFileDialogRequest.FileModeSave --> FileMode" diff --git a/tests/auto/quick/qmltests/data/tst_download.qml b/tests/auto/quick/qmltests/data/tst_download.qml index 04838f57d..c8f783b03 100644 --- a/tests/auto/quick/qmltests/data/tst_download.qml +++ b/tests/auto/quick/qmltests/data/tst_download.qml @@ -43,6 +43,7 @@ TestWebEngineView { property var downloadState: [] property var downloadInterruptReason: null property url downloadUrl: "" + property string suggestedFileName: "" function urlToPath(url) { var path = url.toString() @@ -83,6 +84,7 @@ TestWebEngineView { download.accept() } downloadUrl = download.url + suggestedFileName = download.suggestedFileName } onDownloadFinished: { receivedBytes = download.receivedBytes; @@ -109,6 +111,7 @@ TestWebEngineView { downLoadRequestedSpy.wait() compare(downLoadRequestedSpy.count, 1) compare(downloadUrl, webEngineView.url) + compare(suggestedFileName, "download.zip") compare(downloadState[0], WebEngineDownloadItem.DownloadRequested) verify(!downloadInterruptReason) } @@ -119,6 +122,7 @@ TestWebEngineView { downLoadRequestedSpy.wait() compare(downLoadRequestedSpy.count, 1) compare(downloadUrl, webEngineView.url) + compare(suggestedFileName, "download.zip") compare(totalBytes, 325) verify(!downloadInterruptReason) } @@ -129,6 +133,7 @@ TestWebEngineView { downLoadRequestedSpy.wait() compare(downLoadRequestedSpy.count, 1) compare(downloadUrl, webEngineView.url) + compare(suggestedFileName, "download.zip") compare(downloadState[0], WebEngineDownloadItem.DownloadRequested) tryCompare(downloadState, "1", WebEngineDownloadItem.DownloadInProgress) downloadFinishedSpy.wait() @@ -144,6 +149,7 @@ TestWebEngineView { downLoadRequestedSpy.wait() compare(downLoadRequestedSpy.count, 1) compare(downloadUrl, webEngineView.url) + compare(suggestedFileName, "download.zip") compare(downloadFinishedSpy.count, 1) tryCompare(downloadState, "1", WebEngineDownloadItem.DownloadCancelled) tryCompare(webEngineView, "downloadInterruptReason", WebEngineDownloadItem.UserCanceled) diff --git a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp index 3a38c115d..ab576135f 100644 --- a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp +++ b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp @@ -868,6 +868,7 @@ void tst_QWebEngineDownloadItem::downloadUniqueFilename() QFETCH(QString, extension); QString fileName = QString("%1.%2").arg(baseName).arg(extension); QString downloadedFilePath; + QString suggestedFileName; bool downloadFinished = false; QTemporaryDir tmpDir; @@ -889,6 +890,7 @@ void tst_QWebEngineDownloadItem::downloadUniqueFilename() // Set up profile and download handler ScopedConnection sc2 = connect(m_profile, &QWebEngineProfile::downloadRequested, [&](QWebEngineDownloadItem *item) { + suggestedFileName = item->suggestedFileName(); item->accept(); connect(item, &QWebEngineDownloadItem::finished, [&, item]() { QCOMPARE(item->state(), QWebEngineDownloadItem::DownloadCompleted); @@ -914,6 +916,7 @@ void tst_QWebEngineDownloadItem::downloadUniqueFilename() QTRY_VERIFY(downloadFinished); QVERIFY(QFile(downloadedFilePath).exists()); QCOMPARE(downloadedFilePath, m_profile->downloadPath() + "/" + baseName + " (" + QString::number(i) + ")." + extension); + QCOMPARE(suggestedFileName, fileName); } } @@ -924,6 +927,7 @@ void tst_QWebEngineDownloadItem::downloadUniqueFilenameWithTimestamp() QString extension("txt"); QString fileName = QString("%1.%2").arg(baseName).arg(extension); QString downloadedFilePath; + QString suggestedFileName; bool downloadFinished = false; QTemporaryDir tmpDir; @@ -944,6 +948,7 @@ void tst_QWebEngineDownloadItem::downloadUniqueFilenameWithTimestamp() // Set up profile and download handler ScopedConnection sc2 = connect(m_profile, &QWebEngineProfile::downloadRequested, [&](QWebEngineDownloadItem *item) { + suggestedFileName = item->suggestedFileName(); item->accept(); connect(item, &QWebEngineDownloadItem::finished, [&, item]() { QCOMPARE(item->state(), QWebEngineDownloadItem::DownloadCompleted); @@ -974,6 +979,7 @@ void tst_QWebEngineDownloadItem::downloadUniqueFilenameWithTimestamp() QTRY_VERIFY(downloadFinished); QVERIFY(QFile(downloadedFilePath).exists()); QCOMPARE(downloadedFilePath, m_profile->downloadPath() + "/" + baseName + " (100)." + extension); + QCOMPARE(suggestedFileName, fileName); // Check if the downloaded files are suffixed with timestamp after the 100th download. for (int i = 101; i < 103; i++) { @@ -987,6 +993,7 @@ void tst_QWebEngineDownloadItem::downloadUniqueFilenameWithTimestamp() // ISO 8601 Date and time in UTC QRegExp timestamp("^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9])([0-5][0-9])([0-5][0-9])([.][0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9])[0-5][0-9])?$"); QVERIFY(timestamp.exactMatch(match.captured(1))); + QCOMPARE(suggestedFileName, fileName); } } @@ -1013,6 +1020,7 @@ void tst_QWebEngineDownloadItem::downloadToNonExistentDir() QString extension("txt"); QString fileName = QString("%1.%2").arg(baseName).arg(extension); QString downloadedFilePath; + QString suggestedFileName; bool downloadFinished = false; QTemporaryDir tmpDir; @@ -1034,6 +1042,7 @@ void tst_QWebEngineDownloadItem::downloadToNonExistentDir() // Set up profile and download handler ScopedConnection sc2 = connect(m_profile, &QWebEngineProfile::downloadRequested, [&](QWebEngineDownloadItem *item) { + suggestedFileName = item->suggestedFileName(); item->accept(); connect(item, &QWebEngineDownloadItem::finished, [&, item]() { QCOMPARE(item->state(), QWebEngineDownloadItem::DownloadCompleted); @@ -1055,6 +1064,7 @@ void tst_QWebEngineDownloadItem::downloadToNonExistentDir() QTRY_VERIFY(downloadFinished); QVERIFY(QFile(downloadedFilePath).exists()); QCOMPARE(downloadedFilePath, nonExistentDownloadPath + "/" + fileName); + QCOMPARE(suggestedFileName, fileName); } void tst_QWebEngineDownloadItem::downloadToReadOnlyDir() @@ -1066,6 +1076,7 @@ void tst_QWebEngineDownloadItem::downloadToReadOnlyDir() QString extension("txt"); QString fileName = QString("%1.%2").arg(baseName).arg(extension); QString downloadedFilePath; + QString suggestedFileName; bool downloadAccepted = false; bool downloadFinished = false; @@ -1088,6 +1099,7 @@ void tst_QWebEngineDownloadItem::downloadToReadOnlyDir() QPointer<QWebEngineDownloadItem> downloadItem; ScopedConnection sc2 = connect(m_profile, &QWebEngineProfile::downloadRequested, [&](QWebEngineDownloadItem *item) { + suggestedFileName = item->suggestedFileName(); downloadItem = item; item->accept(); connect(item, &QWebEngineDownloadItem::finished, [&, item]() { @@ -1108,6 +1120,7 @@ void tst_QWebEngineDownloadItem::downloadToReadOnlyDir() QCOMPARE(downloadItem->isFinished(), false); QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::FileAccessDenied); QVERIFY(!QFile(downloadedFilePath).exists()); + QCOMPARE(suggestedFileName, fileName); // Clear m_requestedDownloads explicitly because download is accepted but never finished. m_requestedDownloads.clear(); |