1 // Copyright 2013 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.
5 #include "content/shell/browser/shell_download_manager_delegate.h"
7 #if defined(TOOLKIT_GTK)
16 #include "base/bind.h"
17 #include "base/command_line.h"
18 #include "base/file_util.h"
19 #include "base/logging.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "content/public/browser/browser_context.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/download_manager.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_contents_view.h"
27 #include "content/shell/browser/webkit_test_controller.h"
28 #include "content/shell/common/shell_switches.h"
29 #include "net/base/net_util.h"
32 #include "ui/aura/root_window.h"
33 #include "ui/aura/window.h"
38 ShellDownloadManagerDelegate::ShellDownloadManagerDelegate()
39 : download_manager_(NULL
),
40 suppress_prompting_(false),
41 weak_ptr_factory_(this) {}
43 ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate(){
44 if (download_manager_
) {
45 DCHECK_EQ(static_cast<DownloadManagerDelegate
*>(this),
46 download_manager_
->GetDelegate());
47 download_manager_
->SetDelegate(NULL
);
48 download_manager_
= NULL
;
53 void ShellDownloadManagerDelegate::SetDownloadManager(
54 DownloadManager
* download_manager
) {
55 download_manager_
= download_manager
;
58 void ShellDownloadManagerDelegate::Shutdown() {
59 // Revoke any pending callbacks. download_manager_ et. al. are no longer safe
60 // to access after this point.
61 weak_ptr_factory_
.InvalidateWeakPtrs();
62 download_manager_
= NULL
;
65 bool ShellDownloadManagerDelegate::DetermineDownloadTarget(
66 DownloadItem
* download
,
67 const DownloadTargetCallback
& callback
) {
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
69 // This assignment needs to be here because even at the call to
70 // SetDownloadManager, the system is not fully initialized.
71 if (default_download_path_
.empty()) {
72 default_download_path_
= download_manager_
->GetBrowserContext()->GetPath().
73 Append(FILE_PATH_LITERAL("Downloads"));
76 if (!download
->GetForcedFilePath().empty()) {
77 callback
.Run(download
->GetForcedFilePath(),
78 DownloadItem::TARGET_DISPOSITION_OVERWRITE
,
79 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS
,
80 download
->GetForcedFilePath());
84 FilenameDeterminedCallback filename_determined_callback
=
85 base::Bind(&ShellDownloadManagerDelegate::OnDownloadPathGenerated
,
86 weak_ptr_factory_
.GetWeakPtr(),
90 BrowserThread::PostTask(
93 base::Bind(&ShellDownloadManagerDelegate::GenerateFilename
,
95 download
->GetContentDisposition(),
96 download
->GetSuggestedFilename(),
97 download
->GetMimeType(),
98 default_download_path_
,
99 filename_determined_callback
));
103 bool ShellDownloadManagerDelegate::ShouldOpenDownload(
105 const DownloadOpenDelayedCallback
& callback
) {
106 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
) &&
107 WebKitTestController::Get()->IsMainWindow(item
->GetWebContents()) &&
108 item
->GetMimeType() == "text/html") {
109 WebKitTestController::Get()->OpenURL(
110 net::FilePathToFileURL(item
->GetFullPath()));
115 void ShellDownloadManagerDelegate::GetNextId(
116 const DownloadIdCallback
& callback
) {
117 static uint32 next_id
= DownloadItem::kInvalidId
+ 1;
118 callback
.Run(next_id
++);
122 void ShellDownloadManagerDelegate::GenerateFilename(
124 const std::string
& content_disposition
,
125 const std::string
& suggested_filename
,
126 const std::string
& mime_type
,
127 const base::FilePath
& suggested_directory
,
128 const FilenameDeterminedCallback
& callback
) {
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
130 base::FilePath generated_name
= net::GenerateFileName(url
,
137 if (!base::PathExists(suggested_directory
))
138 base::CreateDirectory(suggested_directory
);
140 base::FilePath
suggested_path(suggested_directory
.Append(generated_name
));
141 BrowserThread::PostTask(
142 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, suggested_path
));
145 void ShellDownloadManagerDelegate::OnDownloadPathGenerated(
147 const DownloadTargetCallback
& callback
,
148 const base::FilePath
& suggested_path
) {
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
150 if (suppress_prompting_
) {
152 callback
.Run(suggested_path
, DownloadItem::TARGET_DISPOSITION_OVERWRITE
,
153 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS
,
154 suggested_path
.AddExtension(FILE_PATH_LITERAL(".crdownload")));
158 ChooseDownloadPath(download_id
, callback
, suggested_path
);
161 void ShellDownloadManagerDelegate::ChooseDownloadPath(
163 const DownloadTargetCallback
& callback
,
164 const base::FilePath
& suggested_path
) {
165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
166 DownloadItem
* item
= download_manager_
->GetDownload(download_id
);
167 if (!item
|| (item
->GetState() != DownloadItem::IN_PROGRESS
))
170 base::FilePath result
;
172 std::wstring file_part
= base::FilePath(suggested_path
).BaseName().value();
173 wchar_t file_name
[MAX_PATH
];
174 base::wcslcpy(file_name
, file_part
.c_str(), arraysize(file_name
));
175 OPENFILENAME save_as
;
176 ZeroMemory(&save_as
, sizeof(save_as
));
177 save_as
.lStructSize
= sizeof(OPENFILENAME
);
178 save_as
.hwndOwner
= item
->GetWebContents()->GetView()->GetNativeView()->
179 GetDispatcher()->host()->GetAcceleratedWidget();
180 save_as
.lpstrFile
= file_name
;
181 save_as
.nMaxFile
= arraysize(file_name
);
183 std::wstring directory
;
184 if (!suggested_path
.empty())
185 directory
= suggested_path
.DirName().value();
187 save_as
.lpstrInitialDir
= directory
.c_str();
188 save_as
.Flags
= OFN_OVERWRITEPROMPT
| OFN_EXPLORER
| OFN_ENABLESIZING
|
189 OFN_NOCHANGEDIR
| OFN_PATHMUSTEXIST
;
191 if (GetSaveFileName(&save_as
))
192 result
= base::FilePath(std::wstring(save_as
.lpstrFile
));
193 #elif defined(TOOLKIT_GTK)
195 gfx::NativeWindow parent_window
;
196 std::string base_name
= base::FilePath(suggested_path
).BaseName().value();
198 parent_window
= item
->GetWebContents()->GetView()->GetTopLevelNativeWindow();
199 dialog
= gtk_file_chooser_dialog_new("Save File",
201 GTK_FILE_CHOOSER_ACTION_SAVE
,
202 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
203 GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
,
205 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog
),
207 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
),
210 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
) {
212 filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
213 result
= base::FilePath(filename
);
216 gtk_widget_destroy(dialog
);
221 callback
.Run(result
, DownloadItem::TARGET_DISPOSITION_PROMPT
,
222 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS
, result
);
225 void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting(
226 const base::FilePath
& default_download_path
) {
227 default_download_path_
= default_download_path
;
228 suppress_prompting_
= true;
231 } // namespace content