| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/shell/shell_download_manager_delegate.h" | 5 #include "content/shell/shell_download_manager_delegate.h" |
| 6 | 6 |
| 7 #if defined(TOOLKIT_GTK) | 7 #if defined(TOOLKIT_GTK) |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 } | 59 } |
| 60 | 60 |
| 61 if (!download->GetForcedFilePath().empty()) { | 61 if (!download->GetForcedFilePath().empty()) { |
| 62 callback.Run(download->GetForcedFilePath(), | 62 callback.Run(download->GetForcedFilePath(), |
| 63 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 63 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 64 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 64 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 65 download->GetForcedFilePath()); | 65 download->GetForcedFilePath()); |
| 66 return true; | 66 return true; |
| 67 } | 67 } |
| 68 | 68 |
| 69 FilePath generated_name = net::GenerateFileName( | 69 base::FilePath generated_name = net::GenerateFileName( |
| 70 download->GetURL(), | 70 download->GetURL(), |
| 71 download->GetContentDisposition(), | 71 download->GetContentDisposition(), |
| 72 EmptyString(), | 72 EmptyString(), |
| 73 download->GetSuggestedFilename(), | 73 download->GetSuggestedFilename(), |
| 74 download->GetMimeType(), | 74 download->GetMimeType(), |
| 75 "download"); | 75 "download"); |
| 76 | 76 |
| 77 BrowserThread::PostTask( | 77 BrowserThread::PostTask( |
| 78 BrowserThread::FILE, | 78 BrowserThread::FILE, |
| 79 FROM_HERE, | 79 FROM_HERE, |
| 80 base::Bind( | 80 base::Bind( |
| 81 &ShellDownloadManagerDelegate::GenerateFilename, | 81 &ShellDownloadManagerDelegate::GenerateFilename, |
| 82 this, download->GetId(), callback, generated_name, | 82 this, download->GetId(), callback, generated_name, |
| 83 default_download_path_)); | 83 default_download_path_)); |
| 84 return true; | 84 return true; |
| 85 } | 85 } |
| 86 | 86 |
| 87 void ShellDownloadManagerDelegate::GenerateFilename( | 87 void ShellDownloadManagerDelegate::GenerateFilename( |
| 88 int32 download_id, | 88 int32 download_id, |
| 89 const DownloadTargetCallback& callback, | 89 const DownloadTargetCallback& callback, |
| 90 const FilePath& generated_name, | 90 const base::FilePath& generated_name, |
| 91 const FilePath& suggested_directory) { | 91 const base::FilePath& suggested_directory) { |
| 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 93 if (!file_util::PathExists(suggested_directory)) | 93 if (!file_util::PathExists(suggested_directory)) |
| 94 file_util::CreateDirectory(suggested_directory); | 94 file_util::CreateDirectory(suggested_directory); |
| 95 | 95 |
| 96 FilePath suggested_path(suggested_directory.Append(generated_name)); | 96 base::FilePath suggested_path(suggested_directory.Append(generated_name)); |
| 97 BrowserThread::PostTask( | 97 BrowserThread::PostTask( |
| 98 BrowserThread::UI, | 98 BrowserThread::UI, |
| 99 FROM_HERE, | 99 FROM_HERE, |
| 100 base::Bind( | 100 base::Bind( |
| 101 &ShellDownloadManagerDelegate::OnDownloadPathGenerated, | 101 &ShellDownloadManagerDelegate::OnDownloadPathGenerated, |
| 102 this, download_id, callback, suggested_path)); | 102 this, download_id, callback, suggested_path)); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void ShellDownloadManagerDelegate::OnDownloadPathGenerated( | 105 void ShellDownloadManagerDelegate::OnDownloadPathGenerated( |
| 106 int32 download_id, | 106 int32 download_id, |
| 107 const DownloadTargetCallback& callback, | 107 const DownloadTargetCallback& callback, |
| 108 const FilePath& suggested_path) { | 108 const base::FilePath& suggested_path) { |
| 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 110 if (suppress_prompting_) { | 110 if (suppress_prompting_) { |
| 111 // Testing exit. | 111 // Testing exit. |
| 112 callback.Run(suggested_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 112 callback.Run(suggested_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 113 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 113 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 114 suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload"))); | 114 suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload"))); |
| 115 return; | 115 return; |
| 116 } | 116 } |
| 117 | 117 |
| 118 ChooseDownloadPath(download_id, callback, suggested_path); | 118 ChooseDownloadPath(download_id, callback, suggested_path); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void ShellDownloadManagerDelegate::ChooseDownloadPath( | 121 void ShellDownloadManagerDelegate::ChooseDownloadPath( |
| 122 int32 download_id, | 122 int32 download_id, |
| 123 const DownloadTargetCallback& callback, | 123 const DownloadTargetCallback& callback, |
| 124 const FilePath& suggested_path) { | 124 const base::FilePath& suggested_path) { |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 126 DownloadItem* item = download_manager_->GetDownload(download_id); | 126 DownloadItem* item = download_manager_->GetDownload(download_id); |
| 127 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) | 127 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) |
| 128 return; | 128 return; |
| 129 | 129 |
| 130 FilePath result; | 130 base::FilePath result; |
| 131 #if defined(OS_WIN) && !defined(USE_AURA) | 131 #if defined(OS_WIN) && !defined(USE_AURA) |
| 132 std::wstring file_part = FilePath(suggested_path).BaseName().value(); | 132 std::wstring file_part = base::FilePath(suggested_path).BaseName().value(); |
| 133 wchar_t file_name[MAX_PATH]; | 133 wchar_t file_name[MAX_PATH]; |
| 134 base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name)); | 134 base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name)); |
| 135 OPENFILENAME save_as; | 135 OPENFILENAME save_as; |
| 136 ZeroMemory(&save_as, sizeof(save_as)); | 136 ZeroMemory(&save_as, sizeof(save_as)); |
| 137 save_as.lStructSize = sizeof(OPENFILENAME); | 137 save_as.lStructSize = sizeof(OPENFILENAME); |
| 138 save_as.hwndOwner = item->GetWebContents()->GetNativeView(); | 138 save_as.hwndOwner = item->GetWebContents()->GetNativeView(); |
| 139 save_as.lpstrFile = file_name; | 139 save_as.lpstrFile = file_name; |
| 140 save_as.nMaxFile = arraysize(file_name); | 140 save_as.nMaxFile = arraysize(file_name); |
| 141 | 141 |
| 142 std::wstring directory; | 142 std::wstring directory; |
| 143 if (!suggested_path.empty()) | 143 if (!suggested_path.empty()) |
| 144 directory = suggested_path.DirName().value(); | 144 directory = suggested_path.DirName().value(); |
| 145 | 145 |
| 146 save_as.lpstrInitialDir = directory.c_str(); | 146 save_as.lpstrInitialDir = directory.c_str(); |
| 147 save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING | | 147 save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING | |
| 148 OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST; | 148 OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST; |
| 149 | 149 |
| 150 if (GetSaveFileName(&save_as)) | 150 if (GetSaveFileName(&save_as)) |
| 151 result = FilePath(std::wstring(save_as.lpstrFile)); | 151 result = base::FilePath(std::wstring(save_as.lpstrFile)); |
| 152 #elif defined(TOOLKIT_GTK) | 152 #elif defined(TOOLKIT_GTK) |
| 153 GtkWidget *dialog; | 153 GtkWidget *dialog; |
| 154 gfx::NativeWindow parent_window; | 154 gfx::NativeWindow parent_window; |
| 155 std::string base_name = FilePath(suggested_path).BaseName().value(); | 155 std::string base_name = base::FilePath(suggested_path).BaseName().value(); |
| 156 | 156 |
| 157 parent_window = item->GetWebContents()->GetView()->GetTopLevelNativeWindow(); | 157 parent_window = item->GetWebContents()->GetView()->GetTopLevelNativeWindow(); |
| 158 dialog = gtk_file_chooser_dialog_new("Save File", | 158 dialog = gtk_file_chooser_dialog_new("Save File", |
| 159 parent_window, | 159 parent_window, |
| 160 GTK_FILE_CHOOSER_ACTION_SAVE, | 160 GTK_FILE_CHOOSER_ACTION_SAVE, |
| 161 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | 161 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
| 162 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, | 162 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, |
| 163 NULL); | 163 NULL); |
| 164 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), | 164 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), |
| 165 TRUE); | 165 TRUE); |
| 166 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), | 166 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), |
| 167 base_name.c_str()); | 167 base_name.c_str()); |
| 168 | 168 |
| 169 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { | 169 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { |
| 170 char *filename; | 170 char *filename; |
| 171 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); | 171 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); |
| 172 result = FilePath(filename); | 172 result = base::FilePath(filename); |
| 173 } | 173 } |
| 174 gtk_widget_destroy(dialog); | 174 gtk_widget_destroy(dialog); |
| 175 #else | 175 #else |
| 176 NOTIMPLEMENTED(); | 176 NOTIMPLEMENTED(); |
| 177 #endif | 177 #endif |
| 178 | 178 |
| 179 callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT, | 179 callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 180 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result); | 180 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result); |
| 181 } | 181 } |
| 182 | 182 |
| 183 void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting( | 183 void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting( |
| 184 const FilePath& default_download_path) { | 184 const base::FilePath& default_download_path) { |
| 185 default_download_path_ = default_download_path; | 185 default_download_path_ = default_download_path; |
| 186 suppress_prompting_ = true; | 186 suppress_prompting_ = true; |
| 187 } | 187 } |
| 188 | 188 |
| 189 } // namespace content | 189 } // namespace content |
| OLD | NEW |