blob: 38664c79ea41fa091c493395ada05a39213ea406 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
[email protected]98d6f152011-09-29 19:35:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]de7d61ff2013-08-20 11:30:415#include "content/shell/browser/shell_download_manager_delegate.h"
[email protected]98d6f152011-09-29 19:35:516
David Vallet11683522017-08-23 03:20:467#include <string>
8
Xiaohan Wangbd084422022-01-15 18:47:519#include "build/build_config.h"
10
11#if BUILDFLAG(IS_WIN)
[email protected]5c12f4a2011-09-30 21:59:0412#include <windows.h>
13#include <commdlg.h>
14#endif
15
Hans Wennborg0917de892020-04-28 20:21:1516#include "base/check_op.h"
[email protected]e6669302013-03-25 14:31:1617#include "base/command_line.h"
Lei Zhangae2ff1e2021-05-25 17:29:3918#include "base/cxx17_backports.h"
thestigb7aad54f2014-09-05 18:25:3919#include "base/files/file_util.h"
Avi Drissmanadac21992023-01-11 23:46:3920#include "base/functional/bind.h"
Hans Wennborg0917de892020-04-28 20:21:1521#include "base/notreached.h"
[email protected]21aa99682013-06-11 07:17:0122#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0023#include "base/strings/utf_string_conversions.h"
Gabriel Charette5e2e72042020-02-25 01:04:0124#include "base/task/thread_pool.h"
[email protected]ccb797302011-12-15 16:55:1125#include "content/public/browser/browser_context.h"
Eric Seckler8652dcd52018-09-20 10:42:2826#include "content/public/browser/browser_task_traits.h"
[email protected]c38831a12011-10-28 12:44:4927#include "content/public/browser/browser_thread.h"
Min Qina9f487872018-02-09 20:43:2328#include "content/public/browser/download_item_utils.h"
[email protected]e582fdd2011-12-20 16:48:1729#include "content/public/browser/download_manager.h"
[email protected]0b659b32012-03-26 21:29:3230#include "content/public/browser/web_contents.h"
[email protected]b7c504c2013-05-07 14:42:1231#include "content/shell/common/shell_switches.h"
[email protected]d96cf752014-04-09 04:05:2832#include "net/base/filename_util.h"
[email protected]98d6f152011-09-29 19:35:5133
Xiaohan Wangbd084422022-01-15 18:47:5134#if BUILDFLAG(IS_WIN)
[email protected]517fce722014-01-15 05:16:1435#include "ui/aura/window.h"
[email protected]7a60cd3a2014-03-20 20:54:5736#include "ui/aura/window_tree_host.h"
[email protected]517fce722014-01-15 05:16:1437#endif
38
[email protected]98d6f152011-09-29 19:35:5139namespace content {
40
41ShellDownloadManagerDelegate::ShellDownloadManagerDelegate()
Jeremy Roman3bca4bf2019-07-11 03:41:2542 : download_manager_(nullptr), suppress_prompting_(false) {}
[email protected]98d6f152011-09-29 19:35:5143
Asanka Herath1ba0e9f2017-04-03 18:48:5344ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate() {
[email protected]7cef82172013-12-17 06:58:3745 if (download_manager_) {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2846 download_manager_->SetDelegate(nullptr);
47 download_manager_ = nullptr;
[email protected]7cef82172013-12-17 06:58:3748 }
[email protected]98d6f152011-09-29 19:35:5149}
50
51
52void ShellDownloadManagerDelegate::SetDownloadManager(
53 DownloadManager* download_manager) {
54 download_manager_ = download_manager;
55}
56
[email protected]854e1312012-07-30 17:26:3057void ShellDownloadManagerDelegate::Shutdown() {
[email protected]7cef82172013-12-17 06:58:3758 // Revoke any pending callbacks. download_manager_ et. al. are no longer safe
59 // to access after this point.
60 weak_ptr_factory_.InvalidateWeakPtrs();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2861 download_manager_ = nullptr;
[email protected]854e1312012-07-30 17:26:3062}
63
[email protected]47665442012-07-27 02:31:2264bool ShellDownloadManagerDelegate::DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:2365 download::DownloadItem* download,
danakja3cfb8332019-12-10 21:13:3366 DownloadTargetCallback* callback) {
mostynb4e363892015-03-23 14:35:0567 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]a558b43a2012-08-30 17:09:2768 // This assignment needs to be here because even at the call to
69 // SetDownloadManager, the system is not fully initialized.
70 if (default_download_path_.empty()) {
71 default_download_path_ = download_manager_->GetBrowserContext()->GetPath().
72 Append(FILE_PATH_LITERAL("Downloads"));
73 }
74
[email protected]3d833de2012-05-30 23:32:0675 if (!download->GetForcedFilePath().empty()) {
danakja3cfb8332019-12-10 21:13:3376 std::move(*callback).Run(
77 download->GetForcedFilePath(),
78 download::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
79 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
Joe DeBlasio70f924e2022-12-12 22:10:2180 download::DownloadItem::InsecureDownloadStatus::UNKNOWN,
Min Qin49bbefe2022-03-17 21:11:1881 download->GetForcedFilePath(), base::FilePath(),
Min Qin4c4e66a2022-06-22 07:45:3782 std::string() /*mime_type*/, download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]98d6f152011-09-29 19:35:5183 return true;
[email protected]3d833de2012-05-30 23:32:0684 }
[email protected]98d6f152011-09-29 19:35:5185
danakja9fe91c2019-05-01 19:02:2986 FilenameDeterminedCallback filename_determined_callback = base::BindOnce(
87 &ShellDownloadManagerDelegate::OnDownloadPathGenerated,
danakja3cfb8332019-12-10 21:13:3388 weak_ptr_factory_.GetWeakPtr(), download->GetId(), std::move(*callback));
[email protected]98d6f152011-09-29 19:35:5189
Gabriel Charette5e2e72042020-02-25 01:04:0190 base::ThreadPool::PostTask(
91 FROM_HERE,
92 {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
93 base::TaskPriority::USER_VISIBLE},
94 base::BindOnce(&ShellDownloadManagerDelegate::GenerateFilename,
95 download->GetURL(), download->GetContentDisposition(),
96 download->GetSuggestedFilename(), download->GetMimeType(),
97 default_download_path_,
98 std::move(filename_determined_callback)));
[email protected]a558b43a2012-08-30 17:09:2799 return true;
[email protected]98d6f152011-09-29 19:35:51100}
101
[email protected]e6669302013-03-25 14:31:16102bool ShellDownloadManagerDelegate::ShouldOpenDownload(
Min Qina9f487872018-02-09 20:43:23103 download::DownloadItem* item,
danakj8c70a442019-12-10 22:48:24104 DownloadOpenDelayedCallback callback) {
[email protected]e6669302013-03-25 14:31:16105 return true;
106}
107
danakja6dd6f52019-12-10 19:04:52108void ShellDownloadManagerDelegate::GetNextId(DownloadIdCallback callback) {
Min Qina9f487872018-02-09 20:43:23109 static uint32_t next_id = download::DownloadItem::kInvalidId + 1;
danakja6dd6f52019-12-10 19:04:52110 std::move(callback).Run(next_id++);
[email protected]530047e2013-07-12 17:02:25111}
112
[email protected]7cef82172013-12-17 06:58:37113// static
[email protected]98d6f152011-09-29 19:35:51114void ShellDownloadManagerDelegate::GenerateFilename(
[email protected]7cef82172013-12-17 06:58:37115 const GURL& url,
116 const std::string& content_disposition,
117 const std::string& suggested_filename,
118 const std::string& mime_type,
119 const base::FilePath& suggested_directory,
danakja9fe91c2019-05-01 19:02:29120 FilenameDeterminedCallback callback) {
[email protected]7cef82172013-12-17 06:58:37121 base::FilePath generated_name = net::GenerateFileName(url,
122 content_disposition,
123 std::string(),
124 suggested_filename,
125 mime_type,
126 "download");
127
[email protected]7567484142013-07-11 17:36:07128 if (!base::PathExists(suggested_directory))
[email protected]426d1c92013-12-03 20:08:54129 base::CreateDirectory(suggested_directory);
[email protected]98d6f152011-09-29 19:35:51130
[email protected]d30a36f2013-02-07 04:16:26131 base::FilePath suggested_path(suggested_directory.Append(generated_name));
Gabriel Charettee7cdc5cd2020-05-27 23:35:05132 GetUIThreadTaskRunner({})->PostTask(
133 FROM_HERE, base::BindOnce(std::move(callback), suggested_path));
[email protected]98d6f152011-09-29 19:35:51134}
135
[email protected]a558b43a2012-08-30 17:09:27136void ShellDownloadManagerDelegate::OnDownloadPathGenerated(
avi66a07722015-12-25 23:38:12137 uint32_t download_id,
danakja3cfb8332019-12-10 21:13:33138 DownloadTargetCallback callback,
[email protected]d30a36f2013-02-07 04:16:26139 const base::FilePath& suggested_path) {
mostynb4e363892015-03-23 14:35:05140 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]a558b43a2012-08-30 17:09:27141 if (suppress_prompting_) {
142 // Testing exit.
danakja3cfb8332019-12-10 21:13:33143 std::move(callback).Run(
144 suggested_path, download::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
145 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
Joe DeBlasio70f924e2022-12-12 22:10:21146 download::DownloadItem::InsecureDownloadStatus::UNKNOWN,
danakja3cfb8332019-12-10 21:13:33147 suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload")),
Min Qin83d70f02022-04-15 21:49:59148 base::FilePath(), std::string() /*mime_type*/,
danakja3cfb8332019-12-10 21:13:33149 download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]a558b43a2012-08-30 17:09:27150 return;
151 }
152
danakja3cfb8332019-12-10 21:13:33153 ChooseDownloadPath(download_id, std::move(callback), suggested_path);
[email protected]a558b43a2012-08-30 17:09:27154}
155
[email protected]47665442012-07-27 02:31:22156void ShellDownloadManagerDelegate::ChooseDownloadPath(
avi66a07722015-12-25 23:38:12157 uint32_t download_id,
danakja3cfb8332019-12-10 21:13:33158 DownloadTargetCallback callback,
[email protected]d30a36f2013-02-07 04:16:26159 const base::FilePath& suggested_path) {
mostynb4e363892015-03-23 14:35:05160 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Min Qina9f487872018-02-09 20:43:23161 download::DownloadItem* item = download_manager_->GetDownload(download_id);
162 if (!item || (item->GetState() != download::DownloadItem::IN_PROGRESS))
[email protected]98d6f152011-09-29 19:35:51163 return;
[email protected]3d833de2012-05-30 23:32:06164
[email protected]d30a36f2013-02-07 04:16:26165 base::FilePath result;
Xiaohan Wangbd084422022-01-15 18:47:51166#if BUILDFLAG(IS_WIN)
[email protected]d30a36f2013-02-07 04:16:26167 std::wstring file_part = base::FilePath(suggested_path).BaseName().value();
[email protected]5c12f4a2011-09-30 21:59:04168 wchar_t file_name[MAX_PATH];
Daniel Chengad44af2f2022-02-26 18:07:54169 base::wcslcpy(file_name, file_part.c_str(), std::size(file_name));
[email protected]5c12f4a2011-09-30 21:59:04170 OPENFILENAME save_as;
171 ZeroMemory(&save_as, sizeof(save_as));
172 save_as.lStructSize = sizeof(OPENFILENAME);
Tsuyoshi Horoab90fd502019-11-13 19:18:59173 WebContents* web_contents = DownloadItemUtils::GetWebContents(item);
174 // |web_contents| could be null if the tab was quickly closed.
175 if (!web_contents)
176 return;
177 save_as.hwndOwner =
178 web_contents->GetNativeView()->GetHost()->GetAcceleratedWidget();
[email protected]5c12f4a2011-09-30 21:59:04179 save_as.lpstrFile = file_name;
Daniel Chengad44af2f2022-02-26 18:07:54180 save_as.nMaxFile = std::size(file_name);
[email protected]5c12f4a2011-09-30 21:59:04181
182 std::wstring directory;
183 if (!suggested_path.empty())
184 directory = suggested_path.DirName().value();
185
186 save_as.lpstrInitialDir = directory.c_str();
187 save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
188 OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
189
190 if (GetSaveFileName(&save_as))
[email protected]d30a36f2013-02-07 04:16:26191 result = base::FilePath(std::wstring(save_as.lpstrFile));
[email protected]5c12f4a2011-09-30 21:59:04192#else
193 NOTIMPLEMENTED();
194#endif
195
Joe DeBlasio70f924e2022-12-12 22:10:21196 std::move(callback).Run(
197 result, download::DownloadItem::TARGET_DISPOSITION_PROMPT,
198 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
199 download::DownloadItem::InsecureDownloadStatus::UNKNOWN, result,
200 base::FilePath(), std::string() /*mime_type*/,
201 download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]98d6f152011-09-29 19:35:51202}
203
[email protected]a558b43a2012-08-30 17:09:27204void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting(
[email protected]d30a36f2013-02-07 04:16:26205 const base::FilePath& default_download_path) {
[email protected]a558b43a2012-08-30 17:09:27206 default_download_path_ = default_download_path;
207 suppress_prompting_ = true;
208}
209
[email protected]98d6f152011-09-29 19:35:51210} // namespace content