#include "components/download/public/common/download_utils.h"
#include <memory>
#include <string>
#include "base/files/file_util.h"
#include "base/format_macros.h"
#include "base/i18n/file_util_icu.h"
#include "base/metrics/field_trial_params.h"
#include "base/numerics/safe_conversions.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "components/download/public/common/download_create_info.h"
#include "components/download/public/common/download_features.h"
#include "components/download/public/common/download_interrupt_reasons_utils.h"
#include "components/download/public/common/download_save_info.h"
#include "components/download/public/common/download_stats.h"
#include "components/download/public/common/download_task_runner.h"
#include "components/download/public/common/download_url_parameters.h"
#include "net/base/isolation_info.h"
#include "net/base/load_flags.h"
#include "net/cookies/site_for_cookies.h"
#include "net/http/http_content_disposition.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_status_code.h"
#include "services/network/public/cpp/resource_request.h"
#include "url/origin.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/content_uri_utils.h"
#include "components/download/internal/common/android/download_collection_bridge.h"
#endif
namespace download {
namespace {
const int64_t kDefaultContentValidationLength = …;
const int64_t kInvalidFileWriteOffset = …;
const int kDefaultDownloadExpiredTimeInDays = …;
const int kDefaultOverwrittenDownloadExpiredTimeInDays = …;
const int kDefaultDownloadFileBufferSize = …;
#if BUILDFLAG(IS_ANDROID)
const int kDefaultMaxFileNameLengthOnAndroid = 127;
DownloadItem::DownloadRenameResult RenameDownloadedFileForContentUri(
const base::FilePath& from_path,
const base::FilePath& display_name) {
if (static_cast<int>(display_name.value().length()) >
kDefaultMaxFileNameLengthOnAndroid) {
return DownloadItem::DownloadRenameResult::FAILURE_NAME_TOO_LONG;
}
if (DownloadCollectionBridge::FileNameExists(display_name))
return DownloadItem::DownloadRenameResult::FAILURE_NAME_CONFLICT;
return DownloadCollectionBridge::RenameDownloadUri(from_path, display_name)
? DownloadItem::DownloadRenameResult::SUCCESS
: DownloadItem::DownloadRenameResult::FAILURE_NAME_INVALID;
}
#endif
void AppendExtraHeaders(net::HttpRequestHeaders* headers,
DownloadUrlParameters* params) { … }
bool IsArbitraryRangeRequest(DownloadSaveInfo* save_info) { … }
bool IsArbitraryRangeRequest(DownloadUrlParameters* parameters) { … }
void AppendRangeHeader(net::HttpRequestHeaders* headers,
DownloadUrlParameters* params) { … }
#if BUILDFLAG(IS_ANDROID)
struct CreateIntermediateUriResult {
public:
CreateIntermediateUriResult(const base::FilePath& content_uri,
const base::FilePath& file_name)
: content_uri(content_uri), file_name(file_name) {}
base::FilePath content_uri;
base::FilePath file_name;
};
CreateIntermediateUriResult CreateIntermediateUri(
const GURL& original_url,
const GURL& referrer_url,
const base::FilePath& current_path,
const base::FilePath& suggested_name,
const std::string& mime_type) {
base::FilePath content_path =
current_path.IsContentUri() && base::ContentUriExists(current_path)
? current_path
: DownloadCollectionBridge::CreateIntermediateUriForPublish(
original_url, referrer_url, suggested_name, mime_type);
base::FilePath file_name;
if (!content_path.empty()) {
file_name = DownloadCollectionBridge::GetDisplayName(content_path);
}
if (file_name.empty())
file_name = suggested_name;
return CreateIntermediateUriResult(content_path, file_name);
}
void OnInterMediateUriCreated(LocalPathCallback callback,
const CreateIntermediateUriResult& result) {
std::move(callback).Run(result.content_uri, result.file_name);
}
#endif
}
const uint32_t DownloadItem::kInvalidId = …;
DownloadInterruptReason HandleRequestCompletionStatus(
net::Error error_code,
bool has_strong_validators,
net::CertStatus cert_status,
bool is_partial_request,
DownloadInterruptReason abort_reason) { … }
DownloadInterruptReason HandleSuccessfulServerResponse(
const net::HttpResponseHeaders& http_headers,
DownloadSaveInfo* save_info,
bool fetch_error_body) { … }
void HandleResponseHeaders(const net::HttpResponseHeaders* headers,
DownloadCreateInfo* create_info) { … }
std::unique_ptr<network::ResourceRequest> CreateResourceRequest(
DownloadUrlParameters* params) { … }
int GetLoadFlags(DownloadUrlParameters* params, bool has_upload_data) { … }
std::unique_ptr<net::HttpRequestHeaders> GetAdditionalRequestHeaders(
DownloadUrlParameters* params) { … }
DownloadDBEntry CreateDownloadDBEntryFromItem(const DownloadItemImpl& item) { … }
std::unique_ptr<DownloadEntry> CreateDownloadEntryFromDownloadDBEntry(
std::optional<DownloadDBEntry> entry) { … }
uint64_t GetUniqueDownloadId() { … }
ResumeMode GetDownloadResumeMode(const GURL& url,
DownloadInterruptReason reason,
bool restart_required,
bool user_action_required) { … }
bool IsDownloadDone(const GURL& url,
DownloadItem::DownloadState state,
DownloadInterruptReason reason) { … }
bool DeleteDownloadedFile(const base::FilePath& path) { … }
DownloadItem::DownloadRenameResult RenameDownloadedFile(
const base::FilePath& from_path,
const base::FilePath& display_name) { … }
int64_t GetDownloadValidationLengthConfig() { … }
base::TimeDelta GetExpiredDownloadDeleteTime() { … }
base::TimeDelta GetOverwrittenDownloadDeleteTime() { … }
size_t GetDownloadFileBufferSize() { … }
void DetermineLocalPath(DownloadItem* download,
const base::FilePath& virtual_path,
LocalPathCallback callback) { … }
bool IsInterruptedDownloadAutoResumable(download::DownloadItem* download_item,
int auto_resumption_size_limit) { … }
bool IsContentDispositionAttachmentInHead(
const network::mojom::URLResponseHead& response_head) { … }
}