diff options
author | Michael BrĂ¼ning <michael.bruning@qt.io> | 2021-01-18 16:03:25 +0100 |
---|---|---|
committer | Michael BrĂ¼ning <michael.bruning@qt.io> | 2021-01-22 15:05:17 +0100 |
commit | a134b801dc38feb536d4829213624dba72702d54 (patch) | |
tree | 1adf4907861cbaf4a7b01c895a90b521dfaf0aa8 | |
parent | 91b3a9c056af7e08aca49d8dc2393d23fe6acf85 (diff) |
Do not extract download file names from certain url schemes
Chromium uses the default download file name if the scheme of
a file to download is either about or data, as the file name extraction
methods do not seems to work for those.
Mimic this behavior in Qt WebEngine as well.
Fixes: QTBUG-90355
Change-Id: I14b8c4f64559500d3f231dd26533a5947bf94bc3
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
-rw-r--r-- | src/core/download_manager_delegate_qt.cpp | 7 | ||||
-rw-r--r-- | tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp | 47 |
2 files changed, 52 insertions, 2 deletions
diff --git a/src/core/download_manager_delegate_qt.cpp b/src/core/download_manager_delegate_qt.cpp index 1f6013cbb..f1ad1e677 100644 --- a/src/core/download_manager_delegate_qt.cpp +++ b/src/core/download_manager_delegate_qt.cpp @@ -162,8 +162,11 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(download::DownloadItem * if (suggestedFilename.isEmpty()) suggestedFilename = toQt(item->GetTargetFilePath().AsUTF8Unsafe()); - if (suggestedFilename.isEmpty()) - suggestedFilename = QUrl::fromPercentEncoding(toQByteArray(item->GetURL().ExtractFileName())); + if (suggestedFilename.isEmpty()) { + GURL itemUrl = item->GetURL(); + if (!itemUrl.SchemeIs("about") && !itemUrl.SchemeIs("data")) + suggestedFilename = QUrl::fromPercentEncoding(toQByteArray(itemUrl.ExtractFileName())); + } if (suggestedFilename.isEmpty()) { suggestedFilename = QStringLiteral("qwe_download"); diff --git a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp index ec88fc581..9a3e7b52e 100644 --- a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp +++ b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp @@ -84,6 +84,8 @@ private Q_SLOTS: #endif void downloadToDirectoryWithFileName_data(); void downloadToDirectoryWithFileName(); + void downloadDataUrls_data(); + void downloadDataUrls(); private: void saveLink(QPoint linkPos); @@ -1392,5 +1394,50 @@ void tst_QWebEngineDownloadItem::downloadToDirectoryWithFileName() QCOMPARE(downloadedSuggestedFileName, fileName); } +void tst_QWebEngineDownloadItem::downloadDataUrls_data() +{ + QTest::addColumn<QByteArray>("htmlData"); + QTest::addColumn<QString>("expectedFileName"); + QTest::newRow("data url without slash") << QByteArrayLiteral("<html><head><meta charset=\"utf-8\"></head><body><a href=\"data:application/gzip;base64,dGVzdA==\">data URL without slash</a><br/></body></html>") << QStringLiteral("qwe_download.gz") ; + QTest::newRow("data url with slash") << QByteArrayLiteral("<html><head><meta charset=\"utf-8\"></head><body><a href=\"data:application/gzip;base64,dGVzcnI/dGVzdA==\">data URL with filename</a><br/></body></html>") << QStringLiteral("qwe_download.gz") ; + QTest::newRow("data url with download tag") << QByteArrayLiteral("<html><head><meta charset=\"utf-8\"></head><body><a href=\"data:application/gzip;base64,dGVzdA/IHRlc3Q=\" download=\"filename.gz\">data URL with filename</a><br/></body></html>") << QStringLiteral("filename.gz") ; + +} + +void tst_QWebEngineDownloadItem::downloadDataUrls() +{ + QFETCH(QByteArray, htmlData); + QFETCH(QString, expectedFileName); + // Set up HTTP server + ScopedConnection sc1 = connect(m_server, &HttpServer::newRequest, [&](HttpReqRep *rr) { + if (rr->requestMethod() == "GET" && rr->requestPath() == "/") { + rr->setResponseHeader(QByteArrayLiteral("content-type"), QByteArrayLiteral("text/html")); + rr->setResponseBody(htmlData); + rr->sendResponse(); + } + }); + + // Set up profile and download handler + QTemporaryDir tmpDir; + QVERIFY(tmpDir.isValid()); + m_profile->setDownloadPath(tmpDir.path()); + + int downloadRequestCount = 0; + ScopedConnection sc2 = connect(m_profile, &QWebEngineProfile::downloadRequested, [&](QWebEngineDownloadItem *item) { + QCOMPARE(item->state(), QWebEngineDownloadItem::DownloadRequested); + QCOMPARE(item->downloadFileName(), expectedFileName); + downloadRequestCount++; + }); + + QSignalSpy loadSpy(m_page, &QWebEnginePage::loadFinished); + m_view->load(m_server->url()); + QTRY_COMPARE(loadSpy.count(), 1); + QCOMPARE(loadSpy.takeFirst().value(0).toBool(), true); + + // Trigger download + simulateUserAction(QPoint(10, 10), UserAction::ClickLink); + QTRY_COMPARE(downloadRequestCount, 1); +} + QTEST_MAIN(tst_QWebEngineDownloadItem) #include "tst_qwebenginedownloaditem.moc" |