[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] | 08c92ac | 2012-08-21 23:41:40 | [diff] [blame] | 7 | #if defined(TOOLKIT_GTK) |
| 8 | #include <gtk/gtk.h> |
| 9 | #endif |
| 10 | |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 11 | #if defined(OS_WIN) |
| 12 | #include <windows.h> |
| 13 | #include <commdlg.h> |
| 14 | #endif |
| 15 | |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 16 | #include "base/bind.h" |
[email protected] | e666930 | 2013-03-25 14:31:16 | [diff] [blame] | 17 | #include "base/command_line.h" |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 18 | #include "base/file_util.h" |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 19 | #include "base/logging.h" |
[email protected] | 21aa9968 | 2013-06-11 07:17:01 | [diff] [blame] | 20 | #include "base/strings/string_util.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 22 | #include "content/public/browser/browser_context.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 23 | #include "content/public/browser/browser_thread.h" |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 24 | #include "content/public/browser/download_manager.h" |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 25 | #include "content/public/browser/web_contents.h" |
[email protected] | 08c92ac | 2012-08-21 23:41:40 | [diff] [blame] | 26 | #include "content/public/browser/web_contents_view.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame^] | 27 | #include "content/shell/browser/webkit_test_controller.h" |
[email protected] | b7c504c | 2013-05-07 14:42:12 | [diff] [blame] | 28 | #include "content/shell/common/shell_switches.h" |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 29 | #include "net/base/net_util.h" |
| 30 | |
| 31 | namespace content { |
| 32 | |
| 33 | ShellDownloadManagerDelegate::ShellDownloadManagerDelegate() |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 34 | : download_manager_(NULL), |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 35 | suppress_prompting_(false) { |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 36 | // Balanced in Shutdown(); |
| 37 | AddRef(); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate(){ |
| 41 | } |
| 42 | |
| 43 | |
| 44 | void ShellDownloadManagerDelegate::SetDownloadManager( |
| 45 | DownloadManager* download_manager) { |
| 46 | download_manager_ = download_manager; |
| 47 | } |
| 48 | |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 49 | void ShellDownloadManagerDelegate::Shutdown() { |
| 50 | Release(); |
| 51 | } |
| 52 | |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 53 | bool ShellDownloadManagerDelegate::DetermineDownloadTarget( |
| 54 | DownloadItem* download, |
| 55 | const DownloadTargetCallback& callback) { |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 56 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 57 | // This assignment needs to be here because even at the call to |
| 58 | // SetDownloadManager, the system is not fully initialized. |
| 59 | if (default_download_path_.empty()) { |
| 60 | default_download_path_ = download_manager_->GetBrowserContext()->GetPath(). |
| 61 | Append(FILE_PATH_LITERAL("Downloads")); |
| 62 | } |
| 63 | |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 64 | if (!download->GetForcedFilePath().empty()) { |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 65 | callback.Run(download->GetForcedFilePath(), |
| 66 | DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 67 | DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 68 | download->GetForcedFilePath()); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 69 | return true; |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 70 | } |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 71 | |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 72 | base::FilePath generated_name = net::GenerateFileName( |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 73 | download->GetURL(), |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 74 | download->GetContentDisposition(), |
[email protected] | a7799617 | 2012-10-23 19:59:54 | [diff] [blame] | 75 | EmptyString(), |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 76 | download->GetSuggestedFilename(), |
| 77 | download->GetMimeType(), |
[email protected] | c9b6eb6 | 2011-10-18 20:49:39 | [diff] [blame] | 78 | "download"); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 79 | |
| 80 | BrowserThread::PostTask( |
| 81 | BrowserThread::FILE, |
| 82 | FROM_HERE, |
| 83 | base::Bind( |
| 84 | &ShellDownloadManagerDelegate::GenerateFilename, |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 85 | this, download->GetId(), callback, generated_name, |
| 86 | default_download_path_)); |
| 87 | return true; |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | e666930 | 2013-03-25 14:31:16 | [diff] [blame] | 90 | bool ShellDownloadManagerDelegate::ShouldOpenDownload( |
| 91 | DownloadItem* item, |
| 92 | const DownloadOpenDelayedCallback& callback) { |
[email protected] | a395408 | 2013-05-14 14:26:18 | [diff] [blame] | 93 | if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree) && |
[email protected] | 91fde4a3 | 2013-06-04 12:35:13 | [diff] [blame] | 94 | WebKitTestController::Get()->IsMainWindow(item->GetWebContents()) && |
| 95 | item->GetMimeType() == "text/html") { |
[email protected] | f8462f5 | 2013-03-25 15:27:46 | [diff] [blame] | 96 | WebKitTestController::Get()->OpenURL( |
| 97 | net::FilePathToFileURL(item->GetFullPath())); |
| 98 | } |
[email protected] | e666930 | 2013-03-25 14:31:16 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 102 | void ShellDownloadManagerDelegate::GetNextId( |
| 103 | const DownloadIdCallback& callback) { |
| 104 | static uint32 next_id = DownloadItem::kInvalidId + 1; |
| 105 | callback.Run(next_id++); |
| 106 | } |
| 107 | |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 108 | void ShellDownloadManagerDelegate::GenerateFilename( |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 109 | uint32 download_id, |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 110 | const DownloadTargetCallback& callback, |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 111 | const base::FilePath& generated_name, |
| 112 | const base::FilePath& suggested_directory) { |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 113 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 114 | if (!base::PathExists(suggested_directory)) |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 115 | file_util::CreateDirectory(suggested_directory); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 116 | |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 117 | base::FilePath suggested_path(suggested_directory.Append(generated_name)); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 118 | BrowserThread::PostTask( |
| 119 | BrowserThread::UI, |
| 120 | FROM_HERE, |
| 121 | base::Bind( |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 122 | &ShellDownloadManagerDelegate::OnDownloadPathGenerated, |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 123 | this, download_id, callback, suggested_path)); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 126 | void ShellDownloadManagerDelegate::OnDownloadPathGenerated( |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 127 | uint32 download_id, |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 128 | const DownloadTargetCallback& callback, |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 129 | const base::FilePath& suggested_path) { |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 130 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 131 | if (suppress_prompting_) { |
| 132 | // Testing exit. |
| 133 | callback.Run(suggested_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
[email protected] | d0d36825 | 2012-09-24 17:13:42 | [diff] [blame] | 134 | DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 135 | suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload"))); |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | |
| 139 | ChooseDownloadPath(download_id, callback, suggested_path); |
| 140 | } |
| 141 | |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 142 | void ShellDownloadManagerDelegate::ChooseDownloadPath( |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 143 | uint32 download_id, |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 144 | const DownloadTargetCallback& callback, |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 145 | const base::FilePath& suggested_path) { |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 146 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | db6234e | 2012-09-25 17:23:01 | [diff] [blame] | 147 | DownloadItem* item = download_manager_->GetDownload(download_id); |
| 148 | if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 149 | return; |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 150 | |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 151 | base::FilePath result; |
[email protected] | fa4a4583 | 2012-04-12 21:32:48 | [diff] [blame] | 152 | #if defined(OS_WIN) && !defined(USE_AURA) |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 153 | std::wstring file_part = base::FilePath(suggested_path).BaseName().value(); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 154 | wchar_t file_name[MAX_PATH]; |
| 155 | base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name)); |
| 156 | OPENFILENAME save_as; |
| 157 | ZeroMemory(&save_as, sizeof(save_as)); |
| 158 | save_as.lStructSize = sizeof(OPENFILENAME); |
[email protected] | f3615f0 | 2013-02-26 06:09:06 | [diff] [blame] | 159 | save_as.hwndOwner = item->GetWebContents()->GetView()->GetNativeView(); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 160 | save_as.lpstrFile = file_name; |
| 161 | save_as.nMaxFile = arraysize(file_name); |
| 162 | |
| 163 | std::wstring directory; |
| 164 | if (!suggested_path.empty()) |
| 165 | directory = suggested_path.DirName().value(); |
| 166 | |
| 167 | save_as.lpstrInitialDir = directory.c_str(); |
| 168 | save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING | |
| 169 | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST; |
| 170 | |
| 171 | if (GetSaveFileName(&save_as)) |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 172 | result = base::FilePath(std::wstring(save_as.lpstrFile)); |
[email protected] | 08c92ac | 2012-08-21 23:41:40 | [diff] [blame] | 173 | #elif defined(TOOLKIT_GTK) |
| 174 | GtkWidget *dialog; |
| 175 | gfx::NativeWindow parent_window; |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 176 | std::string base_name = base::FilePath(suggested_path).BaseName().value(); |
[email protected] | 08c92ac | 2012-08-21 23:41:40 | [diff] [blame] | 177 | |
| 178 | parent_window = item->GetWebContents()->GetView()->GetTopLevelNativeWindow(); |
| 179 | dialog = gtk_file_chooser_dialog_new("Save File", |
| 180 | parent_window, |
| 181 | GTK_FILE_CHOOSER_ACTION_SAVE, |
| 182 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
| 183 | GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, |
| 184 | NULL); |
| 185 | gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), |
| 186 | TRUE); |
| 187 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), |
| 188 | base_name.c_str()); |
| 189 | |
| 190 | if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { |
| 191 | char *filename; |
| 192 | filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 193 | result = base::FilePath(filename); |
[email protected] | 94b7dd2 | 2013-06-05 18:10:31 | [diff] [blame] | 194 | g_free(filename); |
[email protected] | 08c92ac | 2012-08-21 23:41:40 | [diff] [blame] | 195 | } |
| 196 | gtk_widget_destroy(dialog); |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 197 | #else |
| 198 | NOTIMPLEMENTED(); |
| 199 | #endif |
| 200 | |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 201 | callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 202 | DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 205 | void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting( |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 206 | const base::FilePath& default_download_path) { |
[email protected] | a558b43a | 2012-08-30 17:09:27 | [diff] [blame] | 207 | default_download_path_ = default_download_path; |
| 208 | suppress_prompting_ = true; |
| 209 | } |
| 210 | |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 211 | } // namespace content |