blob: 65731c293ea9adca1e0756a33a2b8746a5d33d72 [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"
avi66a07722015-12-25 23:38:1218#include "base/macros.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()
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2840 : download_manager_(nullptr),
[email protected]7cef82172013-12-17 06:58:3741 suppress_prompting_(false),
42 weak_ptr_factory_(this) {}
[email protected]98d6f152011-09-29 19:35:5143
Asanka Herath1ba0e9f2017-04-03 18:48:5344ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate() {
[email protected]7cef82172013-12-17 06:58:3745 if (download_manager_) {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2846 download_manager_->SetDelegate(nullptr);
47 download_manager_ = nullptr;
[email protected]7cef82172013-12-17 06:58:3748 }
[email protected]98d6f152011-09-29 19:35:5149}
50
51
52void ShellDownloadManagerDelegate::SetDownloadManager(
53 DownloadManager* download_manager) {
54 download_manager_ = download_manager;
55}
56
[email protected]854e1312012-07-30 17:26:3057void ShellDownloadManagerDelegate::Shutdown() {
[email protected]7cef82172013-12-17 06:58:3758 // Revoke any pending callbacks. download_manager_ et. al. are no longer safe
59 // to access after this point.
60 weak_ptr_factory_.InvalidateWeakPtrs();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2861 download_manager_ = nullptr;
[email protected]854e1312012-07-30 17:26:3062}
63
[email protected]47665442012-07-27 02:31:2264bool ShellDownloadManagerDelegate::DetermineDownloadTarget(
Min Qina9f487872018-02-09 20:43:2365 download::DownloadItem* download,
[email protected]47665442012-07-27 02:31:2266 const DownloadTargetCallback& callback) {
mostynb4e363892015-03-23 14:35:0567 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]a558b43a2012-08-30 17:09:2768 // This assignment needs to be here because even at the call to
69 // SetDownloadManager, the system is not fully initialized.
70 if (default_download_path_.empty()) {
71 default_download_path_ = download_manager_->GetBrowserContext()->GetPath().
72 Append(FILE_PATH_LITERAL("Downloads"));
73 }
74
[email protected]3d833de2012-05-30 23:32:0675 if (!download->GetForcedFilePath().empty()) {
[email protected]47665442012-07-27 02:31:2276 callback.Run(download->GetForcedFilePath(),
Min Qina9f487872018-02-09 20:43:2377 download::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
Min Qin0ca8e1ee2018-01-31 00:49:3578 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
Min Qineb78b7a2018-02-03 00:43:1679 download->GetForcedFilePath(),
80 download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]98d6f152011-09-29 19:35:5181 return true;
[email protected]3d833de2012-05-30 23:32:0682 }
[email protected]98d6f152011-09-29 19:35:5183
[email protected]7cef82172013-12-17 06:58:3784 FilenameDeterminedCallback filename_determined_callback =
85 base::Bind(&ShellDownloadManagerDelegate::OnDownloadPathGenerated,
86 weak_ptr_factory_.GetWeakPtr(),
87 download->GetId(),
88 callback);
[email protected]98d6f152011-09-29 19:35:5189
Mike West85f2caa2017-07-07 12:55:3090 PostTaskWithTraits(
[email protected]98d6f152011-09-29 19:35:5191 FROM_HERE,
Mike West85f2caa2017-07-07 12:55:3092 {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
93 base::TaskPriority::USER_VISIBLE},
tzikf5430522017-08-17 16:13:3694 base::BindOnce(&ShellDownloadManagerDelegate::GenerateFilename,
95 download->GetURL(), download->GetContentDisposition(),
96 download->GetSuggestedFilename(), download->GetMimeType(),
Tommy Nyquist4b749d02018-03-20 21:46:2997 default_download_path_,
98 std::move(filename_determined_callback)));
[email protected]a558b43a2012-08-30 17:09:2799 return true;
[email protected]98d6f152011-09-29 19:35:51100}
101
[email protected]e6669302013-03-25 14:31:16102bool ShellDownloadManagerDelegate::ShouldOpenDownload(
Min Qina9f487872018-02-09 20:43:23103 download::DownloadItem* item,
104 const DownloadOpenDelayedCallback& callback) {
[email protected]e6669302013-03-25 14:31:16105 return true;
106}
107
[email protected]530047e2013-07-12 17:02:25108void ShellDownloadManagerDelegate::GetNextId(
109 const DownloadIdCallback& callback) {
Min Qina9f487872018-02-09 20:43:23110 static uint32_t next_id = download::DownloadItem::kInvalidId + 1;
[email protected]530047e2013-07-12 17:02:25111 callback.Run(next_id++);
112}
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,
121 const 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));
Eric Seckler8652dcd52018-09-20 10:42:28133 base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
134 base::BindOnce(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,
[email protected]a558b43a2012-08-30 17:09:27139 const 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.
Min Qina9f487872018-02-09 20:43:23144 callback.Run(suggested_path,
145 download::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
Min Qin0ca8e1ee2018-01-31 00:49:35146 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
Asanka Herath1ba0e9f2017-04-03 18:48:53147 suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload")),
Min Qineb78b7a2018-02-03 00:43:16148 download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]a558b43a2012-08-30 17:09:27149 return;
150 }
151
152 ChooseDownloadPath(download_id, callback, suggested_path);
153}
154
[email protected]47665442012-07-27 02:31:22155void ShellDownloadManagerDelegate::ChooseDownloadPath(
avi66a07722015-12-25 23:38:12156 uint32_t download_id,
[email protected]47665442012-07-27 02:31:22157 const DownloadTargetCallback& callback,
[email protected]d30a36f2013-02-07 04:16:26158 const base::FilePath& suggested_path) {
mostynb4e363892015-03-23 14:35:05159 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Min Qina9f487872018-02-09 20:43:23160 download::DownloadItem* item = download_manager_->GetDownload(download_id);
161 if (!item || (item->GetState() != download::DownloadItem::IN_PROGRESS))
[email protected]98d6f152011-09-29 19:35:51162 return;
[email protected]3d833de2012-05-30 23:32:06163
[email protected]d30a36f2013-02-07 04:16:26164 base::FilePath result;
[email protected]517fce722014-01-15 05:16:14165#if defined(OS_WIN)
[email protected]d30a36f2013-02-07 04:16:26166 std::wstring file_part = base::FilePath(suggested_path).BaseName().value();
[email protected]5c12f4a2011-09-30 21:59:04167 wchar_t file_name[MAX_PATH];
168 base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name));
169 OPENFILENAME save_as;
170 ZeroMemory(&save_as, sizeof(save_as));
171 save_as.lStructSize = sizeof(OPENFILENAME);
Min Qina9f487872018-02-09 20:43:23172 save_as.hwndOwner = DownloadItemUtils::GetWebContents(item)
173 ->GetNativeView()
174 ->GetHost()
175 ->GetAcceleratedWidget();
[email protected]5c12f4a2011-09-30 21:59:04176 save_as.lpstrFile = file_name;
177 save_as.nMaxFile = arraysize(file_name);
178
179 std::wstring directory;
180 if (!suggested_path.empty())
181 directory = suggested_path.DirName().value();
182
183 save_as.lpstrInitialDir = directory.c_str();
184 save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
185 OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
186
187 if (GetSaveFileName(&save_as))
[email protected]d30a36f2013-02-07 04:16:26188 result = base::FilePath(std::wstring(save_as.lpstrFile));
[email protected]5c12f4a2011-09-30 21:59:04189#else
190 NOTIMPLEMENTED();
191#endif
192
Min Qina9f487872018-02-09 20:43:23193 callback.Run(result, download::DownloadItem::TARGET_DISPOSITION_PROMPT,
Min Qin0ca8e1ee2018-01-31 00:49:35194 download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result,
Min Qineb78b7a2018-02-03 00:43:16195 download::DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]98d6f152011-09-29 19:35:51196}
197
[email protected]a558b43a2012-08-30 17:09:27198void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting(
[email protected]d30a36f2013-02-07 04:16:26199 const base::FilePath& default_download_path) {
[email protected]a558b43a2012-08-30 17:09:27200 default_download_path_ = default_download_path;
201 suppress_prompting_ = true;
202}
203
[email protected]98d6f152011-09-29 19:35:51204} // namespace content