#include "content/browser/download/save_file.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "components/download/public/common/download_item.h"
#include "components/download/public/common/download_task_runner.h"
#if BUILDFLAG(IS_OHOS)
#include "base/path_service.h"
#include "chrome/common/chrome_paths.h"
#endif
namespace content {
SaveFile::SaveFile(std::unique_ptr<SaveFileCreateInfo> info,
bool calculate_hash)
: file_(download::DownloadItem::kInvalidId), info_(std::move(info)) {
DCHECK(download::GetDownloadTaskRunner()->RunsTasksInCurrentSequence());
DCHECK(info_);
DCHECK(info_->path.empty());
}
SaveFile::~SaveFile() {
DCHECK(download::GetDownloadTaskRunner()->RunsTasksInCurrentSequence());
}
download::DownloadInterruptReason SaveFile::Initialize() {
int64_t bytes_wasted = 0;
#if BUILDFLAG(IS_OHOS)
base::FilePath path;
base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path);
download::DownloadInterruptReason reason = file_.Initialize(
base::FilePath(), path, base::File(), 0, std::string(),
nullptr, false, &bytes_wasted);
#else
download::DownloadInterruptReason reason = file_.Initialize(
base::FilePath(), base::FilePath(),
base::File(), 0, std::string(),
nullptr, false,
&bytes_wasted);
#endif
info_->path = FullPath();
return reason;
}
download::DownloadInterruptReason SaveFile::AppendDataToFile(
base::span<const uint8_t> data) {
return file_.AppendDataToFile(data);
}
download::DownloadInterruptReason SaveFile::Rename(
const base::FilePath& full_path) {
return file_.Rename(full_path);
}
void SaveFile::Detach() {
file_.Detach();
}
void SaveFile::Cancel() {
file_.Cancel();
}
void SaveFile::Finish() {
file_.Finish();
}
void SaveFile::AnnotateWithSourceInformation(
const std::string& client_guid,
const GURL& source_url,
const GURL& referrer_url,
mojo::PendingRemote<quarantine::mojom::Quarantine> remote_quarantine,
download::BaseFile::OnAnnotationDoneCallback on_annotation_done_callback) {
file_.AnnotateWithSourceInformation(
client_guid, source_url, referrer_url, std::nullopt,
std::move(remote_quarantine), std::move(on_annotation_done_callback));
}
base::FilePath SaveFile::FullPath() const {
return file_.full_path();
}
bool SaveFile::InProgress() const {
return file_.in_progress();
}
int64_t SaveFile::BytesSoFar() const {
return file_.bytes_so_far();
}
std::string SaveFile::DebugString() const {
return file_.DebugString();
}
void SaveFile::RunQuarantineCallback() {
if (!info_->quarantine_callback.is_null()) {
std::move(info_->quarantine_callback).Run();
}
}
}