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/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"
|
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(
|
|
|
|
CefRefPtr<CefBrowserHostImpl> browser) {
|
|
|
|
CefRefPtr<CefClient> client = browser->GetClient();
|
|
|
|
if (client.get())
|
|
|
|
return client->GetDownloadHandler();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
const content::DownloadTargetCallback& callback)
|
2012-11-30 20:08:20 +01:00
|
|
|
: manager_(manager),
|
|
|
|
download_id_(download_id),
|
|
|
|
suggested_name_(suggested_name),
|
2017-05-17 11:29:28 +02:00
|
|
|
callback_(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);
|
2018-03-20 21:15:08 +01:00
|
|
|
CEF_POST_USER_VISIBLE_TASK(base::Bind(
|
|
|
|
&CefBeforeDownloadCallbackImpl::GenerateFilename, 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,
|
2017-05-17 11:29:28 +02:00
|
|
|
base::Bind(&CefBeforeDownloadCallbackImpl::Continue, this,
|
|
|
|
download_path, show_dialog));
|
2012-06-28 19:21:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-08-04 02:59:58 +02:00
|
|
|
static void GenerateFilename(
|
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
|
|
|
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) {
|
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,
|
|
|
|
base::Bind(&CefBeforeDownloadCallbackImpl::ChooseDownloadPath, 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,
|
2013-07-24 22:15:18 +02:00
|
|
|
uint32 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);
|
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);
|
2012-10-16 21:28:07 +02:00
|
|
|
CefRefPtr<CefBrowserHostImpl> browser =
|
|
|
|
CefBrowserHostImpl::GetBrowserForContents(web_contents);
|
|
|
|
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,
|
2012-10-16 21:28:07 +02:00
|
|
|
base::Bind(
|
|
|
|
&CefBeforeDownloadCallbackImpl::ChooseDownloadPathCallback,
|
|
|
|
callback));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled) {
|
2017-05-17 11:29:28 +02:00
|
|
|
callback.Run(suggested_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
|
2018-03-20 21:15:08 +01:00
|
|
|
download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, suggested_path,
|
|
|
|
download::DOWNLOAD_INTERRUPT_REASON_NONE);
|
2012-08-04 02:59:58 +02:00
|
|
|
}
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ChooseDownloadPathCallback(
|
|
|
|
const 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.
|
2017-05-17 11:29:28 +02:00
|
|
|
callback.Run(path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
|
2018-03-20 21:15:08 +01:00
|
|
|
download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, path,
|
|
|
|
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_) {
|
|
|
|
manager_->SetDelegate(NULL);
|
|
|
|
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) {
|
2012-11-30 20:08:20 +01:00
|
|
|
CefRefPtr<CefBrowserHostImpl> browser = GetBrowser(download);
|
|
|
|
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);
|
|
|
|
|
|
|
|
download_item->Detach(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefDownloadManagerDelegate::OnDownloadDestroyed(DownloadItem* item) {
|
2016-02-12 23:31:22 +01:00
|
|
|
item->RemoveObserver(this);
|
|
|
|
|
|
|
|
CefBrowserHostImpl* browser = nullptr;
|
|
|
|
|
|
|
|
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) {
|
2016-02-12 23:31:22 +01:00
|
|
|
CefBrowserHostImpl* browser = nullptr;
|
2018-03-20 21:15:08 +01:00
|
|
|
content::WebContents* contents =
|
|
|
|
content::DownloadItemUtils::GetWebContents(item);
|
2017-12-22 22:05:32 +01:00
|
|
|
if (contents) {
|
2016-02-12 23:31:22 +01:00
|
|
|
browser = CefBrowserHostImpl::GetBrowserForContents(contents).get();
|
2017-12-22 22:05:32 +01:00
|
|
|
DCHECK(browser);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
item->AddObserver(this);
|
2016-02-12 23:31:22 +01:00
|
|
|
|
|
|
|
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);
|
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_);
|
|
|
|
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()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
callback.Run(
|
|
|
|
item->GetForcedFilePath(), DownloadItem::TARGET_DISPOSITION_OVERWRITE,
|
2018-03-20 21:15:08 +01:00
|
|
|
download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, item->GetForcedFilePath(),
|
|
|
|
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
|
|
|
|
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(
|
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,
|
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
|
|
|
}
|
2013-07-24 22:15:18 +02:00
|
|
|
|
|
|
|
void CefDownloadManagerDelegate::GetNextId(
|
|
|
|
const content::DownloadIdCallback& callback) {
|
|
|
|
static uint32 next_id = DownloadItem::kInvalidId + 1;
|
|
|
|
callback.Run(next_id++);
|
|
|
|
}
|
2016-02-12 23:31:22 +01:00
|
|
|
|
|
|
|
void CefDownloadManagerDelegate::OnBrowserDestroyed(
|
|
|
|
CefBrowserHostImpl* browser) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CefBrowserHostImpl* CefDownloadManagerDelegate::GetBrowser(DownloadItem* item) {
|
|
|
|
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;
|
|
|
|
}
|