OLD | NEW |
---|---|
1 // Copyright (c) 2009-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009-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/sparse_control.h" | 5 #include "net/disk_cache/sparse_control.h" |
6 | 6 |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 offset_ = offset; | 216 offset_ = offset; |
217 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; | 217 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; |
218 buf_len_ = buf_len; | 218 buf_len_ = buf_len; |
219 user_callback_ = callback; | 219 user_callback_ = callback; |
220 | 220 |
221 result_ = 0; | 221 result_ = 0; |
222 pending_ = false; | 222 pending_ = false; |
223 finished_ = false; | 223 finished_ = false; |
224 abort_ = false; | 224 abort_ = false; |
225 | 225 |
226 entry_->net_log().BeginEvent(net::NetLog::TYPE_SPARSE_CONTROL, NULL); | |
226 DoChildrenIO(); | 227 DoChildrenIO(); |
227 | 228 |
228 if (!pending_) { | 229 if (!pending_) { |
229 // Everything was done synchronously. | 230 // Everything was done synchronously. |
230 operation_ = kNoOperation; | 231 operation_ = kNoOperation; |
231 user_buf_ = NULL; | 232 user_buf_ = NULL; |
232 user_callback_ = NULL; | 233 user_callback_ = NULL; |
233 return result_; | 234 return result_; |
234 } | 235 } |
235 | 236 |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
603 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), | 604 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), |
604 NULL, false); | 605 NULL, false); |
605 if (rv != sizeof(child_data_)) | 606 if (rv != sizeof(child_data_)) |
606 DLOG(ERROR) << "Failed to save child data"; | 607 DLOG(ERROR) << "Failed to save child data"; |
607 SetChildBit(true); | 608 SetChildBit(true); |
608 } | 609 } |
609 | 610 |
610 void SparseControl::DoChildrenIO() { | 611 void SparseControl::DoChildrenIO() { |
611 while (DoChildIO()) continue; | 612 while (DoChildIO()) continue; |
612 | 613 |
613 if (pending_ && finished_) | 614 if (finished_) { |
614 DoUserCallback(); | 615 entry_->net_log().EndEvent(net::NetLog::TYPE_SPARSE_CONTROL, NULL); |
616 if (pending_) | |
617 DoUserCallback(); | |
618 } | |
615 } | 619 } |
616 | 620 |
617 bool SparseControl::DoChildIO() { | 621 bool SparseControl::DoChildIO() { |
618 finished_ = true; | 622 finished_ = true; |
619 if (!buf_len_ || result_ < 0) | 623 if (!buf_len_ || result_ < 0) |
620 return false; | 624 return false; |
621 | 625 |
622 if (!OpenChild()) | 626 if (!OpenChild()) |
623 return false; | 627 return false; |
624 | 628 |
625 if (!VerifyRange()) | 629 if (!VerifyRange()) |
626 return false; | 630 return false; |
627 | 631 |
628 // We have more work to do. Let's not trigger a callback to the caller. | 632 // We have more work to do. Let's not trigger a callback to the caller. |
629 finished_ = false; | 633 finished_ = false; |
630 net::CompletionCallback* callback = user_callback_ ? &child_callback_ : NULL; | 634 net::CompletionCallback* callback = user_callback_ ? &child_callback_ : NULL; |
631 | 635 |
632 int rv = 0; | 636 int rv = 0; |
633 switch (operation_) { | 637 switch (operation_) { |
634 case kReadOperation: | 638 case kReadOperation: |
639 if (entry_->net_log().IsLoggingAllEvents()) { | |
640 entry_->net_log().BeginEvent( | |
641 net::NetLog::TYPE_SPARSE_CONTROL_READ, | |
642 make_scoped_refptr(new net::NetLogSourceParameter( | |
643 "source_dependency", | |
644 child_->net_log().source()))); | |
645 } | |
635 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_, | 646 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_, |
636 child_len_, callback); | 647 child_len_, callback); |
637 break; | 648 break; |
638 case kWriteOperation: | 649 case kWriteOperation: |
650 if (entry_->net_log().IsLoggingAllEvents()) { | |
651 entry_->net_log().BeginEvent( | |
652 net::NetLog::TYPE_SPARSE_CONTROL_WRITE, | |
653 make_scoped_refptr(new net::NetLogSourceParameter( | |
654 "source_dependency", | |
655 child_->net_log().source()))); | |
656 } | |
639 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_, | 657 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_, |
640 child_len_, callback, false); | 658 child_len_, callback, false); |
641 break; | 659 break; |
642 case kGetRangeOperation: | 660 case kGetRangeOperation: |
661 if (entry_->net_log().IsLoggingAllEvents()) { | |
662 entry_->net_log().BeginEvent(net::NetLog::TYPE_SPARSE_CONTROL_GET_RANGE, | |
663 NULL); | |
664 } | |
643 rv = DoGetAvailableRange(); | 665 rv = DoGetAvailableRange(); |
644 break; | 666 break; |
645 default: | 667 default: |
646 NOTREACHED(); | 668 NOTREACHED(); |
647 } | 669 } |
648 | 670 |
649 if (rv == net::ERR_IO_PENDING) { | 671 if (rv == net::ERR_IO_PENDING) { |
650 if (!pending_) { | 672 if (!pending_) { |
651 pending_ = true; | 673 pending_ = true; |
652 // The child will protect himself against closing the entry while IO is in | 674 // The child will protect himself against closing the entry while IO is in |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
705 // Only update offset_ when this query found zeros at the start. | 727 // Only update offset_ when this query found zeros at the start. |
706 if (empty_start) | 728 if (empty_start) |
707 offset_ += empty_start; | 729 offset_ += empty_start; |
708 | 730 |
709 // This will actually break the loop. | 731 // This will actually break the loop. |
710 buf_len_ = 0; | 732 buf_len_ = 0; |
711 return 0; | 733 return 0; |
712 } | 734 } |
713 | 735 |
714 void SparseControl::DoChildIOCompleted(int result) { | 736 void SparseControl::DoChildIOCompleted(int result) { |
737 if (entry_->net_log().IsLoggingAllEvents()) { | |
738 net::NetLog::EventType event_type; | |
739 switch (operation_) { | |
eroman
2011/01/05 02:39:06
I suggest moving this to a helper function.
mmenke
2011/01/05 16:32:25
Done.
| |
740 case kReadOperation: | |
741 event_type = net::NetLog::TYPE_SPARSE_CONTROL_READ; | |
742 break; | |
743 case kWriteOperation: | |
744 event_type = net::NetLog::TYPE_SPARSE_CONTROL_WRITE; | |
745 break; | |
746 case kGetRangeOperation: | |
747 event_type = net::NetLog::TYPE_SPARSE_CONTROL_GET_RANGE; | |
748 break; | |
749 default: | |
750 NOTREACHED(); | |
751 } | |
752 entry_->net_log().EndEventWithErrorCode(event_type, result); | |
753 } | |
715 if (result < 0) { | 754 if (result < 0) { |
716 // We fail the whole operation if we encounter an error. | 755 // We fail the whole operation if we encounter an error. |
717 result_ = result; | 756 result_ = result; |
718 return; | 757 return; |
719 } | 758 } |
720 | 759 |
721 UpdateRange(result); | 760 UpdateRange(result); |
722 | 761 |
723 result_ += result; | 762 result_ += result; |
724 offset_ += result; | 763 offset_ += result; |
725 buf_len_ -= result; | 764 buf_len_ -= result; |
726 | 765 |
727 // We'll be reusing the user provided buffer for the next chunk. | 766 // We'll be reusing the user provided buffer for the next chunk. |
728 if (buf_len_ && user_buf_) | 767 if (buf_len_ && user_buf_) |
729 user_buf_->DidConsume(result); | 768 user_buf_->DidConsume(result); |
730 } | 769 } |
731 | 770 |
732 void SparseControl::OnChildIOCompleted(int result) { | 771 void SparseControl::OnChildIOCompleted(int result) { |
733 DCHECK_NE(net::ERR_IO_PENDING, result); | 772 DCHECK_NE(net::ERR_IO_PENDING, result); |
734 DoChildIOCompleted(result); | 773 DoChildIOCompleted(result); |
735 | 774 |
736 if (abort_) { | 775 if (abort_) { |
737 // We'll return the current result of the operation, which may be less than | 776 // We'll return the current result of the operation, which may be less than |
738 // the bytes to read or write, but the user cancelled the operation. | 777 // the bytes to read or write, but the user cancelled the operation. |
739 abort_ = false; | 778 abort_ = false; |
779 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED, NULL); | |
780 entry_->net_log().EndEvent(net::NetLog::TYPE_SPARSE_CONTROL, NULL); | |
740 DoUserCallback(); | 781 DoUserCallback(); |
741 return DoAbortCallbacks(); | 782 return DoAbortCallbacks(); |
742 } | 783 } |
743 | 784 |
744 // We are running a callback from the message loop. It's time to restart what | 785 // We are running a callback from the message loop. It's time to restart what |
745 // we were doing before. | 786 // we were doing before. |
746 DoChildrenIO(); | 787 DoChildrenIO(); |
747 } | 788 } |
748 | 789 |
749 void SparseControl::DoUserCallback() { | 790 void SparseControl::DoUserCallback() { |
(...skipping 14 matching lines...) Expand all Loading... | |
764 net::CompletionCallback* c = abort_callbacks_[i]; | 805 net::CompletionCallback* c = abort_callbacks_[i]; |
765 if (i == abort_callbacks_.size() - 1) | 806 if (i == abort_callbacks_.size() - 1) |
766 abort_callbacks_.clear(); | 807 abort_callbacks_.clear(); |
767 | 808 |
768 entry_->Release(); // Don't touch object after this line. | 809 entry_->Release(); // Don't touch object after this line. |
769 c->Run(net::OK); | 810 c->Run(net::OK); |
770 } | 811 } |
771 } | 812 } |
772 | 813 |
773 } // namespace disk_cache | 814 } // namespace disk_cache |
OLD | NEW |