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