[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/shell/shell_download_manager_delegate.h" |
| 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" |
| 13 | #include "base/file_util.h" |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 14 | #include "base/logging.h" |
| 15 | #include "base/string_util.h" |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 16 | #include "base/utf_string_conversions.h" |
| 17 | #include "content/browser/browser_context.h" |
| 18 | #include "content/browser/browser_thread.h" |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 19 | #include "content/browser/tab_contents/tab_contents.h" |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 20 | #include "content/browser/download/download_manager.h" |
| 21 | #include "content/browser/download/download_state_info.h" |
| 22 | #include "net/base/net_util.h" |
| 23 | |
| 24 | namespace content { |
| 25 | |
| 26 | ShellDownloadManagerDelegate::ShellDownloadManagerDelegate() |
| 27 | : download_manager_(NULL) { |
| 28 | } |
| 29 | |
| 30 | ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate(){ |
| 31 | } |
| 32 | |
| 33 | |
| 34 | void ShellDownloadManagerDelegate::SetDownloadManager( |
| 35 | DownloadManager* download_manager) { |
| 36 | download_manager_ = download_manager; |
| 37 | } |
| 38 | |
| 39 | void ShellDownloadManagerDelegate::Shutdown() { |
| 40 | } |
| 41 | |
| 42 | bool ShellDownloadManagerDelegate::ShouldStartDownload(int32 download_id) { |
| 43 | DownloadItem* download = |
| 44 | download_manager_->GetActiveDownloadItem(download_id); |
| 45 | DownloadStateInfo state = download->state_info(); |
| 46 | |
| 47 | if (!state.force_file_name.empty()) |
| 48 | return true; |
| 49 | |
| 50 | FilePath generated_name = net::GenerateFileName( |
| 51 | download->GetURL(), |
| 52 | download->content_disposition(), |
| 53 | download->referrer_charset(), |
| 54 | download->suggested_filename(), |
| 55 | download->mime_type(), |
[email protected] | c9b6eb6 | 2011-10-18 20:49:39 | [diff] [blame^] | 56 | "download"); |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 57 | |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 58 | // Since we have no download UI, show the user a dialog always. |
| 59 | state.prompt_user_for_save_location = true; |
| 60 | |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 61 | BrowserThread::PostTask( |
| 62 | BrowserThread::FILE, |
| 63 | FROM_HERE, |
| 64 | base::Bind( |
| 65 | &ShellDownloadManagerDelegate::GenerateFilename, |
| 66 | this, download_id, state, generated_name)); |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | void ShellDownloadManagerDelegate::GenerateFilename( |
| 71 | int32 download_id, |
| 72 | DownloadStateInfo state, |
| 73 | const FilePath& generated_name) { |
| 74 | if (state.suggested_path.empty()) { |
| 75 | state.suggested_path = download_manager_->browser_context()->GetPath(). |
| 76 | Append(FILE_PATH_LITERAL("Downloads")); |
| 77 | if (!file_util::PathExists(state.suggested_path)) |
| 78 | file_util::CreateDirectory(state.suggested_path); |
| 79 | } |
| 80 | |
| 81 | state.suggested_path = state.suggested_path.Append(generated_name); |
| 82 | |
| 83 | BrowserThread::PostTask( |
| 84 | BrowserThread::UI, |
| 85 | FROM_HERE, |
| 86 | base::Bind( |
| 87 | &ShellDownloadManagerDelegate::RestartDownload, |
| 88 | this, download_id, state)); |
| 89 | } |
| 90 | |
| 91 | void ShellDownloadManagerDelegate::RestartDownload( |
| 92 | int32 download_id, |
| 93 | DownloadStateInfo state) { |
| 94 | DownloadItem* download = |
| 95 | download_manager_->GetActiveDownloadItem(download_id); |
| 96 | if (!download) |
| 97 | return; |
| 98 | download->SetFileCheckResults(state); |
| 99 | download_manager_->RestartDownload(download_id); |
| 100 | } |
| 101 | |
| 102 | void ShellDownloadManagerDelegate::ChooseDownloadPath( |
| 103 | TabContents* tab_contents, |
| 104 | const FilePath& suggested_path, |
| 105 | void* data) { |
[email protected] | 5c12f4a | 2011-09-30 21:59:04 | [diff] [blame] | 106 | FilePath result; |
| 107 | #if defined(OS_WIN) |
| 108 | std::wstring file_part = FilePath(suggested_path).BaseName().value(); |
| 109 | wchar_t file_name[MAX_PATH]; |
| 110 | base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name)); |
| 111 | OPENFILENAME save_as; |
| 112 | ZeroMemory(&save_as, sizeof(save_as)); |
| 113 | save_as.lStructSize = sizeof(OPENFILENAME); |
| 114 | save_as.hwndOwner = tab_contents->GetNativeView(); |
| 115 | save_as.lpstrFile = file_name; |
| 116 | save_as.nMaxFile = arraysize(file_name); |
| 117 | |
| 118 | std::wstring directory; |
| 119 | if (!suggested_path.empty()) |
| 120 | directory = suggested_path.DirName().value(); |
| 121 | |
| 122 | save_as.lpstrInitialDir = directory.c_str(); |
| 123 | save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING | |
| 124 | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST; |
| 125 | |
| 126 | if (GetSaveFileName(&save_as)) |
| 127 | result = FilePath(std::wstring(save_as.lpstrFile)); |
| 128 | #else |
| 129 | NOTIMPLEMENTED(); |
| 130 | #endif |
| 131 | |
| 132 | if (result.empty()) { |
| 133 | download_manager_->FileSelectionCanceled(data); |
| 134 | } else { |
| 135 | download_manager_->FileSelected(result, data); |
| 136 | } |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | bool ShellDownloadManagerDelegate::OverrideIntermediatePath( |
| 140 | DownloadItem* item, |
| 141 | FilePath* intermediate_path) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | TabContents* ShellDownloadManagerDelegate:: |
| 146 | GetAlternativeTabContentsToNotifyForDownload() { |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | bool ShellDownloadManagerDelegate::ShouldOpenFileBasedOnExtension( |
| 151 | const FilePath& path) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | bool ShellDownloadManagerDelegate::ShouldOpenDownload(DownloadItem* item) { |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | bool ShellDownloadManagerDelegate::ShouldCompleteDownload(DownloadItem* item) { |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | bool ShellDownloadManagerDelegate::GenerateFileHash() { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | void ShellDownloadManagerDelegate::OnResponseCompleted( |
| 168 | DownloadItem* item, |
| 169 | const std::string& hash) { |
| 170 | } |
| 171 | |
| 172 | void ShellDownloadManagerDelegate::AddItemToPersistentStore( |
| 173 | DownloadItem* item) { |
| 174 | } |
| 175 | |
| 176 | void ShellDownloadManagerDelegate::UpdateItemInPersistentStore( |
| 177 | DownloadItem* item) { |
| 178 | } |
| 179 | |
| 180 | void ShellDownloadManagerDelegate::UpdatePathForItemInPersistentStore( |
| 181 | DownloadItem* item, |
| 182 | const FilePath& new_path) { |
| 183 | } |
| 184 | |
| 185 | void ShellDownloadManagerDelegate::RemoveItemFromPersistentStore( |
| 186 | DownloadItem* item) { |
| 187 | } |
| 188 | |
| 189 | void ShellDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween( |
| 190 | const base::Time remove_begin, |
| 191 | const base::Time remove_end) { |
| 192 | } |
| 193 | |
| 194 | void ShellDownloadManagerDelegate::GetSaveDir( |
| 195 | TabContents* tab_contents, |
| 196 | FilePath* website_save_dir, |
| 197 | FilePath* download_save_dir) { |
| 198 | } |
| 199 | |
| 200 | void ShellDownloadManagerDelegate::ChooseSavePath( |
| 201 | const base::WeakPtr<SavePackage>& save_package, |
| 202 | const FilePath& suggested_path, |
| 203 | bool can_save_as_complete) { |
| 204 | } |
| 205 | |
| 206 | void ShellDownloadManagerDelegate::DownloadProgressUpdated() { |
| 207 | } |
| 208 | |
| 209 | } // namespace content |