Add ICU message format support
[chromium-blink-merge.git] / content / shell / browser / shell_download_manager_delegate.cc
blobda4978ee419b25a575927c51f776f27cbc8e0e3b
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(OS_WIN)
8 #include <windows.h>
9 #include <commdlg.h>
10 #endif
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "base/files/file_util.h"
15 #include "base/logging.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/download_manager.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/shell/common/shell_switches.h"
23 #include "net/base/filename_util.h"
25 #if defined(OS_WIN)
26 #include "ui/aura/window.h"
27 #include "ui/aura/window_tree_host.h"
28 #endif
30 namespace content {
32 ShellDownloadManagerDelegate::ShellDownloadManagerDelegate()
33 : download_manager_(NULL),
34 suppress_prompting_(false),
35 weak_ptr_factory_(this) {}
37 ShellDownloadManagerDelegate::~ShellDownloadManagerDelegate(){
38 if (download_manager_) {
39 DCHECK_EQ(static_cast<DownloadManagerDelegate*>(this),
40 download_manager_->GetDelegate());
41 download_manager_->SetDelegate(NULL);
42 download_manager_ = NULL;
47 void ShellDownloadManagerDelegate::SetDownloadManager(
48 DownloadManager* download_manager) {
49 download_manager_ = download_manager;
52 void ShellDownloadManagerDelegate::Shutdown() {
53 // Revoke any pending callbacks. download_manager_ et. al. are no longer safe
54 // to access after this point.
55 weak_ptr_factory_.InvalidateWeakPtrs();
56 download_manager_ = NULL;
59 bool ShellDownloadManagerDelegate::DetermineDownloadTarget(
60 DownloadItem* download,
61 const DownloadTargetCallback& callback) {
62 DCHECK_CURRENTLY_ON(BrowserThread::UI);
63 // This assignment needs to be here because even at the call to
64 // SetDownloadManager, the system is not fully initialized.
65 if (default_download_path_.empty()) {
66 default_download_path_ = download_manager_->GetBrowserContext()->GetPath().
67 Append(FILE_PATH_LITERAL("Downloads"));
70 if (!download->GetForcedFilePath().empty()) {
71 callback.Run(download->GetForcedFilePath(),
72 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
73 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
74 download->GetForcedFilePath());
75 return true;
78 FilenameDeterminedCallback filename_determined_callback =
79 base::Bind(&ShellDownloadManagerDelegate::OnDownloadPathGenerated,
80 weak_ptr_factory_.GetWeakPtr(),
81 download->GetId(),
82 callback);
84 BrowserThread::PostTask(
85 BrowserThread::FILE,
86 FROM_HERE,
87 base::Bind(&ShellDownloadManagerDelegate::GenerateFilename,
88 download->GetURL(),
89 download->GetContentDisposition(),
90 download->GetSuggestedFilename(),
91 download->GetMimeType(),
92 default_download_path_,
93 filename_determined_callback));
94 return true;
97 bool ShellDownloadManagerDelegate::ShouldOpenDownload(
98 DownloadItem* item,
99 const DownloadOpenDelayedCallback& callback) {
100 return true;
103 void ShellDownloadManagerDelegate::GetNextId(
104 const DownloadIdCallback& callback) {
105 static uint32 next_id = DownloadItem::kInvalidId + 1;
106 callback.Run(next_id++);
109 // static
110 void ShellDownloadManagerDelegate::GenerateFilename(
111 const GURL& url,
112 const std::string& content_disposition,
113 const std::string& suggested_filename,
114 const std::string& mime_type,
115 const base::FilePath& suggested_directory,
116 const FilenameDeterminedCallback& callback) {
117 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
118 base::FilePath generated_name = net::GenerateFileName(url,
119 content_disposition,
120 std::string(),
121 suggested_filename,
122 mime_type,
123 "download");
125 if (!base::PathExists(suggested_directory))
126 base::CreateDirectory(suggested_directory);
128 base::FilePath suggested_path(suggested_directory.Append(generated_name));
129 BrowserThread::PostTask(
130 BrowserThread::UI, FROM_HERE, base::Bind(callback, suggested_path));
133 void ShellDownloadManagerDelegate::OnDownloadPathGenerated(
134 uint32 download_id,
135 const DownloadTargetCallback& callback,
136 const base::FilePath& suggested_path) {
137 DCHECK_CURRENTLY_ON(BrowserThread::UI);
138 if (suppress_prompting_) {
139 // Testing exit.
140 callback.Run(suggested_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
141 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
142 suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload")));
143 return;
146 ChooseDownloadPath(download_id, callback, suggested_path);
149 void ShellDownloadManagerDelegate::ChooseDownloadPath(
150 uint32 download_id,
151 const DownloadTargetCallback& callback,
152 const base::FilePath& suggested_path) {
153 DCHECK_CURRENTLY_ON(BrowserThread::UI);
154 DownloadItem* item = download_manager_->GetDownload(download_id);
155 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS))
156 return;
158 base::FilePath result;
159 #if defined(OS_WIN)
160 std::wstring file_part = base::FilePath(suggested_path).BaseName().value();
161 wchar_t file_name[MAX_PATH];
162 base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name));
163 OPENFILENAME save_as;
164 ZeroMemory(&save_as, sizeof(save_as));
165 save_as.lStructSize = sizeof(OPENFILENAME);
166 save_as.hwndOwner = item->GetWebContents()->GetNativeView()->
167 GetHost()->GetAcceleratedWidget();
168 save_as.lpstrFile = file_name;
169 save_as.nMaxFile = arraysize(file_name);
171 std::wstring directory;
172 if (!suggested_path.empty())
173 directory = suggested_path.DirName().value();
175 save_as.lpstrInitialDir = directory.c_str();
176 save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
177 OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
179 if (GetSaveFileName(&save_as))
180 result = base::FilePath(std::wstring(save_as.lpstrFile));
181 #else
182 NOTIMPLEMENTED();
183 #endif
185 callback.Run(result, DownloadItem::TARGET_DISPOSITION_PROMPT,
186 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result);
189 void ShellDownloadManagerDelegate::SetDownloadBehaviorForTesting(
190 const base::FilePath& default_download_path) {
191 default_download_path_ = default_download_path;
192 suppress_prompting_ = true;
195 } // namespace content