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"
|
2020-09-22 21:54:02 +02:00
|
|
|
#include "libcef/browser/alloy/alloy_browser_host_impl.h"
|
2019-05-07 18:22:16 +02:00
|
|
|
#include "libcef/browser/context.h"
|
2012-06-28 19:21:18 +02:00
|
|
|
#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"
|
2014-11-12 20:25:15 +01:00
|
|
|
#include "base/files/file_util.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#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"
|
2019-05-07 18:22:16 +02:00
|
|
|
#include "chrome/common/chrome_constants.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "content/public/browser/browser_context.h"
|
2018-03-20 21:15:08 +01:00
|
|
|
#include "content/public/browser/download_item_utils.h"
|
2012-04-04 20:18:09 +02:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2014-04-30 19:14:40 +02:00
|
|
|
#include "net/base/filename_util.h"
|
2018-10-02 14:14:11 +02:00
|
|
|
#include "third_party/blink/public/mojom/choosers/file_chooser.mojom.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
using content::DownloadManager;
|
|
|
|
using content::WebContents;
|
2019-01-17 10:56:52 +01:00
|
|
|
using download::DownloadItem;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-06-28 19:21:18 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Helper function to retrieve the CefDownloadHandler.
|
|
|
|
CefRefPtr<CefDownloadHandler> GetDownloadHandler(
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> browser) {
|
2012-06-28 19:21:18 +02:00
|
|
|
CefRefPtr<CefClient> client = browser->GetClient();
|
|
|
|
if (client.get())
|
|
|
|
return client->GetDownloadHandler();
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CefBeforeDownloadCallback implementation.
|
|
|
|
class CefBeforeDownloadCallbackImpl : public CefBeforeDownloadCallback {
|
|
|
|
public:
|
2017-05-17 11:29:28 +02:00
|
|
|
CefBeforeDownloadCallbackImpl(const base::WeakPtr<DownloadManager>& manager,
|
|
|
|
uint32 download_id,
|
|
|
|
const base::FilePath& suggested_name,
|
2020-03-04 01:29:39 +01:00
|
|
|
content::DownloadTargetCallback callback)
|
2012-11-30 20:08:20 +01:00
|
|
|
: manager_(manager),
|
|
|
|
download_id_(download_id),
|
|
|
|
suggested_name_(suggested_name),
|
2020-03-04 01:29:39 +01:00
|
|
|
callback_(std::move(callback)) {}
|
2012-06-28 19:21:18 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void Continue(const CefString& download_path, bool show_dialog) override {
|
2012-06-28 19:21:18 +02:00
|
|
|
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);
|
2020-03-04 01:29:39 +01:00
|
|
|
CEF_POST_USER_VISIBLE_TASK(
|
|
|
|
base::BindOnce(&CefBeforeDownloadCallbackImpl::GenerateFilename,
|
|
|
|
manager_, download_id_, suggested_name_, path,
|
|
|
|
show_dialog, std::move(callback_)));
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
download_id_ = 0;
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
2020-03-04 01:29:39 +01:00
|
|
|
base::BindOnce(&CefBeforeDownloadCallbackImpl::Continue,
|
|
|
|
this, download_path, show_dialog));
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-03-04 01:29:39 +01:00
|
|
|
static void GenerateFilename(base::WeakPtr<DownloadManager> manager,
|
|
|
|
uint32 download_id,
|
|
|
|
const base::FilePath& suggested_name,
|
|
|
|
const base::FilePath& download_path,
|
|
|
|
bool show_dialog,
|
|
|
|
content::DownloadTargetCallback callback) {
|
2018-03-20 21:15:08 +01:00
|
|
|
CEF_REQUIRE_BLOCKING();
|
|
|
|
|
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();
|
2013-07-24 22:15:18 +02:00
|
|
|
if (!base::DirectoryExists(dir_path) &&
|
2013-12-17 23:04:35 +01:00
|
|
|
!base::CreateDirectory(dir_path)) {
|
2012-06-28 19:21:18 +02:00
|
|
|
NOTREACHED() << "failed to create the download directory";
|
|
|
|
suggested_path.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (suggested_path.empty()) {
|
2018-05-14 13:24:05 +02:00
|
|
|
if (base::PathService::Get(base::DIR_TEMP, &suggested_path)) {
|
2012-06-28 19:21:18 +02:00
|
|
|
// Use the temp directory.
|
|
|
|
suggested_path = suggested_path.Append(suggested_name);
|
|
|
|
} else {
|
|
|
|
// Use the current working directory.
|
|
|
|
suggested_path = suggested_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_UIT,
|
2020-03-04 01:29:39 +01:00
|
|
|
base::BindOnce(&CefBeforeDownloadCallbackImpl::ChooseDownloadPath,
|
|
|
|
manager, download_id, suggested_path, show_dialog,
|
|
|
|
std::move(callback)));
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
static void ChooseDownloadPath(base::WeakPtr<DownloadManager> manager,
|
|
|
|
uint32 download_id,
|
|
|
|
const base::FilePath& suggested_path,
|
|
|
|
bool show_dialog,
|
|
|
|
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);
|
2018-03-20 21:15:08 +01:00
|
|
|
if (!item || item->GetState() != 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) {
|
2018-03-20 21:15:08 +01:00
|
|
|
WebContents* web_contents =
|
|
|
|
content::DownloadItemUtils::GetWebContents(item);
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> browser =
|
|
|
|
AlloyBrowserHostImpl::GetBrowserForContents(web_contents);
|
2012-10-16 21:28:07 +02:00
|
|
|
if (browser.get()) {
|
|
|
|
handled = true;
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
CefFileDialogRunner::FileChooserParams params;
|
2018-10-02 14:14:11 +02:00
|
|
|
params.mode = blink::mojom::FileChooserParams::Mode::kSave;
|
2012-10-16 21:28:07 +02:00
|
|
|
if (!suggested_path.empty()) {
|
|
|
|
params.default_file_name = suggested_path;
|
|
|
|
if (!suggested_path.Extension().empty()) {
|
|
|
|
params.accept_types.push_back(
|
|
|
|
CefString(suggested_path.Extension()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
browser->RunFileChooser(
|
|
|
|
params,
|
2020-03-04 01:29:39 +01:00
|
|
|
base::BindOnce(
|
2012-10-16 21:28:07 +02:00
|
|
|
&CefBeforeDownloadCallbackImpl::ChooseDownloadPathCallback,
|
2020-03-04 01:29:39 +01:00
|
|
|
std::move(callback)));
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled) {
|
2020-03-04 01:29:39 +01:00
|
|
|
std::move(callback).Run(
|
|
|
|
suggested_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
|
|
|
|
download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
|
|
|
|
download::DownloadItem::MixedContentStatus::UNKNOWN, suggested_path,
|
2020-07-08 19:23:29 +02:00
|
|
|
base::nullopt /*download_schedule*/,
|
2020-03-04 01:29:39 +01:00
|
|
|
download::DOWNLOAD_INTERRUPT_REASON_NONE);
|
2012-08-04 02:59:58 +02:00
|
|
|
}
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ChooseDownloadPathCallback(
|
2020-03-04 01:29:39 +01:00
|
|
|
content::DownloadTargetCallback callback,
|
2015-01-20 19:24:54 +01:00
|
|
|
int selected_accept_filter,
|
2013-02-23 01:43:28 +01:00
|
|
|
const std::vector<base::FilePath>& file_paths) {
|
2017-05-17 11:29:28 +02:00
|
|
|
DCHECK_LE(file_paths.size(), (size_t)1);
|
2012-10-16 21:28:07 +02:00
|
|
|
|
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.
|
2020-03-04 01:29:39 +01:00
|
|
|
std::move(callback).Run(path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
|
|
|
|
download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
|
|
|
|
download::DownloadItem::MixedContentStatus::UNKNOWN,
|
2020-07-08 19:23:29 +02:00
|
|
|
path, base::nullopt /*download_schedule*/,
|
|
|
|
download::DOWNLOAD_INTERRUPT_REASON_NONE);
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
2012-11-30 20:08:20 +01:00
|
|
|
base::WeakPtr<DownloadManager> manager_;
|
2013-07-24 22:15:18 +02:00
|
|
|
uint32 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,
|
2013-07-24 22:15:18 +02:00
|
|
|
uint32 download_id)
|
2017-05-17 11:29:28 +02:00
|
|
|
: manager_(manager), download_id_(download_id) {}
|
2012-06-28 19:21:18 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void Cancel() override {
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefDownloadItemCallbackImpl::DoCancel, this));
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
2015-01-12 20:47:40 +01:00
|
|
|
void Pause() override {
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefDownloadItemCallbackImpl::DoPause, this));
|
2015-01-12 20:47:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resume() override {
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefDownloadItemCallbackImpl::DoResume, this));
|
2015-01-12 20:47:40 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2012-06-28 19:21:18 +02:00
|
|
|
private:
|
|
|
|
void DoCancel() {
|
|
|
|
if (download_id_ <= 0)
|
|
|
|
return;
|
|
|
|
|
2012-11-30 20:08:20 +01:00
|
|
|
if (manager_) {
|
|
|
|
DownloadItem* item = manager_->GetDownload(download_id_);
|
2018-03-20 21:15:08 +01:00
|
|
|
if (item && item->GetState() == DownloadItem::IN_PROGRESS)
|
2012-06-28 19:21:18 +02:00
|
|
|
item->Cancel(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
download_id_ = 0;
|
|
|
|
}
|
|
|
|
|
2015-01-12 20:47:40 +01:00
|
|
|
void DoPause() {
|
|
|
|
if (download_id_ <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (manager_) {
|
|
|
|
DownloadItem* item = manager_->GetDownload(download_id_);
|
2018-03-20 21:15:08 +01:00
|
|
|
if (item && item->GetState() == DownloadItem::IN_PROGRESS)
|
2015-01-12 20:47:40 +01:00
|
|
|
item->Pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoResume() {
|
|
|
|
if (download_id_ <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (manager_) {
|
|
|
|
DownloadItem* item = manager_->GetDownload(download_id_);
|
|
|
|
if (item && item->CanResume())
|
2019-01-17 10:56:52 +01:00
|
|
|
item->Resume(true);
|
2015-01-12 20:47:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 20:08:20 +01:00
|
|
|
base::WeakPtr<DownloadManager> manager_;
|
2013-07-24 22:15:18 +02:00
|
|
|
uint32 download_id_;
|
2012-06-28 19:21:18 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefDownloadItemCallbackImpl);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefDownloadItemCallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefDownloadManagerDelegate::CefDownloadManagerDelegate(DownloadManager* manager)
|
|
|
|
: manager_(manager), manager_ptr_factory_(manager) {
|
2012-11-30 20:08:20 +01:00
|
|
|
DCHECK(manager);
|
|
|
|
manager->AddObserver(this);
|
|
|
|
|
|
|
|
DownloadManager::DownloadVector items;
|
|
|
|
manager->GetAllDownloads(&items);
|
|
|
|
DownloadManager::DownloadVector::const_iterator it = items.begin();
|
2016-02-12 23:31:22 +01:00
|
|
|
for (; it != items.end(); ++it)
|
|
|
|
OnDownloadCreated(manager, *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_) {
|
2020-01-15 14:36:24 +01:00
|
|
|
manager_->SetDelegate(nullptr);
|
2012-11-30 20:08:20 +01:00
|
|
|
manager_->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
2016-02-12 23:31:22 +01:00
|
|
|
while (!item_browser_map_.empty())
|
|
|
|
OnDownloadDestroyed(item_browser_map_.begin()->first);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefDownloadManagerDelegate::OnDownloadUpdated(DownloadItem* download) {
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> browser = GetBrowser(download);
|
2012-11-30 20:08:20 +01:00
|
|
|
CefRefPtr<CefDownloadHandler> handler;
|
|
|
|
if (browser.get())
|
|
|
|
handler = GetDownloadHandler(browser);
|
|
|
|
|
|
|
|
if (handler.get()) {
|
|
|
|
CefRefPtr<CefDownloadItemImpl> download_item(
|
|
|
|
new CefDownloadItemImpl(download));
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefDownloadItemCallback> callback(new CefDownloadItemCallbackImpl(
|
|
|
|
manager_ptr_factory_.GetWeakPtr(), download->GetId()));
|
2012-11-30 20:08:20 +01:00
|
|
|
|
|
|
|
handler->OnDownloadUpdated(browser.get(), download_item.get(), callback);
|
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
download_item->Detach(nullptr);
|
2012-11-30 20:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefDownloadManagerDelegate::OnDownloadDestroyed(DownloadItem* item) {
|
2016-02-12 23:31:22 +01:00
|
|
|
item->RemoveObserver(this);
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
AlloyBrowserHostImpl* browser = nullptr;
|
2016-02-12 23:31:22 +01:00
|
|
|
|
|
|
|
ItemBrowserMap::iterator it = item_browser_map_.find(item);
|
|
|
|
DCHECK(it != item_browser_map_.end());
|
|
|
|
if (it != item_browser_map_.end()) {
|
|
|
|
browser = it->second;
|
|
|
|
item_browser_map_.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (browser) {
|
|
|
|
// Determine if any remaining DownloadItems are associated with the same
|
|
|
|
// browser. If not, then unregister as an observer.
|
|
|
|
bool has_remaining = false;
|
|
|
|
ItemBrowserMap::const_iterator it2 = item_browser_map_.begin();
|
|
|
|
for (; it2 != item_browser_map_.end(); ++it2) {
|
|
|
|
if (it2->second == browser) {
|
|
|
|
has_remaining = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_remaining)
|
|
|
|
browser->RemoveObserver(this);
|
|
|
|
}
|
2012-11-30 20:08:20 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefDownloadManagerDelegate::OnDownloadCreated(DownloadManager* manager,
|
|
|
|
DownloadItem* item) {
|
2019-07-29 23:27:12 +02:00
|
|
|
// This callback may arrive after DetermineDownloadTarget, so we allow
|
|
|
|
// association from either method.
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> browser = GetOrAssociateBrowser(item);
|
2017-12-22 22:05:32 +01:00
|
|
|
if (!browser) {
|
|
|
|
// If the download is rejected (e.g. ALT+click on an invalid protocol link)
|
|
|
|
// then an "interrupted" download will be started via DownloadManagerImpl::
|
|
|
|
// StartDownloadWithId (originating from CreateInterruptedDownload) with no
|
|
|
|
// associated WebContents and consequently no associated CEF browser. In
|
|
|
|
// that case DetermineDownloadTarget will be called before this method.
|
|
|
|
// TODO(cef): Figure out how to expose this via a client callback.
|
|
|
|
const std::vector<GURL>& url_chain = item->GetUrlChain();
|
|
|
|
if (!url_chain.empty()) {
|
|
|
|
LOG(INFO) << "Rejected download of " << url_chain.back().spec();
|
|
|
|
}
|
|
|
|
item->Cancel(true);
|
|
|
|
}
|
2012-11-30 20:08:20 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefDownloadManagerDelegate::ManagerGoingDown(DownloadManager* manager) {
|
2012-11-30 20:08:20 +01:00
|
|
|
DCHECK_EQ(manager, manager_);
|
2020-01-15 14:36:24 +01:00
|
|
|
manager->SetDelegate(nullptr);
|
2012-11-30 20:08:20 +01:00
|
|
|
manager->RemoveObserver(this);
|
|
|
|
manager_ptr_factory_.InvalidateWeakPtrs();
|
2020-01-15 14:36:24 +01:00
|
|
|
manager_ = nullptr;
|
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,
|
2020-03-04 01:29:39 +01:00
|
|
|
content::DownloadTargetCallback* callback) {
|
2012-06-28 19:21:18 +02:00
|
|
|
if (!item->GetForcedFilePath().empty()) {
|
2020-03-04 01:29:39 +01:00
|
|
|
std::move(*callback).Run(
|
2017-05-17 11:29:28 +02:00
|
|
|
item->GetForcedFilePath(), DownloadItem::TARGET_DISPOSITION_OVERWRITE,
|
2020-03-04 01:29:39 +01:00
|
|
|
download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
|
|
|
|
download::DownloadItem::MixedContentStatus::UNKNOWN,
|
2020-07-08 19:23:29 +02:00
|
|
|
item->GetForcedFilePath(), base::nullopt /*download_schedule*/,
|
|
|
|
download::DOWNLOAD_INTERRUPT_REASON_NONE);
|
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
|
|
|
|
2019-07-29 23:27:12 +02:00
|
|
|
// This callback may arrive before OnDownloadCreated, so we allow association
|
|
|
|
// from either method.
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> browser = GetOrAssociateBrowser(item);
|
2012-06-28 19:21:18 +02:00
|
|
|
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(
|
2017-05-17 11:29:28 +02:00
|
|
|
item->GetURL(), item->GetContentDisposition(), std::string(),
|
|
|
|
item->GetSuggestedFilename(), item->GetMimeType(), "download");
|
2012-06-28 19:21:18 +02:00
|
|
|
|
|
|
|
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,
|
2020-03-04 01:29:39 +01:00
|
|
|
std::move(*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
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
download_item->Detach(nullptr);
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
return true;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2013-07-24 22:15:18 +02:00
|
|
|
|
|
|
|
void CefDownloadManagerDelegate::GetNextId(
|
2020-03-04 01:29:39 +01:00
|
|
|
content::DownloadIdCallback callback) {
|
2013-07-24 22:15:18 +02:00
|
|
|
static uint32 next_id = DownloadItem::kInvalidId + 1;
|
2020-03-04 01:29:39 +01:00
|
|
|
std::move(callback).Run(next_id++);
|
2013-07-24 22:15:18 +02:00
|
|
|
}
|
2016-02-12 23:31:22 +01:00
|
|
|
|
2019-09-04 17:13:32 +02:00
|
|
|
std::string CefDownloadManagerDelegate::ApplicationClientIdForFileScanning() {
|
2019-05-07 18:22:16 +02:00
|
|
|
const CefSettings& settings = CefContext::Get()->settings();
|
|
|
|
if (settings.application_client_id_for_file_scanning.length > 0) {
|
|
|
|
return CefString(&settings.application_client_id_for_file_scanning)
|
|
|
|
.ToString();
|
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2016-02-12 23:31:22 +01:00
|
|
|
void CefDownloadManagerDelegate::OnBrowserDestroyed(
|
2020-09-18 00:24:08 +02:00
|
|
|
CefBrowserHostBase* browser) {
|
2016-02-12 23:31:22 +01:00
|
|
|
ItemBrowserMap::iterator it = item_browser_map_.begin();
|
|
|
|
for (; it != item_browser_map_.end(); ++it) {
|
|
|
|
if (it->second == browser) {
|
|
|
|
// Don't call back into browsers that have been destroyed. We're not
|
|
|
|
// canceling the download so it will continue silently until it completes
|
|
|
|
// or until the associated browser context is destroyed.
|
|
|
|
it->second = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
AlloyBrowserHostImpl* CefDownloadManagerDelegate::GetOrAssociateBrowser(
|
2019-05-01 21:09:59 +02:00
|
|
|
download::DownloadItem* item) {
|
|
|
|
ItemBrowserMap::const_iterator it = item_browser_map_.find(item);
|
|
|
|
if (it != item_browser_map_.end())
|
|
|
|
return it->second;
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
AlloyBrowserHostImpl* browser = nullptr;
|
2019-05-01 21:09:59 +02:00
|
|
|
content::WebContents* contents =
|
|
|
|
content::DownloadItemUtils::GetWebContents(item);
|
|
|
|
if (contents) {
|
2020-09-22 21:54:02 +02:00
|
|
|
browser = AlloyBrowserHostImpl::GetBrowserForContents(contents).get();
|
2019-05-01 21:09:59 +02:00
|
|
|
DCHECK(browser);
|
|
|
|
}
|
|
|
|
if (!browser)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
item->AddObserver(this);
|
|
|
|
|
|
|
|
item_browser_map_.insert(std::make_pair(item, browser));
|
|
|
|
|
|
|
|
// Register as an observer so that we can cancel associated DownloadItems when
|
|
|
|
// the browser is destroyed.
|
|
|
|
if (!browser->HasObserver(this))
|
|
|
|
browser->AddObserver(this);
|
|
|
|
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
AlloyBrowserHostImpl* CefDownloadManagerDelegate::GetBrowser(
|
|
|
|
DownloadItem* item) {
|
2016-02-12 23:31:22 +01:00
|
|
|
ItemBrowserMap::const_iterator it = item_browser_map_.find(item);
|
|
|
|
if (it != item_browser_map_.end())
|
|
|
|
return it->second;
|
|
|
|
|
2017-12-22 22:05:32 +01:00
|
|
|
// If the download is rejected (e.g. ALT+click on an invalid protocol link)
|
|
|
|
// then an "interrupted" download will be started via DownloadManagerImpl::
|
|
|
|
// StartDownloadWithId (originating from CreateInterruptedDownload) with no
|
|
|
|
// associated WebContents and consequently no associated CEF browser. In that
|
|
|
|
// case DetermineDownloadTarget will be called before OnDownloadCreated.
|
2018-03-20 21:15:08 +01:00
|
|
|
DCHECK(!content::DownloadItemUtils::GetWebContents(item));
|
2016-02-12 23:31:22 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|