#include "net/disk_cache/cache_util.h"
#include <limits>
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/safe_base_name.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/metrics/field_trial_params.h"
#include "base/numerics/clamped_math.h"
#include "base/numerics/ostream_operators.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/bind_post_task.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "net/base/cache_type.h"
namespace {
const int kMaxOldFolders = 100;
base::FilePath GetPrefixedName(const base::FilePath& path,
const base::SafeBaseName& basename,
int index) {
const std::string index_str = base::StringPrintf("_%03d", index);
const base::FilePath::StringType filename = base::StrCat({
FILE_PATH_LITERAL("old_"), basename.path().value(),
#if BUILDFLAG(IS_WIN)
base::ASCIIToWide(index_str)
#else
index_str
#endif
});
return path.Append(filename);
}
base::FilePath GetTempCacheName(const base::FilePath& dirname,
const base::SafeBaseName& basename) {
for (int i = 0; i < kMaxOldFolders; i++) {
base::FilePath to_delete = GetPrefixedName(dirname, basename, i);
if (!base::PathExists(to_delete))
return to_delete;
}
return base::FilePath();
}
void CleanupTemporaryDirectories(const base::FilePath& path) {
const base::FilePath dirname = path.DirName();
const std::optional<base::SafeBaseName> basename =
base::SafeBaseName::Create(path);
if (!basename.has_value()) {
return;
}
for (int i = 0; i < kMaxOldFolders; i++) {
base::FilePath to_delete = GetPrefixedName(dirname, *basename, i);
disk_cache::DeleteCache(to_delete, true);
}
}
bool MoveDirectoryToTemporaryDirectory(const base::FilePath& path) {
const base::FilePath dirname = path.DirName();
const std::optional<base::SafeBaseName> basename =
base::SafeBaseName::Create(path);
if (!basename.has_value()) {
return false;
}
const base::FilePath destination = GetTempCacheName(dirname, *basename);
if (destination.empty()) {
return false;
}
return disk_cache::MoveCache(path, destination);
}
bool CleanupDirectoryInternal(const base::FilePath& path) {
const base::FilePath path_to_pass = path.StripTrailingSeparators();
bool result = MoveDirectoryToTemporaryDirectory(path_to_pass);
base::ThreadPool::PostTask(
FROM_HERE,
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&CleanupTemporaryDirectories, path_to_pass));
return result;
}
int64_t PreferredCacheSizeInternal(int64_t available) {
using disk_cache::kDefaultCacheSize;
if (available < kDefaultCacheSize * 10 / 8)
return available * 8 / 10;
if (available < kDefaultCacheSize * 10)
return kDefaultCacheSize;
if (available < static_cast<int64_t>(kDefaultCacheSize) * 25)
return available / 10;
if (available < static_cast<int64_t>(kDefaultCacheSize) * 250)
return kDefaultCacheSize * 5 / 2;
return available / 100;
}
}
namespace disk_cache {
const int kDefaultCacheSize = 80 * 1024 * 1024;
BASE_FEATURE(kChangeGeneratedCodeCacheSizeExperiment,
"ChangeGeneratedCodeCacheSize",
base::FEATURE_DISABLED_BY_DEFAULT);
void DeleteCache(const base::FilePath& path, bool remove_folder) {
if (remove_folder) {
if (!base::DeletePathRecursively(path))
LOG(WARNING) << "Unable to delete cache folder.";
return;
}
base::FileEnumerator iter(
path,
false,
base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
for (base::FilePath file = iter.Next(); !file.value().empty();
file = iter.Next()) {
if (!base::DeletePathRecursively(file)) {
LOG(WARNING) << "Unable to delete cache.";
return;
}
}
}
void CleanupDirectory(const base::FilePath& path,
base::OnceCallback<void(bool)> callback) {
auto task_runner = base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_BLOCKING,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN});
task_runner->PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(CleanupDirectoryInternal, path),
std::move(callback));
}
bool CleanupDirectorySync(const base::FilePath& path) {
base::ScopedAllowBlocking allow_blocking;
return CleanupDirectoryInternal(path);
}
int PreferredCacheSize(int64_t available, net::CacheType type) {
int percent_relative_size = 100;
#if !BUILDFLAG(IS_WIN)
if (type == net::DISK_CACHE) {
percent_relative_size = 400;
}
#endif
if (base::FeatureList::IsEnabled(
disk_cache::kChangeGeneratedCodeCacheSizeExperiment) &&
type == net::GENERATED_BYTE_CODE_CACHE) {
percent_relative_size = base::GetFieldTrialParamByFeatureAsInt(
disk_cache::kChangeGeneratedCodeCacheSizeExperiment,
"percent_relative_size", 400 );
}
if (percent_relative_size > 400)
percent_relative_size = 400;
else if (percent_relative_size < 100)
percent_relative_size = 100;
base::ClampedNumeric<int64_t> scaled_default_disk_cache_size =
(base::ClampedNumeric<int64_t>(disk_cache::kDefaultCacheSize) *
percent_relative_size) /
100;
base::ClampedNumeric<int64_t> preferred_cache_size =
scaled_default_disk_cache_size;
if (available >= 0) {
preferred_cache_size = PreferredCacheSizeInternal(available);
if (preferred_cache_size < available / 5) {
const base::ClampedNumeric<int64_t> clamped_available(available);
preferred_cache_size =
std::min((preferred_cache_size * percent_relative_size) / 100,
clamped_available / 5);
}
}
base::ClampedNumeric<int64_t> size_limit = scaled_default_disk_cache_size * 4;
if (type == net::GENERATED_NATIVE_CODE_CACHE) {
size_limit = (size_limit / 2) * 3;
} else if (type == net::GENERATED_WEBUI_BYTE_CODE_CACHE) {
size_limit = std::min(
size_limit, base::ClampedNumeric<int64_t>(kMaxWebUICodeCacheSize));
}
DCHECK_LT(size_limit, std::numeric_limits<int32_t>::max());
return static_cast<int32_t>(std::min(preferred_cache_size, size_limit));
}
}