[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 | |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 7 | #if defined(OS_WIN) |
| 8 | #include <windows.h> |
| 9 | #include <commdlg.h> |
| 10 | #endif |
| 11 | |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | e666930 | 2013-03-25 14:31:16 | [diff] [blame] | 13 | #include "base/command_line.h" |
thestig | b7aad54f | 2014-09-05 18:25:39 | [diff] [blame] | 14 | #include "base/files/file_util.h" |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 15 | #include "base/logging.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | 21aa9968 | 2013-06-11 07:17:01 | [diff] [blame] | 17 | #include "base/strings/string_util.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
Mike West | 85f2caa | 2017-07-07 12:55:30 | [diff] [blame] | 19 | #include "base/task_scheduler/post_task.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 20 | #include "build/build_config.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 21 | #include "content/public/browser/browser_context.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 22 | #include "content/public/browser/browser_thread.h" |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 23 | #include "content/public/browser/download_manager.h" |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 24 | #include "content/public/browser/web_contents.h" |
[email protected] | b7c504c | 2013-05-07 14:42:12 | [diff] [blame] | 25 | #include "content/shell/common/shell_switches.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 26 | #include "net/base/filename_util.h" |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 27 | |
[email protected] | 517fce72 | 2014-01-15 05:16:14 | [diff] [blame] | 28 | #if defined(OS_WIN) |
[email protected] | 517fce72 | 2014-01-15 05:16:14 | [diff] [blame] | 29 | #include "ui/aura/window.h" |
[email protected] | 7a60cd3a | 2014-03-20 20:54:57 | [diff] [blame] | 30 | #include "ui/aura/window_tree_host.h" |
[email protected] | 517fce72 | 2014-01-15 05:16:14 | [diff] [blame] | 31 | #endif |
| 32 | |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 33 | namespace content { |
| 34 | |
| 35 | ShellDownloadManagerDelegate::ShellDownloadManagerDelegate() |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 36 | : download_manager_(NULL), |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 37 | suppress_prompting_(false), |
| 38 | weak_ptr_factory_(this) {} |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 39 | |
Asanka Herath | 1ba0e9f | 2017-04-03 18:48:53 | [diff] [blame] | 40 | ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate() { |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 41 | if (download_manager_) { |
| 42 | DCHECK_EQ(static_cast<DownloadManagerDelegate*>(this), |
| 43 | download_manager_->GetDelegate()); |
| 44 | download_manager_->SetDelegate(NULL); |
| 45 | download_manager_ = NULL; |
| 46 | } |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
| 50 | void ShellDownloadManagerDelegate::SetDownloadManager( |
| 51 | DownloadManager* download_manager) { |
| 52 | download_manager_ = download_manager; |
| 53 | } |
| 54 | |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 55 | void ShellDownloadManagerDelegate::Shutdown() { |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 56 | // Revoke any pending callbacks. download_manager_ et. al. are no longer safe |
| 57 | // to access after this point. |
| 58 | weak_ptr_factory_.InvalidateWeakPtrs(); |
| 59 | download_manager_ = NULL; |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 62 | bool ShellDownloadManagerDelegate::DetermineDownloadTarget( |
| 63 | DownloadItem* download, |
| 64 | const DownloadTargetCallback& callback) { |
mostynb | 4e36389 | 2015-03-23 14:35:05 | [diff] [blame] | 65 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 66 | // 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] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 73 | if (!download->GetForcedFilePath().empty()) { |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 74 | callback.Run(download->GetForcedFilePath(), |
| 75 | DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 76 | DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
Asanka Herath | 1ba0e9f | 2017-04-03 18:48:53 | [diff] [blame] | 77 | download->GetForcedFilePath(), DOWNLOAD_INTERRUPT_REASON_NONE); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 78 | return true; |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 79 | } |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 80 | |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 81 | FilenameDeterminedCallback filename_determined_callback = |
| 82 | base::Bind(&ShellDownloadManagerDelegate::OnDownloadPathGenerated, |
| 83 | weak_ptr_factory_.GetWeakPtr(), |
| 84 | download->GetId(), |
| 85 | callback); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 86 | |
Mike West | 85f2caa | 2017-07-07 12:55:30 | [diff] [blame] | 87 | PostTaskWithTraits( |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 88 | FROM_HERE, |
Mike West | 85f2caa | 2017-07-07 12:55:30 | [diff] [blame] | 89 | {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN, |
| 90 | base::TaskPriority::USER_VISIBLE}, |
tzik | f543052 | 2017-08-17 16:13:36 | [diff] [blame^] | 91 | base::BindOnce(&ShellDownloadManagerDelegate::GenerateFilename, |
| 92 | download->GetURL(), download->GetContentDisposition(), |
| 93 | download->GetSuggestedFilename(), download->GetMimeType(), |
| 94 | default_download_path_, filename_determined_callback)); |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 95 | return true; |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | e666930 | 2013-03-25 14:31:16 | [diff] [blame] | 98 | bool ShellDownloadManagerDelegate::ShouldOpenDownload( |
| 99 | DownloadItem* item, |
| 100 | const DownloadOpenDelayedCallback& callback) { |
[email protected] | e666930 | 2013-03-25 14:31:16 | [diff] [blame] | 101 | return true; |
| 102 | } |
| 103 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 104 | void ShellDownloadManagerDelegate::GetNextId( |
| 105 | const DownloadIdCallback& callback) { |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 106 | static uint32_t next_id = DownloadItem::kInvalidId + 1; |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 107 | callback.Run(next_id++); |
| 108 | } |
| 109 | |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 110 | // static |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 111 | void ShellDownloadManagerDelegate::GenerateFilename( |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 112 | 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, |
| 117 | const FilenameDeterminedCallback& callback) { |
[email protected] | 7cef8217 | 2013-12-17 06:58:37 | [diff] [blame] | 118 | base::FilePath generated_name = net::GenerateFileName(url, |
| 119 | content_disposition, |
| 120 | std::string(), |
| 121 | suggested_filename, |
| 122 | mime_type, |
| 123 | "download"); |
| 124 | |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 125 | if (!base::PathExists(suggested_directory)) |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 126 | base::CreateDirectory(suggested_directory); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 127 | |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 128 | base::FilePath suggested_path(suggested_directory.Append(generated_name)); |
tzik | f543052 | 2017-08-17 16:13:36 | [diff] [blame^] | 129 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 130 | base::BindOnce(callback, suggested_path)); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 133 | void ShellDownloadManagerDelegate::OnDownloadPathGenerated( |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 134 | uint32_t download_id, |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 135 | const DownloadTargetCallback& callback, |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 136 | const base::FilePath& suggested_path) { |
mostynb | 4e36389 | 2015-03-23 14:35:05 | [diff] [blame] | 137 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 138 | if (suppress_prompting_) { |
| 139 | // Testing exit. |
| 140 | callback.Run(suggested_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
[email protected] | d0d36825 | 2012-09-24 17:13:42 | [diff] [blame] | 141 | DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
Asanka Herath | 1ba0e9f | 2017-04-03 18:48:53 | [diff] [blame] | 142 | suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload")), |
| 143 | DOWNLOAD_INTERRUPT_REASON_NONE); |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
| 147 | ChooseDownloadPath(download_id, callback, suggested_path); |
| 148 | } |
| 149 | |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 150 | void ShellDownloadManagerDelegate::ChooseDownloadPath( |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 151 | uint32_t download_id, |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 152 | const DownloadTargetCallback& callback, |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 153 | const base::FilePath& suggested_path) { |
mostynb | 4e36389 | 2015-03-23 14:35:05 | [diff] [blame] | 154 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | db6234e | 2012-09-25 17:23:01 | [diff] [blame] | 155 | DownloadItem* item = download_manager_->GetDownload(download_id); |
| 156 | if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 157 | return; |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 158 | |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 159 | base::FilePath result; |
[email protected] | 517fce72 | 2014-01-15 05:16:14 | [diff] [blame] | 160 | #if defined(OS_WIN) |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 161 | std::wstring file_part = base::FilePath(suggested_path).BaseName().value(); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 162 | wchar_t file_name[MAX_PATH]; |
| 163 | base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name)); |
| 164 | OPENFILENAME save_as; |
| 165 | ZeroMemory(&save_as, sizeof(save_as)); |
| 166 | save_as.lStructSize = sizeof(OPENFILENAME); |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 167 | save_as.hwndOwner = item->GetWebContents()->GetNativeView()-> |
[email protected] | 2374d181 | 2014-03-04 03:42:27 | [diff] [blame] | 168 | GetHost()->GetAcceleratedWidget(); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 169 | save_as.lpstrFile = file_name; |
| 170 | save_as.nMaxFile = arraysize(file_name); |
| 171 | |
| 172 | std::wstring directory; |
| 173 | if (!suggested_path.empty()) |
| 174 | directory = suggested_path.DirName().value(); |
| 175 | |
| 176 | save_as.lpstrInitialDir = directory.c_str(); |
| 177 | save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING | |
| 178 | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST; |
| 179 | |
| 180 | if (GetSaveFileName(&save_as)) |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 181 | result = base::FilePath(std::wstring(save_as.lpstrFile)); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 182 | #else |
| 183 | NOTIMPLEMENTED(); |
| 184 | #endif |
| 185 | |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 186 | callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT, |
Asanka Herath | 1ba0e9f | 2017-04-03 18:48:53 | [diff] [blame] | 187 | DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result, |
| 188 | DOWNLOAD_INTERRUPT_REASON_NONE); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 191 | void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting( |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 192 | const base::FilePath& default_download_path) { |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 193 | default_download_path_ = default_download_path; |
| 194 | suppress_prompting_ = true; |
| 195 | } |
| 196 | |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 197 | } // namespace content |