Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 5 | #include "content/shell/browser/shell_download_manager_delegate.h" |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 6 | |
Ho Cheung | 8a5929d | 2023-04-26 17:46:47 | [diff] [blame] | 7 | #include <algorithm> |
David Vallet | 1168352 | 2017-08-23 03:20:46 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Wenbo Jie | 0bc233c | 2024-04-09 04:26:15 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 11 | #include "build/build_config.h" |
Avi Drissman | 62a617a | 2024-01-19 22:37:12 | [diff] [blame] | 12 | #include "components/download/public/common/download_target_info.h" |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 13 | |
| 14 | #if BUILDFLAG(IS_WIN) |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 15 | #include <windows.h> |
Takuto Ikuta | c8d6b16f | 2024-04-15 16:59:19 | [diff] [blame] | 16 | |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 17 | #include <commdlg.h> |
| 18 | #endif |
| 19 | |
Hans Wennborg | 0917de89 | 2020-04-28 20:21:15 | [diff] [blame] | 20 | #include "base/check_op.h" |
[email protected] | e666930 | 2013-03-25 14:31:16 | [diff] [blame] | 21 | #include "base/command_line.h" |
Tom Sepez | 315bfe8 | 2025-03-24 18:00:30 | [diff] [blame] | 22 | #include "base/compiler_specific.h" |
thestig | b7aad54f | 2014-09-05 18:25:39 | [diff] [blame] | 23 | #include "base/files/file_util.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 24 | #include "base/functional/bind.h" |
Hans Wennborg | 0917de89 | 2020-04-28 20:21:15 | [diff] [blame] | 25 | #include "base/notreached.h" |
[email protected] | 21aa9968 | 2013-06-11 07:17:01 | [diff] [blame] | 26 | #include "base/strings/string_util.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 27 | #include "base/strings/utf_string_conversions.h" |
Gabriel Charette | 5e2e7204 | 2020-02-25 01:04:01 | [diff] [blame] | 28 | #include "base/task/thread_pool.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 29 | #include "content/public/browser/browser_context.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 30 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 31 | #include "content/public/browser/browser_thread.h" |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 32 | #include "content/public/browser/download_item_utils.h" |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 33 | #include "content/public/browser/download_manager.h" |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 34 | #include "content/public/browser/web_contents.h" |
[email protected] | b7c504c | 2013-05-07 14:42:12 | [diff] [blame] | 35 | #include "content/shell/common/shell_switches.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 36 | #include "net/base/filename_util.h" |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 37 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 38 | #if BUILDFLAG(IS_WIN) |
[email protected] | 517fce72 | 2014-01-15 05:16:14 | [diff] [blame] | 39 | #include "ui/aura/window.h" |
[email protected] | 7a60cd3a | 2014-03-20 20:54:57 | [diff] [blame] | 40 | #include "ui/aura/window_tree_host.h" |
[email protected] | 517fce72 | 2014-01-15 05:16:14 | [diff] [blame] | 41 | #endif |
| 42 | |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 43 | namespace content { |
| 44 | |
| 45 | ShellDownloadManagerDelegate::ShellDownloadManagerDelegate() |
Jeremy Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 46 | : download_manager_(nullptr), suppress_prompting_(false) {} |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 47 | |
Asanka Herath | 1ba0e9f | 2017-04-03 18:48:53 | [diff] [blame] | 48 | ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate() { |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 49 | if (download_manager_) { |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 50 | download_manager_->SetDelegate(nullptr); |
| 51 | download_manager_ = nullptr; |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 52 | } |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
| 56 | void ShellDownloadManagerDelegate::SetDownloadManager( |
| 57 | DownloadManager* download_manager) { |
| 58 | download_manager_ = download_manager; |
| 59 | } |
| 60 | |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 61 | void ShellDownloadManagerDelegate::Shutdown() { |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 62 | // Revoke any pending callbacks. download_manager_ et. al. are no longer safe |
| 63 | // to access after this point. |
| 64 | weak_ptr_factory_.InvalidateWeakPtrs(); |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 65 | download_manager_ = nullptr; |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 68 | bool ShellDownloadManagerDelegate::DetermineDownloadTarget( |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 69 | download::DownloadItem* download, |
Avi Drissman | 62a617a | 2024-01-19 22:37:12 | [diff] [blame] | 70 | download::DownloadTargetCallback* callback) { |
mostynb | 4e36389 | 2015-03-23 14:35:05 | [diff] [blame] | 71 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 72 | // This assignment needs to be here because even at the call to |
| 73 | // SetDownloadManager, the system is not fully initialized. |
| 74 | if (default_download_path_.empty()) { |
Wenbo Jie | 0bc233c | 2024-04-09 04:26:15 | [diff] [blame] | 75 | default_download_path_ = download_manager_->GetBrowserContext() |
| 76 | ->GetPath() |
| 77 | #if BUILDFLAG(IS_CHROMEOS) |
| 78 | .Append(FILE_PATH_LITERAL("MyFiles")) |
| 79 | #endif |
| 80 | .Append(FILE_PATH_LITERAL("Downloads")); |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 83 | if (!download->GetForcedFilePath().empty()) { |
Avi Drissman | 62a617a | 2024-01-19 22:37:12 | [diff] [blame] | 84 | download::DownloadTargetInfo target_info; |
| 85 | target_info.target_path = download->GetForcedFilePath(); |
| 86 | target_info.intermediate_path = download->GetForcedFilePath(); |
| 87 | |
| 88 | std::move(*callback).Run(std::move(target_info)); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 89 | return true; |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 90 | } |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 91 | |
danakj | a9fe91c | 2019-05-01 19:02:29 | [diff] [blame] | 92 | FilenameDeterminedCallback filename_determined_callback = base::BindOnce( |
| 93 | &ShellDownloadManagerDelegate::OnDownloadPathGenerated, |
danakj | a3cfb833 | 2019-12-10 21:13:33 | [diff] [blame] | 94 | weak_ptr_factory_.GetWeakPtr(), download->GetId(), std::move(*callback)); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 95 | |
Gabriel Charette | 5e2e7204 | 2020-02-25 01:04:01 | [diff] [blame] | 96 | base::ThreadPool::PostTask( |
| 97 | FROM_HERE, |
| 98 | {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN, |
| 99 | base::TaskPriority::USER_VISIBLE}, |
| 100 | base::BindOnce(&ShellDownloadManagerDelegate::GenerateFilename, |
| 101 | download->GetURL(), download->GetContentDisposition(), |
| 102 | download->GetSuggestedFilename(), download->GetMimeType(), |
| 103 | default_download_path_, |
| 104 | std::move(filename_determined_callback))); |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 105 | return true; |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | e666930 | 2013-03-25 14:31:16 | [diff] [blame] | 108 | bool ShellDownloadManagerDelegate::ShouldOpenDownload( |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 109 | download::DownloadItem* item, |
danakj | 8c70a44 | 2019-12-10 22:48:24 | [diff] [blame] | 110 | DownloadOpenDelayedCallback callback) { |
[email protected] | e666930 | 2013-03-25 14:31:16 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
danakj | a6dd6f5 | 2019-12-10 19:04:52 | [diff] [blame] | 114 | void ShellDownloadManagerDelegate::GetNextId(DownloadIdCallback callback) { |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 115 | static uint32_t next_id = download::DownloadItem::kInvalidId + 1; |
danakj | a6dd6f5 | 2019-12-10 19:04:52 | [diff] [blame] | 116 | std::move(callback).Run(next_id++); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 119 | // static |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 120 | void ShellDownloadManagerDelegate::GenerateFilename( |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 121 | const GURL& url, |
| 122 | const std::string& content_disposition, |
| 123 | const std::string& suggested_filename, |
| 124 | const std::string& mime_type, |
| 125 | const base::FilePath& suggested_directory, |
danakj | a9fe91c | 2019-05-01 19:02:29 | [diff] [blame] | 126 | FilenameDeterminedCallback callback) { |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 127 | base::FilePath generated_name = net::GenerateFileName(url, |
| 128 | content_disposition, |
| 129 | std::string(), |
| 130 | suggested_filename, |
| 131 | mime_type, |
| 132 | "download"); |
| 133 | |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 134 | if (!base::PathExists(suggested_directory)) |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 135 | base::CreateDirectory(suggested_directory); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 136 | |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 137 | base::FilePath suggested_path(suggested_directory.Append(generated_name)); |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 138 | GetUIThreadTaskRunner({})->PostTask( |
| 139 | FROM_HERE, base::BindOnce(std::move(callback), suggested_path)); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 142 | void ShellDownloadManagerDelegate::OnDownloadPathGenerated( |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 143 | uint32_t download_id, |
Avi Drissman | 62a617a | 2024-01-19 22:37:12 | [diff] [blame] | 144 | download::DownloadTargetCallback callback, |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 145 | const base::FilePath& suggested_path) { |
mostynb | 4e36389 | 2015-03-23 14:35:05 | [diff] [blame] | 146 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 147 | if (suppress_prompting_) { |
| 148 | // Testing exit. |
Avi Drissman | 62a617a | 2024-01-19 22:37:12 | [diff] [blame] | 149 | download::DownloadTargetInfo target_info; |
| 150 | target_info.target_path = suggested_path; |
| 151 | target_info.intermediate_path = |
| 152 | suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload")); |
| 153 | |
| 154 | std::move(callback).Run(std::move(target_info)); |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
danakj | a3cfb833 | 2019-12-10 21:13:33 | [diff] [blame] | 158 | ChooseDownloadPath(download_id, std::move(callback), suggested_path); |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 159 | } |
| 160 | |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 161 | void ShellDownloadManagerDelegate::ChooseDownloadPath( |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 162 | uint32_t download_id, |
Avi Drissman | 62a617a | 2024-01-19 22:37:12 | [diff] [blame] | 163 | download::DownloadTargetCallback callback, |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 164 | const base::FilePath& suggested_path) { |
mostynb | 4e36389 | 2015-03-23 14:35:05 | [diff] [blame] | 165 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 166 | download::DownloadItem* item = download_manager_->GetDownload(download_id); |
| 167 | if (!item || (item->GetState() != download::DownloadItem::IN_PROGRESS)) |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 168 | return; |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 169 | |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 170 | base::FilePath result; |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 171 | #if BUILDFLAG(IS_WIN) |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 172 | std::wstring file_part = base::FilePath(suggested_path).BaseName().value(); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 173 | wchar_t file_name[MAX_PATH]; |
Tom Sepez | 315bfe8 | 2025-03-24 18:00:30 | [diff] [blame] | 174 | UNSAFE_TODO( |
| 175 | base::wcslcpy(file_name, file_part.c_str(), std::size(file_name))); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 176 | OPENFILENAME save_as; |
Tom Sepez | 315bfe8 | 2025-03-24 18:00:30 | [diff] [blame] | 177 | UNSAFE_TODO(ZeroMemory(&save_as, sizeof(save_as))); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 178 | save_as.lStructSize = sizeof(OPENFILENAME); |
Tsuyoshi Horo | ab90fd50 | 2019-11-13 19:18:59 | [diff] [blame] | 179 | WebContents* web_contents = DownloadItemUtils::GetWebContents(item); |
| 180 | // |web_contents| could be null if the tab was quickly closed. |
| 181 | if (!web_contents) |
| 182 | return; |
| 183 | save_as.hwndOwner = |
| 184 | web_contents->GetNativeView()->GetHost()->GetAcceleratedWidget(); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 185 | save_as.lpstrFile = file_name; |
Daniel Cheng | ad44af2f | 2022-02-26 18:07:54 | [diff] [blame] | 186 | save_as.nMaxFile = std::size(file_name); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 187 | |
| 188 | std::wstring directory; |
| 189 | if (!suggested_path.empty()) |
| 190 | directory = suggested_path.DirName().value(); |
| 191 | |
| 192 | save_as.lpstrInitialDir = directory.c_str(); |
| 193 | save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING | |
| 194 | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST; |
| 195 | |
| 196 | if (GetSaveFileName(&save_as)) |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 197 | result = base::FilePath(std::wstring(save_as.lpstrFile)); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 198 | #else |
| 199 | NOTIMPLEMENTED(); |
| 200 | #endif |
| 201 | |
Avi Drissman | 62a617a | 2024-01-19 22:37:12 | [diff] [blame] | 202 | download::DownloadTargetInfo target_info; |
| 203 | target_info.target_path = result; |
| 204 | target_info.intermediate_path = result; |
| 205 | target_info.target_disposition = |
| 206 | download::DownloadItem::TARGET_DISPOSITION_PROMPT; |
| 207 | |
| 208 | std::move(callback).Run(std::move(target_info)); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 211 | void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting( |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 212 | const base::FilePath& default_download_path) { |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 213 | default_download_path_ = default_download_path; |
| 214 | suppress_prompting_ = true; |
| 215 | } |
| 216 | |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 217 | } // namespace content |