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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 } | 131 } |
132 std::string child_name = GenerateChildName(name_, signature_, child_id); | 132 std::string child_name = GenerateChildName(name_, signature_, child_id); |
133 backend_->SyncDoomEntry(child_name); | 133 backend_->SyncDoomEntry(child_name); |
134 children_map_.Set(child_id, false); | 134 children_map_.Set(child_id, false); |
135 | 135 |
136 // Post a task to delete the next child. | 136 // Post a task to delete the next child. |
137 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 137 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
138 this, &ChildrenDeleter::DeleteChildren)); | 138 this, &ChildrenDeleter::DeleteChildren)); |
139 } | 139 } |
140 | 140 |
141 // Logs the end event for |operation|, if all events are being logged. | |
142 void LogOperationEnd(const net::BoundNetLog& net_log, | |
143 disk_cache::SparseControl::SparseOperation operation, | |
144 int result) { | |
145 if (net_log.IsLoggingAllEvents()) { | |
146 net::NetLog::EventType event_type; | |
147 switch (operation) { | |
148 case disk_cache::SparseControl::kReadOperation: | |
149 event_type = net::NetLog::TYPE_SPARSE_CONTROL_READ; | |
150 break; | |
151 case disk_cache::SparseControl::kWriteOperation: | |
152 event_type = net::NetLog::TYPE_SPARSE_CONTROL_WRITE; | |
153 break; | |
154 case disk_cache::SparseControl::kGetRangeOperation: | |
155 event_type = net::NetLog::TYPE_SPARSE_CONTROL_GET_RANGE; | |
156 break; | |
157 default: | |
158 NOTREACHED(); | |
159 } | |
160 net_log.EndEventWithNetErrorCode(event_type, result); | |
161 } | |
162 } | |
163 | |
164 } // namespace. | 141 } // namespace. |
165 | 142 |
166 namespace disk_cache { | 143 namespace disk_cache { |
167 | 144 |
168 SparseControl::SparseControl(EntryImpl* entry) | 145 SparseControl::SparseControl(EntryImpl* entry) |
169 : entry_(entry), | 146 : entry_(entry), |
170 child_(NULL), | 147 child_(NULL), |
171 operation_(kNoOperation), | 148 operation_(kNoOperation), |
172 init_(false), | 149 init_(false), |
173 child_map_(child_data_.bitmap, kNumSparseBits, kNumSparseBits / 32), | 150 child_map_(child_data_.bitmap, kNumSparseBits, kNumSparseBits / 32), |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 offset_ = offset; | 216 offset_ = offset; |
240 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; | 217 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; |
241 buf_len_ = buf_len; | 218 buf_len_ = buf_len; |
242 user_callback_ = callback; | 219 user_callback_ = callback; |
243 | 220 |
244 result_ = 0; | 221 result_ = 0; |
245 pending_ = false; | 222 pending_ = false; |
246 finished_ = false; | 223 finished_ = false; |
247 abort_ = false; | 224 abort_ = false; |
248 | 225 |
249 entry_->net_log().BeginEvent(net::NetLog::TYPE_SPARSE_CONTROL, NULL); | |
250 DoChildrenIO(); | 226 DoChildrenIO(); |
251 | 227 |
252 if (!pending_) { | 228 if (!pending_) { |
253 // Everything was done synchronously. | 229 // Everything was done synchronously. |
254 operation_ = kNoOperation; | 230 operation_ = kNoOperation; |
255 user_buf_ = NULL; | 231 user_buf_ = NULL; |
256 user_callback_ = NULL; | 232 user_callback_ = NULL; |
257 return result_; | 233 return result_; |
258 } | 234 } |
259 | 235 |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), | 603 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), |
628 NULL, false); | 604 NULL, false); |
629 if (rv != sizeof(child_data_)) | 605 if (rv != sizeof(child_data_)) |
630 DLOG(ERROR) << "Failed to save child data"; | 606 DLOG(ERROR) << "Failed to save child data"; |
631 SetChildBit(true); | 607 SetChildBit(true); |
632 } | 608 } |
633 | 609 |
634 void SparseControl::DoChildrenIO() { | 610 void SparseControl::DoChildrenIO() { |
635 while (DoChildIO()) continue; | 611 while (DoChildIO()) continue; |
636 | 612 |
637 if (finished_) { | 613 if (pending_ && finished_) |
638 entry_->net_log().EndEvent(net::NetLog::TYPE_SPARSE_CONTROL, NULL); | 614 DoUserCallback(); |
639 if (pending_) | |
640 DoUserCallback(); | |
641 } | |
642 } | 615 } |
643 | 616 |
644 bool SparseControl::DoChildIO() { | 617 bool SparseControl::DoChildIO() { |
645 finished_ = true; | 618 finished_ = true; |
646 if (!buf_len_ || result_ < 0) | 619 if (!buf_len_ || result_ < 0) |
647 return false; | 620 return false; |
648 | 621 |
649 if (!OpenChild()) | 622 if (!OpenChild()) |
650 return false; | 623 return false; |
651 | 624 |
652 if (!VerifyRange()) | 625 if (!VerifyRange()) |
653 return false; | 626 return false; |
654 | 627 |
655 // We have more work to do. Let's not trigger a callback to the caller. | 628 // We have more work to do. Let's not trigger a callback to the caller. |
656 finished_ = false; | 629 finished_ = false; |
657 net::CompletionCallback* callback = user_callback_ ? &child_callback_ : NULL; | 630 net::CompletionCallback* callback = user_callback_ ? &child_callback_ : NULL; |
658 | 631 |
659 int rv = 0; | 632 int rv = 0; |
660 switch (operation_) { | 633 switch (operation_) { |
661 case kReadOperation: | 634 case kReadOperation: |
662 if (entry_->net_log().IsLoggingAllEvents()) { | |
663 entry_->net_log().BeginEvent( | |
664 net::NetLog::TYPE_SPARSE_CONTROL_READ, | |
665 make_scoped_refptr(new net::NetLogSourceParameter( | |
666 "source_dependency", | |
667 child_->net_log().source()))); | |
668 } | |
669 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_, | 635 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_, |
670 child_len_, callback); | 636 child_len_, callback); |
671 break; | 637 break; |
672 case kWriteOperation: | 638 case kWriteOperation: |
673 if (entry_->net_log().IsLoggingAllEvents()) { | |
674 entry_->net_log().BeginEvent( | |
675 net::NetLog::TYPE_SPARSE_CONTROL_WRITE, | |
676 make_scoped_refptr(new net::NetLogSourceParameter( | |
677 "source_dependency", | |
678 child_->net_log().source()))); | |
679 } | |
680 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_, | 639 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_, |
681 child_len_, callback, false); | 640 child_len_, callback, false); |
682 break; | 641 break; |
683 case kGetRangeOperation: | 642 case kGetRangeOperation: |
684 if (entry_->net_log().IsLoggingAllEvents()) { | |
685 entry_->net_log().BeginEvent(net::NetLog::TYPE_SPARSE_CONTROL_GET_RANGE, | |
686 NULL); | |
687 } | |
688 rv = DoGetAvailableRange(); | 643 rv = DoGetAvailableRange(); |
689 break; | 644 break; |
690 default: | 645 default: |
691 NOTREACHED(); | 646 NOTREACHED(); |
692 } | 647 } |
693 | 648 |
694 if (rv == net::ERR_IO_PENDING) { | 649 if (rv == net::ERR_IO_PENDING) { |
695 if (!pending_) { | 650 if (!pending_) { |
696 pending_ = true; | 651 pending_ = true; |
697 // The child will protect himself against closing the entry while IO is in | 652 // 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... |
750 // Only update offset_ when this query found zeros at the start. | 705 // Only update offset_ when this query found zeros at the start. |
751 if (empty_start) | 706 if (empty_start) |
752 offset_ += empty_start; | 707 offset_ += empty_start; |
753 | 708 |
754 // This will actually break the loop. | 709 // This will actually break the loop. |
755 buf_len_ = 0; | 710 buf_len_ = 0; |
756 return 0; | 711 return 0; |
757 } | 712 } |
758 | 713 |
759 void SparseControl::DoChildIOCompleted(int result) { | 714 void SparseControl::DoChildIOCompleted(int result) { |
760 LogOperationEnd(entry_->net_log(), operation_, result); | |
761 if (result < 0) { | 715 if (result < 0) { |
762 // We fail the whole operation if we encounter an error. | 716 // We fail the whole operation if we encounter an error. |
763 result_ = result; | 717 result_ = result; |
764 return; | 718 return; |
765 } | 719 } |
766 | 720 |
767 UpdateRange(result); | 721 UpdateRange(result); |
768 | 722 |
769 result_ += result; | 723 result_ += result; |
770 offset_ += result; | 724 offset_ += result; |
771 buf_len_ -= result; | 725 buf_len_ -= result; |
772 | 726 |
773 // We'll be reusing the user provided buffer for the next chunk. | 727 // We'll be reusing the user provided buffer for the next chunk. |
774 if (buf_len_ && user_buf_) | 728 if (buf_len_ && user_buf_) |
775 user_buf_->DidConsume(result); | 729 user_buf_->DidConsume(result); |
776 } | 730 } |
777 | 731 |
778 void SparseControl::OnChildIOCompleted(int result) { | 732 void SparseControl::OnChildIOCompleted(int result) { |
779 DCHECK_NE(net::ERR_IO_PENDING, result); | 733 DCHECK_NE(net::ERR_IO_PENDING, result); |
780 DoChildIOCompleted(result); | 734 DoChildIOCompleted(result); |
781 | 735 |
782 if (abort_) { | 736 if (abort_) { |
783 // We'll return the current result of the operation, which may be less than | 737 // We'll return the current result of the operation, which may be less than |
784 // the bytes to read or write, but the user cancelled the operation. | 738 // the bytes to read or write, but the user cancelled the operation. |
785 abort_ = false; | 739 abort_ = false; |
786 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED, NULL); | |
787 entry_->net_log().EndEvent(net::NetLog::TYPE_SPARSE_CONTROL, NULL); | |
788 DoUserCallback(); | 740 DoUserCallback(); |
789 return DoAbortCallbacks(); | 741 return DoAbortCallbacks(); |
790 } | 742 } |
791 | 743 |
792 // We are running a callback from the message loop. It's time to restart what | 744 // We are running a callback from the message loop. It's time to restart what |
793 // we were doing before. | 745 // we were doing before. |
794 DoChildrenIO(); | 746 DoChildrenIO(); |
795 } | 747 } |
796 | 748 |
797 void SparseControl::DoUserCallback() { | 749 void SparseControl::DoUserCallback() { |
(...skipping 14 matching lines...) Expand all Loading... |
812 net::CompletionCallback* c = abort_callbacks_[i]; | 764 net::CompletionCallback* c = abort_callbacks_[i]; |
813 if (i == abort_callbacks_.size() - 1) | 765 if (i == abort_callbacks_.size() - 1) |
814 abort_callbacks_.clear(); | 766 abort_callbacks_.clear(); |
815 | 767 |
816 entry_->Release(); // Don't touch object after this line. | 768 entry_->Release(); // Don't touch object after this line. |
817 c->Run(net::OK); | 769 c->Run(net::OK); |
818 } | 770 } |
819 } | 771 } |
820 | 772 |
821 } // namespace disk_cache | 773 } // namespace disk_cache |
OLD | NEW |