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