blob: 3c68e9952ef0715881cfc82e1ec08b7313d062ac [file] [log] [blame]
[email protected]de7d61ff2013-08-20 11:30:411// Copyright 2013 The Chromium Authors. All rights reserved.
[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
[email protected]98d6f152011-09-29 19:35:5116#include "base/bind.h"
Hans Wennborg0917de892020-04-28 20:21:1517#include "base/check_op.h"
[email protected]e6669302013-03-25 14:31:1618#include "base/command_line.h"
Lei Zhangae2ff1e2021-05-25 17:29:3919#include "base/cxx17_backports.h"
thestigb7aad54f2014-09-05 18:25:3920#include "base/files/file_util.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"
avi66a07722015-12-25 23:38:1225#include "build/build_config.h"
[email protected]ccb797302011-12-15 16:55:1126#include "content/public/browser/browser_context.h"
Eric Seckler8652dcd52018-09-20 10:42:2827#include "content/public/browser/browser_task_traits.h"
[email protected]c38831a12011-10-28 12:44:4928#include "content/public/browser/browser_thread.h"
Min Qina9f487872018-02-09 20:43:2329#include "content/public/browser/download_item_utils.h"
[email protected]e582fdd2011-12-20 16:48:1730#include "content/public/browser/download_manager.h"
[email protected]0b659b32012-03-26 21:29:3231#include "content/public/browser/web_contents.h"
[email protected]b7c504c2013-05-07 14:42:1232#include "content/shell/common/shell_switches.h"
[email protected]d96cf752014-04-09 04:05:2833#include "net/base/filename_util.h"
[email protected]98d6f152011-09-29 19:35:5134
Xiaohan Wangbd084422022-01-15 18:47:5135#if BUILDFLAG(IS_WIN)
[email protected]517fce722014-01-15 05:16:1436#include "ui/aura/window.h"
[email protected]7a60cd3a2014-03-20 20:54:5737#include "ui/aura/window_tree_host.h"
[email protected]517fce722014-01-15 05:16:1438#endif
39
[email protected]98d6f152011-09-29 19:35:5140namespace content {
41
42ShellDownloadManagerDelegate::ShellDownloadManagerDelegate()
Jeremy Roman3bca4bf2019-07-11 03:41:2543 : download_manager_(nullptr), suppress_prompting_(false) {}
[email protected]98d6f152011-09-29 19:35:5144
Asanka Herath1ba0e9f2017-04-03 18:48:5345ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate() {
[email protected]7cef82172013-12-17 06:58:3746 if (download_manager_) {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2847 download_manager_->SetDelegate(nullptr);
48 download_manager_ = nullptr;
[email protected]7cef82172013-12-17 06:58:3749 }
[email protected]98d6f152011-09-29 19:35:5150}
51
52
53void ShellDownloadManagerDelegate::SetDownloadManager(
54 DownloadManager* download_manager) {
55 download_manager_ = download_manager;
56}
57
[email protected]854e1312012-07-30 17:26:3058void ShellDownloadManagerDelegate::Shutdown() {
[email protected]7cef82172013-12-17 06:58:3759 // Revoke any pending callbacks. download_manager_ et. al. are no longer safe
60 // to access after this point.
61 weak_ptr_factory_.InvalidateWeakPtrs();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2862 download_manager_ = nullptr;
[email protected]854e1312012-07-30 17:26:3063}
64
[email protected]47665442012-07-27 02:31:2265bool ShellDownloadManagerDelegate::DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:2366 download::DownloadItem* download,
danakja3cfb8332019-12-10 21:13:3367 DownloadTargetCallback* callback) {
mostynb4e363892015-03-23 14:35:0568 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]a558b43a2012-08-30 17:09:2769 // 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]3d833de2012-05-30 23:32:0676 if (!download->GetForcedFilePath().empty()) {
danakja3cfb8332019-12-10 21:13:3377 std::move(*callback).Run(
78 download->GetForcedFilePath(),
79 download::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
80 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
Joe DeBlasioc427ca032020-01-17 19:15:4381 download::DownloadItem::MixedContentStatus::UNKNOWN,
Min Qin49bbefe2022-03-17 21:11:1882 download->GetForcedFilePath(), base::FilePath(),
Min Qin4c4e66a2022-06-22 07:45:3783 std::string() /*mime_type*/, download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]98d6f152011-09-29 19:35:5184 return true;
[email protected]3d833de2012-05-30 23:32:0685 }
[email protected]98d6f152011-09-29 19:35:5186
danakja9fe91c2019-05-01 19:02:2987 FilenameDeterminedCallback filename_determined_callback = base::BindOnce(
88 &ShellDownloadManagerDelegate::OnDownloadPathGenerated,
danakja3cfb8332019-12-10 21:13:3389 weak_ptr_factory_.GetWeakPtr(), download->GetId(), std::move(*callback));
[email protected]98d6f152011-09-29 19:35:5190
Gabriel Charette5e2e72042020-02-25 01:04:0191 base::ThreadPool::PostTask(
92 FROM_HERE,
93 {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
94 base::TaskPriority::USER_VISIBLE},
95 base::BindOnce(&ShellDownloadManagerDelegate::GenerateFilename,
96 download->GetURL(), download->GetContentDisposition(),
97 download->GetSuggestedFilename(), download->GetMimeType(),
98 default_download_path_,
99 std::move(filename_determined_callback)));
[email protected]a558b43a2012-08-30 17:09:27100 return true;
[email protected]98d6f152011-09-29 19:35:51101}
102
[email protected]e6669302013-03-25 14:31:16103bool ShellDownloadManagerDelegate::ShouldOpenDownload(
Min Qina9f487872018-02-09 20:43:23104 download::DownloadItem* item,
danakj8c70a442019-12-10 22:48:24105 DownloadOpenDelayedCallback callback) {
[email protected]e6669302013-03-25 14:31:16106 return true;
107}
108
danakja6dd6f52019-12-10 19:04:52109void ShellDownloadManagerDelegate::GetNextId(DownloadIdCallback callback) {
Min Qina9f487872018-02-09 20:43:23110 static uint32_t next_id = download::DownloadItem::kInvalidId + 1;
danakja6dd6f52019-12-10 19:04:52111 std::move(callback).Run(next_id++);
[email protected]530047e2013-07-12 17:02:25112}
113
[email protected]7cef82172013-12-17 06:58:37114// static
[email protected]98d6f152011-09-29 19:35:51115void ShellDownloadManagerDelegate::GenerateFilename(
[email protected]7cef82172013-12-17 06:58:37116 const GURL& url,
117 const std::string& content_disposition,
118 const std::string& suggested_filename,
119 const std::string& mime_type,
120 const base::FilePath& suggested_directory,
danakja9fe91c2019-05-01 19:02:29121 FilenameDeterminedCallback callback) {
[email protected]7cef82172013-12-17 06:58:37122 base::FilePath generated_name = net::GenerateFileName(url,
123 content_disposition,
124 std::string(),
125 suggested_filename,
126 mime_type,
127 "download");
128
[email protected]7567484142013-07-11 17:36:07129 if (!base::PathExists(suggested_directory))
[email protected]426d1c92013-12-03 20:08:54130 base::CreateDirectory(suggested_directory);
[email protected]98d6f152011-09-29 19:35:51131
[email protected]d30a36f2013-02-07 04:16:26132 base::FilePath suggested_path(suggested_directory.Append(generated_name));
Gabriel Charettee7cdc5cd2020-05-27 23:35:05133 GetUIThreadTaskRunner({})->PostTask(
134 FROM_HERE, base::BindOnce(std::move(callback), suggested_path));
[email protected]98d6f152011-09-29 19:35:51135}
136
[email protected]a558b43a2012-08-30 17:09:27137void ShellDownloadManagerDelegate::OnDownloadPathGenerated(
avi66a07722015-12-25 23:38:12138 uint32_t download_id,
danakja3cfb8332019-12-10 21:13:33139 DownloadTargetCallback callback,
[email protected]d30a36f2013-02-07 04:16:26140 const base::FilePath& suggested_path) {
mostynb4e363892015-03-23 14:35:05141 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]a558b43a2012-08-30 17:09:27142 if (suppress_prompting_) {
143 // Testing exit.
danakja3cfb8332019-12-10 21:13:33144 std::move(callback).Run(
145 suggested_path, download::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
146 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
Joe DeBlasioc427ca032020-01-17 19:15:43147 download::DownloadItem::MixedContentStatus::UNKNOWN,
danakja3cfb8332019-12-10 21:13:33148 suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload")),
Min Qin83d70f02022-04-15 21:49:59149 base::FilePath(), std::string() /*mime_type*/,
danakja3cfb8332019-12-10 21:13:33150 download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]a558b43a2012-08-30 17:09:27151 return;
152 }
153
danakja3cfb8332019-12-10 21:13:33154 ChooseDownloadPath(download_id, std::move(callback), suggested_path);
[email protected]a558b43a2012-08-30 17:09:27155}
156
[email protected]47665442012-07-27 02:31:22157void ShellDownloadManagerDelegate::ChooseDownloadPath(
avi66a07722015-12-25 23:38:12158 uint32_t download_id,
danakja3cfb8332019-12-10 21:13:33159 DownloadTargetCallback callback,
[email protected]d30a36f2013-02-07 04:16:26160 const base::FilePath& suggested_path) {
mostynb4e363892015-03-23 14:35:05161 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Min Qina9f487872018-02-09 20:43:23162 download::DownloadItem* item = download_manager_->GetDownload(download_id);
163 if (!item || (item->GetState() != download::DownloadItem::IN_PROGRESS))
[email protected]98d6f152011-09-29 19:35:51164 return;
[email protected]3d833de2012-05-30 23:32:06165
[email protected]d30a36f2013-02-07 04:16:26166 base::FilePath result;
Xiaohan Wangbd084422022-01-15 18:47:51167#if BUILDFLAG(IS_WIN)
[email protected]d30a36f2013-02-07 04:16:26168 std::wstring file_part = base::FilePath(suggested_path).BaseName().value();
[email protected]5c12f4a2011-09-30 21:59:04169 wchar_t file_name[MAX_PATH];
Daniel Chengad44af2f2022-02-26 18:07:54170 base::wcslcpy(file_name, file_part.c_str(), std::size(file_name));
[email protected]5c12f4a2011-09-30 21:59:04171 OPENFILENAME save_as;
172 ZeroMemory(&save_as, sizeof(save_as));
173 save_as.lStructSize = sizeof(OPENFILENAME);
Tsuyoshi Horoab90fd502019-11-13 19:18:59174 WebContents* web_contents = DownloadItemUtils::GetWebContents(item);
175 // |web_contents| could be null if the tab was quickly closed.
176 if (!web_contents)
177 return;
178 save_as.hwndOwner =
179 web_contents->GetNativeView()->GetHost()->GetAcceleratedWidget();
[email protected]5c12f4a2011-09-30 21:59:04180 save_as.lpstrFile = file_name;
Daniel Chengad44af2f2022-02-26 18:07:54181 save_as.nMaxFile = std::size(file_name);
[email protected]5c12f4a2011-09-30 21:59:04182
183 std::wstring directory;
184 if (!suggested_path.empty())
185 directory = suggested_path.DirName().value();
186
187 save_as.lpstrInitialDir = directory.c_str();
188 save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
189 OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
190
191 if (GetSaveFileName(&save_as))
[email protected]d30a36f2013-02-07 04:16:26192 result = base::FilePath(std::wstring(save_as.lpstrFile));
[email protected]5c12f4a2011-09-30 21:59:04193#else
194 NOTIMPLEMENTED();
195#endif
196
Min Qin83d70f02022-04-15 21:49:59197 std::move(callback).Run(result,
198 download::DownloadItem::TARGET_DISPOSITION_PROMPT,
199 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
200 download::DownloadItem::MixedContentStatus::UNKNOWN,
201 result, base::FilePath(), std::string() /*mime_type*/,
Min Qin83d70f02022-04-15 21:49:59202 download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]98d6f152011-09-29 19:35:51203}
204
[email protected]a558b43a2012-08-30 17:09:27205void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting(
[email protected]d30a36f2013-02-07 04:16:26206 const base::FilePath& default_download_path) {
[email protected]a558b43a2012-08-30 17:09:27207 default_download_path_ = default_download_path;
208 suppress_prompting_ = true;
209}
210
[email protected]98d6f152011-09-29 19:35:51211} // namespace content