chromium/components/download/internal/common/download_utils.cc

// Copyright 2018 The Chromium Authors
// 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_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  // BUILDFLAG(IS_ANDROID)

namespace download {

namespace {
// Default value for |kDownloadContentValidationLengthFinchKey|, when no
// parameter is specified.
const int64_t kDefaultContentValidationLength =;

// If the file_offset value from SaveInfo is equal to this, no content
// validation will be performed and download stream will be written to
// file starting at the offset from the response.
const int64_t kInvalidFileWriteOffset =;

// Default expiration time of download in days. Canceled and interrupted
// downloads will be deleted after expiration.
const int kDefaultDownloadExpiredTimeInDays =;

// Default time for an overwritten download to be removed from the history.
const int kDefaultOverwrittenDownloadExpiredTimeInDays =;

// Default buffer size in bytes to write to the download file.
const int kDefaultDownloadFileBufferSize =;

#if BUILDFLAG(IS_ANDROID)
// Default maximum length of a downloaded file name on 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  // BUILDFLAG(IS_ANDROID)

void AppendExtraHeaders(net::HttpRequestHeaders* headers,
                        DownloadUrlParameters* params) {}

// Return whether the download is explicitly to fetch part of the file.
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  // BUILDFLAG(IS_ANDROID)

}  // namespace

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) {}

}  // namespace download