1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 #include "xwalk/runtime/browser/runtime_download_manager_delegate.h"
15 #include "base/bind.h"
16 #include "base/command_line.h"
17 #include "base/file_util.h"
18 #include "base/logging.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "content/public/browser/browser_context.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/download_manager.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/shell/common/shell_switches.h"
26 #include "content/shell/browser/webkit_test_controller.h"
27 #include "net/base/filename_util.h"
29 using content::BrowserThread;
33 RuntimeDownloadManagerDelegate::RuntimeDownloadManagerDelegate()
34 : download_manager_(NULL),
35 suppress_prompting_(false) {
36 // Balanced in Shutdown();
40 RuntimeDownloadManagerDelegate::~RuntimeDownloadManagerDelegate() {}
42 void RuntimeDownloadManagerDelegate::SetDownloadManager(
43 content::DownloadManager* download_manager) {
44 download_manager_ = download_manager;
47 void RuntimeDownloadManagerDelegate::Shutdown() {
51 bool RuntimeDownloadManagerDelegate::DetermineDownloadTarget(
52 content::DownloadItem* download,
53 const content::DownloadTargetCallback& callback) {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
55 // This assignment needs to be here because even at the call to
56 // SetDownloadManager, the system is not fully initialized.
57 if (default_download_path_.empty()) {
58 default_download_path_ = download_manager_->GetBrowserContext()->GetPath().
59 Append(FILE_PATH_LITERAL("Downloads"));
62 if (!download->GetForcedFilePath().empty()) {
63 callback.Run(download->GetForcedFilePath(),
64 content::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
65 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
66 download->GetForcedFilePath());
70 base::FilePath generated_name = net::GenerateFileName(
72 download->GetContentDisposition(),
74 download->GetSuggestedFilename(),
75 download->GetMimeType(),
78 BrowserThread::PostTask(
82 &RuntimeDownloadManagerDelegate::GenerateFilename,
83 this, download->GetId(), callback, generated_name,
84 default_download_path_));
88 bool RuntimeDownloadManagerDelegate::ShouldOpenDownload(
89 content::DownloadItem* item,
90 const content::DownloadOpenDelayedCallback& callback) {
94 void RuntimeDownloadManagerDelegate::GetNextId(
95 const content::DownloadIdCallback& callback) {
96 static uint32 next_id = content::DownloadItem::kInvalidId + 1;
97 callback.Run(next_id++);
100 void RuntimeDownloadManagerDelegate::GenerateFilename(
102 const content::DownloadTargetCallback& callback,
103 const base::FilePath& generated_name,
104 const base::FilePath& suggested_directory) {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
106 if (!base::CreateDirectory(suggested_directory)) {
107 LOG(ERROR) << "Failed to create directory: "
108 << suggested_directory.value();
112 base::FilePath suggested_path(suggested_directory.Append(generated_name));
113 BrowserThread::PostTask(
117 &RuntimeDownloadManagerDelegate::OnDownloadPathGenerated,
118 this, download_id, callback, suggested_path));
121 void RuntimeDownloadManagerDelegate::OnDownloadPathGenerated(
123 const content::DownloadTargetCallback& callback,
124 const base::FilePath& suggested_path) {
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
126 if (suppress_prompting_) {
128 callback.Run(suggested_path,
129 content::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
130 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
131 suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload")));
135 ChooseDownloadPath(download_id, callback, suggested_path);
138 void RuntimeDownloadManagerDelegate::ChooseDownloadPath(
140 const content::DownloadTargetCallback& callback,
141 const base::FilePath& suggested_path) {
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
143 content::DownloadItem* item = download_manager_->GetDownload(download_id);
144 if (!item || (item->GetState() != content::DownloadItem::IN_PROGRESS))
147 base::FilePath result;
148 #if defined(OS_WIN) && !defined(USE_AURA)
149 std::wstring file_part = base::FilePath(suggested_path).BaseName().value();
150 wchar_t file_name[MAX_PATH];
151 base::wcslcpy(file_name, file_part.c_str(), arraysize(file_name));
152 OPENFILENAME save_as;
153 ZeroMemory(&save_as, sizeof(save_as));
154 save_as.lStructSize = sizeof(OPENFILENAME);
155 save_as.hwndOwner = item->GetWebContents()->GetView()->GetNativeView();
156 save_as.lpstrFile = file_name;
157 save_as.nMaxFile = arraysize(file_name);
159 std::wstring directory;
160 if (!suggested_path.empty())
161 directory = suggested_path.DirName().value();
163 save_as.lpstrInitialDir = directory.c_str();
164 save_as.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
165 OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
167 if (GetSaveFileName(&save_as))
168 result = base::FilePath(std::wstring(save_as.lpstrFile));
173 callback.Run(result, content::DownloadItem::TARGET_DISPOSITION_PROMPT,
174 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, result);
177 void RuntimeDownloadManagerDelegate::SetDownloadBehaviorForTesting(
178 const base::FilePath& default_download_path) {
179 default_download_path_ = default_download_path;
180 suppress_prompting_ = true;