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.
5 #include "content/shell/shell_download_manager_delegate.h"
12 #include "base/bind.h"
13 #include "base/file_util.h"
14 #include "base/logging.h"
15 #include "base/string_util.h"
16 #include "base/utf_string_conversions.h"
17 #include "content/browser/browser_context.h"
18 #include "content/browser/download/download_manager.h"
19 #include "content/browser/download/download_state_info.h"
20 #include "content/browser/tab_contents/tab_contents.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "net/base/net_util.h"
26 ShellDownloadManagerDelegate::ShellDownloadManagerDelegate()
27 : download_manager_(NULL
) {
30 ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate(){
34 void ShellDownloadManagerDelegate::SetDownloadManager(
35 DownloadManager
* download_manager
) {
36 download_manager_
= download_manager
;
39 void ShellDownloadManagerDelegate::Shutdown() {
42 bool ShellDownloadManagerDelegate::ShouldStartDownload(int32 download_id
) {
43 DownloadItem
* download
=
44 download_manager_
->GetActiveDownloadItem(download_id
);
45 DownloadStateInfo state
= download
->GetStateInfo();
47 if (!state
.force_file_name
.empty())
50 FilePath generated_name
= net::GenerateFileName(
52 download
->GetContentDisposition(),
53 download
->GetReferrerCharset(),
54 download
->GetSuggestedFilename(),
55 download
->GetMimeType(),
58 // Since we have no download UI, show the user a dialog always.
59 state
.prompt_user_for_save_location
= true;
61 BrowserThread::PostTask(
65 &ShellDownloadManagerDelegate::GenerateFilename
,
66 this, download_id
, state
, generated_name
));
70 void ShellDownloadManagerDelegate::GenerateFilename(
72 DownloadStateInfo state
,
73 const FilePath
& generated_name
) {
74 if (state
.suggested_path
.empty()) {
75 state
.suggested_path
= download_manager_
->BrowserContext()->GetPath().
76 Append(FILE_PATH_LITERAL("Downloads"));
77 if (!file_util::PathExists(state
.suggested_path
))
78 file_util::CreateDirectory(state
.suggested_path
);
81 state
.suggested_path
= state
.suggested_path
.Append(generated_name
);
83 BrowserThread::PostTask(
87 &ShellDownloadManagerDelegate::RestartDownload
,
88 this, download_id
, state
));
91 void ShellDownloadManagerDelegate::RestartDownload(
93 DownloadStateInfo state
) {
94 DownloadItem
* download
=
95 download_manager_
->GetActiveDownloadItem(download_id
);
98 download
->SetFileCheckResults(state
);
99 download_manager_
->RestartDownload(download_id
);
102 void ShellDownloadManagerDelegate::ChooseDownloadPath(
103 TabContents
* tab_contents
,
104 const FilePath
& suggested_path
,
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
);
118 std::wstring directory
;
119 if (!suggested_path
.empty())
120 directory
= suggested_path
.DirName().value();
122 save_as
.lpstrInitialDir
= directory
.c_str();
123 save_as
.Flags
= OFN_OVERWRITEPROMPT
| OFN_EXPLORER
| OFN_ENABLESIZING
|
124 OFN_NOCHANGEDIR
| OFN_PATHMUSTEXIST
;
126 if (GetSaveFileName(&save_as
))
127 result
= FilePath(std::wstring(save_as
.lpstrFile
));
132 if (result
.empty()) {
133 download_manager_
->FileSelectionCanceled(data
);
135 download_manager_
->FileSelected(result
, data
);
139 bool ShellDownloadManagerDelegate::OverrideIntermediatePath(
141 FilePath
* intermediate_path
) {
145 TabContents
* ShellDownloadManagerDelegate::
146 GetAlternativeTabContentsToNotifyForDownload() {
150 bool ShellDownloadManagerDelegate::ShouldOpenFileBasedOnExtension(
151 const FilePath
& path
) {
155 bool ShellDownloadManagerDelegate::ShouldCompleteDownload(DownloadItem
* item
) {
159 bool ShellDownloadManagerDelegate::ShouldOpenDownload(DownloadItem
* item
) {
163 bool ShellDownloadManagerDelegate::GenerateFileHash() {
167 void ShellDownloadManagerDelegate::OnResponseCompleted(DownloadItem
* item
) {
170 void ShellDownloadManagerDelegate::AddItemToPersistentStore(
171 DownloadItem
* item
) {
174 void ShellDownloadManagerDelegate::UpdateItemInPersistentStore(
175 DownloadItem
* item
) {
178 void ShellDownloadManagerDelegate::UpdatePathForItemInPersistentStore(
180 const FilePath
& new_path
) {
183 void ShellDownloadManagerDelegate::RemoveItemFromPersistentStore(
184 DownloadItem
* item
) {
187 void ShellDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween(
188 const base::Time remove_begin
,
189 const base::Time remove_end
) {
192 void ShellDownloadManagerDelegate::GetSaveDir(
193 TabContents
* tab_contents
,
194 FilePath
* website_save_dir
,
195 FilePath
* download_save_dir
) {
198 void ShellDownloadManagerDelegate::ChooseSavePath(
199 const base::WeakPtr
<SavePackage
>& save_package
,
200 const FilePath
& suggested_path
,
201 bool can_save_as_complete
) {
204 void ShellDownloadManagerDelegate::DownloadProgressUpdated() {
207 } // namespace content