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 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 int max_bytes, net::CacheType type, | 327 int max_bytes, net::CacheType type, |
328 uint32 flags, base::MessageLoopProxy* thread, | 328 uint32 flags, base::MessageLoopProxy* thread, |
329 Backend** backend, | 329 Backend** backend, |
330 CompletionCallback* callback) { | 330 CompletionCallback* callback) { |
331 CacheCreator* creator = new CacheCreator(full_path, force, max_bytes, type, | 331 CacheCreator* creator = new CacheCreator(full_path, force, max_bytes, type, |
332 flags, thread, backend, callback); | 332 flags, thread, backend, callback); |
333 // This object will self-destroy when finished. | 333 // This object will self-destroy when finished. |
334 return creator->Run(); | 334 return creator->Run(); |
335 } | 335 } |
336 | 336 |
| 337 int BackendImpl::Init(CompletionCallback* callback) { |
| 338 background_queue_.Init(callback); |
| 339 return net::ERR_IO_PENDING; |
| 340 } |
| 341 |
| 342 BackendImpl::~BackendImpl() { |
| 343 background_queue_.WaitForPendingIO(); |
| 344 |
| 345 if (background_queue_.BackgroundIsCurrentThread()) { |
| 346 // Unit tests may use the same thread for everything. |
| 347 CleanupCache(); |
| 348 } else { |
| 349 background_queue_.background_thread()->PostTask(FROM_HERE, |
| 350 new FinalCleanup(this)); |
| 351 done_.Wait(); |
| 352 } |
| 353 } |
| 354 |
| 355 // ------------------------------------------------------------------------ |
| 356 |
| 357 int32 BackendImpl::GetEntryCount() const { |
| 358 if (!index_) |
| 359 return 0; |
| 360 // num_entries includes entries already evicted. |
| 361 int32 not_deleted = data_->header.num_entries - |
| 362 data_->header.lru.sizes[Rankings::DELETED]; |
| 363 |
| 364 if (not_deleted < 0) { |
| 365 NOTREACHED(); |
| 366 not_deleted = 0; |
| 367 } |
| 368 |
| 369 return not_deleted; |
| 370 } |
| 371 |
| 372 int BackendImpl::OpenEntry(const std::string& key, Entry** entry, |
| 373 CompletionCallback* callback) { |
| 374 DCHECK(callback); |
| 375 background_queue_.OpenEntry(key, entry, callback); |
| 376 return net::ERR_IO_PENDING; |
| 377 } |
| 378 |
| 379 int BackendImpl::CreateEntry(const std::string& key, Entry** entry, |
| 380 CompletionCallback* callback) { |
| 381 DCHECK(callback); |
| 382 background_queue_.CreateEntry(key, entry, callback); |
| 383 return net::ERR_IO_PENDING; |
| 384 } |
| 385 |
| 386 int BackendImpl::DoomEntry(const std::string& key, |
| 387 CompletionCallback* callback) { |
| 388 DCHECK(callback); |
| 389 background_queue_.DoomEntry(key, callback); |
| 390 return net::ERR_IO_PENDING; |
| 391 } |
| 392 |
| 393 int BackendImpl::DoomAllEntries(CompletionCallback* callback) { |
| 394 DCHECK(callback); |
| 395 background_queue_.DoomAllEntries(callback); |
| 396 return net::ERR_IO_PENDING; |
| 397 } |
| 398 |
| 399 int BackendImpl::DoomEntriesBetween(const base::Time initial_time, |
| 400 const base::Time end_time, |
| 401 CompletionCallback* callback) { |
| 402 DCHECK(callback); |
| 403 background_queue_.DoomEntriesBetween(initial_time, end_time, callback); |
| 404 return net::ERR_IO_PENDING; |
| 405 } |
| 406 |
| 407 int BackendImpl::DoomEntriesSince(const base::Time initial_time, |
| 408 CompletionCallback* callback) { |
| 409 DCHECK(callback); |
| 410 background_queue_.DoomEntriesSince(initial_time, callback); |
| 411 return net::ERR_IO_PENDING; |
| 412 } |
| 413 |
| 414 int BackendImpl::OpenNextEntry(void** iter, Entry** next_entry, |
| 415 CompletionCallback* callback) { |
| 416 DCHECK(callback); |
| 417 background_queue_.OpenNextEntry(iter, next_entry, callback); |
| 418 return net::ERR_IO_PENDING; |
| 419 } |
| 420 |
| 421 void BackendImpl::EndEnumeration(void** iter) { |
| 422 background_queue_.EndEnumeration(*iter); |
| 423 *iter = NULL; |
| 424 } |
| 425 |
| 426 void BackendImpl::GetStats(StatsItems* stats) { |
| 427 if (disabled_) |
| 428 return; |
| 429 |
| 430 std::pair<std::string, std::string> item; |
| 431 |
| 432 item.first = "Entries"; |
| 433 item.second = StringPrintf("%d", data_->header.num_entries); |
| 434 stats->push_back(item); |
| 435 |
| 436 item.first = "Pending IO"; |
| 437 item.second = StringPrintf("%d", num_pending_io_); |
| 438 stats->push_back(item); |
| 439 |
| 440 item.first = "Max size"; |
| 441 item.second = StringPrintf("%d", max_size_); |
| 442 stats->push_back(item); |
| 443 |
| 444 item.first = "Current size"; |
| 445 item.second = StringPrintf("%d", data_->header.num_bytes); |
| 446 stats->push_back(item); |
| 447 |
| 448 stats_.GetItems(stats); |
| 449 } |
| 450 |
| 451 // ------------------------------------------------------------------------ |
| 452 |
337 int BackendImpl::SyncInit() { | 453 int BackendImpl::SyncInit() { |
338 DCHECK(!init_); | 454 DCHECK(!init_); |
339 if (init_) | 455 if (init_) |
340 return net::ERR_FAILED; | 456 return net::ERR_FAILED; |
341 | 457 |
342 bool create_files = false; | 458 bool create_files = false; |
343 if (!InitBackingStore(&create_files)) { | 459 if (!InitBackingStore(&create_files)) { |
344 ReportError(ERR_STORAGE_ERROR); | 460 ReportError(ERR_STORAGE_ERROR); |
345 return net::ERR_FAILED; | 461 return net::ERR_FAILED; |
346 } | 462 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 disabled_ = !rankings_.Init(this, new_eviction_); | 518 disabled_ = !rankings_.Init(this, new_eviction_); |
403 eviction_.Init(this); | 519 eviction_.Init(this); |
404 | 520 |
405 // Setup load-time data only for the main cache. | 521 // Setup load-time data only for the main cache. |
406 if (cache_type() == net::DISK_CACHE) | 522 if (cache_type() == net::DISK_CACHE) |
407 SetFieldTrialInfo(GetSizeGroup()); | 523 SetFieldTrialInfo(GetSizeGroup()); |
408 | 524 |
409 return disabled_ ? net::ERR_FAILED : net::OK; | 525 return disabled_ ? net::ERR_FAILED : net::OK; |
410 } | 526 } |
411 | 527 |
412 int BackendImpl::Init(CompletionCallback* callback) { | 528 void BackendImpl::CleanupCache() { |
413 background_queue_.Init(callback); | 529 Trace("Backend Cleanup"); |
414 return net::ERR_IO_PENDING; | 530 if (init_) { |
415 } | 531 if (data_) |
| 532 data_->header.crash = 0; |
416 | 533 |
417 BackendImpl::~BackendImpl() { | 534 timer_.Stop(); |
418 background_queue_.WaitForPendingIO(); | 535 File::WaitForPendingIO(&num_pending_io_); |
419 | 536 DCHECK(!num_refs_); |
420 if (background_queue_.BackgroundIsCurrentThread()) { | |
421 // Unit tests may use the same thread for everything. | |
422 CleanupCache(); | |
423 } else { | |
424 background_queue_.background_thread()->PostTask(FROM_HERE, | |
425 new FinalCleanup(this)); | |
426 done_.Wait(); | |
427 } | 537 } |
| 538 factory_.RevokeAll(); |
| 539 done_.Signal(); |
428 } | 540 } |
429 | 541 |
430 // ------------------------------------------------------------------------ | 542 // ------------------------------------------------------------------------ |
431 | 543 |
432 int32 BackendImpl::GetEntryCount() const { | 544 int BackendImpl::OpenPrevEntry(void** iter, Entry** prev_entry, |
433 if (!index_) | 545 CompletionCallback* callback) { |
434 return 0; | |
435 // num_entries includes entries already evicted. | |
436 int32 not_deleted = data_->header.num_entries - | |
437 data_->header.lru.sizes[Rankings::DELETED]; | |
438 | |
439 if (not_deleted < 0) { | |
440 NOTREACHED(); | |
441 not_deleted = 0; | |
442 } | |
443 | |
444 return not_deleted; | |
445 } | |
446 | |
447 int BackendImpl::OpenEntry(const std::string& key, Entry** entry, | |
448 CompletionCallback* callback) { | |
449 DCHECK(callback); | 546 DCHECK(callback); |
450 background_queue_.OpenEntry(key, entry, callback); | 547 background_queue_.OpenPrevEntry(iter, prev_entry, callback); |
451 return net::ERR_IO_PENDING; | 548 return net::ERR_IO_PENDING; |
452 } | 549 } |
453 | 550 |
454 int BackendImpl::CreateEntry(const std::string& key, Entry** entry, | 551 int BackendImpl::SyncOpenEntry(const std::string& key, Entry** entry) { |
455 CompletionCallback* callback) { | 552 DCHECK(entry); |
456 DCHECK(callback); | 553 *entry = OpenEntryImpl(key); |
457 background_queue_.CreateEntry(key, entry, callback); | 554 return (*entry) ? net::OK : net::ERR_FAILED; |
458 return net::ERR_IO_PENDING; | 555 } |
| 556 |
| 557 int BackendImpl::SyncCreateEntry(const std::string& key, Entry** entry) { |
| 558 DCHECK(entry); |
| 559 *entry = CreateEntryImpl(key); |
| 560 return (*entry) ? net::OK : net::ERR_FAILED; |
459 } | 561 } |
460 | 562 |
461 int BackendImpl::SyncDoomEntry(const std::string& key) { | 563 int BackendImpl::SyncDoomEntry(const std::string& key) { |
462 if (disabled_) | 564 if (disabled_) |
463 return net::ERR_FAILED; | 565 return net::ERR_FAILED; |
464 | 566 |
465 EntryImpl* entry = OpenEntryImpl(key); | 567 EntryImpl* entry = OpenEntryImpl(key); |
466 if (!entry) | 568 if (!entry) |
467 return net::ERR_FAILED; | 569 return net::ERR_FAILED; |
468 | 570 |
469 entry->DoomImpl(); | 571 entry->DoomImpl(); |
470 entry->Release(); | 572 entry->Release(); |
471 return net::OK; | 573 return net::OK; |
472 } | 574 } |
473 | 575 |
474 int BackendImpl::DoomEntry(const std::string& key, | |
475 CompletionCallback* callback) { | |
476 DCHECK(callback); | |
477 background_queue_.DoomEntry(key, callback); | |
478 return net::ERR_IO_PENDING; | |
479 } | |
480 | |
481 int BackendImpl::SyncDoomAllEntries() { | 576 int BackendImpl::SyncDoomAllEntries() { |
482 if (!num_refs_) { | 577 if (!num_refs_) { |
483 PrepareForRestart(); | 578 PrepareForRestart(); |
484 DeleteCache(path_, false); | 579 DeleteCache(path_, false); |
485 return SyncInit(); | 580 return SyncInit(); |
486 } else { | 581 } else { |
487 if (disabled_) | 582 if (disabled_) |
488 return net::ERR_FAILED; | 583 return net::ERR_FAILED; |
489 | 584 |
490 eviction_.TrimCache(true); | 585 eviction_.TrimCache(true); |
491 stats_.OnEvent(Stats::DOOM_CACHE); | 586 stats_.OnEvent(Stats::DOOM_CACHE); |
492 return net::OK; | 587 return net::OK; |
493 } | 588 } |
494 } | 589 } |
495 | 590 |
496 int BackendImpl::DoomAllEntries(CompletionCallback* callback) { | |
497 DCHECK(callback); | |
498 background_queue_.DoomAllEntries(callback); | |
499 return net::ERR_IO_PENDING; | |
500 } | |
501 | |
502 int BackendImpl::SyncDoomEntriesBetween(const base::Time initial_time, | 591 int BackendImpl::SyncDoomEntriesBetween(const base::Time initial_time, |
503 const base::Time end_time) { | 592 const base::Time end_time) { |
504 if (end_time.is_null()) | 593 if (end_time.is_null()) |
505 return SyncDoomEntriesSince(initial_time); | 594 return SyncDoomEntriesSince(initial_time); |
506 | 595 |
507 DCHECK(end_time >= initial_time); | 596 DCHECK(end_time >= initial_time); |
508 | 597 |
509 if (disabled_) | 598 if (disabled_) |
510 return net::ERR_FAILED; | 599 return net::ERR_FAILED; |
511 | 600 |
(...skipping 16 matching lines...) Expand all Loading... |
528 next = NULL; | 617 next = NULL; |
529 SyncEndEnumeration(iter); | 618 SyncEndEnumeration(iter); |
530 } | 619 } |
531 | 620 |
532 node->Release(); | 621 node->Release(); |
533 } | 622 } |
534 | 623 |
535 return net::OK; | 624 return net::OK; |
536 } | 625 } |
537 | 626 |
538 int BackendImpl::DoomEntriesBetween(const base::Time initial_time, | |
539 const base::Time end_time, | |
540 CompletionCallback* callback) { | |
541 DCHECK(callback); | |
542 background_queue_.DoomEntriesBetween(initial_time, end_time, callback); | |
543 return net::ERR_IO_PENDING; | |
544 } | |
545 | |
546 // We use OpenNextEntryImpl to retrieve elements from the cache, until we get | 627 // We use OpenNextEntryImpl to retrieve elements from the cache, until we get |
547 // entries that are too old. | 628 // entries that are too old. |
548 int BackendImpl::SyncDoomEntriesSince(const base::Time initial_time) { | 629 int BackendImpl::SyncDoomEntriesSince(const base::Time initial_time) { |
549 if (disabled_) | 630 if (disabled_) |
550 return net::ERR_FAILED; | 631 return net::ERR_FAILED; |
551 | 632 |
552 for (;;) { | 633 for (;;) { |
553 void* iter = NULL; | 634 void* iter = NULL; |
554 EntryImpl* entry = OpenNextEntryImpl(&iter); | 635 EntryImpl* entry = OpenNextEntryImpl(&iter); |
555 if (!entry) | 636 if (!entry) |
556 return net::OK; | 637 return net::OK; |
557 | 638 |
558 if (initial_time > entry->GetLastUsed()) { | 639 if (initial_time > entry->GetLastUsed()) { |
559 entry->Release(); | 640 entry->Release(); |
560 SyncEndEnumeration(iter); | 641 SyncEndEnumeration(iter); |
561 return net::OK; | 642 return net::OK; |
562 } | 643 } |
563 | 644 |
564 entry->DoomImpl(); | 645 entry->DoomImpl(); |
565 entry->Release(); | 646 entry->Release(); |
566 SyncEndEnumeration(iter); // Dooming the entry invalidates the iterator. | 647 SyncEndEnumeration(iter); // Dooming the entry invalidates the iterator. |
567 } | 648 } |
568 } | 649 } |
569 | 650 |
570 int BackendImpl::DoomEntriesSince(const base::Time initial_time, | |
571 CompletionCallback* callback) { | |
572 DCHECK(callback); | |
573 background_queue_.DoomEntriesSince(initial_time, callback); | |
574 return net::ERR_IO_PENDING; | |
575 } | |
576 | |
577 int BackendImpl::OpenNextEntry(void** iter, Entry** next_entry, | |
578 CompletionCallback* callback) { | |
579 DCHECK(callback); | |
580 background_queue_.OpenNextEntry(iter, next_entry, callback); | |
581 return net::ERR_IO_PENDING; | |
582 } | |
583 | |
584 void BackendImpl::EndEnumeration(void** iter) { | |
585 background_queue_.EndEnumeration(*iter); | |
586 *iter = NULL; | |
587 } | |
588 | |
589 void BackendImpl::GetStats(StatsItems* stats) { | |
590 if (disabled_) | |
591 return; | |
592 | |
593 std::pair<std::string, std::string> item; | |
594 | |
595 item.first = "Entries"; | |
596 item.second = StringPrintf("%d", data_->header.num_entries); | |
597 stats->push_back(item); | |
598 | |
599 item.first = "Pending IO"; | |
600 item.second = StringPrintf("%d", num_pending_io_); | |
601 stats->push_back(item); | |
602 | |
603 item.first = "Max size"; | |
604 item.second = StringPrintf("%d", max_size_); | |
605 stats->push_back(item); | |
606 | |
607 item.first = "Current size"; | |
608 item.second = StringPrintf("%d", data_->header.num_bytes); | |
609 stats->push_back(item); | |
610 | |
611 stats_.GetItems(stats); | |
612 } | |
613 | |
614 // ------------------------------------------------------------------------ | |
615 | |
616 void BackendImpl::CleanupCache() { | |
617 Trace("Backend Cleanup"); | |
618 if (init_) { | |
619 if (data_) | |
620 data_->header.crash = 0; | |
621 | |
622 timer_.Stop(); | |
623 File::WaitForPendingIO(&num_pending_io_); | |
624 DCHECK(!num_refs_); | |
625 } | |
626 factory_.RevokeAll(); | |
627 done_.Signal(); | |
628 } | |
629 | |
630 // ------------------------------------------------------------------------ | |
631 | |
632 int BackendImpl::OpenPrevEntry(void** iter, Entry** prev_entry, | |
633 CompletionCallback* callback) { | |
634 DCHECK(callback); | |
635 background_queue_.OpenPrevEntry(iter, prev_entry, callback); | |
636 return net::ERR_IO_PENDING; | |
637 } | |
638 | |
639 int BackendImpl::SyncOpenEntry(const std::string& key, Entry** entry) { | |
640 DCHECK(entry); | |
641 *entry = OpenEntryImpl(key); | |
642 return (*entry) ? net::OK : net::ERR_FAILED; | |
643 } | |
644 | |
645 int BackendImpl::SyncCreateEntry(const std::string& key, Entry** entry) { | |
646 DCHECK(entry); | |
647 *entry = CreateEntryImpl(key); | |
648 return (*entry) ? net::OK : net::ERR_FAILED; | |
649 } | |
650 | |
651 int BackendImpl::SyncOpenNextEntry(void** iter, Entry** next_entry) { | 651 int BackendImpl::SyncOpenNextEntry(void** iter, Entry** next_entry) { |
652 *next_entry = OpenNextEntryImpl(iter); | 652 *next_entry = OpenNextEntryImpl(iter); |
653 return (*next_entry) ? net::OK : net::ERR_FAILED; | 653 return (*next_entry) ? net::OK : net::ERR_FAILED; |
654 } | 654 } |
655 | 655 |
656 int BackendImpl::SyncOpenPrevEntry(void** iter, Entry** prev_entry) { | 656 int BackendImpl::SyncOpenPrevEntry(void** iter, Entry** prev_entry) { |
657 *prev_entry = OpenPrevEntryImpl(iter); | 657 *prev_entry = OpenPrevEntryImpl(iter); |
658 return (*prev_entry) ? net::OK : net::ERR_FAILED; | 658 return (*prev_entry) ? net::OK : net::ERR_FAILED; |
659 } | 659 } |
660 | 660 |
(...skipping 1195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1856 | 1856 |
1857 return num_dirty; | 1857 return num_dirty; |
1858 } | 1858 } |
1859 | 1859 |
1860 bool BackendImpl::CheckEntry(EntryImpl* cache_entry) { | 1860 bool BackendImpl::CheckEntry(EntryImpl* cache_entry) { |
1861 RankingsNode* rankings = cache_entry->rankings()->Data(); | 1861 RankingsNode* rankings = cache_entry->rankings()->Data(); |
1862 return !rankings->dummy; | 1862 return !rankings->dummy; |
1863 } | 1863 } |
1864 | 1864 |
1865 } // namespace disk_cache | 1865 } // namespace disk_cache |
OLD | NEW |