| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/disk_cache/backend_impl.h" | 5 #include "net/disk_cache/backend_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 // Returns a fully qualified name from path and name, using a given name prefix | 80 // Returns a fully qualified name from path and name, using a given name prefix |
| 81 // and index number. For instance, if the arguments are "/foo", "bar" and 5, it | 81 // and index number. For instance, if the arguments are "/foo", "bar" and 5, it |
| 82 // will return "/foo/old_bar_005". | 82 // will return "/foo/old_bar_005". |
| 83 FilePath GetPrefixedName(const FilePath& path, const std::string& name, | 83 FilePath GetPrefixedName(const FilePath& path, const std::string& name, |
| 84 int index) { | 84 int index) { |
| 85 std::string tmp = base::StringPrintf("%s%s_%03d", "old_", | 85 std::string tmp = base::StringPrintf("%s%s_%03d", "old_", |
| 86 name.c_str(), index); | 86 name.c_str(), index); |
| 87 return path.AppendASCII(tmp); | 87 return path.AppendASCII(tmp); |
| 88 } | 88 } |
| 89 | 89 |
| 90 // This is a simple Task to cleanup old caches. | 90 // This is a simple callback to cleanup old caches. |
| 91 class CleanupTask : public Task { | 91 void CleanupCallback(const FilePath& path, const std::string& name) { |
| 92 public: | |
| 93 CleanupTask(const FilePath& path, const std::string& name) | |
| 94 : path_(path), name_(name) {} | |
| 95 | |
| 96 virtual void Run(); | |
| 97 | |
| 98 private: | |
| 99 FilePath path_; | |
| 100 std::string name_; | |
| 101 DISALLOW_COPY_AND_ASSIGN(CleanupTask); | |
| 102 }; | |
| 103 | |
| 104 void CleanupTask::Run() { | |
| 105 for (int i = 0; i < kMaxOldFolders; i++) { | 92 for (int i = 0; i < kMaxOldFolders; i++) { |
| 106 FilePath to_delete = GetPrefixedName(path_, name_, i); | 93 FilePath to_delete = GetPrefixedName(path, name, i); |
| 107 disk_cache::DeleteCache(to_delete, true); | 94 disk_cache::DeleteCache(to_delete, true); |
| 108 } | 95 } |
| 109 } | 96 } |
| 110 | 97 |
| 111 // Returns a full path to rename the current cache, in order to delete it. path | 98 // Returns a full path to rename the current cache, in order to delete it. path |
| 112 // is the current folder location, and name is the current folder name. | 99 // is the current folder location, and name is the current folder name. |
| 113 FilePath GetTempCacheName(const FilePath& path, const std::string& name) { | 100 FilePath GetTempCacheName(const FilePath& path, const std::string& name) { |
| 114 // We'll attempt to have up to kMaxOldFolders folders for deletion. | 101 // We'll attempt to have up to kMaxOldFolders folders for deletion. |
| 115 for (int i = 0; i < kMaxOldFolders; i++) { | 102 for (int i = 0; i < kMaxOldFolders; i++) { |
| 116 FilePath to_delete = GetPrefixedName(path, name, i); | 103 FilePath to_delete = GetPrefixedName(path, name, i); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 141 if (to_delete.empty()) { | 128 if (to_delete.empty()) { |
| 142 LOG(ERROR) << "Unable to get another cache folder"; | 129 LOG(ERROR) << "Unable to get another cache folder"; |
| 143 return false; | 130 return false; |
| 144 } | 131 } |
| 145 | 132 |
| 146 if (!disk_cache::MoveCache(full_path, to_delete)) { | 133 if (!disk_cache::MoveCache(full_path, to_delete)) { |
| 147 LOG(ERROR) << "Unable to move cache folder"; | 134 LOG(ERROR) << "Unable to move cache folder"; |
| 148 return false; | 135 return false; |
| 149 } | 136 } |
| 150 | 137 |
| 151 base::WorkerPool::PostTask(FROM_HERE, new CleanupTask(path, name_str), true); | 138 base::WorkerPool::PostTask( |
| 139 FROM_HERE, base::Bind(&CleanupCallback, path, name_str), true); |
| 152 return true; | 140 return true; |
| 153 } | 141 } |
| 154 | 142 |
| 155 // Initializes the field trial structures to allow performance measurements | 143 // Initializes the field trial structures to allow performance measurements |
| 156 // for the current cache configuration. | 144 // for the current cache configuration. |
| 157 void SetFieldTrialInfo(int group) { | 145 void SetFieldTrialInfo(int group) { |
| 158 static bool first = true; | 146 static bool first = true; |
| 159 if (!first) | 147 if (!first) |
| 160 return; | 148 return; |
| 161 | 149 |
| (...skipping 2068 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2230 if (total_memory > kMaxBuffersSize || total_memory <= 0) | 2218 if (total_memory > kMaxBuffersSize || total_memory <= 0) |
| 2231 total_memory = kMaxBuffersSize; | 2219 total_memory = kMaxBuffersSize; |
| 2232 | 2220 |
| 2233 done = true; | 2221 done = true; |
| 2234 } | 2222 } |
| 2235 | 2223 |
| 2236 return static_cast<int>(total_memory); | 2224 return static_cast<int>(total_memory); |
| 2237 } | 2225 } |
| 2238 | 2226 |
| 2239 } // namespace disk_cache | 2227 } // namespace disk_cache |
| OLD | NEW |