OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/sparse_control.h" | 5 #include "net/disk_cache/sparse_control.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 } | 133 } |
134 std::string child_name = GenerateChildName(name_, signature_, child_id); | 134 std::string child_name = GenerateChildName(name_, signature_, child_id); |
135 backend_->SyncDoomEntry(child_name); | 135 backend_->SyncDoomEntry(child_name); |
136 children_map_.Set(child_id, false); | 136 children_map_.Set(child_id, false); |
137 | 137 |
138 // Post a task to delete the next child. | 138 // Post a task to delete the next child. |
139 base::MessageLoop::current()->PostTask( | 139 base::MessageLoop::current()->PostTask( |
140 FROM_HERE, base::Bind(&ChildrenDeleter::DeleteChildren, this)); | 140 FROM_HERE, base::Bind(&ChildrenDeleter::DeleteChildren, this)); |
141 } | 141 } |
142 | 142 |
| 143 // ----------------------------------------------------------------------- |
| 144 |
143 // Returns the NetLog event type corresponding to a SparseOperation. | 145 // Returns the NetLog event type corresponding to a SparseOperation. |
144 net::NetLog::EventType GetSparseEventType( | 146 net::NetLog::EventType GetSparseEventType( |
145 disk_cache::SparseControl::SparseOperation operation) { | 147 disk_cache::SparseControl::SparseOperation operation) { |
146 switch (operation) { | 148 switch (operation) { |
147 case disk_cache::SparseControl::kReadOperation: | 149 case disk_cache::SparseControl::kReadOperation: |
148 return net::NetLog::TYPE_SPARSE_READ; | 150 return net::NetLog::TYPE_SPARSE_READ; |
149 case disk_cache::SparseControl::kWriteOperation: | 151 case disk_cache::SparseControl::kWriteOperation: |
150 return net::NetLog::TYPE_SPARSE_WRITE; | 152 return net::NetLog::TYPE_SPARSE_WRITE; |
151 case disk_cache::SparseControl::kGetRangeOperation: | 153 case disk_cache::SparseControl::kGetRangeOperation: |
152 return net::NetLog::TYPE_SPARSE_GET_RANGE; | 154 return net::NetLog::TYPE_SPARSE_GET_RANGE; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 memset(&child_data_, 0, sizeof(child_data_)); | 205 memset(&child_data_, 0, sizeof(child_data_)); |
204 } | 206 } |
205 | 207 |
206 SparseControl::~SparseControl() { | 208 SparseControl::~SparseControl() { |
207 if (child_) | 209 if (child_) |
208 CloseChild(); | 210 CloseChild(); |
209 if (init_) | 211 if (init_) |
210 WriteSparseData(); | 212 WriteSparseData(); |
211 } | 213 } |
212 | 214 |
213 int SparseControl::Init() { | |
214 DCHECK(!init_); | |
215 | |
216 // We should not have sparse data for the exposed entry. | |
217 if (entry_->GetDataSize(kSparseData)) | |
218 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | |
219 | |
220 // Now see if there is something where we store our data. | |
221 int rv = net::OK; | |
222 int data_len = entry_->GetDataSize(kSparseIndex); | |
223 if (!data_len) { | |
224 rv = CreateSparseEntry(); | |
225 } else { | |
226 rv = OpenSparseEntry(data_len); | |
227 } | |
228 | |
229 if (rv == net::OK) | |
230 init_ = true; | |
231 return rv; | |
232 } | |
233 | |
234 bool SparseControl::CouldBeSparse() const { | 215 bool SparseControl::CouldBeSparse() const { |
235 DCHECK(!init_); | 216 DCHECK(!init_); |
236 | 217 |
237 if (entry_->GetDataSize(kSparseData)) | 218 if (entry_->GetDataSize(kSparseData)) |
238 return false; | 219 return false; |
239 | 220 |
240 // We don't verify the data, just see if it could be there. | 221 // We don't verify the data, just see if it could be there. |
241 return (entry_->GetDataSize(kSparseIndex) != 0); | 222 return (entry_->GetDataSize(kSparseIndex) != 0); |
242 } | 223 } |
243 | 224 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 base::MessageLoop::current()->PostTask( | 341 base::MessageLoop::current()->PostTask( |
361 FROM_HERE, | 342 FROM_HERE, |
362 base::Bind(&ChildrenDeleter::Start, deleter, buffer, data_len)); | 343 base::Bind(&ChildrenDeleter::Start, deleter, buffer, data_len)); |
363 } else { | 344 } else { |
364 base::MessageLoop::current()->PostTask( | 345 base::MessageLoop::current()->PostTask( |
365 FROM_HERE, | 346 FROM_HERE, |
366 base::Bind(&ChildrenDeleter::ReadData, deleter, address, data_len)); | 347 base::Bind(&ChildrenDeleter::ReadData, deleter, address, data_len)); |
367 } | 348 } |
368 } | 349 } |
369 | 350 |
| 351 // ----------------------------------------------------------------------- |
| 352 |
| 353 int SparseControl::Init() { |
| 354 DCHECK(!init_); |
| 355 |
| 356 // We should not have sparse data for the exposed entry. |
| 357 if (entry_->GetDataSize(kSparseData)) |
| 358 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 359 |
| 360 // Now see if there is something where we store our data. |
| 361 int rv = net::OK; |
| 362 int data_len = entry_->GetDataSize(kSparseIndex); |
| 363 if (!data_len) { |
| 364 rv = CreateSparseEntry(); |
| 365 } else { |
| 366 rv = OpenSparseEntry(data_len); |
| 367 } |
| 368 |
| 369 if (rv == net::OK) |
| 370 init_ = true; |
| 371 return rv; |
| 372 } |
| 373 |
370 // We are going to start using this entry to store sparse data, so we have to | 374 // We are going to start using this entry to store sparse data, so we have to |
371 // initialize our control info. | 375 // initialize our control info. |
372 int SparseControl::CreateSparseEntry() { | 376 int SparseControl::CreateSparseEntry() { |
373 if (CHILD_ENTRY & entry_->GetEntryFlags()) | 377 if (CHILD_ENTRY & entry_->GetEntryFlags()) |
374 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 378 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
375 | 379 |
376 memset(&sparse_header_, 0, sizeof(sparse_header_)); | 380 memset(&sparse_header_, 0, sizeof(sparse_header_)); |
377 sparse_header_.signature = Time::Now().ToInternalValue(); | 381 sparse_header_.signature = Time::Now().ToInternalValue(); |
378 sparse_header_.magic = kIndexMagic; | 382 sparse_header_.magic = kIndexMagic; |
379 sparse_header_.parent_key_len = entry_->GetKey().size(); | 383 sparse_header_.parent_key_len = entry_->GetKey().size(); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 // Save the allocation bitmap before closing the child entry. | 502 // Save the allocation bitmap before closing the child entry. |
499 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), | 503 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), |
500 CompletionCallback(), | 504 CompletionCallback(), |
501 false); | 505 false); |
502 if (rv != sizeof(child_data_)) | 506 if (rv != sizeof(child_data_)) |
503 DLOG(ERROR) << "Failed to save child data"; | 507 DLOG(ERROR) << "Failed to save child data"; |
504 child_->Release(); | 508 child_->Release(); |
505 child_ = NULL; | 509 child_ = NULL; |
506 } | 510 } |
507 | 511 |
508 std::string SparseControl::GenerateChildKey() { | |
509 return GenerateChildName(entry_->GetKey(), sparse_header_.signature, | |
510 offset_ >> 20); | |
511 } | |
512 | |
513 // We are deleting the child because something went wrong. | |
514 bool SparseControl::KillChildAndContinue(const std::string& key, bool fatal) { | |
515 SetChildBit(false); | |
516 child_->DoomImpl(); | |
517 child_->Release(); | |
518 child_ = NULL; | |
519 if (fatal) { | |
520 result_ = net::ERR_CACHE_READ_FAILURE; | |
521 return false; | |
522 } | |
523 return ContinueWithoutChild(key); | |
524 } | |
525 | |
526 // We were not able to open this child; see what we can do. | 512 // We were not able to open this child; see what we can do. |
527 bool SparseControl::ContinueWithoutChild(const std::string& key) { | 513 bool SparseControl::ContinueWithoutChild(const std::string& key) { |
528 if (kReadOperation == operation_) | 514 if (kReadOperation == operation_) |
529 return false; | 515 return false; |
530 if (kGetRangeOperation == operation_) | 516 if (kGetRangeOperation == operation_) |
531 return true; | 517 return true; |
532 | 518 |
533 if (!entry_->backend_) | 519 if (!entry_->backend_) |
534 return false; | 520 return false; |
535 | 521 |
536 child_ = entry_->backend_->CreateEntryImpl(key); | 522 child_ = entry_->backend_->CreateEntryImpl(key); |
537 if (!child_) { | 523 if (!child_) { |
538 child_ = NULL; | 524 child_ = NULL; |
539 result_ = net::ERR_CACHE_READ_FAILURE; | 525 result_ = net::ERR_CACHE_READ_FAILURE; |
540 return false; | 526 return false; |
541 } | 527 } |
542 // Write signature. | 528 // Write signature. |
543 InitChildData(); | 529 InitChildData(); |
544 return true; | 530 return true; |
545 } | 531 } |
546 | 532 |
| 533 void SparseControl::WriteSparseData() { |
| 534 scoped_refptr<net::IOBuffer> buf(new net::WrappedIOBuffer( |
| 535 reinterpret_cast<const char*>(children_map_.GetMap()))); |
| 536 |
| 537 int len = children_map_.ArraySize() * 4; |
| 538 int rv = entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf, len, |
| 539 CompletionCallback(), false); |
| 540 if (rv != len) { |
| 541 DLOG(ERROR) << "Unable to save sparse map"; |
| 542 } |
| 543 } |
| 544 |
| 545 bool SparseControl::DoChildIO() { |
| 546 finished_ = true; |
| 547 if (!buf_len_ || result_ < 0) |
| 548 return false; |
| 549 |
| 550 if (!OpenChild()) |
| 551 return false; |
| 552 |
| 553 if (!VerifyRange()) |
| 554 return false; |
| 555 |
| 556 // We have more work to do. Let's not trigger a callback to the caller. |
| 557 finished_ = false; |
| 558 CompletionCallback callback; |
| 559 if (!user_callback_.is_null()) { |
| 560 callback = |
| 561 base::Bind(&SparseControl::OnChildIOCompleted, base::Unretained(this)); |
| 562 } |
| 563 |
| 564 int rv = 0; |
| 565 switch (operation_) { |
| 566 case kReadOperation: |
| 567 if (entry_->net_log().IsLoggingAllEvents()) { |
| 568 entry_->net_log().BeginEvent( |
| 569 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, |
| 570 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), |
| 571 child_len_)); |
| 572 } |
| 573 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_, |
| 574 child_len_, callback); |
| 575 break; |
| 576 case kWriteOperation: |
| 577 if (entry_->net_log().IsLoggingAllEvents()) { |
| 578 entry_->net_log().BeginEvent( |
| 579 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, |
| 580 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), |
| 581 child_len_)); |
| 582 } |
| 583 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_, |
| 584 child_len_, callback, false); |
| 585 break; |
| 586 case kGetRangeOperation: |
| 587 rv = DoGetAvailableRange(); |
| 588 break; |
| 589 default: |
| 590 NOTREACHED(); |
| 591 } |
| 592 |
| 593 if (rv == net::ERR_IO_PENDING) { |
| 594 if (!pending_) { |
| 595 pending_ = true; |
| 596 // The child will protect himself against closing the entry while IO is in |
| 597 // progress. However, this entry can still be closed, and that would not |
| 598 // be a good thing for us, so we increase the refcount until we're |
| 599 // finished doing sparse stuff. |
| 600 entry_->AddRef(); // Balanced in DoUserCallback. |
| 601 } |
| 602 return false; |
| 603 } |
| 604 if (!rv) |
| 605 return false; |
| 606 |
| 607 DoChildIOCompleted(rv); |
| 608 return true; |
| 609 } |
| 610 |
| 611 void SparseControl::DoChildIOCompleted(int result) { |
| 612 LogChildOperationEnd(entry_->net_log(), operation_, result); |
| 613 if (result < 0) { |
| 614 // We fail the whole operation if we encounter an error. |
| 615 result_ = result; |
| 616 return; |
| 617 } |
| 618 |
| 619 UpdateRange(result); |
| 620 |
| 621 result_ += result; |
| 622 offset_ += result; |
| 623 buf_len_ -= result; |
| 624 |
| 625 // We'll be reusing the user provided buffer for the next chunk. |
| 626 if (buf_len_ && user_buf_) |
| 627 user_buf_->DidConsume(result); |
| 628 } |
| 629 |
| 630 std::string SparseControl::GenerateChildKey() { |
| 631 return GenerateChildName(entry_->GetKey(), sparse_header_.signature, |
| 632 offset_ >> 20); |
| 633 } |
| 634 |
| 635 // We are deleting the child because something went wrong. |
| 636 bool SparseControl::KillChildAndContinue(const std::string& key, bool fatal) { |
| 637 SetChildBit(false); |
| 638 child_->DoomImpl(); |
| 639 child_->Release(); |
| 640 child_ = NULL; |
| 641 if (fatal) { |
| 642 result_ = net::ERR_CACHE_READ_FAILURE; |
| 643 return false; |
| 644 } |
| 645 return ContinueWithoutChild(key); |
| 646 } |
| 647 |
547 bool SparseControl::ChildPresent() { | 648 bool SparseControl::ChildPresent() { |
548 int child_bit = static_cast<int>(offset_ >> 20); | 649 int child_bit = static_cast<int>(offset_ >> 20); |
549 if (children_map_.Size() <= child_bit) | 650 if (children_map_.Size() <= child_bit) |
550 return false; | 651 return false; |
551 | 652 |
552 return children_map_.Get(child_bit); | 653 return children_map_.Get(child_bit); |
553 } | 654 } |
554 | 655 |
555 void SparseControl::SetChildBit(bool value) { | 656 void SparseControl::SetChildBit(bool value) { |
556 int child_bit = static_cast<int>(offset_ >> 20); | 657 int child_bit = static_cast<int>(offset_ >> 20); |
557 | 658 |
558 // We may have to increase the bitmap of child entries. | 659 // We may have to increase the bitmap of child entries. |
559 if (children_map_.Size() <= child_bit) | 660 if (children_map_.Size() <= child_bit) |
560 children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true); | 661 children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true); |
561 | 662 |
562 children_map_.Set(child_bit, value); | 663 children_map_.Set(child_bit, value); |
563 } | 664 } |
564 | 665 |
565 void SparseControl::WriteSparseData() { | |
566 scoped_refptr<net::IOBuffer> buf(new net::WrappedIOBuffer( | |
567 reinterpret_cast<const char*>(children_map_.GetMap()))); | |
568 | |
569 int len = children_map_.ArraySize() * 4; | |
570 int rv = entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf, len, | |
571 CompletionCallback(), false); | |
572 if (rv != len) { | |
573 DLOG(ERROR) << "Unable to save sparse map"; | |
574 } | |
575 } | |
576 | |
577 bool SparseControl::VerifyRange() { | 666 bool SparseControl::VerifyRange() { |
578 DCHECK_GE(result_, 0); | 667 DCHECK_GE(result_, 0); |
579 | 668 |
580 child_offset_ = static_cast<int>(offset_) & (kMaxEntrySize - 1); | 669 child_offset_ = static_cast<int>(offset_) & (kMaxEntrySize - 1); |
581 child_len_ = std::min(buf_len_, kMaxEntrySize - child_offset_); | 670 child_len_ = std::min(buf_len_, kMaxEntrySize - child_offset_); |
582 | 671 |
583 // We can write to (or get info from) anywhere in this child. | 672 // We can write to (or get info from) anywhere in this child. |
584 if (operation_ != kReadOperation) | 673 if (operation_ != kReadOperation) |
585 return true; | 674 return true; |
586 | 675 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 scoped_refptr<net::WrappedIOBuffer> buf( | 760 scoped_refptr<net::WrappedIOBuffer> buf( |
672 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); | 761 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
673 | 762 |
674 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), | 763 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), |
675 CompletionCallback(), false); | 764 CompletionCallback(), false); |
676 if (rv != sizeof(child_data_)) | 765 if (rv != sizeof(child_data_)) |
677 DLOG(ERROR) << "Failed to save child data"; | 766 DLOG(ERROR) << "Failed to save child data"; |
678 SetChildBit(true); | 767 SetChildBit(true); |
679 } | 768 } |
680 | 769 |
681 void SparseControl::DoChildrenIO() { | |
682 while (DoChildIO()) continue; | |
683 | |
684 // Range operations are finished synchronously, often without setting | |
685 // |finished_| to true. | |
686 if (kGetRangeOperation == operation_ && | |
687 entry_->net_log().IsLoggingAllEvents()) { | |
688 entry_->net_log().EndEvent( | |
689 net::NetLog::TYPE_SPARSE_GET_RANGE, | |
690 CreateNetLogGetAvailableRangeResultCallback(offset_, result_)); | |
691 } | |
692 if (finished_) { | |
693 if (kGetRangeOperation != operation_ && | |
694 entry_->net_log().IsLoggingAllEvents()) { | |
695 entry_->net_log().EndEvent(GetSparseEventType(operation_)); | |
696 } | |
697 if (pending_) | |
698 DoUserCallback(); // Don't touch this object after this point. | |
699 } | |
700 } | |
701 | |
702 bool SparseControl::DoChildIO() { | |
703 finished_ = true; | |
704 if (!buf_len_ || result_ < 0) | |
705 return false; | |
706 | |
707 if (!OpenChild()) | |
708 return false; | |
709 | |
710 if (!VerifyRange()) | |
711 return false; | |
712 | |
713 // We have more work to do. Let's not trigger a callback to the caller. | |
714 finished_ = false; | |
715 CompletionCallback callback; | |
716 if (!user_callback_.is_null()) { | |
717 callback = | |
718 base::Bind(&SparseControl::OnChildIOCompleted, base::Unretained(this)); | |
719 } | |
720 | |
721 int rv = 0; | |
722 switch (operation_) { | |
723 case kReadOperation: | |
724 if (entry_->net_log().IsLoggingAllEvents()) { | |
725 entry_->net_log().BeginEvent( | |
726 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, | |
727 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), | |
728 child_len_)); | |
729 } | |
730 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_, | |
731 child_len_, callback); | |
732 break; | |
733 case kWriteOperation: | |
734 if (entry_->net_log().IsLoggingAllEvents()) { | |
735 entry_->net_log().BeginEvent( | |
736 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, | |
737 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), | |
738 child_len_)); | |
739 } | |
740 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_, | |
741 child_len_, callback, false); | |
742 break; | |
743 case kGetRangeOperation: | |
744 rv = DoGetAvailableRange(); | |
745 break; | |
746 default: | |
747 NOTREACHED(); | |
748 } | |
749 | |
750 if (rv == net::ERR_IO_PENDING) { | |
751 if (!pending_) { | |
752 pending_ = true; | |
753 // The child will protect himself against closing the entry while IO is in | |
754 // progress. However, this entry can still be closed, and that would not | |
755 // be a good thing for us, so we increase the refcount until we're | |
756 // finished doing sparse stuff. | |
757 entry_->AddRef(); // Balanced in DoUserCallback. | |
758 } | |
759 return false; | |
760 } | |
761 if (!rv) | |
762 return false; | |
763 | |
764 DoChildIOCompleted(rv); | |
765 return true; | |
766 } | |
767 | |
768 int SparseControl::DoGetAvailableRange() { | 770 int SparseControl::DoGetAvailableRange() { |
769 if (!child_) | 771 if (!child_) |
770 return child_len_; // Move on to the next child. | 772 return child_len_; // Move on to the next child. |
771 | 773 |
772 // Check that there are no holes in this range. | 774 // Check that there are no holes in this range. |
773 int last_bit = (child_offset_ + child_len_ + 1023) >> 10; | 775 int last_bit = (child_offset_ + child_len_ + 1023) >> 10; |
774 int start = child_offset_ >> 10; | 776 int start = child_offset_ >> 10; |
775 int partial_start_bytes = PartialBlockLength(start); | 777 int partial_start_bytes = PartialBlockLength(start); |
776 int found = start; | 778 int found = start; |
777 int bits_found = child_map_.FindBits(&found, last_bit, true); | 779 int bits_found = child_map_.FindBits(&found, last_bit, true); |
(...skipping 27 matching lines...) Expand all Loading... |
805 | 807 |
806 // Only update offset_ when this query found zeros at the start. | 808 // Only update offset_ when this query found zeros at the start. |
807 if (empty_start) | 809 if (empty_start) |
808 offset_ += empty_start; | 810 offset_ += empty_start; |
809 | 811 |
810 // This will actually break the loop. | 812 // This will actually break the loop. |
811 buf_len_ = 0; | 813 buf_len_ = 0; |
812 return 0; | 814 return 0; |
813 } | 815 } |
814 | 816 |
815 void SparseControl::DoChildIOCompleted(int result) { | 817 void SparseControl::DoUserCallback() { |
816 LogChildOperationEnd(entry_->net_log(), operation_, result); | 818 DCHECK(!user_callback_.is_null()); |
817 if (result < 0) { | 819 CompletionCallback cb = user_callback_; |
818 // We fail the whole operation if we encounter an error. | 820 user_callback_.Reset(); |
819 result_ = result; | 821 user_buf_ = NULL; |
820 return; | 822 pending_ = false; |
| 823 operation_ = kNoOperation; |
| 824 int rv = result_; |
| 825 entry_->Release(); // Don't touch object after this line. |
| 826 cb.Run(rv); |
| 827 } |
| 828 |
| 829 void SparseControl::DoAbortCallbacks() { |
| 830 for (size_t i = 0; i < abort_callbacks_.size(); i++) { |
| 831 // Releasing all references to entry_ may result in the destruction of this |
| 832 // object so we should not be touching it after the last Release(). |
| 833 CompletionCallback cb = abort_callbacks_[i]; |
| 834 if (i == abort_callbacks_.size() - 1) |
| 835 abort_callbacks_.clear(); |
| 836 |
| 837 entry_->Release(); // Don't touch object after this line. |
| 838 cb.Run(net::OK); |
821 } | 839 } |
822 | |
823 UpdateRange(result); | |
824 | |
825 result_ += result; | |
826 offset_ += result; | |
827 buf_len_ -= result; | |
828 | |
829 // We'll be reusing the user provided buffer for the next chunk. | |
830 if (buf_len_ && user_buf_) | |
831 user_buf_->DidConsume(result); | |
832 } | 840 } |
833 | 841 |
834 void SparseControl::OnChildIOCompleted(int result) { | 842 void SparseControl::OnChildIOCompleted(int result) { |
835 DCHECK_NE(net::ERR_IO_PENDING, result); | 843 DCHECK_NE(net::ERR_IO_PENDING, result); |
836 DoChildIOCompleted(result); | 844 DoChildIOCompleted(result); |
837 | 845 |
838 if (abort_) { | 846 if (abort_) { |
839 // We'll return the current result of the operation, which may be less than | 847 // We'll return the current result of the operation, which may be less than |
840 // the bytes to read or write, but the user cancelled the operation. | 848 // the bytes to read or write, but the user cancelled the operation. |
841 abort_ = false; | 849 abort_ = false; |
842 if (entry_->net_log().IsLoggingAllEvents()) { | 850 if (entry_->net_log().IsLoggingAllEvents()) { |
843 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED); | 851 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED); |
844 entry_->net_log().EndEvent(GetSparseEventType(operation_)); | 852 entry_->net_log().EndEvent(GetSparseEventType(operation_)); |
845 } | 853 } |
846 // We have an indirect reference to this object for every callback so if | 854 // We have an indirect reference to this object for every callback so if |
847 // there is only one callback, we may delete this object before reaching | 855 // there is only one callback, we may delete this object before reaching |
848 // DoAbortCallbacks. | 856 // DoAbortCallbacks. |
849 bool has_abort_callbacks = !abort_callbacks_.empty(); | 857 bool has_abort_callbacks = !abort_callbacks_.empty(); |
850 DoUserCallback(); | 858 DoUserCallback(); |
851 if (has_abort_callbacks) | 859 if (has_abort_callbacks) |
852 DoAbortCallbacks(); | 860 DoAbortCallbacks(); |
853 return; | 861 return; |
854 } | 862 } |
855 | 863 |
856 // We are running a callback from the message loop. It's time to restart what | 864 // We are running a callback from the message loop. It's time to restart what |
857 // we were doing before. | 865 // we were doing before. |
858 DoChildrenIO(); | 866 DoChildrenIO(); |
859 } | 867 } |
860 | 868 |
861 void SparseControl::DoUserCallback() { | |
862 DCHECK(!user_callback_.is_null()); | |
863 CompletionCallback cb = user_callback_; | |
864 user_callback_.Reset(); | |
865 user_buf_ = NULL; | |
866 pending_ = false; | |
867 operation_ = kNoOperation; | |
868 int rv = result_; | |
869 entry_->Release(); // Don't touch object after this line. | |
870 cb.Run(rv); | |
871 } | |
872 | |
873 void SparseControl::DoAbortCallbacks() { | |
874 for (size_t i = 0; i < abort_callbacks_.size(); i++) { | |
875 // Releasing all references to entry_ may result in the destruction of this | |
876 // object so we should not be touching it after the last Release(). | |
877 CompletionCallback cb = abort_callbacks_[i]; | |
878 if (i == abort_callbacks_.size() - 1) | |
879 abort_callbacks_.clear(); | |
880 | |
881 entry_->Release(); // Don't touch object after this line. | |
882 cb.Run(net::OK); | |
883 } | |
884 } | |
885 | |
886 } // namespace disk_cache | 869 } // namespace disk_cache |
OLD | NEW |