| 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/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 return trial2->group() == group2a; | 187 return trial2->group() == group2a; |
| 188 } | 188 } |
| 189 | 189 |
| 190 // ------------------------------------------------------------------------ | 190 // ------------------------------------------------------------------------ |
| 191 | 191 |
| 192 // This class takes care of building an instance of the backend. | 192 // This class takes care of building an instance of the backend. |
| 193 class CacheCreator { | 193 class CacheCreator { |
| 194 public: | 194 public: |
| 195 CacheCreator(const FilePath& path, bool force, int max_bytes, | 195 CacheCreator(const FilePath& path, bool force, int max_bytes, |
| 196 net::CacheType type, uint32 flags, | 196 net::CacheType type, uint32 flags, |
| 197 base::MessageLoopProxy* thread, net::NetLog* net_log, | 197 base::MessageLoopProxy* thread, disk_cache::Backend** backend, |
| 198 disk_cache::Backend** backend, | |
| 199 net::CompletionCallback* callback) | 198 net::CompletionCallback* callback) |
| 200 : path_(path), force_(force), retry_(false), max_bytes_(max_bytes), | 199 : path_(path), force_(force), retry_(false), max_bytes_(max_bytes), |
| 201 type_(type), flags_(flags), thread_(thread), backend_(backend), | 200 type_(type), flags_(flags), thread_(thread), backend_(backend), |
| 202 callback_(callback), cache_(NULL), net_log_(net_log), | 201 callback_(callback), cache_(NULL), |
| 203 ALLOW_THIS_IN_INITIALIZER_LIST( | 202 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 204 my_callback_(this, &CacheCreator::OnIOComplete)) { | 203 my_callback_(this, &CacheCreator::OnIOComplete)) { |
| 205 } | 204 } |
| 206 ~CacheCreator() {} | 205 ~CacheCreator() {} |
| 207 | 206 |
| 208 // Creates the backend. | 207 // Creates the backend. |
| 209 int Run(); | 208 int Run(); |
| 210 | 209 |
| 211 // Callback implementation. | 210 // Callback implementation. |
| 212 void OnIOComplete(int result); | 211 void OnIOComplete(int result); |
| 213 | 212 |
| 214 private: | 213 private: |
| 215 void DoCallback(int result); | 214 void DoCallback(int result); |
| 216 | 215 |
| 217 const FilePath& path_; | 216 const FilePath& path_; |
| 218 bool force_; | 217 bool force_; |
| 219 bool retry_; | 218 bool retry_; |
| 220 int max_bytes_; | 219 int max_bytes_; |
| 221 net::CacheType type_; | 220 net::CacheType type_; |
| 222 uint32 flags_; | 221 uint32 flags_; |
| 223 scoped_refptr<base::MessageLoopProxy> thread_; | 222 scoped_refptr<base::MessageLoopProxy> thread_; |
| 224 disk_cache::Backend** backend_; | 223 disk_cache::Backend** backend_; |
| 225 net::CompletionCallback* callback_; | 224 net::CompletionCallback* callback_; |
| 226 disk_cache::BackendImpl* cache_; | 225 disk_cache::BackendImpl* cache_; |
| 227 net::NetLog* net_log_; | |
| 228 net::CompletionCallbackImpl<CacheCreator> my_callback_; | 226 net::CompletionCallbackImpl<CacheCreator> my_callback_; |
| 229 | 227 |
| 230 DISALLOW_COPY_AND_ASSIGN(CacheCreator); | 228 DISALLOW_COPY_AND_ASSIGN(CacheCreator); |
| 231 }; | 229 }; |
| 232 | 230 |
| 233 int CacheCreator::Run() { | 231 int CacheCreator::Run() { |
| 234 cache_ = new disk_cache::BackendImpl(path_, thread_, net_log_); | 232 cache_ = new disk_cache::BackendImpl(path_, thread_); |
| 235 cache_->SetMaxSize(max_bytes_); | 233 cache_->SetMaxSize(max_bytes_); |
| 236 cache_->SetType(type_); | 234 cache_->SetType(type_); |
| 237 cache_->SetFlags(flags_); | 235 cache_->SetFlags(flags_); |
| 238 int rv = cache_->Init(&my_callback_); | 236 int rv = cache_->Init(&my_callback_); |
| 239 DCHECK_EQ(net::ERR_IO_PENDING, rv); | 237 DCHECK_EQ(net::ERR_IO_PENDING, rv); |
| 240 return rv; | 238 return rv; |
| 241 } | 239 } |
| 242 | 240 |
| 243 void CacheCreator::OnIOComplete(int result) { | 241 void CacheCreator::OnIOComplete(int result) { |
| 244 if (result == net::OK || !force_ || retry_) | 242 if (result == net::OK || !force_ || retry_) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 } | 288 } |
| 291 | 289 |
| 292 } // namespace | 290 } // namespace |
| 293 | 291 |
| 294 // ------------------------------------------------------------------------ | 292 // ------------------------------------------------------------------------ |
| 295 | 293 |
| 296 namespace disk_cache { | 294 namespace disk_cache { |
| 297 | 295 |
| 298 int CreateCacheBackend(net::CacheType type, const FilePath& path, int max_bytes, | 296 int CreateCacheBackend(net::CacheType type, const FilePath& path, int max_bytes, |
| 299 bool force, base::MessageLoopProxy* thread, | 297 bool force, base::MessageLoopProxy* thread, |
| 300 net::NetLog* net_log, Backend** backend, | 298 Backend** backend, CompletionCallback* callback) { |
| 301 CompletionCallback* callback) { | |
| 302 DCHECK(callback); | 299 DCHECK(callback); |
| 303 if (type == net::MEMORY_CACHE) { | 300 if (type == net::MEMORY_CACHE) { |
| 304 *backend = MemBackendImpl::CreateBackend(max_bytes); | 301 *backend = MemBackendImpl::CreateBackend(max_bytes); |
| 305 return *backend ? net::OK : net::ERR_FAILED; | 302 return *backend ? net::OK : net::ERR_FAILED; |
| 306 } | 303 } |
| 307 DCHECK(thread); | 304 DCHECK(thread); |
| 308 | 305 |
| 309 return BackendImpl::CreateBackend(path, force, max_bytes, type, kNone, thread, | 306 return BackendImpl::CreateBackend(path, force, max_bytes, type, kNone, thread, |
| 310 net_log, backend, callback); | 307 backend, callback); |
| 311 } | 308 } |
| 312 | 309 |
| 313 // Returns the preferred maximum number of bytes for the cache given the | 310 // Returns the preferred maximum number of bytes for the cache given the |
| 314 // number of available bytes. | 311 // number of available bytes. |
| 315 int PreferedCacheSize(int64 available) { | 312 int PreferedCacheSize(int64 available) { |
| 316 // Return 80% of the available space if there is not enough space to use | 313 // Return 80% of the available space if there is not enough space to use |
| 317 // kDefaultCacheSize. | 314 // kDefaultCacheSize. |
| 318 if (available < kDefaultCacheSize * 10 / 8) | 315 if (available < kDefaultCacheSize * 10 / 8) |
| 319 return static_cast<int32>(available * 8 / 10); | 316 return static_cast<int32>(available * 8 / 10); |
| 320 | 317 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 347 // number, (located on the same parent folder), and spawn a worker thread to | 344 // number, (located on the same parent folder), and spawn a worker thread to |
| 348 // delete all the files on all the stale cache folders. The whole process can | 345 // delete all the files on all the stale cache folders. The whole process can |
| 349 // still fail if we are not able to rename the cache folder (for instance due to | 346 // still fail if we are not able to rename the cache folder (for instance due to |
| 350 // a sharing violation), and in that case a cache for this profile (on the | 347 // a sharing violation), and in that case a cache for this profile (on the |
| 351 // desired path) cannot be created. | 348 // desired path) cannot be created. |
| 352 // | 349 // |
| 353 // Static. | 350 // Static. |
| 354 int BackendImpl::CreateBackend(const FilePath& full_path, bool force, | 351 int BackendImpl::CreateBackend(const FilePath& full_path, bool force, |
| 355 int max_bytes, net::CacheType type, | 352 int max_bytes, net::CacheType type, |
| 356 uint32 flags, base::MessageLoopProxy* thread, | 353 uint32 flags, base::MessageLoopProxy* thread, |
| 357 net::NetLog* net_log, Backend** backend, | 354 Backend** backend, |
| 358 CompletionCallback* callback) { | 355 CompletionCallback* callback) { |
| 359 DCHECK(callback); | 356 DCHECK(callback); |
| 360 CacheCreator* creator = new CacheCreator(full_path, force, max_bytes, type, | 357 CacheCreator* creator = new CacheCreator(full_path, force, max_bytes, type, |
| 361 flags, thread, net_log, backend, | 358 flags, thread, backend, callback); |
| 362 callback); | |
| 363 // This object will self-destroy when finished. | 359 // This object will self-destroy when finished. |
| 364 return creator->Run(); | 360 return creator->Run(); |
| 365 } | 361 } |
| 366 | 362 |
| 367 int BackendImpl::Init(CompletionCallback* callback) { | 363 int BackendImpl::Init(CompletionCallback* callback) { |
| 368 background_queue_.Init(callback); | 364 background_queue_.Init(callback); |
| 369 return net::ERR_IO_PENDING; | 365 return net::ERR_IO_PENDING; |
| 370 } | 366 } |
| 371 | 367 |
| 372 BackendImpl::BackendImpl(const FilePath& path, | 368 BackendImpl::BackendImpl(const FilePath& path, |
| 373 base::MessageLoopProxy* cache_thread, | 369 base::MessageLoopProxy* cache_thread) |
| 374 net::NetLog* net_log) | |
| 375 : ALLOW_THIS_IN_INITIALIZER_LIST(background_queue_(this, cache_thread)), | 370 : ALLOW_THIS_IN_INITIALIZER_LIST(background_queue_(this, cache_thread)), |
| 376 path_(path), | 371 path_(path), |
| 377 block_files_(path), | 372 block_files_(path), |
| 378 mask_(0), | 373 mask_(0), |
| 379 max_size_(0), | 374 max_size_(0), |
| 380 io_delay_(0), | 375 io_delay_(0), |
| 381 cache_type_(net::DISK_CACHE), | 376 cache_type_(net::DISK_CACHE), |
| 382 uma_report_(0), | 377 uma_report_(0), |
| 383 user_flags_(0), | 378 user_flags_(0), |
| 384 init_(false), | 379 init_(false), |
| 385 restarted_(false), | 380 restarted_(false), |
| 386 unit_test_(false), | 381 unit_test_(false), |
| 387 read_only_(false), | 382 read_only_(false), |
| 388 new_eviction_(false), | 383 new_eviction_(false), |
| 389 first_timer_(true), | 384 first_timer_(true), |
| 390 throttle_requests_(false), | 385 throttle_requests_(false), |
| 391 net_log_(net_log), | |
| 392 done_(true, false), | 386 done_(true, false), |
| 393 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)), | 387 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)), |
| 394 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) { | 388 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) { |
| 395 } | 389 } |
| 396 | 390 |
| 397 BackendImpl::BackendImpl(const FilePath& path, | 391 BackendImpl::BackendImpl(const FilePath& path, |
| 398 uint32 mask, | 392 uint32 mask, |
| 399 base::MessageLoopProxy* cache_thread, | 393 base::MessageLoopProxy* cache_thread) |
| 400 net::NetLog* net_log) | |
| 401 : ALLOW_THIS_IN_INITIALIZER_LIST(background_queue_(this, cache_thread)), | 394 : ALLOW_THIS_IN_INITIALIZER_LIST(background_queue_(this, cache_thread)), |
| 402 path_(path), | 395 path_(path), |
| 403 block_files_(path), | 396 block_files_(path), |
| 404 mask_(mask), | 397 mask_(mask), |
| 405 max_size_(0), | 398 max_size_(0), |
| 406 io_delay_(0), | 399 io_delay_(0), |
| 407 cache_type_(net::DISK_CACHE), | 400 cache_type_(net::DISK_CACHE), |
| 408 uma_report_(0), | 401 uma_report_(0), |
| 409 user_flags_(kMask), | 402 user_flags_(kMask), |
| 410 init_(false), | 403 init_(false), |
| 411 restarted_(false), | 404 restarted_(false), |
| 412 unit_test_(false), | 405 unit_test_(false), |
| 413 read_only_(false), | 406 read_only_(false), |
| 414 new_eviction_(false), | 407 new_eviction_(false), |
| 415 first_timer_(true), | 408 first_timer_(true), |
| 416 throttle_requests_(false), | 409 throttle_requests_(false), |
| 417 net_log_(net_log), | |
| 418 done_(true, false), | 410 done_(true, false), |
| 419 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)), | 411 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)), |
| 420 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) { | 412 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) { |
| 421 } | 413 } |
| 422 | 414 |
| 423 BackendImpl::~BackendImpl() { | 415 BackendImpl::~BackendImpl() { |
| 424 background_queue_.WaitForPendingIO(); | 416 background_queue_.WaitForPendingIO(); |
| 425 | 417 |
| 426 if (background_queue_.BackgroundIsCurrentThread()) { | 418 if (background_queue_.BackgroundIsCurrentThread()) { |
| 427 // Unit tests may use the same thread for everything. | 419 // Unit tests may use the same thread for everything. |
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 IncreaseNumRefs(); | 841 IncreaseNumRefs(); |
| 850 | 842 |
| 851 if (!cache_entry->CreateEntry(node_address, key, hash)) { | 843 if (!cache_entry->CreateEntry(node_address, key, hash)) { |
| 852 block_files_.DeleteBlock(entry_address, false); | 844 block_files_.DeleteBlock(entry_address, false); |
| 853 block_files_.DeleteBlock(node_address, false); | 845 block_files_.DeleteBlock(node_address, false); |
| 854 LOG(ERROR) << "Create entry failed " << key.c_str(); | 846 LOG(ERROR) << "Create entry failed " << key.c_str(); |
| 855 stats_.OnEvent(Stats::CREATE_ERROR); | 847 stats_.OnEvent(Stats::CREATE_ERROR); |
| 856 return NULL; | 848 return NULL; |
| 857 } | 849 } |
| 858 | 850 |
| 859 cache_entry->BeginLogging(net_log_, true); | |
| 860 | |
| 861 // We are not failing the operation; let's add this to the map. | 851 // We are not failing the operation; let's add this to the map. |
| 862 open_entries_[entry_address.value()] = cache_entry; | 852 open_entries_[entry_address.value()] = cache_entry; |
| 863 | 853 |
| 864 if (parent.get()) | 854 if (parent.get()) |
| 865 parent->SetNextAddress(entry_address); | 855 parent->SetNextAddress(entry_address); |
| 866 | 856 |
| 867 block_files_.GetFile(entry_address)->Store(cache_entry->entry()); | 857 block_files_.GetFile(entry_address)->Store(cache_entry->entry()); |
| 868 block_files_.GetFile(node_address)->Store(cache_entry->rankings()); | 858 block_files_.GetFile(node_address)->Store(cache_entry->rankings()); |
| 869 | 859 |
| 870 IncreaseNumEntries(); | 860 IncreaseNumEntries(); |
| (...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1540 return ERR_INVALID_LINKS; | 1530 return ERR_INVALID_LINKS; |
| 1541 | 1531 |
| 1542 if (*dirty) { | 1532 if (*dirty) { |
| 1543 Trace("Dirty entry 0x%p 0x%x", reinterpret_cast<void*>(cache_entry.get()), | 1533 Trace("Dirty entry 0x%p 0x%x", reinterpret_cast<void*>(cache_entry.get()), |
| 1544 address.value()); | 1534 address.value()); |
| 1545 } else { | 1535 } else { |
| 1546 // We only add clean entries to the map. | 1536 // We only add clean entries to the map. |
| 1547 open_entries_[address.value()] = cache_entry; | 1537 open_entries_[address.value()] = cache_entry; |
| 1548 } | 1538 } |
| 1549 | 1539 |
| 1550 cache_entry->BeginLogging(net_log_, false); | |
| 1551 cache_entry.swap(entry); | 1540 cache_entry.swap(entry); |
| 1552 return 0; | 1541 return 0; |
| 1553 } | 1542 } |
| 1554 | 1543 |
| 1555 EntryImpl* BackendImpl::MatchEntry(const std::string& key, uint32 hash, | 1544 EntryImpl* BackendImpl::MatchEntry(const std::string& key, uint32 hash, |
| 1556 bool find_parent) { | 1545 bool find_parent) { |
| 1557 Addr address(data_->table[hash & mask_]); | 1546 Addr address(data_->table[hash & mask_]); |
| 1558 scoped_refptr<EntryImpl> cache_entry, parent_entry; | 1547 scoped_refptr<EntryImpl> cache_entry, parent_entry; |
| 1559 EntryImpl* tmp = NULL; | 1548 EntryImpl* tmp = NULL; |
| 1560 bool found = false; | 1549 bool found = false; |
| (...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2095 if (total_memory > kMaxBuffersSize || total_memory <= 0) | 2084 if (total_memory > kMaxBuffersSize || total_memory <= 0) |
| 2096 total_memory = kMaxBuffersSize; | 2085 total_memory = kMaxBuffersSize; |
| 2097 | 2086 |
| 2098 done = true; | 2087 done = true; |
| 2099 } | 2088 } |
| 2100 | 2089 |
| 2101 return static_cast<int>(total_memory); | 2090 return static_cast<int>(total_memory); |
| 2102 } | 2091 } |
| 2103 | 2092 |
| 2104 } // namespace disk_cache | 2093 } // namespace disk_cache |
| OLD | NEW |