| OLD | NEW |
| 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 if (!first) | 155 if (!first) |
| 156 return; | 156 return; |
| 157 | 157 |
| 158 // Field trials involve static objects so we have to do this only once. | 158 // Field trials involve static objects so we have to do this only once. |
| 159 first = false; | 159 first = false; |
| 160 scoped_refptr<FieldTrial> trial1 = new FieldTrial("CacheSize", 10); | 160 scoped_refptr<FieldTrial> trial1 = new FieldTrial("CacheSize", 10); |
| 161 std::string group1 = StringPrintf("CacheSizeGroup_%d", size_group); | 161 std::string group1 = StringPrintf("CacheSizeGroup_%d", size_group); |
| 162 trial1->AppendGroup(group1, FieldTrial::kAllRemainingProbability); | 162 trial1->AppendGroup(group1, FieldTrial::kAllRemainingProbability); |
| 163 } | 163 } |
| 164 | 164 |
| 165 // ------------------------------------------------------------------------ | |
| 166 | |
| 167 // This class takes care of building an instance of the backend. | |
| 168 class CacheCreator { | |
| 169 public: | |
| 170 CacheCreator(const FilePath& path, bool force, int max_bytes, | |
| 171 net::CacheType type, uint32 flags, | |
| 172 base::MessageLoopProxy* thread, disk_cache::Backend** backend, | |
| 173 net::CompletionCallback* callback) | |
| 174 : path_(path), force_(force), retry_(false), max_bytes_(max_bytes), | |
| 175 type_(type), flags_(flags), thread_(thread), backend_(backend), | |
| 176 callback_(callback), cache_(NULL), | |
| 177 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 178 my_callback_(this, &CacheCreator::OnIOComplete)) { | |
| 179 } | |
| 180 ~CacheCreator() {} | |
| 181 | |
| 182 // Creates the backend. | |
| 183 int Run(); | |
| 184 | |
| 185 // Callback implementation. | |
| 186 void OnIOComplete(int result); | |
| 187 | |
| 188 private: | |
| 189 void DoCallback(int result); | |
| 190 | |
| 191 const FilePath& path_; | |
| 192 bool force_; | |
| 193 bool retry_; | |
| 194 int max_bytes_; | |
| 195 net::CacheType type_; | |
| 196 uint32 flags_; | |
| 197 scoped_refptr<base::MessageLoopProxy> thread_; | |
| 198 disk_cache::Backend** backend_; | |
| 199 net::CompletionCallback* callback_; | |
| 200 disk_cache::BackendImpl* cache_; | |
| 201 net::CompletionCallbackImpl<CacheCreator> my_callback_; | |
| 202 | |
| 203 DISALLOW_COPY_AND_ASSIGN(CacheCreator); | |
| 204 }; | |
| 205 | |
| 206 int CacheCreator::Run() { | |
| 207 cache_ = new disk_cache::BackendImpl(path_, thread_); | |
| 208 cache_->SetMaxSize(max_bytes_); | |
| 209 cache_->SetType(type_); | |
| 210 cache_->SetFlags(flags_); | |
| 211 int rv = cache_->Init(&my_callback_); | |
| 212 DCHECK_EQ(net::ERR_IO_PENDING, rv); | |
| 213 return rv; | |
| 214 } | |
| 215 | |
| 216 void CacheCreator::OnIOComplete(int result) { | |
| 217 if (result == net::OK || !force_ || retry_) | |
| 218 return DoCallback(result); | |
| 219 | |
| 220 // This is a failure and we are supposed to try again, so delete the object, | |
| 221 // delete all the files, and try again. | |
| 222 retry_ = true; | |
| 223 delete cache_; | |
| 224 if (!DelayedCacheCleanup(path_)) | |
| 225 return DoCallback(result); | |
| 226 | |
| 227 // The worker thread will start deleting files soon, but the original folder | |
| 228 // is not there anymore... let's create a new set of files. | |
| 229 int rv = Run(); | |
| 230 DCHECK_EQ(net::ERR_IO_PENDING, rv); | |
| 231 } | |
| 232 | |
| 233 void CacheCreator::DoCallback(int result) { | |
| 234 DCHECK_NE(net::ERR_IO_PENDING, result); | |
| 235 if (result == net::OK) { | |
| 236 *backend_ = cache_; | |
| 237 } else { | |
| 238 LOG(ERROR) << "Unable to create cache"; | |
| 239 *backend_ = NULL; | |
| 240 delete cache_; | |
| 241 } | |
| 242 callback_->Run(result); | |
| 243 delete this; | |
| 244 } | |
| 245 | |
| 246 // ------------------------------------------------------------------------ | |
| 247 | |
| 248 // A task to perform final cleanup on the background thread. | |
| 249 class FinalCleanup : public Task { | |
| 250 public: | |
| 251 explicit FinalCleanup(disk_cache::BackendImpl* backend) : backend_(backend) {} | |
| 252 ~FinalCleanup() {} | |
| 253 | |
| 254 virtual void Run(); | |
| 255 private: | |
| 256 disk_cache::BackendImpl* backend_; | |
| 257 DISALLOW_EVIL_CONSTRUCTORS(FinalCleanup); | |
| 258 }; | |
| 259 | |
| 260 void FinalCleanup::Run() { | |
| 261 backend_->CleanupCache(); | |
| 262 } | |
| 263 | |
| 264 } // namespace | 165 } // namespace |
| 265 | 166 |
| 266 // ------------------------------------------------------------------------ | 167 // ------------------------------------------------------------------------ |
| 267 | 168 |
| 268 namespace disk_cache { | 169 namespace disk_cache { |
| 269 | 170 |
| 270 int CreateCacheBackend(net::CacheType type, const FilePath& path, int max_bytes, | 171 int CreateCacheBackend(net::CacheType type, const FilePath& path, int max_bytes, |
| 271 bool force, base::MessageLoopProxy* thread, | 172 bool force, base::MessageLoopProxy* thread, |
| 272 Backend** backend, CompletionCallback* callback) { | 173 Backend** backend, CompletionCallback* callback) { |
| 273 DCHECK(callback); | 174 DCHECK(callback); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 // 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 |
| 321 // 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 |
| 322 // desired path) cannot be created. | 223 // desired path) cannot be created. |
| 323 // | 224 // |
| 324 // Static. | 225 // Static. |
| 325 int BackendImpl::CreateBackend(const FilePath& full_path, bool force, | 226 int BackendImpl::CreateBackend(const FilePath& full_path, bool force, |
| 326 int max_bytes, net::CacheType type, | 227 int max_bytes, net::CacheType type, |
| 327 uint32 flags, base::MessageLoopProxy* thread, | 228 uint32 flags, base::MessageLoopProxy* thread, |
| 328 Backend** backend, | 229 Backend** backend, |
| 329 CompletionCallback* callback) { | 230 CompletionCallback* callback) { |
| 330 CacheCreator* creator = new CacheCreator(full_path, force, max_bytes, type, | 231 BackendImpl* cache = new BackendImpl(full_path, thread); |
| 331 flags, thread, backend, callback); | 232 cache->SetMaxSize(max_bytes); |
| 332 // This object will self-destroy when finished. | 233 cache->SetType(type); |
| 333 return creator->Run(); | 234 cache->SetFlags(flags); |
| 334 } | 235 if (cache->Init()) { |
| 236 *backend = cache; |
| 237 return net::OK; |
| 238 } |
| 335 | 239 |
| 336 int BackendImpl::SyncInit() { | 240 *backend = NULL; |
| 337 if (Init()) | 241 delete cache; |
| 242 if (!force) |
| 243 return net::ERR_FAILED; |
| 244 |
| 245 if (!DelayedCacheCleanup(full_path)) |
| 246 return net::ERR_FAILED; |
| 247 |
| 248 // The worker thread will start deleting files soon, but the original folder |
| 249 // is not there anymore... let's create a new set of files. |
| 250 cache = new BackendImpl(full_path, thread); |
| 251 cache->SetMaxSize(max_bytes); |
| 252 cache->SetType(type); |
| 253 cache->SetFlags(flags); |
| 254 if (cache->Init()) { |
| 255 *backend = cache; |
| 338 return net::OK; | 256 return net::OK; |
| 257 } |
| 339 | 258 |
| 259 delete cache; |
| 260 LOG(ERROR) << "Unable to create cache"; |
| 340 return net::ERR_FAILED; | 261 return net::ERR_FAILED; |
| 341 } | 262 } |
| 342 | 263 |
| 343 bool BackendImpl::Init() { | 264 bool BackendImpl::Init() { |
| 344 DCHECK(!init_); | 265 DCHECK(!init_); |
| 345 if (init_) | 266 if (init_) |
| 346 return false; | 267 return false; |
| 347 | 268 |
| 348 bool create_files = false; | 269 bool create_files = false; |
| 349 if (!InitBackingStore(&create_files)) { | 270 if (!InitBackingStore(&create_files)) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 376 new_eviction_ = (cache_type_ == net::DISK_CACHE); | 297 new_eviction_ = (cache_type_ == net::DISK_CACHE); |
| 377 } | 298 } |
| 378 | 299 |
| 379 if (!CheckIndex()) { | 300 if (!CheckIndex()) { |
| 380 ReportError(ERR_INIT_FAILED); | 301 ReportError(ERR_INIT_FAILED); |
| 381 return false; | 302 return false; |
| 382 } | 303 } |
| 383 | 304 |
| 384 // We don't care if the value overflows. The only thing we care about is that | 305 // We don't care if the value overflows. The only thing we care about is that |
| 385 // the id cannot be zero, because that value is used as "not dirty". | 306 // the id cannot be zero, because that value is used as "not dirty". |
| 386 // Increasing the value once per second gives us many years before we start | 307 // Increasing the value once per second gives us many years before a we start |
| 387 // having collisions. | 308 // having collisions. |
| 388 data_->header.this_id++; | 309 data_->header.this_id++; |
| 389 if (!data_->header.this_id) | 310 if (!data_->header.this_id) |
| 390 data_->header.this_id++; | 311 data_->header.this_id++; |
| 391 | 312 |
| 392 if (data_->header.crash) { | 313 if (data_->header.crash) { |
| 393 ReportError(ERR_PREVIOUS_CRASH); | 314 ReportError(ERR_PREVIOUS_CRASH); |
| 394 } else { | 315 } else { |
| 395 ReportError(0); | 316 ReportError(0); |
| 396 data_->header.crash = 1; | 317 data_->header.crash = 1; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 407 disabled_ = !rankings_.Init(this, new_eviction_); | 328 disabled_ = !rankings_.Init(this, new_eviction_); |
| 408 eviction_.Init(this); | 329 eviction_.Init(this); |
| 409 | 330 |
| 410 // Setup load-time data only for the main cache. | 331 // Setup load-time data only for the main cache. |
| 411 if (cache_type() == net::DISK_CACHE) | 332 if (cache_type() == net::DISK_CACHE) |
| 412 SetFieldTrialInfo(GetSizeGroup()); | 333 SetFieldTrialInfo(GetSizeGroup()); |
| 413 | 334 |
| 414 return !disabled_; | 335 return !disabled_; |
| 415 } | 336 } |
| 416 | 337 |
| 417 int BackendImpl::Init(CompletionCallback* callback) { | 338 BackendImpl::~BackendImpl() { |
| 418 background_queue_.Init(callback); | 339 Trace("Backend destructor"); |
| 419 return net::ERR_IO_PENDING; | 340 if (!init_) |
| 420 } | 341 return; |
| 421 | 342 |
| 422 BackendImpl::~BackendImpl() { | 343 if (data_) |
| 423 background_queue_.WaitForPendingIO(); | 344 data_->header.crash = 0; |
| 424 | 345 |
| 425 if (background_queue_.BackgroundIsCurrentThread()) { | 346 timer_.Stop(); |
| 426 // Unit tests may use the same thread for everything. | |
| 427 CleanupCache(); | |
| 428 } else { | |
| 429 background_queue_.background_thread()->PostTask(FROM_HERE, | |
| 430 new FinalCleanup(this)); | |
| 431 done_.Wait(); | |
| 432 } | |
| 433 } | |
| 434 | 347 |
| 435 void BackendImpl::CleanupCache() { | 348 File::WaitForPendingIO(&num_pending_io_); |
| 436 Trace("Backend Cleanup"); | 349 DCHECK(!num_refs_); |
| 437 if (init_) { | |
| 438 if (data_) | |
| 439 data_->header.crash = 0; | |
| 440 | |
| 441 timer_.Stop(); | |
| 442 File::WaitForPendingIO(&num_pending_io_); | |
| 443 DCHECK(!num_refs_); | |
| 444 } | |
| 445 factory_.RevokeAll(); | |
| 446 done_.Signal(); | |
| 447 } | 350 } |
| 448 | 351 |
| 449 // ------------------------------------------------------------------------ | 352 // ------------------------------------------------------------------------ |
| 450 | 353 |
| 451 int32 BackendImpl::GetEntryCount() const { | 354 int32 BackendImpl::GetEntryCount() const { |
| 452 if (!index_) | 355 if (!index_) |
| 453 return 0; | 356 return 0; |
| 454 // num_entries includes entries already evicted. | 357 // num_entries includes entries already evicted. |
| 455 int32 not_deleted = data_->header.num_entries - | 358 int32 not_deleted = data_->header.num_entries - |
| 456 data_->header.lru.sizes[Rankings::DELETED]; | 359 data_->header.lru.sizes[Rankings::DELETED]; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 483 return NULL; | 386 return NULL; |
| 484 } | 387 } |
| 485 | 388 |
| 486 eviction_.OnOpenEntry(cache_entry); | 389 eviction_.OnOpenEntry(cache_entry); |
| 487 | 390 |
| 488 CACHE_UMA(AGE_MS, "OpenTime", GetSizeGroup(), start); | 391 CACHE_UMA(AGE_MS, "OpenTime", GetSizeGroup(), start); |
| 489 stats_.OnEvent(Stats::OPEN_HIT); | 392 stats_.OnEvent(Stats::OPEN_HIT); |
| 490 return cache_entry; | 393 return cache_entry; |
| 491 } | 394 } |
| 492 | 395 |
| 493 int BackendImpl::SyncOpenEntry(const std::string& key, Entry** entry) { | 396 bool BackendImpl::OpenEntry(const std::string& key, Entry** entry) { |
| 494 DCHECK(entry); | 397 DCHECK(entry); |
| 495 *entry = OpenEntryImpl(key); | 398 *entry = OpenEntryImpl(key); |
| 496 return (*entry) ? net::OK : net::ERR_FAILED; | 399 return (*entry) ? true : false; |
| 497 } | 400 } |
| 498 | 401 |
| 499 int BackendImpl::OpenEntry(const std::string& key, Entry** entry, | 402 int BackendImpl::OpenEntry(const std::string& key, Entry** entry, |
| 500 CompletionCallback* callback) { | 403 CompletionCallback* callback) { |
| 501 DCHECK(callback); | 404 if (OpenEntry(key, entry)) |
| 502 background_queue_.OpenEntry(key, entry, callback); | 405 return net::OK; |
| 503 return net::ERR_IO_PENDING; | 406 |
| 407 return net::ERR_FAILED; |
| 504 } | 408 } |
| 505 | 409 |
| 506 EntryImpl* BackendImpl::CreateEntryImpl(const std::string& key) { | 410 EntryImpl* BackendImpl::CreateEntryImpl(const std::string& key) { |
| 507 if (disabled_ || key.empty()) | 411 if (disabled_ || key.empty()) |
| 508 return NULL; | 412 return NULL; |
| 509 | 413 |
| 510 TimeTicks start = TimeTicks::Now(); | 414 TimeTicks start = TimeTicks::Now(); |
| 511 uint32 hash = Hash(key); | 415 uint32 hash = Hash(key); |
| 512 | 416 |
| 513 scoped_refptr<EntryImpl> parent; | 417 scoped_refptr<EntryImpl> parent; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 eviction_.OnCreateEntry(cache_entry); | 477 eviction_.OnCreateEntry(cache_entry); |
| 574 if (!parent.get()) | 478 if (!parent.get()) |
| 575 data_->table[hash & mask_] = entry_address.value(); | 479 data_->table[hash & mask_] = entry_address.value(); |
| 576 | 480 |
| 577 CACHE_UMA(AGE_MS, "CreateTime", GetSizeGroup(), start); | 481 CACHE_UMA(AGE_MS, "CreateTime", GetSizeGroup(), start); |
| 578 stats_.OnEvent(Stats::CREATE_HIT); | 482 stats_.OnEvent(Stats::CREATE_HIT); |
| 579 Trace("create entry hit "); | 483 Trace("create entry hit "); |
| 580 return cache_entry.release(); | 484 return cache_entry.release(); |
| 581 } | 485 } |
| 582 | 486 |
| 583 int BackendImpl::SyncCreateEntry(const std::string& key, Entry** entry) { | 487 bool BackendImpl::CreateEntry(const std::string& key, Entry** entry) { |
| 584 DCHECK(entry); | 488 DCHECK(entry); |
| 585 *entry = CreateEntryImpl(key); | 489 *entry = CreateEntryImpl(key); |
| 586 return (*entry) ? net::OK : net::ERR_FAILED; | 490 return (*entry) ? true : false; |
| 587 } | 491 } |
| 588 | 492 |
| 589 int BackendImpl::CreateEntry(const std::string& key, Entry** entry, | 493 int BackendImpl::CreateEntry(const std::string& key, Entry** entry, |
| 590 CompletionCallback* callback) { | 494 CompletionCallback* callback) { |
| 591 DCHECK(callback); | 495 if (CreateEntry(key, entry)) |
| 592 background_queue_.CreateEntry(key, entry, callback); | |
| 593 return net::ERR_IO_PENDING; | |
| 594 } | |
| 595 | |
| 596 int BackendImpl::SyncDoomEntry(const std::string& key) { | |
| 597 if (DoomEntry(key)) | |
| 598 return net::OK; | 496 return net::OK; |
| 599 | 497 |
| 600 return net::ERR_FAILED; | 498 return net::ERR_FAILED; |
| 601 } | 499 } |
| 602 | 500 |
| 603 bool BackendImpl::DoomEntry(const std::string& key) { | 501 bool BackendImpl::DoomEntry(const std::string& key) { |
| 604 if (disabled_) | 502 if (disabled_) |
| 605 return false; | 503 return false; |
| 606 | 504 |
| 607 EntryImpl* entry = OpenEntryImpl(key); | 505 Entry* entry; |
| 608 if (!entry) | 506 if (!OpenEntry(key, &entry)) |
| 609 return false; | 507 return false; |
| 610 | 508 |
| 611 entry->DoomImpl(); | 509 // Note that you'd think you could just pass &entry_impl to OpenEntry, |
| 612 entry->Release(); | 510 // but that triggers strict aliasing problems with gcc. |
| 511 EntryImpl* entry_impl = reinterpret_cast<EntryImpl*>(entry); |
| 512 entry_impl->Doom(); |
| 513 entry_impl->Release(); |
| 613 return true; | 514 return true; |
| 614 } | 515 } |
| 615 | 516 |
| 616 int BackendImpl::DoomEntry(const std::string& key, | 517 int BackendImpl::DoomEntry(const std::string& key, |
| 617 CompletionCallback* callback) { | 518 CompletionCallback* callback) { |
| 618 DCHECK(callback); | 519 if (DoomEntry(key)) |
| 619 background_queue_.DoomEntry(key, callback); | |
| 620 return net::ERR_IO_PENDING; | |
| 621 } | |
| 622 | |
| 623 int BackendImpl::SyncDoomAllEntries() { | |
| 624 if (DoomAllEntries()) | |
| 625 return net::OK; | 520 return net::OK; |
| 626 | 521 |
| 627 return net::ERR_FAILED; | 522 return net::ERR_FAILED; |
| 628 } | 523 } |
| 629 | 524 |
| 630 bool BackendImpl::DoomAllEntries() { | 525 bool BackendImpl::DoomAllEntries() { |
| 631 if (!num_refs_) { | 526 if (!num_refs_) { |
| 632 PrepareForRestart(); | 527 PrepareForRestart(); |
| 633 DeleteCache(path_, false); | 528 DeleteCache(path_, false); |
| 634 return Init(); | 529 return Init(); |
| 635 } else { | 530 } else { |
| 636 if (disabled_) | 531 if (disabled_) |
| 637 return false; | 532 return false; |
| 638 | 533 |
| 639 eviction_.TrimCache(true); | 534 eviction_.TrimCache(true); |
| 640 stats_.OnEvent(Stats::DOOM_CACHE); | 535 stats_.OnEvent(Stats::DOOM_CACHE); |
| 641 return true; | 536 return true; |
| 642 } | 537 } |
| 643 } | 538 } |
| 644 | 539 |
| 645 int BackendImpl::DoomAllEntries(CompletionCallback* callback) { | 540 int BackendImpl::DoomAllEntries(CompletionCallback* callback) { |
| 646 DCHECK(callback); | 541 if (DoomAllEntries()) |
| 647 background_queue_.DoomAllEntries(callback); | |
| 648 return net::ERR_IO_PENDING; | |
| 649 } | |
| 650 | |
| 651 int BackendImpl::SyncDoomEntriesBetween(const base::Time initial_time, | |
| 652 const base::Time end_time) { | |
| 653 if (DoomEntriesBetween(initial_time, end_time)) | |
| 654 return net::OK; | 542 return net::OK; |
| 655 | 543 |
| 656 return net::ERR_FAILED; | 544 return net::ERR_FAILED; |
| 657 } | 545 } |
| 658 | 546 |
| 659 bool BackendImpl::DoomEntriesBetween(const Time initial_time, | 547 bool BackendImpl::DoomEntriesBetween(const Time initial_time, |
| 660 const Time end_time) { | 548 const Time end_time) { |
| 661 if (end_time.is_null()) | 549 if (end_time.is_null()) |
| 662 return DoomEntriesSince(initial_time); | 550 return DoomEntriesSince(initial_time); |
| 663 | 551 |
| 664 DCHECK(end_time >= initial_time); | 552 DCHECK(end_time >= initial_time); |
| 665 | 553 |
| 666 if (disabled_) | 554 if (disabled_) |
| 667 return false; | 555 return false; |
| 668 | 556 |
| 669 EntryImpl* node; | 557 Entry* node, *next; |
| 670 void* iter = NULL; | 558 void* iter = NULL; |
| 671 EntryImpl* next = OpenNextEntryImpl(&iter); | 559 if (!OpenNextEntry(&iter, &next)) |
| 672 if (!next) | |
| 673 return true; | 560 return true; |
| 674 | 561 |
| 675 while (next) { | 562 while (next) { |
| 676 node = next; | 563 node = next; |
| 677 next = OpenNextEntryImpl(&iter); | 564 if (!OpenNextEntry(&iter, &next)) |
| 565 next = NULL; |
| 678 | 566 |
| 679 if (node->GetLastUsed() >= initial_time && | 567 if (node->GetLastUsed() >= initial_time && |
| 680 node->GetLastUsed() < end_time) { | 568 node->GetLastUsed() < end_time) { |
| 681 node->DoomImpl(); | 569 node->Doom(); |
| 682 } else if (node->GetLastUsed() < initial_time) { | 570 } else if (node->GetLastUsed() < initial_time) { |
| 683 if (next) | 571 if (next) |
| 684 next->Release(); | 572 next->Close(); |
| 685 next = NULL; | 573 next = NULL; |
| 686 SyncEndEnumeration(iter); | 574 EndEnumeration(&iter); |
| 687 } | 575 } |
| 688 | 576 |
| 689 node->Release(); | 577 node->Close(); |
| 690 } | 578 } |
| 691 | 579 |
| 692 return true; | 580 return true; |
| 693 } | 581 } |
| 694 | 582 |
| 695 int BackendImpl::DoomEntriesBetween(const base::Time initial_time, | 583 int BackendImpl::DoomEntriesBetween(const base::Time initial_time, |
| 696 const base::Time end_time, | 584 const base::Time end_time, |
| 697 CompletionCallback* callback) { | 585 CompletionCallback* callback) { |
| 698 DCHECK(callback); | 586 if (DoomEntriesBetween(initial_time, end_time)) |
| 699 background_queue_.DoomEntriesBetween(initial_time, end_time, callback); | 587 return net::OK; |
| 700 return net::ERR_IO_PENDING; | 588 |
| 589 return net::ERR_FAILED; |
| 701 } | 590 } |
| 702 | 591 |
| 703 int BackendImpl::SyncDoomEntriesSince(const base::Time initial_time) { | 592 // We use OpenNextEntry to retrieve elements from the cache, until we get |
| 593 // entries that are too old. |
| 594 bool BackendImpl::DoomEntriesSince(const Time initial_time) { |
| 595 if (disabled_) |
| 596 return false; |
| 597 |
| 598 for (;;) { |
| 599 Entry* entry; |
| 600 void* iter = NULL; |
| 601 if (!OpenNextEntry(&iter, &entry)) |
| 602 return true; |
| 603 |
| 604 if (initial_time > entry->GetLastUsed()) { |
| 605 entry->Close(); |
| 606 EndEnumeration(&iter); |
| 607 return true; |
| 608 } |
| 609 |
| 610 entry->Doom(); |
| 611 entry->Close(); |
| 612 EndEnumeration(&iter); // Dooming the entry invalidates the iterator. |
| 613 } |
| 614 } |
| 615 |
| 616 int BackendImpl::DoomEntriesSince(const base::Time initial_time, |
| 617 CompletionCallback* callback) { |
| 704 if (DoomEntriesSince(initial_time)) | 618 if (DoomEntriesSince(initial_time)) |
| 705 return net::OK; | 619 return net::OK; |
| 706 | 620 |
| 707 return net::ERR_FAILED; | 621 return net::ERR_FAILED; |
| 708 } | 622 } |
| 709 | 623 |
| 710 // We use OpenNextEntryImpl to retrieve elements from the cache, until we get | 624 bool BackendImpl::OpenNextEntry(void** iter, Entry** next_entry) { |
| 711 // entries that are too old. | 625 return OpenFollowingEntry(true, iter, next_entry); |
| 712 bool BackendImpl::DoomEntriesSince(const Time initial_time) { | |
| 713 if (disabled_) | |
| 714 return false; | |
| 715 | |
| 716 for (;;) { | |
| 717 void* iter = NULL; | |
| 718 EntryImpl* entry = OpenNextEntryImpl(&iter); | |
| 719 if (!entry) | |
| 720 return true; | |
| 721 | |
| 722 if (initial_time > entry->GetLastUsed()) { | |
| 723 entry->Release(); | |
| 724 SyncEndEnumeration(iter); | |
| 725 return true; | |
| 726 } | |
| 727 | |
| 728 entry->DoomImpl(); | |
| 729 entry->Release(); | |
| 730 SyncEndEnumeration(iter); // Dooming the entry invalidates the iterator. | |
| 731 } | |
| 732 } | |
| 733 | |
| 734 int BackendImpl::DoomEntriesSince(const base::Time initial_time, | |
| 735 CompletionCallback* callback) { | |
| 736 DCHECK(callback); | |
| 737 background_queue_.DoomEntriesSince(initial_time, callback); | |
| 738 return net::ERR_IO_PENDING; | |
| 739 } | |
| 740 | |
| 741 int BackendImpl::SyncOpenNextEntry(void** iter, Entry** next_entry) { | |
| 742 *next_entry = OpenNextEntryImpl(iter); | |
| 743 return (*next_entry) ? net::OK : net::ERR_FAILED; | |
| 744 } | |
| 745 | |
| 746 EntryImpl* BackendImpl::OpenNextEntryImpl(void** iter) { | |
| 747 return OpenFollowingEntry(true, iter); | |
| 748 } | 626 } |
| 749 | 627 |
| 750 int BackendImpl::OpenNextEntry(void** iter, Entry** next_entry, | 628 int BackendImpl::OpenNextEntry(void** iter, Entry** next_entry, |
| 751 CompletionCallback* callback) { | 629 CompletionCallback* callback) { |
| 752 DCHECK(callback); | 630 if (OpenNextEntry(iter, next_entry)) |
| 753 background_queue_.OpenNextEntry(iter, next_entry, callback); | 631 return net::OK; |
| 754 return net::ERR_IO_PENDING; | |
| 755 } | |
| 756 | 632 |
| 757 void BackendImpl::SyncEndEnumeration(void* iter) { | 633 return net::ERR_FAILED; |
| 758 scoped_ptr<Rankings::Iterator> iterator( | |
| 759 reinterpret_cast<Rankings::Iterator*>(iter)); | |
| 760 } | 634 } |
| 761 | 635 |
| 762 void BackendImpl::EndEnumeration(void** iter) { | 636 void BackendImpl::EndEnumeration(void** iter) { |
| 763 background_queue_.EndEnumeration(*iter); | 637 scoped_ptr<Rankings::Iterator> iterator( |
| 638 reinterpret_cast<Rankings::Iterator*>(*iter)); |
| 764 *iter = NULL; | 639 *iter = NULL; |
| 765 } | 640 } |
| 766 | 641 |
| 767 void BackendImpl::GetStats(StatsItems* stats) { | 642 void BackendImpl::GetStats(StatsItems* stats) { |
| 768 if (disabled_) | 643 if (disabled_) |
| 769 return; | 644 return; |
| 770 | 645 |
| 771 std::pair<std::string, std::string> item; | 646 std::pair<std::string, std::string> item; |
| 772 | 647 |
| 773 item.first = "Entries"; | 648 item.first = "Entries"; |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1134 } | 1009 } |
| 1135 | 1010 |
| 1136 void BackendImpl::SetFlags(uint32 flags) { | 1011 void BackendImpl::SetFlags(uint32 flags) { |
| 1137 user_flags_ |= flags; | 1012 user_flags_ |= flags; |
| 1138 } | 1013 } |
| 1139 | 1014 |
| 1140 void BackendImpl::ClearRefCountForTest() { | 1015 void BackendImpl::ClearRefCountForTest() { |
| 1141 num_refs_ = 0; | 1016 num_refs_ = 0; |
| 1142 } | 1017 } |
| 1143 | 1018 |
| 1144 int BackendImpl::FlushQueueForTest(CompletionCallback* callback) { | |
| 1145 background_queue_.FlushQueue(callback); | |
| 1146 return net::ERR_IO_PENDING; | |
| 1147 } | |
| 1148 | |
| 1149 int BackendImpl::SelfCheck() { | 1019 int BackendImpl::SelfCheck() { |
| 1150 if (!init_) { | 1020 if (!init_) { |
| 1151 LOG(ERROR) << "Init failed"; | 1021 LOG(ERROR) << "Init failed"; |
| 1152 return ERR_INIT_FAILED; | 1022 return ERR_INIT_FAILED; |
| 1153 } | 1023 } |
| 1154 | 1024 |
| 1155 int num_entries = rankings_.SelfCheck(); | 1025 int num_entries = rankings_.SelfCheck(); |
| 1156 if (num_entries < 0) { | 1026 if (num_entries < 0) { |
| 1157 LOG(ERROR) << "Invalid rankings list, error " << num_entries; | 1027 LOG(ERROR) << "Invalid rankings list, error " << num_entries; |
| 1158 return num_entries; | 1028 return num_entries; |
| 1159 } | 1029 } |
| 1160 | 1030 |
| 1161 if (num_entries != data_->header.num_entries) { | 1031 if (num_entries != data_->header.num_entries) { |
| 1162 LOG(ERROR) << "Number of entries mismatch"; | 1032 LOG(ERROR) << "Number of entries mismatch"; |
| 1163 return ERR_NUM_ENTRIES_MISMATCH; | 1033 return ERR_NUM_ENTRIES_MISMATCH; |
| 1164 } | 1034 } |
| 1165 | 1035 |
| 1166 return CheckAllEntries(); | 1036 return CheckAllEntries(); |
| 1167 } | 1037 } |
| 1168 | 1038 |
| 1169 int BackendImpl::SyncOpenPrevEntry(void** iter, Entry** prev_entry) { | 1039 bool BackendImpl::OpenPrevEntry(void** iter, Entry** prev_entry) { |
| 1170 *prev_entry = OpenPrevEntryImpl(iter); | 1040 return OpenFollowingEntry(false, iter, prev_entry); |
| 1171 return (*prev_entry) ? net::OK : net::ERR_FAILED; | |
| 1172 } | |
| 1173 | |
| 1174 int BackendImpl::OpenPrevEntry(void** iter, Entry** prev_entry, | |
| 1175 CompletionCallback* callback) { | |
| 1176 DCHECK(callback); | |
| 1177 background_queue_.OpenPrevEntry(iter, prev_entry, callback); | |
| 1178 return net::ERR_IO_PENDING; | |
| 1179 } | |
| 1180 | |
| 1181 EntryImpl* BackendImpl::OpenPrevEntryImpl(void** iter) { | |
| 1182 return OpenFollowingEntry(false, iter); | |
| 1183 } | 1041 } |
| 1184 | 1042 |
| 1185 // ------------------------------------------------------------------------ | 1043 // ------------------------------------------------------------------------ |
| 1186 | 1044 |
| 1187 // We just created a new file so we're going to write the header and set the | 1045 // We just created a new file so we're going to write the header and set the |
| 1188 // file length to include the hash table (zero filled). | 1046 // file length to include the hash table (zero filled). |
| 1189 bool BackendImpl::CreateBackingStore(disk_cache::File* file) { | 1047 bool BackendImpl::CreateBackingStore(disk_cache::File* file) { |
| 1190 AdjustMaxCacheSize(0); | 1048 AdjustMaxCacheSize(0); |
| 1191 | 1049 |
| 1192 IndexHeader header; | 1050 IndexHeader header; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1286 DCHECK(!open_entries_.size()); | 1144 DCHECK(!open_entries_.size()); |
| 1287 PrepareForRestart(); | 1145 PrepareForRestart(); |
| 1288 DelayedCacheCleanup(path_); | 1146 DelayedCacheCleanup(path_); |
| 1289 | 1147 |
| 1290 int64 errors = stats_.GetCounter(Stats::FATAL_ERROR); | 1148 int64 errors = stats_.GetCounter(Stats::FATAL_ERROR); |
| 1291 | 1149 |
| 1292 // Don't call Init() if directed by the unit test: we are simulating a failure | 1150 // Don't call Init() if directed by the unit test: we are simulating a failure |
| 1293 // trying to re-enable the cache. | 1151 // trying to re-enable the cache. |
| 1294 if (unit_test_) | 1152 if (unit_test_) |
| 1295 init_ = true; // Let the destructor do proper cleanup. | 1153 init_ = true; // Let the destructor do proper cleanup. |
| 1296 else if (SyncInit()) | 1154 else if (Init()) |
| 1297 stats_.SetCounter(Stats::FATAL_ERROR, errors + 1); | 1155 stats_.SetCounter(Stats::FATAL_ERROR, errors + 1); |
| 1298 } | 1156 } |
| 1299 | 1157 |
| 1300 void BackendImpl::PrepareForRestart() { | 1158 void BackendImpl::PrepareForRestart() { |
| 1301 // Reset the mask_ if it was not given by the user. | 1159 // Reset the mask_ if it was not given by the user. |
| 1302 if (!(user_flags_ & kMask)) | 1160 if (!(user_flags_ & kMask)) |
| 1303 mask_ = 0; | 1161 mask_ = 0; |
| 1304 | 1162 |
| 1305 if (!(user_flags_ & kNewEviction)) | 1163 if (!(user_flags_ & kNewEviction)) |
| 1306 new_eviction_ = false; | 1164 new_eviction_ = false; |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1436 parent_entry = NULL; | 1294 parent_entry = NULL; |
| 1437 | 1295 |
| 1438 if (cache_entry && (find_parent || !found)) | 1296 if (cache_entry && (find_parent || !found)) |
| 1439 cache_entry = NULL; | 1297 cache_entry = NULL; |
| 1440 | 1298 |
| 1441 find_parent ? parent_entry.swap(&tmp) : cache_entry.swap(&tmp); | 1299 find_parent ? parent_entry.swap(&tmp) : cache_entry.swap(&tmp); |
| 1442 return tmp; | 1300 return tmp; |
| 1443 } | 1301 } |
| 1444 | 1302 |
| 1445 // This is the actual implementation for OpenNextEntry and OpenPrevEntry. | 1303 // This is the actual implementation for OpenNextEntry and OpenPrevEntry. |
| 1446 EntryImpl* BackendImpl::OpenFollowingEntry(bool forward, void** iter) { | 1304 bool BackendImpl::OpenFollowingEntry(bool forward, void** iter, |
| 1305 Entry** next_entry) { |
| 1447 if (disabled_) | 1306 if (disabled_) |
| 1448 return NULL; | 1307 return false; |
| 1449 | 1308 |
| 1450 DCHECK(iter); | 1309 DCHECK(iter); |
| 1310 DCHECK(next_entry); |
| 1311 *next_entry = NULL; |
| 1451 | 1312 |
| 1452 const int kListsToSearch = 3; | 1313 const int kListsToSearch = 3; |
| 1453 scoped_refptr<EntryImpl> entries[kListsToSearch]; | 1314 scoped_refptr<EntryImpl> entries[kListsToSearch]; |
| 1454 scoped_ptr<Rankings::Iterator> iterator( | 1315 scoped_ptr<Rankings::Iterator> iterator( |
| 1455 reinterpret_cast<Rankings::Iterator*>(*iter)); | 1316 reinterpret_cast<Rankings::Iterator*>(*iter)); |
| 1456 *iter = NULL; | 1317 *iter = NULL; |
| 1457 | 1318 |
| 1458 if (!iterator.get()) { | 1319 if (!iterator.get()) { |
| 1459 iterator.reset(new Rankings::Iterator(&rankings_)); | 1320 iterator.reset(new Rankings::Iterator(&rankings_)); |
| 1460 bool ret = false; | 1321 bool ret = false; |
| 1461 | 1322 |
| 1462 // Get an entry from each list. | 1323 // Get an entry from each list. |
| 1463 for (int i = 0; i < kListsToSearch; i++) { | 1324 for (int i = 0; i < kListsToSearch; i++) { |
| 1464 EntryImpl* temp = NULL; | 1325 EntryImpl* temp = NULL; |
| 1465 ret |= OpenFollowingEntryFromList(forward, static_cast<Rankings::List>(i), | 1326 ret |= OpenFollowingEntryFromList(forward, static_cast<Rankings::List>(i), |
| 1466 &iterator->nodes[i], &temp); | 1327 &iterator->nodes[i], &temp); |
| 1467 entries[i].swap(&temp); // The entry was already addref'd. | 1328 entries[i].swap(&temp); // The entry was already addref'd. |
| 1468 } | 1329 } |
| 1469 if (!ret) | 1330 if (!ret) |
| 1470 return NULL; | 1331 return false; |
| 1471 } else { | 1332 } else { |
| 1472 // Get the next entry from the last list, and the actual entries for the | 1333 // Get the next entry from the last list, and the actual entries for the |
| 1473 // elements on the other lists. | 1334 // elements on the other lists. |
| 1474 for (int i = 0; i < kListsToSearch; i++) { | 1335 for (int i = 0; i < kListsToSearch; i++) { |
| 1475 EntryImpl* temp = NULL; | 1336 EntryImpl* temp = NULL; |
| 1476 if (iterator->list == i) { | 1337 if (iterator->list == i) { |
| 1477 OpenFollowingEntryFromList(forward, iterator->list, | 1338 OpenFollowingEntryFromList(forward, iterator->list, |
| 1478 &iterator->nodes[i], &temp); | 1339 &iterator->nodes[i], &temp); |
| 1479 } else { | 1340 } else { |
| 1480 temp = GetEnumeratedEntry(iterator->nodes[i], false); | 1341 temp = GetEnumeratedEntry(iterator->nodes[i], false); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1496 continue; | 1357 continue; |
| 1497 } | 1358 } |
| 1498 if (access_times[i] > access_times[newest]) | 1359 if (access_times[i] > access_times[newest]) |
| 1499 newest = i; | 1360 newest = i; |
| 1500 if (access_times[i] < access_times[oldest]) | 1361 if (access_times[i] < access_times[oldest]) |
| 1501 oldest = i; | 1362 oldest = i; |
| 1502 } | 1363 } |
| 1503 } | 1364 } |
| 1504 | 1365 |
| 1505 if (newest < 0 || oldest < 0) | 1366 if (newest < 0 || oldest < 0) |
| 1506 return NULL; | 1367 return false; |
| 1507 | 1368 |
| 1508 EntryImpl* next_entry; | |
| 1509 if (forward) { | 1369 if (forward) { |
| 1510 next_entry = entries[newest].release(); | 1370 entries[newest].swap(reinterpret_cast<EntryImpl**>(next_entry)); |
| 1511 iterator->list = static_cast<Rankings::List>(newest); | 1371 iterator->list = static_cast<Rankings::List>(newest); |
| 1512 } else { | 1372 } else { |
| 1513 next_entry = entries[oldest].release(); | 1373 entries[oldest].swap(reinterpret_cast<EntryImpl**>(next_entry)); |
| 1514 iterator->list = static_cast<Rankings::List>(oldest); | 1374 iterator->list = static_cast<Rankings::List>(oldest); |
| 1515 } | 1375 } |
| 1516 | 1376 |
| 1517 *iter = iterator.release(); | 1377 *iter = iterator.release(); |
| 1518 return next_entry; | 1378 return true; |
| 1519 } | 1379 } |
| 1520 | 1380 |
| 1521 bool BackendImpl::OpenFollowingEntryFromList(bool forward, Rankings::List list, | 1381 bool BackendImpl::OpenFollowingEntryFromList(bool forward, Rankings::List list, |
| 1522 CacheRankingsBlock** from_entry, | 1382 CacheRankingsBlock** from_entry, |
| 1523 EntryImpl** next_entry) { | 1383 EntryImpl** next_entry) { |
| 1524 if (disabled_) | 1384 if (disabled_) |
| 1525 return false; | 1385 return false; |
| 1526 | 1386 |
| 1527 if (!new_eviction_ && Rankings::NO_USE != list) | 1387 if (!new_eviction_ && Rankings::NO_USE != list) |
| 1528 return false; | 1388 return false; |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1849 | 1709 |
| 1850 return num_dirty; | 1710 return num_dirty; |
| 1851 } | 1711 } |
| 1852 | 1712 |
| 1853 bool BackendImpl::CheckEntry(EntryImpl* cache_entry) { | 1713 bool BackendImpl::CheckEntry(EntryImpl* cache_entry) { |
| 1854 RankingsNode* rankings = cache_entry->rankings()->Data(); | 1714 RankingsNode* rankings = cache_entry->rankings()->Data(); |
| 1855 return !rankings->dummy; | 1715 return !rankings->dummy; |
| 1856 } | 1716 } |
| 1857 | 1717 |
| 1858 } // namespace disk_cache | 1718 } // namespace disk_cache |
| OLD | NEW |