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