| 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 new net::NetLogSourceParameter("source_dependency", |
| 643 child_->net_log().source())); |
| 644 } |
| 635 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_, | 645 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_, |
| 636 child_len_, callback); | 646 child_len_, callback); |
| 637 break; | 647 break; |
| 638 case kWriteOperation: | 648 case kWriteOperation: |
| 649 if (entry_->net_log().IsLoggingAllEvents()) { |
| 650 entry_->net_log().BeginEvent( |
| 651 net::NetLog::TYPE_SPARSE_CONTROL_WRITE, |
| 652 new net::NetLogSourceParameter("source_dependency", |
| 653 child_->net_log().source())); |
| 654 } |
| 639 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_, | 655 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_, |
| 640 child_len_, callback, false); | 656 child_len_, callback, false); |
| 641 break; | 657 break; |
| 642 case kGetRangeOperation: | 658 case kGetRangeOperation: |
| 659 if (entry_->net_log().IsLoggingAllEvents()) { |
| 660 entry_->net_log().BeginEvent(net::NetLog::TYPE_SPARSE_CONTROL_GET_RANGE, |
| 661 NULL); |
| 662 } |
| 643 rv = DoGetAvailableRange(); | 663 rv = DoGetAvailableRange(); |
| 644 break; | 664 break; |
| 645 default: | 665 default: |
| 646 NOTREACHED(); | 666 NOTREACHED(); |
| 647 } | 667 } |
| 648 | 668 |
| 649 if (rv == net::ERR_IO_PENDING) { | 669 if (rv == net::ERR_IO_PENDING) { |
| 650 if (!pending_) { | 670 if (!pending_) { |
| 651 pending_ = true; | 671 pending_ = true; |
| 652 // The child will protect himself against closing the entry while IO is in | 672 // 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. | 725 // Only update offset_ when this query found zeros at the start. |
| 706 if (empty_start) | 726 if (empty_start) |
| 707 offset_ += empty_start; | 727 offset_ += empty_start; |
| 708 | 728 |
| 709 // This will actually break the loop. | 729 // This will actually break the loop. |
| 710 buf_len_ = 0; | 730 buf_len_ = 0; |
| 711 return 0; | 731 return 0; |
| 712 } | 732 } |
| 713 | 733 |
| 714 void SparseControl::DoChildIOCompleted(int result) { | 734 void SparseControl::DoChildIOCompleted(int result) { |
| 735 if (entry_->net_log().IsLoggingAllEvents()) { |
| 736 net::NetLog::EventType event_type; |
| 737 switch (operation_) { |
| 738 case kReadOperation: |
| 739 event_type = net::NetLog::TYPE_SPARSE_CONTROL_READ; |
| 740 break; |
| 741 case kWriteOperation: |
| 742 event_type = net::NetLog::TYPE_SPARSE_CONTROL_WRITE; |
| 743 break; |
| 744 case kGetRangeOperation: |
| 745 event_type = net::NetLog::TYPE_SPARSE_CONTROL_GET_RANGE; |
| 746 break; |
| 747 default: |
| 748 NOTREACHED(); |
| 749 } |
| 750 entry_->net_log().EndEventWithErrorCode(event_type, result); |
| 751 } |
| 715 if (result < 0) { | 752 if (result < 0) { |
| 716 // We fail the whole operation if we encounter an error. | 753 // We fail the whole operation if we encounter an error. |
| 717 result_ = result; | 754 result_ = result; |
| 718 return; | 755 return; |
| 719 } | 756 } |
| 720 | 757 |
| 721 UpdateRange(result); | 758 UpdateRange(result); |
| 722 | 759 |
| 723 result_ += result; | 760 result_ += result; |
| 724 offset_ += result; | 761 offset_ += result; |
| 725 buf_len_ -= result; | 762 buf_len_ -= result; |
| 726 | 763 |
| 727 // We'll be reusing the user provided buffer for the next chunk. | 764 // We'll be reusing the user provided buffer for the next chunk. |
| 728 if (buf_len_ && user_buf_) | 765 if (buf_len_ && user_buf_) |
| 729 user_buf_->DidConsume(result); | 766 user_buf_->DidConsume(result); |
| 730 } | 767 } |
| 731 | 768 |
| 732 void SparseControl::OnChildIOCompleted(int result) { | 769 void SparseControl::OnChildIOCompleted(int result) { |
| 733 DCHECK_NE(net::ERR_IO_PENDING, result); | 770 DCHECK_NE(net::ERR_IO_PENDING, result); |
| 734 DoChildIOCompleted(result); | 771 DoChildIOCompleted(result); |
| 735 | 772 |
| 736 if (abort_) { | 773 if (abort_) { |
| 737 // We'll return the current result of the operation, which may be less than | 774 // 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. | 775 // the bytes to read or write, but the user cancelled the operation. |
| 739 abort_ = false; | 776 abort_ = false; |
| 777 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED, NULL); |
| 778 entry_->net_log().EndEvent( |
| 779 net::NetLog::TYPE_SPARSE_CONTROL, NULL); |
| 740 DoUserCallback(); | 780 DoUserCallback(); |
| 741 return DoAbortCallbacks(); | 781 return DoAbortCallbacks(); |
| 742 } | 782 } |
| 743 | 783 |
| 744 // We are running a callback from the message loop. It's time to restart what | 784 // We are running a callback from the message loop. It's time to restart what |
| 745 // we were doing before. | 785 // we were doing before. |
| 746 DoChildrenIO(); | 786 DoChildrenIO(); |
| 747 } | 787 } |
| 748 | 788 |
| 749 void SparseControl::DoUserCallback() { | 789 void SparseControl::DoUserCallback() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 764 net::CompletionCallback* c = abort_callbacks_[i]; | 804 net::CompletionCallback* c = abort_callbacks_[i]; |
| 765 if (i == abort_callbacks_.size() - 1) | 805 if (i == abort_callbacks_.size() - 1) |
| 766 abort_callbacks_.clear(); | 806 abort_callbacks_.clear(); |
| 767 | 807 |
| 768 entry_->Release(); // Don't touch object after this line. | 808 entry_->Release(); // Don't touch object after this line. |
| 769 c->Run(net::OK); | 809 c->Run(net::OK); |
| 770 } | 810 } |
| 771 } | 811 } |
| 772 | 812 |
| 773 } // namespace disk_cache | 813 } // namespace disk_cache |
| OLD | NEW |