blob: 31f5fa2bf0e7706b130adc09fd66ed171e7e8e06 [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
[email protected]5c12f4a2011-09-30 21:59:049#if defined(OS_WIN)
10#include <windows.h>
11#include <commdlg.h>
12#endif
13
[email protected]98d6f152011-09-29 19:35:5114#include "base/bind.h"
[email protected]e6669302013-03-25 14:31:1615#include "base/command_line.h"
thestigb7aad54f2014-09-05 18:25:3916#include "base/files/file_util.h"
[email protected]5c12f4a2011-09-30 21:59:0417#include "base/logging.h"
Avi Drissman9d3ded92018-12-25 20:50:2118#include "base/stl_util.h"
[email protected]21aa99682013-06-11 07:17:0119#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0020#include "base/strings/utf_string_conversions.h"
Gabriel Charette44db1422018-08-06 11:19:3321#include "base/task/post_task.h"
avi66a07722015-12-25 23:38:1222#include "build/build_config.h"
[email protected]ccb797302011-12-15 16:55:1123#include "content/public/browser/browser_context.h"
Eric Seckler8652dcd52018-09-20 10:42:2824#include "content/public/browser/browser_task_traits.h"
[email protected]c38831a12011-10-28 12:44:4925#include "content/public/browser/browser_thread.h"
Min Qina9f487872018-02-09 20:43:2326#include "content/public/browser/download_item_utils.h"
[email protected]e582fdd2011-12-20 16:48:1727#include "content/public/browser/download_manager.h"
[email protected]0b659b32012-03-26 21:29:3228#include "content/public/browser/web_contents.h"
[email protected]b7c504c2013-05-07 14:42:1229#include "content/shell/common/shell_switches.h"
[email protected]d96cf752014-04-09 04:05:2830#include "net/base/filename_util.h"
[email protected]98d6f152011-09-29 19:35:5131
[email protected]517fce722014-01-15 05:16:1432#if defined(OS_WIN)
[email protected]517fce722014-01-15 05:16:1433#include "ui/aura/window.h"
[email protected]7a60cd3a2014-03-20 20:54:5734#include "ui/aura/window_tree_host.h"
[email protected]517fce722014-01-15 05:16:1435#endif
36
[email protected]98d6f152011-09-29 19:35:5137namespace content {
38
39ShellDownloadManagerDelegate::ShellDownloadManagerDelegate()
Jeremy Roman3bca4bf2019-07-11 03:41:2540 : download_manager_(nullptr), suppress_prompting_(false) {}
[email protected]98d6f152011-09-29 19:35:5141
Asanka Herath1ba0e9f2017-04-03 18:48:5342ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate() {
[email protected]7cef82172013-12-17 06:58:3743 if (download_manager_) {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2844 download_manager_->SetDelegate(nullptr);
45 download_manager_ = nullptr;
[email protected]7cef82172013-12-17 06:58:3746 }
[email protected]98d6f152011-09-29 19:35:5147}
48
49
50void ShellDownloadManagerDelegate::SetDownloadManager(
51 DownloadManager* download_manager) {
52 download_manager_ = download_manager;
53}
54
[email protected]854e1312012-07-30 17:26:3055void ShellDownloadManagerDelegate::Shutdown() {
[email protected]7cef82172013-12-17 06:58:3756 // Revoke any pending callbacks. download_manager_ et. al. are no longer safe
57 // to access after this point.
58 weak_ptr_factory_.InvalidateWeakPtrs();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2859 download_manager_ = nullptr;
[email protected]854e1312012-07-30 17:26:3060}
61
[email protected]47665442012-07-27 02:31:2262bool ShellDownloadManagerDelegate::DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:2363 download::DownloadItem* download,
danakja3cfb8332019-12-10 21:13:3364 DownloadTargetCallback* callback) {
mostynb4e363892015-03-23 14:35:0565 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]a558b43a2012-08-30 17:09:2766 // This assignment needs to be here because even at the call to
67 // SetDownloadManager, the system is not fully initialized.
68 if (default_download_path_.empty()) {
69 default_download_path_ = download_manager_->GetBrowserContext()->GetPath().
70 Append(FILE_PATH_LITERAL("Downloads"));
71 }
72
[email protected]3d833de2012-05-30 23:32:0673 if (!download->GetForcedFilePath().empty()) {
danakja3cfb8332019-12-10 21:13:3374 std::move(*callback).Run(
75 download->GetForcedFilePath(),
76 download::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
77 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
78 download->GetForcedFilePath(),
79 download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]98d6f152011-09-29 19:35:5180 return true;
[email protected]3d833de2012-05-30 23:32:0681 }
[email protected]98d6f152011-09-29 19:35:5182
danakja9fe91c2019-05-01 19:02:2983 FilenameDeterminedCallback filename_determined_callback = base::BindOnce(
84 &ShellDownloadManagerDelegate::OnDownloadPathGenerated,
danakja3cfb8332019-12-10 21:13:3385 weak_ptr_factory_.GetWeakPtr(), download->GetId(), std::move(*callback));
[email protected]98d6f152011-09-29 19:35:5186
Sami Kyostila807cd4d2019-08-01 16:15:0787 PostTask(FROM_HERE,
88 {base::ThreadPool(), base::MayBlock(),
89 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
90 base::TaskPriority::USER_VISIBLE},
91 base::BindOnce(&ShellDownloadManagerDelegate::GenerateFilename,
92 download->GetURL(), download->GetContentDisposition(),
93 download->GetSuggestedFilename(),
94 download->GetMimeType(), default_download_path_,
95 std::move(filename_determined_callback)));
[email protected]a558b43a2012-08-30 17:09:2796 return true;
[email protected]98d6f152011-09-29 19:35:5197}
98
[email protected]e6669302013-03-25 14:31:1699bool ShellDownloadManagerDelegate::ShouldOpenDownload(
Min Qina9f487872018-02-09 20:43:23100 download::DownloadItem* item,
101 const DownloadOpenDelayedCallback& callback) {
[email protected]e6669302013-03-25 14:31:16102 return true;
103}
104
danakja6dd6f52019-12-10 19:04:52105void ShellDownloadManagerDelegate::GetNextId(DownloadIdCallback callback) {
Min Qina9f487872018-02-09 20:43:23106 static uint32_t next_id = download::DownloadItem::kInvalidId + 1;
danakja6dd6f52019-12-10 19:04:52107 std::move(callback).Run(next_id++);
[email protected]530047e2013-07-12 17:02:25108}
109
[email protected]7cef82172013-12-17 06:58:37110// static
[email protected]98d6f152011-09-29 19:35:51111void ShellDownloadManagerDelegate::GenerateFilename(
[email protected]7cef82172013-12-17 06:58:37112 const GURL& url,
113 const std::string& content_disposition,
114 const std::string& suggested_filename,
115 const std::string& mime_type,
116 const base::FilePath& suggested_directory,
danakja9fe91c2019-05-01 19:02:29117 FilenameDeterminedCallback callback) {
[email protected]7cef82172013-12-17 06:58:37118 base::FilePath generated_name = net::GenerateFileName(url,
119 content_disposition,
120 std::string(),
121 suggested_filename,
122 mime_type,
123 "download");
124
[email protected]7567484142013-07-11 17:36:07125 if (!base::PathExists(suggested_directory))
[email protected]426d1c92013-12-03 20:08:54126 base::CreateDirectory(suggested_directory);
[email protected]98d6f152011-09-29 19:35:51127
[email protected]d30a36f2013-02-07 04:16:26128 base::FilePath suggested_path(suggested_directory.Append(generated_name));
Sami Kyostila807cd4d2019-08-01 16:15:07129 base::PostTask(FROM_HERE, {BrowserThread::UI},
130 base::BindOnce(std::move(callback), suggested_path));
[email protected]98d6f152011-09-29 19:35:51131}
132
[email protected]a558b43a2012-08-30 17:09:27133void ShellDownloadManagerDelegate::OnDownloadPathGenerated(
avi66a07722015-12-25 23:38:12134 uint32_t download_id,
danakja3cfb8332019-12-10 21:13:33135 DownloadTargetCallback callback,
[email protected]d30a36f2013-02-07 04:16:26136 const base::FilePath& suggested_path) {
mostynb4e363892015-03-23 14:35:05137 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]a558b43a2012-08-30 17:09:27138 if (suppress_prompting_) {
139 // Testing exit.
danakja3cfb8332019-12-10 21:13:33140 std::move(callback).Run(
141 suggested_path, download::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
142 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
143 suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload")),
144 download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]a558b43a2012-08-30 17:09:27145 return;
146 }
147
danakja3cfb8332019-12-10 21:13:33148 ChooseDownloadPath(download_id, std::move(callback), suggested_path);
[email protected]a558b43a2012-08-30 17:09:27149}
150
[email protected]47665442012-07-27 02:31:22151void ShellDownloadManagerDelegate::ChooseDownloadPath(
avi66a07722015-12-25 23:38:12152 uint32_t download_id,
danakja3cfb8332019-12-10 21:13:33153 DownloadTargetCallback callback,
[email protected]d30a36f2013-02-07 04:16:26154 const base::FilePath& suggested_path) {
mostynb4e363892015-03-23 14:35:05155 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Min Qina9f487872018-02-09 20:43:23156 download::DownloadItem* item = download_manager_->GetDownload(download_id);
157 if (!item || (item->GetState() != download::DownloadItem::IN_PROGRESS))
[email protected]98d6f152011-09-29 19:35:51158 return;
[email protected]3d833de2012-05-30 23:32:06159
[email protected]d30a36f2013-02-07 04:16:26160 base::FilePath result;
[email protected]517fce722014-01-15 05:16:14161#if defined(OS_WIN)
[email protected]d30a36f2013-02-07 04:16:26162 std::wstring file_part = base::FilePath(suggested_path).BaseName().value();
[email protected]5c12f4a2011-09-30 21:59:04163 wchar_t file_name[MAX_PATH];
Avi Drissman9d3ded92018-12-25 20:50:21164 base::wcslcpy(file_name, file_part.c_str(), base::size(file_name));
[email protected]5c12f4a2011-09-30 21:59:04165 OPENFILENAME save_as;
166 ZeroMemory(&save_as, sizeof(save_as));
167 save_as.lStructSize = sizeof(OPENFILENAME);
Tsuyoshi Horoab90fd502019-11-13 19:18:59168 WebContents* web_contents = DownloadItemUtils::GetWebContents(item);
169 // |web_contents| could be null if the tab was quickly closed.
170 if (!web_contents)
171 return;
172 save_as.hwndOwner =
173 web_contents->GetNativeView()->GetHost()->GetAcceleratedWidget();
[email protected]5c12f4a2011-09-30 21:59:04174 save_as.lpstrFile = file_name;
Avi Drissman9d3ded92018-12-25 20:50:21175 save_as.nMaxFile = base::size(file_name);
[email protected]5c12f4a2011-09-30 21:59:04176
177 std::wstring directory;
178 if (!suggested_path.empty())
179 directory = suggested_path.DirName().value();
180
181 save_as.lpstrInitialDir = directory.c_str();
182 save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
183 OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
184
185 if (GetSaveFileName(&save_as))
[email protected]d30a36f2013-02-07 04:16:26186 result = base::FilePath(std::wstring(save_as.lpstrFile));
[email protected]5c12f4a2011-09-30 21:59:04187#else
188 NOTIMPLEMENTED();
189#endif
190
danakja3cfb8332019-12-10 21:13:33191 std::move(callback).Run(result,
192 download::DownloadItem::TARGET_DISPOSITION_PROMPT,
193 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result,
194 download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]98d6f152011-09-29 19:35:51195}
196
[email protected]a558b43a2012-08-30 17:09:27197void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting(
[email protected]d30a36f2013-02-07 04:16:26198 const base::FilePath& default_download_path) {
[email protected]a558b43a2012-08-30 17:09:27199 default_download_path_ = default_download_path;
200 suppress_prompting_ = true;
201}
202
[email protected]98d6f152011-09-29 19:35:51203} // namespace content