Commit 172d4d65 authored by Roger Tawa's avatar Roger Tawa Committed by Chromium LUCI CQ

Allow embedder to override download rename behaviour.

Change-Id: I46e011578002acef68e2c3e187883db8a2461aea
Bug: 1150852
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2535685
Commit-Queue: Roger Tawa <rogerta@chromium.org>
Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833389}
parent 0fce24dd
......@@ -31,6 +31,7 @@ source_set("internal") {
"download_interrupt_reasons_utils.cc",
"download_item_impl.cc",
"download_item_impl_delegate.cc",
"download_item_rename_handler.cc",
"download_job.cc",
"download_job_factory.cc",
"download_job_impl.cc",
......
......@@ -984,6 +984,10 @@ DownloadFile* DownloadItemImpl::GetDownloadFile() {
return download_file_.get();
}
DownloadItemRenameHandler* DownloadItemImpl::GetRenameHandler() {
return rename_handler_.get();
}
bool DownloadItemImpl::IsDangerous() const {
return danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE ||
danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_URL ||
......@@ -1932,11 +1936,20 @@ void DownloadItemImpl::OnDownloadCompleting() {
DCHECK(!IsDangerous());
DCHECK(download_file_);
// Unilaterally rename; even if it already has the right name,
// we need theannotation.
// we need the annotation.
DownloadFile::RenameCompletionCallback callback =
base::BindOnce(&DownloadItemImpl::OnDownloadRenamedToFinalName,
weak_ptr_factory_.GetWeakPtr());
// If an alternate rename handler is specified, use it instead.
rename_handler_ = delegate_->GetRenameHandlerForDownload(this);
if (rename_handler_) {
rename_handler_->Start(this, std::move(callback));
return;
}
#if defined(OS_ANDROID)
if (GetTargetFilePath().IsContentUri()) {
GetDownloadTaskRunner()->PostTask(
......@@ -1953,6 +1966,7 @@ void DownloadItemImpl::OnDownloadCompleting() {
auto quarantine_callback = delegate_->GetQuarantineConnectionCallback();
if (quarantine_callback)
quarantine_callback.Run(quarantine.InitWithNewPipeAndPassReceiver());
GetDownloadTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(&DownloadFile::RenameAndAnnotate,
......
......@@ -111,4 +111,10 @@ DownloadItemImplDelegate::GetQuarantineConnectionCallback() {
return base::NullCallback();
}
std::unique_ptr<DownloadItemRenameHandler>
DownloadItemImplDelegate::GetRenameHandlerForDownload(
DownloadItemImpl* download_item) {
return nullptr;
}
} // namespace download
// Copyright 2020 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 "components/download/public/common/download_item_rename_handler.h"
#include "components/download/public/common/download_interrupt_reasons.h"
#include "components/download/public/common/download_item.h"
namespace download {
DownloadItemRenameHandler::DownloadItemRenameHandler() = default;
DownloadItemRenameHandler::~DownloadItemRenameHandler() = default;
void DownloadItemRenameHandler::Start(DownloadItem* download_item,
Callback callback) {
std::move(callback).Run(DOWNLOAD_INTERRUPT_REASON_NONE,
download_item->GetTargetFilePath());
}
} // namespace download
......@@ -37,6 +37,7 @@ component("public") {
"download_item_factory.h",
"download_item_impl.h",
"download_item_impl_delegate.h",
"download_item_rename_handler.h",
"download_job.h",
"download_job_factory.h",
"download_path_reservation_tracker.h",
......
......@@ -51,6 +51,7 @@ class HttpResponseHeaders;
namespace download {
class DownloadFile;
class DownloadItemRenameHandler;
// One DownloadItem per download. This is the model class that stores all the
// state for a download.
......@@ -432,6 +433,12 @@ class COMPONENTS_DOWNLOAD_EXPORT DownloadItem : public base::SupportsUserData {
// Gets the pointer to the DownloadFile owned by this object.
virtual DownloadFile* GetDownloadFile() = 0;
// Gets a handler to perform the rename for a download item. If no special
// rename handling is required, this function returns null and the default
// rename handling is performed. The caller does not own the returned
// pointer.
virtual DownloadItemRenameHandler* GetRenameHandler() = 0;
// Progress State accessors -----------------------------------------------
// Simple calculation of the amount of time remaining to completion. Fills
......
......@@ -274,6 +274,7 @@ class COMPONENTS_DOWNLOAD_EXPORT DownloadItemImpl
bool GetFileExternallyRemoved() const override;
void DeleteFile(base::OnceCallback<void(bool)> callback) override;
DownloadFile* GetDownloadFile() override;
DownloadItemRenameHandler* GetRenameHandler() override;
bool IsDangerous() const override;
bool IsMixedContent() const override;
DownloadDangerType GetDangerType() const override;
......@@ -862,6 +863,9 @@ class COMPONENTS_DOWNLOAD_EXPORT DownloadItemImpl
// Defines when to start the download. Used by download later feature.
base::Optional<DownloadSchedule> download_schedule_;
// A handler for renaming and helping with display the item.
std::unique_ptr<DownloadItemRenameHandler> rename_handler_;
THREAD_CHECKER(thread_checker_);
base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_{this};
......
......@@ -7,12 +7,15 @@
#include <stdint.h>
#include <memory>
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/optional.h"
#include "components/download/public/common/download_export.h"
#include "components/download/public/common/download_item.h"
#include "components/download/public/common/download_item_rename_handler.h"
#include "components/download/public/common/download_schedule.h"
#include "components/download/public/common/download_url_parameters.h"
#include "components/download/public/common/quarantine_connection.h"
......@@ -124,6 +127,12 @@ class COMPONENTS_DOWNLOAD_EXPORT DownloadItemImplDelegate {
// Gets a callback that can connect to the Quarantine Service if available.
virtual QuarantineConnectionCallback GetQuarantineConnectionCallback();
// Gets a handler to perform the rename for a download item. If no special
// rename handling is required, this function returns null and the default
// rename handling is performed.
virtual std::unique_ptr<DownloadItemRenameHandler>
GetRenameHandlerForDownload(DownloadItemImpl* download_item);
private:
// For "Outlives attached DownloadItemImpl" invariant assertion.
int count_;
......
// Copyright 2020 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.
#ifndef COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_ITEM_RENAME_HANDLER_H_
#define COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_ITEM_RENAME_HANDLER_H_
#include "base/callback.h"
#include "components/download/public/common/download_interrupt_reasons.h"
namespace base {
class FilePath;
}
namespace download {
class DownloadItem;
// Interface implemented by an object that overrides the default method
// of renaming a file to its final name once the download completes.
// DownloadItemImpl attempts to retrieve the object from its delegate, and
// if valid will call the Start() method instead of using
// DownloadFile::RenameAndAnnotate().
class COMPONENTS_DOWNLOAD_EXPORT DownloadItemRenameHandler {
public:
using Callback = base::OnceCallback<void(DownloadInterruptReason reason,
const base::FilePath& path)>;
DownloadItemRenameHandler();
virtual ~DownloadItemRenameHandler();
// Starts the process of renaming the file and invokes |callback| when
// done.
virtual void Start(DownloadItem* download_item, Callback callback);
};
} // namespace download
#endif // COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_ITEM_RENAME_HANDLER_H_
......@@ -99,6 +99,7 @@ class MockDownloadItem : public DownloadItem {
}
MOCK_METHOD1(DeleteFile_, void(base::OnceCallback<void(bool)>& cb));
MOCK_METHOD0(GetDownloadFile, DownloadFile*());
MOCK_METHOD0(GetRenameHandler, DownloadItemRenameHandler*());
MOCK_CONST_METHOD0(IsDangerous, bool());
MOCK_CONST_METHOD0(IsMixedContent, bool());
MOCK_CONST_METHOD0(GetDangerType, DownloadDangerType());
......
......@@ -417,6 +417,10 @@ download::DownloadFile* FakeDownloadItem::GetDownloadFile() {
return nullptr;
}
download::DownloadItemRenameHandler* FakeDownloadItem::GetRenameHandler() {
return nullptr;
}
bool FakeDownloadItem::IsDangerous() const {
NOTREACHED();
return false;
......
......@@ -89,6 +89,7 @@ class FakeDownloadItem : public download::DownloadItem {
const std::string& GetHash() const override;
void DeleteFile(base::OnceCallback<void(bool)> callback) override;
download::DownloadFile* GetDownloadFile() override;
download::DownloadItemRenameHandler* GetRenameHandler() override;
bool IsDangerous() const override;
bool IsMixedContent() const override;
download::DownloadDangerType GetDangerType() const override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment