summaryrefslogtreecommitdiffstats
path: root/src/core/download_manager_delegate_qt.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/download_manager_delegate_qt.cpp')
-rw-r--r--src/core/download_manager_delegate_qt.cpp87
1 files changed, 59 insertions, 28 deletions
diff --git a/src/core/download_manager_delegate_qt.cpp b/src/core/download_manager_delegate_qt.cpp
index b5ac6868c..c0fd0d3ee 100644
--- a/src/core/download_manager_delegate_qt.cpp
+++ b/src/core/download_manager_delegate_qt.cpp
@@ -54,14 +54,18 @@ void DownloadManagerDelegateQt::cancelDownload(content::DownloadTargetCallback c
download::DownloadDangerType::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
download::DownloadItem::UNKNOWN,
base::FilePath(),
- absl::nullopt,
+ base::FilePath(),
+ std::string(),
download::DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
}
-void DownloadManagerDelegateQt::cancelDownload(quint32 downloadId)
+bool DownloadManagerDelegateQt::cancelDownload(quint32 downloadId)
{
- if (download::DownloadItem *download = findDownloadById(downloadId))
+ if (download::DownloadItem *download = findDownloadById(downloadId)) {
download->Cancel(/* user_cancel */ true);
+ return true;
+ }
+ return false;
}
void DownloadManagerDelegateQt::pauseDownload(quint32 downloadId)
@@ -95,14 +99,43 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(download::DownloadItem *
download::DownloadDangerType::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
download::DownloadItem::VALIDATED,
item->GetForcedFilePath(),
- absl::nullopt,
+ item->GetFileNameToReportUser(),
+ item->GetMimeType(),
download::DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE);
return true;
}
- QString suggestedFilename = toQt(item->GetSuggestedFilename());
+ bool acceptedByDefault = false;
+ QString suggestedFilePath;
+ QString suggestedFilename;
+ bool isSavePageDownload = false;
+ WebContentsAdapterClient *adapterClient = nullptr;
+ if (content::WebContents *webContents = content::DownloadItemUtils::GetWebContents(item)) {
+ WebContentsDelegateQt *contentsDelegate = static_cast<WebContentsDelegateQt *>(webContents->GetDelegate());
+ adapterClient = contentsDelegate->adapterClient();
+ if (SavePageInfo *spi = contentsDelegate->savePageInfo()) {
+ // We end up here when saving non text-based files (MHTML, PDF or images)
+ suggestedFilePath = spi->requestedFilePath;
+ const QFileInfo fileInfo(suggestedFilePath);
+ if (fileInfo.isRelative()) {
+ const QDir downloadDir(m_profileAdapter->downloadPath());
+ suggestedFilePath = downloadDir.absoluteFilePath(suggestedFilePath);
+ }
+ suggestedFilename = fileInfo.fileName();
+
+ if (!suggestedFilePath.isEmpty() && !suggestedFilename.isEmpty())
+ acceptedByDefault = true;
+ isSavePageDownload = true;
+
+ // Clear the delegate's SavePageInfo. It's only valid for the page currently being saved.
+ contentsDelegate->setSavePageInfo(nullptr);
+ }
+ }
+
QString mimeTypeString = toQt(item->GetMimeType());
+ if (suggestedFilename.isEmpty())
+ suggestedFilename = toQt(item->GetSuggestedFilename());
if (suggestedFilename.isEmpty())
suggestedFilename = toQt(net::HttpContentDisposition(item->GetContentDisposition(), net::kCharsetLatin1).filename());
@@ -125,16 +158,12 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(download::DownloadItem *
QDir defaultDownloadDirectory(m_profileAdapter->downloadPath());
- QString suggestedFilePath = m_profileAdapter->determineDownloadPath(defaultDownloadDirectory.absolutePath(), suggestedFilename, item->GetStartTime().ToTimeT());
+ if (suggestedFilePath.isEmpty())
+ suggestedFilePath = m_profileAdapter->determineDownloadPath(defaultDownloadDirectory.absolutePath(), suggestedFilename, item->GetStartTime().ToTimeT());
item->AddObserver(this);
QList<ProfileAdapterClient*> clients = m_profileAdapter->clients();
if (!clients.isEmpty()) {
- content::WebContents *webContents = content::DownloadItemUtils::GetWebContents(item);
- WebContentsAdapterClient *adapterClient = nullptr;
- if (webContents)
- adapterClient = static_cast<WebContentsDelegateQt *>(webContents->GetDelegate())->adapterClient();
-
Q_ASSERT(m_currentId == item->GetId());
ProfileAdapterClient::DownloadItemInfo info = {
item->GetId(),
@@ -145,17 +174,17 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(download::DownloadItem *
mimeTypeString,
suggestedFilePath,
ProfileAdapterClient::UnknownSavePageFormat,
- false /* accepted */,
+ acceptedByDefault,
false /* paused */,
false /* done */,
- false /* isSavePageDownload */,
+ isSavePageDownload,
item->GetLastReason(),
adapterClient,
suggestedFilename,
item->GetStartTime().ToTimeT()
};
- for (ProfileAdapterClient *client : qAsConst(clients)) {
+ for (ProfileAdapterClient *client : std::as_const(clients)) {
client->downloadRequested(info);
if (info.accepted)
break;
@@ -164,14 +193,14 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(download::DownloadItem *
QFileInfo suggestedFile(info.path);
if (info.accepted && !suggestedFile.absoluteDir().mkpath(suggestedFile.absolutePath())) {
-#if defined(OS_WIN)
+#if defined(Q_OS_WIN)
// TODO: Remove this when https://bugreports.qt.io/browse/QTBUG-85997 is fixed.
QDir suggestedDir = QDir(suggestedFile.absolutePath());
if (!suggestedDir.isRoot() || !suggestedDir.exists()) {
#endif
qWarning("Creating download path failed, download cancelled: %s", suggestedFile.absolutePath().toUtf8().data());
info.accepted = false;
-#if defined(OS_WIN)
+#if defined(Q_OS_WIN)
}
#endif
}
@@ -187,7 +216,8 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(download::DownloadItem *
download::DownloadDangerType::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
download::DownloadItem::VALIDATED,
filePathForCallback.AddExtension(toFilePathString("download")),
- absl::nullopt,
+ base::FilePath(),
+ item->GetMimeType(),
download::DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE);
} else
cancelDownload(std::move(*callback));
@@ -217,12 +247,18 @@ void DownloadManagerDelegateQt::ChooseSavePath(content::WebContents *web_content
if (clients.isEmpty())
return;
+ bool acceptedByDefault = false;
+ QString suggestedFilePath;
+ ProfileAdapterClient::SavePageFormat suggestedSaveFormat = ProfileAdapterClient::UnknownSavePageFormat;
WebContentsDelegateQt *contentsDelegate = static_cast<WebContentsDelegateQt *>(
web_contents->GetDelegate());
- const SavePageInfo &spi = contentsDelegate->savePageInfo();
+ if (SavePageInfo *spi = contentsDelegate->savePageInfo()) {
+ suggestedFilePath = spi->requestedFilePath;
+ suggestedSaveFormat = static_cast<ProfileAdapterClient::SavePageFormat>(spi->requestedFormat);
+ // Clear the delegate's SavePageInfo. It's only valid for the page currently being saved.
+ contentsDelegate->setSavePageInfo(nullptr);
+ }
- bool acceptedByDefault = false;
- QString suggestedFilePath = spi.requestedFilePath;
if (suggestedFilePath.isEmpty()) {
suggestedFilePath = QFileInfo(toQt(suggested_path.AsUTF8Unsafe())).completeBaseName()
+ QStringLiteral(".mhtml");
@@ -234,14 +270,9 @@ void DownloadManagerDelegateQt::ChooseSavePath(content::WebContents *web_content
suggestedFilePath = downloadDir.absoluteFilePath(suggestedFilePath);
}
- ProfileAdapterClient::SavePageFormat suggestedSaveFormat
- = static_cast<ProfileAdapterClient::SavePageFormat>(spi.requestedFormat);
if (suggestedSaveFormat == ProfileAdapterClient::UnknownSavePageFormat)
suggestedSaveFormat = ProfileAdapterClient::MimeHtmlSaveFormat;
- // Clear the delegate's SavePageInfo. It's only valid for the page currently being saved.
- contentsDelegate->setSavePageInfo(SavePageInfo());
-
WebContentsAdapterClient *adapterClient = nullptr;
if (web_contents)
adapterClient = static_cast<WebContentsDelegateQt *>(web_contents->GetDelegate())->adapterClient();
@@ -251,7 +282,7 @@ void DownloadManagerDelegateQt::ChooseSavePath(content::WebContents *web_content
++m_currentId,
toQt(web_contents->GetURL()),
download::DownloadItem::IN_PROGRESS,
- 0, /* totalBytes */
+ -1, /* totalBytes */
0, /* receivedBytes */
QStringLiteral("application/x-mimearchive"),
suggestedFilePath,
@@ -266,7 +297,7 @@ void DownloadManagerDelegateQt::ChooseSavePath(content::WebContents *web_content
QDateTime::currentMSecsSinceEpoch()
};
- for (ProfileAdapterClient *client : qAsConst(clients)) {
+ for (ProfileAdapterClient *client : std::as_const(clients)) {
client->downloadRequested(info);
if (info.accepted)
break;
@@ -314,7 +345,7 @@ void DownloadManagerDelegateQt::OnDownloadUpdated(download::DownloadItem *downlo
download->GetStartTime().ToTimeT()
};
- for (ProfileAdapterClient *client : qAsConst(clients)) {
+ for (ProfileAdapterClient *client : std::as_const(clients)) {
client->downloadUpdated(info);
}
}