This source file includes following definitions.
- GetPrefixedName
- CleanupCallback
- GetTempCacheName
- PreferredCacheSizeInternal
- DeleteCache
- DelayedCacheCleanup
- PreferredCacheSize
#include "net/disk_cache/cache_util.h"
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/location.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "base/threading/worker_pool.h"
namespace {
const int kMaxOldFolders = 100;
base::FilePath GetPrefixedName(const base::FilePath& path,
const std::string& name,
int index) {
std::string tmp = base::StringPrintf("%s%s_%03d", "old_",
name.c_str(), index);
return path.AppendASCII(tmp);
}
void CleanupCallback(const base::FilePath& path, const std::string& name) {
for (int i = 0; i < kMaxOldFolders; i++) {
base::FilePath to_delete = GetPrefixedName(path, name, i);
disk_cache::DeleteCache(to_delete, true);
}
}
base::FilePath GetTempCacheName(const base::FilePath& path,
const std::string& name) {
for (int i = 0; i < kMaxOldFolders; i++) {
base::FilePath to_delete = GetPrefixedName(path, name, i);
if (!base::PathExists(to_delete))
return to_delete;
}
return base::FilePath();
}
int64 PreferredCacheSizeInternal(int64 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>(kDefaultCacheSize) * 25)
return available / 10;
if (available < static_cast<int64>(kDefaultCacheSize) * 250)
return kDefaultCacheSize * 5 / 2;
return available / 100;
}
}
namespace disk_cache {
const int kDefaultCacheSize = 80 * 1024 * 1024;
void DeleteCache(const base::FilePath& path, bool remove_folder) {
if (remove_folder) {
if (!base::DeleteFile(path, true))
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::DeleteFile(file, true)) {
LOG(WARNING) << "Unable to delete cache.";
return;
}
}
}
bool DelayedCacheCleanup(const base::FilePath& full_path) {
base::ThreadRestrictions::ScopedAllowIO allow_io;
base::FilePath current_path = full_path.StripTrailingSeparators();
base::FilePath path = current_path.DirName();
base::FilePath name = current_path.BaseName();
#if defined(OS_POSIX)
std::string name_str = name.value();
#elif defined(OS_WIN)
std::string name_str = base::UTF16ToASCII(name.value());
#endif
base::FilePath to_delete = GetTempCacheName(path, name_str);
if (to_delete.empty()) {
LOG(ERROR) << "Unable to get another cache folder";
return false;
}
if (!disk_cache::MoveCache(full_path, to_delete)) {
LOG(ERROR) << "Unable to move cache folder " << full_path.value() << " to "
<< to_delete.value();
return false;
}
base::WorkerPool::PostTask(
FROM_HERE, base::Bind(&CleanupCallback, path, name_str), true);
return true;
}
int PreferredCacheSize(int64 available) {
if (available < 0)
return kDefaultCacheSize;
int64 max_size = PreferredCacheSizeInternal(available);
DCHECK(kDefaultCacheSize * 4 < kint32max);
if (max_size > kDefaultCacheSize * 4)
max_size = kDefaultCacheSize * 4;
return implicit_cast<int32>(max_size);
}
}