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