From 21ce229776c8ed7a7576702b7850bd862fa45411 Mon Sep 17 00:00:00 2001 From: Szabolcs David Date: Fri, 8 Mar 2024 14:52:54 +0100 Subject: Make download API asynchronous Modify the already existing downloadRequest() API. Now the requests not necessarily have to be answered in a directly connected signal handler and the API users can postpone their user's decision. The only exceptions are automatically accepted save page requests. There is no better place to call their callback than directly after the signal emission. Adapt auto tests to the new lifecycle of download items. Modify the quicknanobrowser example to use a simple async method to wait for the user's decision. Keep simplebrowser to use its old synchronous method to illustrate that it's still possible. Correct documentation at some places. [ChangeLog] QWebEngineProfile::downloadRequested() is not limited to synchronous usage anymore. QWebEngineDownloadRequest can be accepted or rejected later without blocking the browsing session. Fixes: QTBUG-118584 Change-Id: Ic1be6508126574dc77aa686f85bf35feafdb080d Reviewed-by: Peter Varga --- src/core/api/qwebengineprofile.cpp | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'src/core/api/qwebengineprofile.cpp') diff --git a/src/core/api/qwebengineprofile.cpp b/src/core/api/qwebengineprofile.cpp index b0824af35..1c5807a93 100644 --- a/src/core/api/qwebengineprofile.cpp +++ b/src/core/api/qwebengineprofile.cpp @@ -17,7 +17,7 @@ #include "visited_links_manager_qt.h" #include "web_engine_settings.h" -#include +#include #include QT_BEGIN_NAMESPACE @@ -220,6 +220,12 @@ void QWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) { Q_Q(QWebEngineProfile); + if (!q->receivers(SIGNAL(downloadRequested(QWebEngineDownloadRequest *)))) { + m_profileAdapter->acceptDownload(info.id, info.accepted, info.useDownloadTargetCallback, info.path, + info.savePageFormat); + return; + } + Q_ASSERT(!m_ongoingDownloads.contains(info.id)); QWebEngineDownloadRequestPrivate *itemPrivate = new QWebEngineDownloadRequestPrivate(m_profileAdapter); @@ -235,6 +241,7 @@ void QWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) itemPrivate->mimeType = info.mimeType; itemPrivate->savePageFormat = static_cast(info.savePageFormat); itemPrivate->isSavePageDownload = info.isSavePageDownload; + itemPrivate->useDownloadTargetCallback = info.useDownloadTargetCallback; if (info.page && info.page->clientType() == QtWebEngineCore::WebContentsAdapterClient::WidgetsClient) itemPrivate->adapterClient = info.page; else @@ -247,18 +254,9 @@ void QWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) Q_EMIT q->downloadRequested(download); - QWebEngineDownloadRequest::DownloadState state = download->state(); - - info.path = QDir(download->downloadDirectory()).filePath(download->downloadFileName()); - info.savePageFormat = static_cast( - download->savePageFormat()); - info.accepted = state != QWebEngineDownloadRequest::DownloadCancelled; - - if (state == QWebEngineDownloadRequest::DownloadRequested) { - // Delete unaccepted downloads. - info.accepted = false; - delete download; - } + // Callbacks of automatically accepted save operations have to be called here + if (info.isSavePageDownload && info.accepted) + itemPrivate->answer(); } void QWebEngineProfilePrivate::downloadUpdated(const DownloadItemInfo &info) -- cgit v1.2.3