blob: 2476f25d2e06e58be2d651c955e275ab42c12dca [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
Ho Cheung8a5929d2023-04-26 17:46:477#include <algorithm>
David Vallet11683522017-08-23 03:20:468#include <string>
9
Wenbo Jie0bc233c2024-04-09 04:26:1510#include "base/files/file_path.h"
Xiaohan Wangbd084422022-01-15 18:47:5111#include "build/build_config.h"
Avi Drissman62a617a2024-01-19 22:37:1212#include "components/download/public/common/download_target_info.h"
Xiaohan Wangbd084422022-01-15 18:47:5113
14#if BUILDFLAG(IS_WIN)
[email protected]5c12f4a2011-09-30 21:59:0415#include <windows.h>
Takuto Ikutac8d6b16f2024-04-15 16:59:1916
[email protected]5c12f4a2011-09-30 21:59:0417#include <commdlg.h>
18#endif
19
Hans Wennborg0917de892020-04-28 20:21:1520#include "base/check_op.h"
[email protected]e6669302013-03-25 14:31:1621#include "base/command_line.h"
Tom Sepez315bfe82025-03-24 18:00:3022#include "base/compiler_specific.h"
thestigb7aad54f2014-09-05 18:25:3923#include "base/files/file_util.h"
Avi Drissmanadac21992023-01-11 23:46:3924#include "base/functional/bind.h"
Hans Wennborg0917de892020-04-28 20:21:1525#include "base/notreached.h"
[email protected]21aa99682013-06-11 07:17:0126#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0027#include "base/strings/utf_string_conversions.h"
Gabriel Charette5e2e72042020-02-25 01:04:0128#include "base/task/thread_pool.h"
[email protected]ccb797302011-12-15 16:55:1129#include "content/public/browser/browser_context.h"
Eric Seckler8652dcd52018-09-20 10:42:2830#include "content/public/browser/browser_task_traits.h"
[email protected]c38831a12011-10-28 12:44:4931#include "content/public/browser/browser_thread.h"
Min Qina9f487872018-02-09 20:43:2332#include "content/public/browser/download_item_utils.h"
[email protected]e582fdd2011-12-20 16:48:1733#include "content/public/browser/download_manager.h"
[email protected]0b659b32012-03-26 21:29:3234#include "content/public/browser/web_contents.h"
[email protected]b7c504c2013-05-07 14:42:1235#include "content/shell/common/shell_switches.h"
[email protected]d96cf752014-04-09 04:05:2836#include "net/base/filename_util.h"
[email protected]98d6f152011-09-29 19:35:5137
Xiaohan Wangbd084422022-01-15 18:47:5138#if BUILDFLAG(IS_WIN)
[email protected]517fce722014-01-15 05:16:1439#include "ui/aura/window.h"
[email protected]7a60cd3a2014-03-20 20:54:5740#include "ui/aura/window_tree_host.h"
[email protected]517fce722014-01-15 05:16:1441#endif
42
[email protected]98d6f152011-09-29 19:35:5143namespace content {
44
45ShellDownloadManagerDelegate::ShellDownloadManagerDelegate()
Jeremy Roman3bca4bf2019-07-11 03:41:2546 : download_manager_(nullptr), suppress_prompting_(false) {}
[email protected]98d6f152011-09-29 19:35:5147
Asanka Herath1ba0e9f2017-04-03 18:48:5348ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate() {
[email protected]7cef82172013-12-17 06:58:3749 if (download_manager_) {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2850 download_manager_->SetDelegate(nullptr);
51 download_manager_ = nullptr;
[email protected]7cef82172013-12-17 06:58:3752 }
[email protected]98d6f152011-09-29 19:35:5153}
54
55
56void ShellDownloadManagerDelegate::SetDownloadManager(
57 DownloadManager* download_manager) {
58 download_manager_ = download_manager;
59}
60
[email protected]854e1312012-07-30 17:26:3061void ShellDownloadManagerDelegate::Shutdown() {
[email protected]7cef82172013-12-17 06:58:3762 // Revoke any pending callbacks. download_manager_ et. al. are no longer safe
63 // to access after this point.
64 weak_ptr_factory_.InvalidateWeakPtrs();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2865 download_manager_ = nullptr;
[email protected]854e1312012-07-30 17:26:3066}
67
[email protected]47665442012-07-27 02:31:2268bool ShellDownloadManagerDelegate::DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:2369 download::DownloadItem* download,
Avi Drissman62a617a2024-01-19 22:37:1270 download::DownloadTargetCallback* callback) {
mostynb4e363892015-03-23 14:35:0571 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]a558b43a2012-08-30 17:09:2772 // 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 Jie0bc233c2024-04-09 04:26:1575 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]a558b43a2012-08-30 17:09:2781 }
82
[email protected]3d833de2012-05-30 23:32:0683 if (!download->GetForcedFilePath().empty()) {
Avi Drissman62a617a2024-01-19 22:37:1284 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]98d6f152011-09-29 19:35:5189 return true;
[email protected]3d833de2012-05-30 23:32:0690 }
[email protected]98d6f152011-09-29 19:35:5191
danakja9fe91c2019-05-01 19:02:2992 FilenameDeterminedCallback filename_determined_callback = base::BindOnce(
93 &ShellDownloadManagerDelegate::OnDownloadPathGenerated,
danakja3cfb8332019-12-10 21:13:3394 weak_ptr_factory_.GetWeakPtr(), download->GetId(), std::move(*callback));
[email protected]98d6f152011-09-29 19:35:5195
Gabriel Charette5e2e72042020-02-25 01:04:0196 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]a558b43a2012-08-30 17:09:27105 return true;
[email protected]98d6f152011-09-29 19:35:51106}
107
[email protected]e6669302013-03-25 14:31:16108bool ShellDownloadManagerDelegate::ShouldOpenDownload(
Min Qina9f487872018-02-09 20:43:23109 download::DownloadItem* item,
danakj8c70a442019-12-10 22:48:24110 DownloadOpenDelayedCallback callback) {
[email protected]e6669302013-03-25 14:31:16111 return true;
112}
113
danakja6dd6f52019-12-10 19:04:52114void ShellDownloadManagerDelegate::GetNextId(DownloadIdCallback callback) {
Min Qina9f487872018-02-09 20:43:23115 static uint32_t next_id = download::DownloadItem::kInvalidId + 1;
danakja6dd6f52019-12-10 19:04:52116 std::move(callback).Run(next_id++);
[email protected]530047e2013-07-12 17:02:25117}
118
[email protected]7cef82172013-12-17 06:58:37119// static
[email protected]98d6f152011-09-29 19:35:51120void ShellDownloadManagerDelegate::GenerateFilename(
[email protected]7cef82172013-12-17 06:58:37121 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,
danakja9fe91c2019-05-01 19:02:29126 FilenameDeterminedCallback callback) {
[email protected]7cef82172013-12-17 06:58:37127 base::FilePath generated_name = net::GenerateFileName(url,
128 content_disposition,
129 std::string(),
130 suggested_filename,
131 mime_type,
132 "download");
133
[email protected]7567484142013-07-11 17:36:07134 if (!base::PathExists(suggested_directory))
[email protected]426d1c92013-12-03 20:08:54135 base::CreateDirectory(suggested_directory);
[email protected]98d6f152011-09-29 19:35:51136
[email protected]d30a36f2013-02-07 04:16:26137 base::FilePath suggested_path(suggested_directory.Append(generated_name));
Gabriel Charettee7cdc5cd2020-05-27 23:35:05138 GetUIThreadTaskRunner({})->PostTask(
139 FROM_HERE, base::BindOnce(std::move(callback), suggested_path));
[email protected]98d6f152011-09-29 19:35:51140}
141
[email protected]a558b43a2012-08-30 17:09:27142void ShellDownloadManagerDelegate::OnDownloadPathGenerated(
avi66a07722015-12-25 23:38:12143 uint32_t download_id,
Avi Drissman62a617a2024-01-19 22:37:12144 download::DownloadTargetCallback callback,
[email protected]d30a36f2013-02-07 04:16:26145 const base::FilePath& suggested_path) {
mostynb4e363892015-03-23 14:35:05146 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]a558b43a2012-08-30 17:09:27147 if (suppress_prompting_) {
148 // Testing exit.
Avi Drissman62a617a2024-01-19 22:37:12149 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]a558b43a2012-08-30 17:09:27155 return;
156 }
157
danakja3cfb8332019-12-10 21:13:33158 ChooseDownloadPath(download_id, std::move(callback), suggested_path);
[email protected]a558b43a2012-08-30 17:09:27159}
160
[email protected]47665442012-07-27 02:31:22161void ShellDownloadManagerDelegate::ChooseDownloadPath(
avi66a07722015-12-25 23:38:12162 uint32_t download_id,
Avi Drissman62a617a2024-01-19 22:37:12163 download::DownloadTargetCallback callback,
[email protected]d30a36f2013-02-07 04:16:26164 const base::FilePath& suggested_path) {
mostynb4e363892015-03-23 14:35:05165 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Min Qina9f487872018-02-09 20:43:23166 download::DownloadItem* item = download_manager_->GetDownload(download_id);
167 if (!item || (item->GetState() != download::DownloadItem::IN_PROGRESS))
[email protected]98d6f152011-09-29 19:35:51168 return;
[email protected]3d833de2012-05-30 23:32:06169
[email protected]d30a36f2013-02-07 04:16:26170 base::FilePath result;
Xiaohan Wangbd084422022-01-15 18:47:51171#if BUILDFLAG(IS_WIN)
[email protected]d30a36f2013-02-07 04:16:26172 std::wstring file_part = base::FilePath(suggested_path).BaseName().value();
[email protected]5c12f4a2011-09-30 21:59:04173 wchar_t file_name[MAX_PATH];
Tom Sepez315bfe82025-03-24 18:00:30174 UNSAFE_TODO(
175 base::wcslcpy(file_name, file_part.c_str(), std::size(file_name)));
[email protected]5c12f4a2011-09-30 21:59:04176 OPENFILENAME save_as;
Tom Sepez315bfe82025-03-24 18:00:30177 UNSAFE_TODO(ZeroMemory(&save_as, sizeof(save_as)));
[email protected]5c12f4a2011-09-30 21:59:04178 save_as.lStructSize = sizeof(OPENFILENAME);
Tsuyoshi Horoab90fd502019-11-13 19:18:59179 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]5c12f4a2011-09-30 21:59:04185 save_as.lpstrFile = file_name;
Daniel Chengad44af2f2022-02-26 18:07:54186 save_as.nMaxFile = std::size(file_name);
[email protected]5c12f4a2011-09-30 21:59:04187
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]d30a36f2013-02-07 04:16:26197 result = base::FilePath(std::wstring(save_as.lpstrFile));
[email protected]5c12f4a2011-09-30 21:59:04198#else
199 NOTIMPLEMENTED();
200#endif
201
Avi Drissman62a617a2024-01-19 22:37:12202 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]98d6f152011-09-29 19:35:51209}
210
[email protected]a558b43a2012-08-30 17:09:27211void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting(
[email protected]d30a36f2013-02-07 04:16:26212 const base::FilePath& default_download_path) {
[email protected]a558b43a2012-08-30 17:09:27213 default_download_path_ = default_download_path;
214 suppress_prompting_ = true;
215}
216
[email protected]98d6f152011-09-29 19:35:51217} // namespace content