| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/field_trial.h" | 7 #include "base/field_trial.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/histogram.h" | 10 #include "base/histogram.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
| 15 #include "base/timer.h" | 15 #include "base/timer.h" |
| 16 #include "base/worker_pool.h" | 16 #include "base/worker_pool.h" |
| 17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/disk_cache/cache_util.h" | 18 #include "net/disk_cache/cache_util.h" |
| 19 #include "net/disk_cache/entry_impl.h" | 19 #include "net/disk_cache/entry_impl.h" |
| 20 #include "net/disk_cache/errors.h" | 20 #include "net/disk_cache/errors.h" |
| 21 #include "net/disk_cache/hash.h" | 21 #include "net/disk_cache/hash.h" |
| 22 #include "net/disk_cache/file.h" | 22 #include "net/disk_cache/file.h" |
| 23 #include "net/disk_cache/mem_backend_impl.h" |
| 23 | 24 |
| 24 // This has to be defined before including histogram_macros.h from this file. | 25 // This has to be defined before including histogram_macros.h from this file. |
| 25 #define NET_DISK_CACHE_BACKEND_IMPL_CC_ | 26 #define NET_DISK_CACHE_BACKEND_IMPL_CC_ |
| 26 #include "net/disk_cache/histogram_macros.h" | 27 #include "net/disk_cache/histogram_macros.h" |
| 27 | 28 |
| 28 using base::Time; | 29 using base::Time; |
| 29 using base::TimeDelta; | 30 using base::TimeDelta; |
| 30 using base::TimeTicks; | 31 using base::TimeTicks; |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 std::string group1 = StringPrintf("CacheSizeGroup_%d", size_group); | 161 std::string group1 = StringPrintf("CacheSizeGroup_%d", size_group); |
| 161 trial1->AppendGroup(group1, FieldTrial::kAllRemainingProbability); | 162 trial1->AppendGroup(group1, FieldTrial::kAllRemainingProbability); |
| 162 } | 163 } |
| 163 | 164 |
| 164 } // namespace | 165 } // namespace |
| 165 | 166 |
| 166 // ------------------------------------------------------------------------ | 167 // ------------------------------------------------------------------------ |
| 167 | 168 |
| 168 namespace disk_cache { | 169 namespace disk_cache { |
| 169 | 170 |
| 170 Backend* CreateCacheBackend(const FilePath& full_path, bool force, | |
| 171 int max_bytes, net::CacheType type) { | |
| 172 // Create a backend without extra flags. | |
| 173 return BackendImpl::CreateBackend(full_path, force, max_bytes, type, kNone); | |
| 174 } | |
| 175 | |
| 176 int CreateCacheBackend(net::CacheType type, const FilePath& path, int max_bytes, | 171 int CreateCacheBackend(net::CacheType type, const FilePath& path, int max_bytes, |
| 177 bool force, base::MessageLoopProxy* thread, | 172 bool force, base::MessageLoopProxy* thread, |
| 178 Backend** backend, CompletionCallback* callback) { | 173 Backend** backend, CompletionCallback* callback) { |
| 179 if (type == net::MEMORY_CACHE) | 174 DCHECK(callback); |
| 180 *backend = CreateInMemoryCacheBackend(max_bytes); | 175 if (type == net::MEMORY_CACHE) { |
| 181 else | 176 *backend = MemBackendImpl::CreateBackend(max_bytes); |
| 182 *backend = BackendImpl::CreateBackend(path, force, max_bytes, type, kNone); | 177 return *backend ? net::OK : net::ERR_FAILED; |
| 183 return *backend ? net::OK : net::ERR_FAILED; | 178 } |
| 179 DCHECK(thread); |
| 180 |
| 181 return BackendImpl::CreateBackend(path, force, max_bytes, type, kNone, thread, |
| 182 backend, callback); |
| 184 } | 183 } |
| 185 | 184 |
| 186 // Returns the preferred maximum number of bytes for the cache given the | 185 // Returns the preferred maximum number of bytes for the cache given the |
| 187 // number of available bytes. | 186 // number of available bytes. |
| 188 int PreferedCacheSize(int64 available) { | 187 int PreferedCacheSize(int64 available) { |
| 189 // Return 80% of the available space if there is not enough space to use | 188 // Return 80% of the available space if there is not enough space to use |
| 190 // kDefaultCacheSize. | 189 // kDefaultCacheSize. |
| 191 if (available < kDefaultCacheSize * 10 / 8) | 190 if (available < kDefaultCacheSize * 10 / 8) |
| 192 return static_cast<int32>(available * 8 / 10); | 191 return static_cast<int32>(available * 8 / 10); |
| 193 | 192 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 217 // If the initialization of the cache fails, and force is true, we will discard | 216 // If the initialization of the cache fails, and force is true, we will discard |
| 218 // the whole cache and create a new one. In order to process a potentially large | 217 // the whole cache and create a new one. In order to process a potentially large |
| 219 // number of files, we'll rename the cache folder to old_ + original_name + | 218 // number of files, we'll rename the cache folder to old_ + original_name + |
| 220 // number, (located on the same parent folder), and spawn a worker thread to | 219 // number, (located on the same parent folder), and spawn a worker thread to |
| 221 // delete all the files on all the stale cache folders. The whole process can | 220 // delete all the files on all the stale cache folders. The whole process can |
| 222 // still fail if we are not able to rename the cache folder (for instance due to | 221 // still fail if we are not able to rename the cache folder (for instance due to |
| 223 // a sharing violation), and in that case a cache for this profile (on the | 222 // a sharing violation), and in that case a cache for this profile (on the |
| 224 // desired path) cannot be created. | 223 // desired path) cannot be created. |
| 225 // | 224 // |
| 226 // Static. | 225 // Static. |
| 227 Backend* BackendImpl::CreateBackend(const FilePath& full_path, bool force, | 226 int BackendImpl::CreateBackend(const FilePath& full_path, bool force, |
| 228 int max_bytes, net::CacheType type, | 227 int max_bytes, net::CacheType type, |
| 229 BackendFlags flags) { | 228 uint32 flags, base::MessageLoopProxy* thread, |
| 230 BackendImpl* cache = new BackendImpl(full_path); | 229 Backend** backend, |
| 230 CompletionCallback* callback) { |
| 231 BackendImpl* cache = new BackendImpl(full_path, thread); |
| 231 cache->SetMaxSize(max_bytes); | 232 cache->SetMaxSize(max_bytes); |
| 232 cache->SetType(type); | 233 cache->SetType(type); |
| 233 cache->SetFlags(flags); | 234 cache->SetFlags(flags); |
| 234 if (cache->Init()) | 235 if (cache->Init()) { |
| 235 return cache; | 236 *backend = cache; |
| 237 return net::OK; |
| 238 } |
| 236 | 239 |
| 240 *backend = NULL; |
| 237 delete cache; | 241 delete cache; |
| 238 if (!force) | 242 if (!force) |
| 239 return NULL; | 243 return net::ERR_FAILED; |
| 240 | 244 |
| 241 if (!DelayedCacheCleanup(full_path)) | 245 if (!DelayedCacheCleanup(full_path)) |
| 242 return NULL; | 246 return net::ERR_FAILED; |
| 243 | 247 |
| 244 // The worker thread will start deleting files soon, but the original folder | 248 // The worker thread will start deleting files soon, but the original folder |
| 245 // is not there anymore... let's create a new set of files. | 249 // is not there anymore... let's create a new set of files. |
| 246 cache = new BackendImpl(full_path); | 250 cache = new BackendImpl(full_path, thread); |
| 247 cache->SetMaxSize(max_bytes); | 251 cache->SetMaxSize(max_bytes); |
| 248 cache->SetType(type); | 252 cache->SetType(type); |
| 249 cache->SetFlags(flags); | 253 cache->SetFlags(flags); |
| 250 if (cache->Init()) | 254 if (cache->Init()) { |
| 251 return cache; | 255 *backend = cache; |
| 256 return net::OK; |
| 257 } |
| 252 | 258 |
| 253 delete cache; | 259 delete cache; |
| 254 LOG(ERROR) << "Unable to create cache"; | 260 LOG(ERROR) << "Unable to create cache"; |
| 255 return NULL; | 261 return net::ERR_FAILED; |
| 256 } | 262 } |
| 257 | 263 |
| 258 bool BackendImpl::Init() { | 264 bool BackendImpl::Init() { |
| 259 DCHECK(!init_); | 265 DCHECK(!init_); |
| 260 if (init_) | 266 if (init_) |
| 261 return false; | 267 return false; |
| 262 | 268 |
| 263 bool create_files = false; | 269 bool create_files = false; |
| 264 if (!InitBackingStore(&create_files)) { | 270 if (!InitBackingStore(&create_files)) { |
| 265 ReportError(ERR_STORAGE_ERROR); | 271 ReportError(ERR_STORAGE_ERROR); |
| (...skipping 1433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1699 | 1705 |
| 1700 return num_dirty; | 1706 return num_dirty; |
| 1701 } | 1707 } |
| 1702 | 1708 |
| 1703 bool BackendImpl::CheckEntry(EntryImpl* cache_entry) { | 1709 bool BackendImpl::CheckEntry(EntryImpl* cache_entry) { |
| 1704 RankingsNode* rankings = cache_entry->rankings()->Data(); | 1710 RankingsNode* rankings = cache_entry->rankings()->Data(); |
| 1705 return !rankings->dummy; | 1711 return !rankings->dummy; |
| 1706 } | 1712 } |
| 1707 | 1713 |
| 1708 } // namespace disk_cache | 1714 } // namespace disk_cache |
| OLD | NEW |