1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/devtools/protocol/devtools_download_manager_delegate.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/task/thread_pool.h"
#include "components/download/public/common/download_target_info.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/browser/download_manager.h"
#include "net/base/filename_util.h"
namespace content {
class WebContents;
namespace protocol {
const char kDevToolsDownloadManagerDelegateName[] =
"devtools_download_manager_delegate";
DevToolsDownloadManagerDelegate::DevToolsDownloadManagerDelegate(
content::BrowserContext* browser_context) {
download_manager_ = browser_context->GetDownloadManager();
DCHECK(download_manager_);
original_download_delegate_ = download_manager_->GetDelegate();
download_manager_->SetDelegate(this);
}
// static
DevToolsDownloadManagerDelegate*
DevToolsDownloadManagerDelegate::GetOrCreateInstance(BrowserContext* context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!DevToolsDownloadManagerDelegate::GetInstance(context)) {
auto delegate_owned =
base::WrapUnique(new DevToolsDownloadManagerDelegate(context));
context->SetUserData(kDevToolsDownloadManagerDelegateName,
std::move(delegate_owned));
}
return DevToolsDownloadManagerDelegate::GetInstance(context);
}
// static
DevToolsDownloadManagerDelegate* DevToolsDownloadManagerDelegate::GetInstance(
BrowserContext* context) {
return static_cast<DevToolsDownloadManagerDelegate*>(
context->GetUserData(kDevToolsDownloadManagerDelegateName));
}
void DevToolsDownloadManagerDelegate::Shutdown() {
if (original_download_delegate_)
original_download_delegate_.ExtractAsDangling()->Shutdown();
// Revoke any pending callbacks. download_manager_ et. al. are no longer safe
// to access after this point.
download_manager_ = nullptr;
}
bool DevToolsDownloadManagerDelegate::DetermineDownloadTarget(
download::DownloadItem* item,
download::DownloadTargetCallback* callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// Check if we should failback to delegate.
if (original_download_delegate_ &&
download_behavior_ == DownloadBehavior::DEFAULT) {
return original_download_delegate_->DetermineDownloadTarget(item, callback);
}
// In headless mode there's no no proxy delegate set, so if there's no
// information associated to the download, we deny it by default.
if (download_behavior_ != DownloadBehavior::ALLOW &&
download_behavior_ != DownloadBehavior::ALLOW_AND_NAME) {
download::DownloadTargetInfo target_info;
target_info.interrupt_reason =
download::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED;
std::move(*callback).Run(std::move(target_info));
return true;
}
base::FilePath download_path = base::FilePath::FromUTF8Unsafe(download_path_);
if (download_behavior_ == DownloadBehavior::ALLOW_AND_NAME) {
base::FilePath suggested_path(download_path.AppendASCII(item->GetGuid()));
OnDownloadPathGenerated(item->GetId(), std::move(*callback),
suggested_path);
return true;
}
FilenameDeterminedCallback filename_determined_callback = base::BindOnce(
&DevToolsDownloadManagerDelegate::OnDownloadPathGenerated,
base::Unretained(this), item->GetId(), std::move(*callback));
base::ThreadPool::PostTask(
FROM_HERE,
{base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
base::TaskPriority::USER_VISIBLE},
base::BindOnce(&DevToolsDownloadManagerDelegate::GenerateFilename,
item->GetURL(), item->GetContentDisposition(),
item->GetSuggestedFilename(), item->GetMimeType(),
download_path, std::move(filename_determined_callback)));
return true;
}
bool DevToolsDownloadManagerDelegate::ShouldOpenDownload(
download::DownloadItem* item,
content::DownloadOpenDelayedCallback callback) {
if (original_download_delegate_ &&
download_behavior_ == DownloadBehavior::DEFAULT) {
return original_download_delegate_->ShouldOpenDownload(item,
std::move(callback));
}
// Immediately transition to the completed stage.
return true;
}
void DevToolsDownloadManagerDelegate::GetNextId(
content::DownloadIdCallback callback) {
static uint32_t next_id = download::DownloadItem::kInvalidId + 1;
// Be sure to follow the proxy delegate Ids to avoid compatibility problems
// with the download manager.
if (original_download_delegate_) {
original_download_delegate_->GetNextId(std::move(callback));
return;
}
std::move(callback).Run(next_id++);
}
// static
void DevToolsDownloadManagerDelegate::GenerateFilename(
const GURL& url,
const std::string& content_disposition,
const std::string& suggested_filename,
const std::string& mime_type,
const base::FilePath& suggested_directory,
FilenameDeterminedCallback callback) {
base::FilePath generated_name =
net::GenerateFileName(url, content_disposition, std::string(),
suggested_filename, mime_type, "download");
if (!base::PathExists(suggested_directory))
base::CreateDirectory(suggested_directory);
base::FilePath suggested_path(suggested_directory.Append(generated_name));
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), suggested_path));
}
void DevToolsDownloadManagerDelegate::OnDownloadPathGenerated(
uint32_t download_id,
download::DownloadTargetCallback callback,
const base::FilePath& suggested_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
download::DownloadTargetInfo target_info;
target_info.target_path = suggested_path;
target_info.intermediate_path =
suggested_path.AddExtension(FILE_PATH_LITERAL(".crdownload"));
target_info.display_name = suggested_path.BaseName();
target_info.danger_type =
download::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT;
std::move(callback).Run(std::move(target_info));
}
download::DownloadItem* DevToolsDownloadManagerDelegate::GetDownloadByGuid(
const std::string& guid) {
if (!download_manager_) {
return nullptr;
}
return download_manager_->GetDownloadByGuid(guid);
}
} // namespace protocol
} // namespace content
|