forked from nwjs/nw.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_download_manager_delegate.cc
More file actions
129 lines (108 loc) · 3.87 KB
/
shell_download_manager_delegate.cc
File metadata and controls
129 lines (108 loc) · 3.87 KB
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
// Copyright (c) 2012 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/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/web_contents.h"
#include "net/base/net_util.h"
namespace content {
ShellDownloadManagerDelegate::ShellDownloadManagerDelegate()
: download_manager_(NULL) {
// Balanced in Shutdown();
AddRef();
}
ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate(){
}
void ShellDownloadManagerDelegate::SetDownloadManager(
DownloadManager* download_manager) {
download_manager_ = download_manager;
}
void ShellDownloadManagerDelegate::Shutdown() {
Release();
}
bool ShellDownloadManagerDelegate::DetermineDownloadTarget(
DownloadItem* download,
const DownloadTargetCallback& callback) {
if (!download->GetForcedFilePath().empty()) {
callback.Run(download->GetForcedFilePath(),
DownloadItem::TARGET_DISPOSITION_OVERWRITE,
DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
download->GetForcedFilePath());
return true;
}
FilePath generated_name = net::GenerateFileName(
download->GetURL(),
download->GetContentDisposition(),
download->GetReferrerCharset(),
download->GetSuggestedFilename(),
download->GetMimeType(),
"download");
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(
&ShellDownloadManagerDelegate::GenerateFilename,
this, download->GetId(), callback, generated_name));
return false;
}
void ShellDownloadManagerDelegate::GenerateFilename(
int32 download_id,
const DownloadTargetCallback& callback,
const FilePath& generated_name) {
FilePath suggested_path = download_manager_->GetBrowserContext()->GetPath().
Append(FILE_PATH_LITERAL("Downloads"));
if (!file_util::PathExists(suggested_path))
file_util::CreateDirectory(suggested_path);
suggested_path = suggested_path.Append(generated_name);
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(
&ShellDownloadManagerDelegate::ChooseDownloadPath,
this, download_id, callback, suggested_path));
}
void ShellDownloadManagerDelegate::ChooseDownloadPath(
int32 download_id,
const DownloadTargetCallback& callback,
const FilePath& suggested_path) {
DownloadItem* item =
download_manager_->GetActiveDownloadItem(download_id);
if (!item)
return;
FilePath result;
#if defined(OS_WIN) && !defined(USE_AURA)
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 = item->GetWebContents()->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
callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT,
DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result);
}
} // namespace content