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"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "base/strings/string_util.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "content/public/browser/browser_context.h"
|
2012-04-04 20:18:09 +02:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2012-10-16 21:28:07 +02:00
|
|
|
#include "content/public/common/file_chooser_params.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefBeforeDownloadCallback implementation.
|
|
|
|
class CefBeforeDownloadCallbackImpl : public CefBeforeDownloadCallback {
|
|
|
|
public:
|
2012-11-30 20:08:20 +01:00
|
|
|
CefBeforeDownloadCallbackImpl(
|
|
|
|
const base::WeakPtr<DownloadManager>& manager,
|
|
|
|
int32 download_id,
|
2013-02-23 01:43:28 +01:00
|
|
|
const base::FilePath& suggested_name,
|
2012-11-30 20:08:20 +01:00
|
|
|
const content::DownloadTargetCallback& callback)
|
|
|
|
: manager_(manager),
|
|
|
|
download_id_(download_id),
|
|
|
|
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;
|
|
|
|
|
2012-11-30 20:08:20 +01:00
|
|
|
if (manager_) {
|
2013-02-23 01:43:28 +01:00
|
|
|
base::FilePath path = base::FilePath(download_path);
|
2012-06-28 19:21:18 +02:00
|
|
|
CEF_POST_TASK(CEF_FILET,
|
|
|
|
base::Bind(&CefBeforeDownloadCallbackImpl::GenerateFilename,
|
2012-11-30 20:08:20 +01:00
|
|
|
manager_, 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(
|
2012-11-30 20:08:20 +01:00
|
|
|
base::WeakPtr<DownloadManager> manager,
|
2012-08-04 02:59:58 +02:00
|
|
|
int32 download_id,
|
2013-02-23 01:43:28 +01:00
|
|
|
const base::FilePath& suggested_name,
|
|
|
|
const base::FilePath& download_path,
|
2012-08-04 02:59:58 +02:00
|
|
|
bool show_dialog,
|
|
|
|
const content::DownloadTargetCallback& callback) {
|
2013-02-23 01:43:28 +01:00
|
|
|
base::FilePath suggested_path = download_path;
|
2012-06-28 19:21:18 +02:00
|
|
|
if (!suggested_path.empty()) {
|
|
|
|
// Create the directory if necessary.
|
2013-02-23 01:43:28 +01:00
|
|
|
base::FilePath dir_path = suggested_path.DirName();
|
2012-06-28 19:21:18 +02:00
|
|
|
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,
|
2012-11-30 20:08:20 +01:00
|
|
|
manager, 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(
|
2012-11-30 20:08:20 +01:00
|
|
|
base::WeakPtr<DownloadManager> manager,
|
2012-08-04 02:59:58 +02:00
|
|
|
int32 download_id,
|
2013-02-23 01:43:28 +01:00
|
|
|
const base::FilePath& suggested_path,
|
2012-08-04 02:59:58 +02:00
|
|
|
bool show_dialog,
|
|
|
|
const content::DownloadTargetCallback& callback) {
|
2012-06-28 19:21:18 +02:00
|
|
|
if (!manager)
|
|
|
|
return;
|
|
|
|
|
2012-10-04 21:17:13 +02:00
|
|
|
DownloadItem* item = manager->GetDownload(download_id);
|
2013-06-22 04:06:32 +02:00
|
|
|
if (!item || item->GetState() != content::DownloadItem::IN_PROGRESS)
|
2012-06-28 19:21:18 +02:00
|
|
|
return;
|
|
|
|
|
2012-10-16 21:28:07 +02:00
|
|
|
bool handled = false;
|
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
if (show_dialog) {
|
|
|
|
WebContents* web_contents = item->GetWebContents();
|
2012-10-16 21:28:07 +02:00
|
|
|
CefRefPtr<CefBrowserHostImpl> browser =
|
|
|
|
CefBrowserHostImpl::GetBrowserForContents(web_contents);
|
|
|
|
if (browser.get()) {
|
|
|
|
handled = true;
|
|
|
|
|
|
|
|
content::FileChooserParams params;
|
|
|
|
params.mode = content::FileChooserParams::Save;
|
|
|
|
if (!suggested_path.empty()) {
|
|
|
|
params.default_file_name = suggested_path;
|
|
|
|
if (!suggested_path.Extension().empty()) {
|
|
|
|
params.accept_types.push_back(
|
|
|
|
CefString(suggested_path.Extension()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
browser->RunFileChooser(params,
|
|
|
|
base::Bind(
|
|
|
|
&CefBeforeDownloadCallbackImpl::ChooseDownloadPathCallback,
|
|
|
|
callback));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled) {
|
|
|
|
callback.Run(suggested_path,
|
|
|
|
DownloadItem::TARGET_DISPOSITION_OVERWRITE,
|
|
|
|
content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
|
|
|
|
suggested_path);
|
2012-08-04 02:59:58 +02:00
|
|
|
}
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ChooseDownloadPathCallback(
|
|
|
|
const content::DownloadTargetCallback& callback,
|
2013-02-23 01:43:28 +01:00
|
|
|
const std::vector<base::FilePath>& file_paths) {
|
2012-10-16 21:28:07 +02:00
|
|
|
DCHECK_LE(file_paths.size(), (size_t) 1);
|
|
|
|
|
2013-02-23 01:43:28 +01:00
|
|
|
base::FilePath path;
|
2012-10-16 21:28:07 +02:00
|
|
|
if (file_paths.size() > 0)
|
|
|
|
path = file_paths.front();
|
2012-08-04 02:59:58 +02:00
|
|
|
|
2012-10-16 21:28:07 +02:00
|
|
|
// The download will be cancelled if |path| is empty.
|
|
|
|
callback.Run(path,
|
2012-08-04 02:59:58 +02:00
|
|
|
DownloadItem::TARGET_DISPOSITION_OVERWRITE,
|
|
|
|
content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
|
2012-10-16 21:28:07 +02:00
|
|
|
path);
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
2012-11-30 20:08:20 +01:00
|
|
|
base::WeakPtr<DownloadManager> manager_;
|
2012-06-28 19:21:18 +02:00
|
|
|
int32 download_id_;
|
2013-02-23 01:43:28 +01:00
|
|
|
base::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:
|
2012-11-30 20:08:20 +01:00
|
|
|
explicit CefDownloadItemCallbackImpl(
|
|
|
|
const base::WeakPtr<DownloadManager>& manager,
|
|
|
|
int32 download_id)
|
|
|
|
: manager_(manager),
|
|
|
|
download_id_(download_id) {
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Cancel() OVERRIDE {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefDownloadItemCallbackImpl::DoCancel, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void DoCancel() {
|
|
|
|
if (download_id_ <= 0)
|
|
|
|
return;
|
|
|
|
|
2012-11-30 20:08:20 +01:00
|
|
|
if (manager_) {
|
|
|
|
DownloadItem* item = manager_->GetDownload(download_id_);
|
2013-06-22 04:06:32 +02:00
|
|
|
if (item && item->GetState() == content::DownloadItem::IN_PROGRESS)
|
2012-06-28 19:21:18 +02:00
|
|
|
item->Cancel(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
download_id_ = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-30 20:08:20 +01:00
|
|
|
base::WeakPtr<DownloadManager> manager_;
|
2012-06-28 19:21:18 +02:00
|
|
|
int32 download_id_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefDownloadItemCallbackImpl);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefDownloadItemCallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2012-11-30 20:08:20 +01:00
|
|
|
CefDownloadManagerDelegate::CefDownloadManagerDelegate(
|
|
|
|
DownloadManager* manager)
|
|
|
|
: manager_(manager),
|
|
|
|
manager_ptr_factory_(manager) {
|
|
|
|
DCHECK(manager);
|
|
|
|
manager->AddObserver(this);
|
|
|
|
|
|
|
|
DownloadManager::DownloadVector items;
|
|
|
|
manager->GetAllDownloads(&items);
|
|
|
|
DownloadManager::DownloadVector::const_iterator it = items.begin();
|
|
|
|
for (; it != items.end(); ++it) {
|
|
|
|
(*it)->AddObserver(this);
|
|
|
|
observing_.insert(*it);
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2012-06-28 19:21:18 +02:00
|
|
|
CefDownloadManagerDelegate::~CefDownloadManagerDelegate() {
|
2012-11-30 20:08:20 +01:00
|
|
|
if (manager_) {
|
|
|
|
manager_->SetDelegate(NULL);
|
|
|
|
manager_->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<DownloadItem*>::const_iterator it = observing_.begin();
|
|
|
|
for (; it != observing_.end(); ++it)
|
|
|
|
(*it)->RemoveObserver(this);
|
|
|
|
observing_.clear();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2012-11-30 20:08:20 +01:00
|
|
|
void CefDownloadManagerDelegate::OnDownloadUpdated(
|
|
|
|
DownloadItem* download) {
|
|
|
|
CefRefPtr<CefBrowserHostImpl> browser = GetBrowser(download);
|
|
|
|
CefRefPtr<CefDownloadHandler> handler;
|
|
|
|
if (browser.get())
|
|
|
|
handler = GetDownloadHandler(browser);
|
|
|
|
|
|
|
|
if (handler.get()) {
|
|
|
|
CefRefPtr<CefDownloadItemImpl> download_item(
|
|
|
|
new CefDownloadItemImpl(download));
|
|
|
|
CefRefPtr<CefDownloadItemCallback> callback(
|
|
|
|
new CefDownloadItemCallbackImpl(manager_ptr_factory_.GetWeakPtr(),
|
|
|
|
download->GetId()));
|
|
|
|
|
|
|
|
handler->OnDownloadUpdated(browser.get(), download_item.get(), callback);
|
|
|
|
|
|
|
|
download_item->Detach(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDownloadManagerDelegate::OnDownloadDestroyed(
|
|
|
|
DownloadItem* download) {
|
|
|
|
download->RemoveObserver(this);
|
|
|
|
observing_.erase(download);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDownloadManagerDelegate::OnDownloadCreated(
|
|
|
|
DownloadManager* manager,
|
|
|
|
DownloadItem* item) {
|
|
|
|
item->AddObserver(this);
|
|
|
|
observing_.insert(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDownloadManagerDelegate::ManagerGoingDown(
|
|
|
|
DownloadManager* manager) {
|
|
|
|
DCHECK_EQ(manager, manager_);
|
|
|
|
manager->SetDelegate(NULL);
|
|
|
|
manager->RemoveObserver(this);
|
|
|
|
manager_ptr_factory_.InvalidateWeakPtrs();
|
|
|
|
manager_ = NULL;
|
2012-08-04 02:59:58 +02:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
bool CefDownloadManagerDelegate::DetermineDownloadTarget(
|
2012-11-30 20:08:20 +01:00
|
|
|
DownloadItem* item,
|
2012-08-04 02:59:58 +02:00
|
|
|
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()) {
|
2013-02-23 01:43:28 +01:00
|
|
|
base::FilePath suggested_name = net::GenerateFileName(
|
2012-06-28 19:21:18 +02:00
|
|
|
item->GetURL(),
|
|
|
|
item->GetContentDisposition(),
|
2012-11-05 21:18:20 +01:00
|
|
|
EmptyString(),
|
2012-06-28 19:21:18 +02:00
|
|
|
item->GetSuggestedFilename(),
|
|
|
|
item->GetMimeType(),
|
|
|
|
"download");
|
|
|
|
|
|
|
|
CefRefPtr<CefDownloadItemImpl> download_item(new CefDownloadItemImpl(item));
|
2012-08-04 02:59:58 +02:00
|
|
|
CefRefPtr<CefBeforeDownloadCallback> callbackObj(
|
2012-11-30 20:08:20 +01:00
|
|
|
new CefBeforeDownloadCallbackImpl(manager_ptr_factory_.GetWeakPtr(),
|
|
|
|
item->GetId(), suggested_name,
|
2012-08-04 02:59:58 +02:00
|
|
|
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
|
|
|
}
|