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"
|
|
|
|
|
2012-06-28 19:21:18 +02:00
|
|
|
#include "include/cef_download_handler.h"
|
|
|
|
#include "libcef/browser/browser_context.h"
|
|
|
|
#include "libcef/browser/browser_host_impl.h"
|
|
|
|
#include "libcef/browser/context.h"
|
|
|
|
#include "libcef/browser/download_item_impl.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/file_util.h"
|
|
|
|
#include "base/logging.h"
|
2012-06-28 19:21:18 +02:00
|
|
|
#include "base/path_service.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "base/string_util.h"
|
|
|
|
#include "base/utf_string_conversions.h"
|
|
|
|
#include "content/public/browser/browser_context.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::DownloadItem;
|
|
|
|
using content::DownloadManager;
|
|
|
|
using content::WebContents;
|
|
|
|
|
2012-06-28 19:21:18 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Helper function to retrieve the CefBrowserHostImpl.
|
|
|
|
CefRefPtr<CefBrowserHostImpl> GetBrowser(DownloadItem* item) {
|
|
|
|
content::WebContents* contents = item->GetWebContents();
|
|
|
|
if (!contents)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return CefBrowserHostImpl::GetBrowserForContents(contents).get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2012-06-28 19:21:18 +02:00
|
|
|
// Helper function to retrieve the CefDownloadHandler.
|
|
|
|
CefRefPtr<CefDownloadHandler> GetDownloadHandler(
|
|
|
|
CefRefPtr<CefBrowserHostImpl> browser) {
|
|
|
|
CefRefPtr<CefClient> client = browser->GetClient();
|
|
|
|
if (client.get())
|
|
|
|
return client->GetDownloadHandler();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to retrieve the DownloadManager.
|
|
|
|
scoped_refptr<content::DownloadManager> GetDownloadManager() {
|
|
|
|
return content::BrowserContext::GetDownloadManager(
|
|
|
|
_Context->browser_context());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefBeforeDownloadCallback implementation.
|
|
|
|
class CefBeforeDownloadCallbackImpl : public CefBeforeDownloadCallback {
|
|
|
|
public:
|
|
|
|
CefBeforeDownloadCallbackImpl(int32 download_id,
|
2012-08-04 02:59:58 +02:00
|
|
|
const FilePath& suggested_name,
|
|
|
|
const content::DownloadTargetCallback& callback)
|
2012-06-28 19:21:18 +02:00
|
|
|
: download_id_(download_id),
|
2012-08-04 02:59:58 +02:00
|
|
|
suggested_name_(suggested_name),
|
|
|
|
callback_(callback) {
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Continue(const CefString& download_path,
|
|
|
|
bool show_dialog) OVERRIDE {
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
if (download_id_ <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
scoped_refptr<content::DownloadManager> manager = GetDownloadManager();
|
|
|
|
if (manager) {
|
|
|
|
FilePath path = FilePath(download_path);
|
|
|
|
CEF_POST_TASK(CEF_FILET,
|
|
|
|
base::Bind(&CefBeforeDownloadCallbackImpl::GenerateFilename,
|
2012-08-04 02:59:58 +02:00
|
|
|
download_id_, suggested_name_, path, show_dialog,
|
|
|
|
callback_));
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
download_id_ = 0;
|
2012-08-04 02:59:58 +02:00
|
|
|
callback_.Reset();
|
2012-06-28 19:21:18 +02:00
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefBeforeDownloadCallbackImpl::Continue, this,
|
|
|
|
download_path, show_dialog));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-08-04 02:59:58 +02:00
|
|
|
static void GenerateFilename(
|
|
|
|
int32 download_id,
|
|
|
|
const FilePath& suggested_name,
|
|
|
|
const FilePath& download_path,
|
|
|
|
bool show_dialog,
|
|
|
|
const content::DownloadTargetCallback& callback) {
|
2012-06-28 19:21:18 +02:00
|
|
|
FilePath suggested_path = download_path;
|
|
|
|
if (!suggested_path.empty()) {
|
|
|
|
// Create the directory if necessary.
|
|
|
|
FilePath dir_path = suggested_path.DirName();
|
|
|
|
if (!file_util::DirectoryExists(dir_path) &&
|
|
|
|
!file_util::CreateDirectory(dir_path)) {
|
|
|
|
NOTREACHED() << "failed to create the download directory";
|
|
|
|
suggested_path.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (suggested_path.empty()) {
|
|
|
|
if (PathService::Get(base::DIR_TEMP, &suggested_path)) {
|
|
|
|
// Use the temp directory.
|
|
|
|
suggested_path = suggested_path.Append(suggested_name);
|
|
|
|
} else {
|
|
|
|
// Use the current working directory.
|
|
|
|
suggested_path = suggested_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
2012-08-04 02:59:58 +02:00
|
|
|
base::Bind(&CefBeforeDownloadCallbackImpl::ChooseDownloadPath,
|
|
|
|
download_id, suggested_path, show_dialog, callback));
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
static void ChooseDownloadPath(
|
|
|
|
int32 download_id,
|
|
|
|
const FilePath& suggested_path,
|
|
|
|
bool show_dialog,
|
|
|
|
const content::DownloadTargetCallback& callback) {
|
2012-06-28 19:21:18 +02:00
|
|
|
scoped_refptr<content::DownloadManager> manager = GetDownloadManager();
|
|
|
|
if (!manager)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DownloadItem* item = manager->GetActiveDownloadItem(download_id);
|
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
FilePath result;
|
|
|
|
if (show_dialog) {
|
2012-08-29 00:26:35 +02:00
|
|
|
#if defined(OS_WIN) || defined(OS_MACOSX) || defined(TOOLKIT_GTK)
|
2012-08-04 02:59:58 +02:00
|
|
|
WebContents* web_contents = item->GetWebContents();
|
|
|
|
result = CefDownloadManagerDelegate::PlatformChooseDownloadPath(
|
|
|
|
web_contents, suggested_path);
|
|
|
|
#else
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
result = suggested_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.Run(result,
|
|
|
|
DownloadItem::TARGET_DISPOSITION_OVERWRITE,
|
|
|
|
content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
|
|
|
|
result);
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int32 download_id_;
|
|
|
|
FilePath suggested_name_;
|
2012-08-04 02:59:58 +02:00
|
|
|
content::DownloadTargetCallback callback_;
|
2012-06-28 19:21:18 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefBeforeDownloadCallbackImpl);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefBeforeDownloadCallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// CefDownloadItemCallback implementation.
|
|
|
|
class CefDownloadItemCallbackImpl : public CefDownloadItemCallback {
|
|
|
|
public:
|
|
|
|
explicit CefDownloadItemCallbackImpl(int32 download_id)
|
|
|
|
: download_id_(download_id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Cancel() OVERRIDE {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefDownloadItemCallbackImpl::DoCancel, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void DoCancel() {
|
|
|
|
if (download_id_ <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
scoped_refptr<content::DownloadManager> manager = GetDownloadManager();
|
|
|
|
if (manager) {
|
|
|
|
content::DownloadItem* item =
|
|
|
|
manager->GetActiveDownloadItem(download_id_);
|
|
|
|
if (item && item->IsInProgress())
|
|
|
|
item->Cancel(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
download_id_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 download_id_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefDownloadItemCallbackImpl);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefDownloadItemCallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
CefDownloadManagerDelegate::CefDownloadManagerDelegate() {
|
2012-08-04 02:59:58 +02:00
|
|
|
// Balanced in Shutdown();
|
|
|
|
AddRef();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2012-06-28 19:21:18 +02:00
|
|
|
CefDownloadManagerDelegate::~CefDownloadManagerDelegate() {
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
void CefDownloadManagerDelegate::Shutdown() {
|
|
|
|
Release();
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
bool CefDownloadManagerDelegate::DetermineDownloadTarget(
|
|
|
|
content::DownloadItem* item,
|
|
|
|
const content::DownloadTargetCallback& callback) {
|
2012-06-28 19:21:18 +02:00
|
|
|
if (!item->GetForcedFilePath().empty()) {
|
2012-08-04 02:59:58 +02:00
|
|
|
callback.Run(item->GetForcedFilePath(),
|
|
|
|
DownloadItem::TARGET_DISPOSITION_OVERWRITE,
|
|
|
|
content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
|
|
|
|
item->GetForcedFilePath());
|
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
|
|
|
|
2012-06-28 19:21:18 +02:00
|
|
|
CefRefPtr<CefBrowserHostImpl> browser = GetBrowser(item);
|
|
|
|
CefRefPtr<CefDownloadHandler> handler;
|
|
|
|
if (browser.get())
|
|
|
|
handler = GetDownloadHandler(browser);
|
|
|
|
|
|
|
|
if (handler.get()) {
|
|
|
|
FilePath suggested_name = net::GenerateFileName(
|
|
|
|
item->GetURL(),
|
|
|
|
item->GetContentDisposition(),
|
|
|
|
item->GetReferrerCharset(),
|
|
|
|
item->GetSuggestedFilename(),
|
|
|
|
item->GetMimeType(),
|
|
|
|
"download");
|
|
|
|
|
|
|
|
CefRefPtr<CefDownloadItemImpl> download_item(new CefDownloadItemImpl(item));
|
2012-08-04 02:59:58 +02:00
|
|
|
CefRefPtr<CefBeforeDownloadCallback> callbackObj(
|
|
|
|
new CefBeforeDownloadCallbackImpl(item->GetId(), suggested_name,
|
|
|
|
callback));
|
2012-06-28 19:21:18 +02:00
|
|
|
|
|
|
|
handler->OnBeforeDownload(browser.get(), download_item.get(),
|
2012-08-04 02:59:58 +02:00
|
|
|
suggested_name.value(), callbackObj);
|
2012-06-28 19:21:18 +02:00
|
|
|
|
|
|
|
download_item->Detach(NULL);
|
|
|
|
}
|
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
return true;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2012-06-11 19:35:23 +02:00
|
|
|
void CefDownloadManagerDelegate::AddItemToPersistentStore(
|
2012-06-28 19:21:18 +02:00
|
|
|
DownloadItem* item) {
|
2012-06-11 19:35:23 +02:00
|
|
|
static int next_id;
|
2012-06-28 19:21:18 +02:00
|
|
|
scoped_refptr<content::DownloadManager> manager = GetDownloadManager();
|
|
|
|
manager->OnItemAddedToPersistentStore(item->GetId(), ++next_id);
|
2012-06-11 19:35:23 +02:00
|
|
|
}
|
|
|
|
|
2012-06-28 19:21:18 +02:00
|
|
|
void CefDownloadManagerDelegate::UpdateItemInPersistentStore(
|
|
|
|
DownloadItem* item) {
|
|
|
|
CefRefPtr<CefBrowserHostImpl> browser = GetBrowser(item);
|
|
|
|
CefRefPtr<CefDownloadHandler> handler;
|
|
|
|
if (browser.get())
|
|
|
|
handler = GetDownloadHandler(browser);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-06-28 19:21:18 +02:00
|
|
|
if (handler.get()) {
|
|
|
|
CefRefPtr<CefDownloadItemImpl> download_item(new CefDownloadItemImpl(item));
|
|
|
|
CefRefPtr<CefDownloadItemCallback> callback(
|
|
|
|
new CefDownloadItemCallbackImpl(item->GetId()));
|
|
|
|
|
|
|
|
handler->OnDownloadUpdated(browser.get(), download_item.get(), callback);
|
|
|
|
|
|
|
|
download_item->Detach(NULL);
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|