2012-04-03 03:34:16 +02:00
|
|
|
// 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 "libcef/browser/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"
|
2012-04-04 20:18:09 +02:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "net/base/net_util.h"
|
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
using content::DownloadItem;
|
|
|
|
using content::DownloadManager;
|
|
|
|
using content::WebContents;
|
|
|
|
|
|
|
|
CefDownloadManagerDelegate::CefDownloadManagerDelegate()
|
|
|
|
: download_manager_(NULL) {
|
|
|
|
}
|
|
|
|
|
|
|
|
CefDownloadManagerDelegate::~CefDownloadManagerDelegate() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDownloadManagerDelegate::SetDownloadManager(
|
|
|
|
DownloadManager* download_manager) {
|
|
|
|
download_manager_ = download_manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDownloadManagerDelegate::ShouldStartDownload(int32 download_id) {
|
|
|
|
DownloadItem* download =
|
|
|
|
download_manager_->GetActiveDownloadItem(download_id);
|
|
|
|
|
2012-06-08 18:01:01 +02:00
|
|
|
if (!download->GetForcedFilePath().empty()) {
|
|
|
|
download->OnTargetPathDetermined(
|
|
|
|
download->GetForcedFilePath(),
|
|
|
|
DownloadItem::TARGET_DISPOSITION_OVERWRITE,
|
|
|
|
content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
|
2012-04-03 03:34:16 +02:00
|
|
|
return true;
|
2012-06-08 18:01:01 +02:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
FilePath generated_name = net::GenerateFileName(
|
|
|
|
download->GetURL(),
|
|
|
|
download->GetContentDisposition(),
|
|
|
|
download->GetReferrerCharset(),
|
|
|
|
download->GetSuggestedFilename(),
|
|
|
|
download->GetMimeType(),
|
|
|
|
"download");
|
|
|
|
|
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::FILE,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(
|
|
|
|
&CefDownloadManagerDelegate::GenerateFilename,
|
2012-06-08 18:01:01 +02:00
|
|
|
this, download_id, generated_name));
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDownloadManagerDelegate::ChooseDownloadPath(
|
2012-06-22 00:50:34 +02:00
|
|
|
content::DownloadItem* item) {
|
2012-04-03 03:34:16 +02:00
|
|
|
FilePath result;
|
2012-06-11 19:35:23 +02:00
|
|
|
#if defined(OS_WIN) || defined(OS_MACOSX)
|
2012-06-22 00:50:34 +02:00
|
|
|
WebContents* web_contents = item->GetWebContents();
|
|
|
|
const FilePath suggested_path(item->GetTargetFilePath());
|
2012-06-11 19:35:23 +02:00
|
|
|
result = PlatformChooseDownloadPath(web_contents, suggested_path);
|
2012-04-03 03:34:16 +02:00
|
|
|
#else
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (result.empty()) {
|
2012-06-22 00:50:34 +02:00
|
|
|
download_manager_->FileSelectionCanceled(item->GetId());
|
2012-04-03 03:34:16 +02:00
|
|
|
} else {
|
2012-06-22 00:50:34 +02:00
|
|
|
download_manager_->FileSelected(result, item->GetId());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-11 19:35:23 +02:00
|
|
|
void CefDownloadManagerDelegate::AddItemToPersistentStore(
|
|
|
|
content::DownloadItem* item) {
|
|
|
|
static int next_id;
|
|
|
|
download_manager_->OnItemAddedToPersistentStore(item->GetId(), ++next_id);
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void CefDownloadManagerDelegate::GenerateFilename(
|
|
|
|
int32 download_id,
|
|
|
|
const FilePath& generated_name) {
|
2012-06-08 18:01:01 +02:00
|
|
|
FilePath suggested_path = download_manager_->GetBrowserContext()->GetPath().
|
|
|
|
Append(FILE_PATH_LITERAL("Downloads"));
|
|
|
|
if (!file_util::PathExists(suggested_path))
|
|
|
|
file_util::CreateDirectory(suggested_path);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-06-08 18:01:01 +02:00
|
|
|
suggested_path = suggested_path.Append(generated_name);
|
2012-04-03 03:34:16 +02:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(
|
|
|
|
&CefDownloadManagerDelegate::RestartDownload,
|
2012-06-08 18:01:01 +02:00
|
|
|
this, download_id, suggested_path));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDownloadManagerDelegate::RestartDownload(
|
|
|
|
int32 download_id,
|
2012-06-08 18:01:01 +02:00
|
|
|
const FilePath& suggested_path) {
|
2012-04-03 03:34:16 +02:00
|
|
|
DownloadItem* download =
|
|
|
|
download_manager_->GetActiveDownloadItem(download_id);
|
|
|
|
if (!download)
|
|
|
|
return;
|
2012-06-08 18:01:01 +02:00
|
|
|
|
|
|
|
// Since we have no download UI, show the user a dialog always.
|
|
|
|
download->OnTargetPathDetermined(suggested_path,
|
|
|
|
DownloadItem::TARGET_DISPOSITION_PROMPT,
|
|
|
|
content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
|
2012-04-03 03:34:16 +02:00
|
|
|
download_manager_->RestartDownload(download_id);
|
|
|
|
}
|