| 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/blockfile/sparse_control.h" | 5 #include "net/disk_cache/blockfile/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/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 NOTREACHED(); | 154 NOTREACHED(); |
| 155 return net::NetLog::TYPE_CANCELLED; | 155 return net::NetLog::TYPE_CANCELLED; |
| 156 } | 156 } |
| 157 } | 157 } |
| 158 | 158 |
| 159 // Logs the end event for |operation| on a child entry. Range operations log | 159 // Logs the end event for |operation| on a child entry. Range operations log |
| 160 // no events for each child they search through. | 160 // no events for each child they search through. |
| 161 void LogChildOperationEnd(const net::BoundNetLog& net_log, | 161 void LogChildOperationEnd(const net::BoundNetLog& net_log, |
| 162 disk_cache::SparseControl::SparseOperation operation, | 162 disk_cache::SparseControl::SparseOperation operation, |
| 163 int result) { | 163 int result) { |
| 164 if (net_log.GetCaptureMode().enabled()) { | 164 if (net_log.HasObservers()) { |
| 165 net::NetLog::EventType event_type; | 165 net::NetLog::EventType event_type; |
| 166 switch (operation) { | 166 switch (operation) { |
| 167 case disk_cache::SparseControl::kReadOperation: | 167 case disk_cache::SparseControl::kReadOperation: |
| 168 event_type = net::NetLog::TYPE_SPARSE_READ_CHILD_DATA; | 168 event_type = net::NetLog::TYPE_SPARSE_READ_CHILD_DATA; |
| 169 break; | 169 break; |
| 170 case disk_cache::SparseControl::kWriteOperation: | 170 case disk_cache::SparseControl::kWriteOperation: |
| 171 event_type = net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA; | 171 event_type = net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA; |
| 172 break; | 172 break; |
| 173 case disk_cache::SparseControl::kGetRangeOperation: | 173 case disk_cache::SparseControl::kGetRangeOperation: |
| 174 return; | 174 return; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 offset_ = offset; | 268 offset_ = offset; |
| 269 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; | 269 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; |
| 270 buf_len_ = buf_len; | 270 buf_len_ = buf_len; |
| 271 user_callback_ = callback; | 271 user_callback_ = callback; |
| 272 | 272 |
| 273 result_ = 0; | 273 result_ = 0; |
| 274 pending_ = false; | 274 pending_ = false; |
| 275 finished_ = false; | 275 finished_ = false; |
| 276 abort_ = false; | 276 abort_ = false; |
| 277 | 277 |
| 278 if (entry_->net_log().GetCaptureMode().enabled()) { | 278 if (entry_->net_log().HasObservers()) { |
| 279 entry_->net_log().BeginEvent( | 279 entry_->net_log().BeginEvent( |
| 280 GetSparseEventType(operation_), | 280 GetSparseEventType(operation_), |
| 281 CreateNetLogSparseOperationCallback(offset_, buf_len_)); | 281 CreateNetLogSparseOperationCallback(offset_, buf_len_)); |
| 282 } | 282 } |
| 283 DoChildrenIO(); | 283 DoChildrenIO(); |
| 284 | 284 |
| 285 if (!pending_) { | 285 if (!pending_) { |
| 286 // Everything was done synchronously. | 286 // Everything was done synchronously. |
| 287 operation_ = kNoOperation; | 287 operation_ = kNoOperation; |
| 288 user_buf_ = NULL; | 288 user_buf_ = NULL; |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 if (rv != sizeof(child_data_)) | 671 if (rv != sizeof(child_data_)) |
| 672 DLOG(ERROR) << "Failed to save child data"; | 672 DLOG(ERROR) << "Failed to save child data"; |
| 673 SetChildBit(true); | 673 SetChildBit(true); |
| 674 } | 674 } |
| 675 | 675 |
| 676 void SparseControl::DoChildrenIO() { | 676 void SparseControl::DoChildrenIO() { |
| 677 while (DoChildIO()) continue; | 677 while (DoChildIO()) continue; |
| 678 | 678 |
| 679 // Range operations are finished synchronously, often without setting | 679 // Range operations are finished synchronously, often without setting |
| 680 // |finished_| to true. | 680 // |finished_| to true. |
| 681 if (kGetRangeOperation == operation_ && | 681 if (kGetRangeOperation == operation_ && entry_->net_log().HasObservers()) { |
| 682 entry_->net_log().GetCaptureMode().enabled()) { | |
| 683 entry_->net_log().EndEvent( | 682 entry_->net_log().EndEvent( |
| 684 net::NetLog::TYPE_SPARSE_GET_RANGE, | 683 net::NetLog::TYPE_SPARSE_GET_RANGE, |
| 685 CreateNetLogGetAvailableRangeResultCallback(offset_, result_)); | 684 CreateNetLogGetAvailableRangeResultCallback(offset_, result_)); |
| 686 } | 685 } |
| 687 if (finished_) { | 686 if (finished_) { |
| 688 if (kGetRangeOperation != operation_ && | 687 if (kGetRangeOperation != operation_ && entry_->net_log().HasObservers()) { |
| 689 entry_->net_log().GetCaptureMode().enabled()) { | |
| 690 entry_->net_log().EndEvent(GetSparseEventType(operation_)); | 688 entry_->net_log().EndEvent(GetSparseEventType(operation_)); |
| 691 } | 689 } |
| 692 if (pending_) | 690 if (pending_) |
| 693 DoUserCallback(); // Don't touch this object after this point. | 691 DoUserCallback(); // Don't touch this object after this point. |
| 694 } | 692 } |
| 695 } | 693 } |
| 696 | 694 |
| 697 bool SparseControl::DoChildIO() { | 695 bool SparseControl::DoChildIO() { |
| 698 finished_ = true; | 696 finished_ = true; |
| 699 if (!buf_len_ || result_ < 0) | 697 if (!buf_len_ || result_ < 0) |
| 700 return false; | 698 return false; |
| 701 | 699 |
| 702 if (!OpenChild()) | 700 if (!OpenChild()) |
| 703 return false; | 701 return false; |
| 704 | 702 |
| 705 if (!VerifyRange()) | 703 if (!VerifyRange()) |
| 706 return false; | 704 return false; |
| 707 | 705 |
| 708 // We have more work to do. Let's not trigger a callback to the caller. | 706 // We have more work to do. Let's not trigger a callback to the caller. |
| 709 finished_ = false; | 707 finished_ = false; |
| 710 CompletionCallback callback; | 708 CompletionCallback callback; |
| 711 if (!user_callback_.is_null()) { | 709 if (!user_callback_.is_null()) { |
| 712 callback = | 710 callback = |
| 713 base::Bind(&SparseControl::OnChildIOCompleted, base::Unretained(this)); | 711 base::Bind(&SparseControl::OnChildIOCompleted, base::Unretained(this)); |
| 714 } | 712 } |
| 715 | 713 |
| 716 int rv = 0; | 714 int rv = 0; |
| 717 switch (operation_) { | 715 switch (operation_) { |
| 718 case kReadOperation: | 716 case kReadOperation: |
| 719 if (entry_->net_log().GetCaptureMode().enabled()) { | 717 if (entry_->net_log().HasObservers()) { |
| 720 entry_->net_log().BeginEvent( | 718 entry_->net_log().BeginEvent( |
| 721 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, | 719 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, |
| 722 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), | 720 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), |
| 723 child_len_)); | 721 child_len_)); |
| 724 } | 722 } |
| 725 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_.get(), | 723 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_.get(), |
| 726 child_len_, callback); | 724 child_len_, callback); |
| 727 break; | 725 break; |
| 728 case kWriteOperation: | 726 case kWriteOperation: |
| 729 if (entry_->net_log().GetCaptureMode().enabled()) { | 727 if (entry_->net_log().HasObservers()) { |
| 730 entry_->net_log().BeginEvent( | 728 entry_->net_log().BeginEvent( |
| 731 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, | 729 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, |
| 732 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), | 730 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), |
| 733 child_len_)); | 731 child_len_)); |
| 734 } | 732 } |
| 735 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_.get(), | 733 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_.get(), |
| 736 child_len_, callback, false); | 734 child_len_, callback, false); |
| 737 break; | 735 break; |
| 738 case kGetRangeOperation: | 736 case kGetRangeOperation: |
| 739 rv = DoGetAvailableRange(); | 737 rv = DoGetAvailableRange(); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 } | 847 } |
| 850 | 848 |
| 851 void SparseControl::OnChildIOCompleted(int result) { | 849 void SparseControl::OnChildIOCompleted(int result) { |
| 852 DCHECK_NE(net::ERR_IO_PENDING, result); | 850 DCHECK_NE(net::ERR_IO_PENDING, result); |
| 853 DoChildIOCompleted(result); | 851 DoChildIOCompleted(result); |
| 854 | 852 |
| 855 if (abort_) { | 853 if (abort_) { |
| 856 // We'll return the current result of the operation, which may be less than | 854 // We'll return the current result of the operation, which may be less than |
| 857 // the bytes to read or write, but the user cancelled the operation. | 855 // the bytes to read or write, but the user cancelled the operation. |
| 858 abort_ = false; | 856 abort_ = false; |
| 859 if (entry_->net_log().GetCaptureMode().enabled()) { | 857 if (entry_->net_log().HasObservers()) { |
| 860 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED); | 858 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED); |
| 861 entry_->net_log().EndEvent(GetSparseEventType(operation_)); | 859 entry_->net_log().EndEvent(GetSparseEventType(operation_)); |
| 862 } | 860 } |
| 863 // We have an indirect reference to this object for every callback so if | 861 // We have an indirect reference to this object for every callback so if |
| 864 // there is only one callback, we may delete this object before reaching | 862 // there is only one callback, we may delete this object before reaching |
| 865 // DoAbortCallbacks. | 863 // DoAbortCallbacks. |
| 866 bool has_abort_callbacks = !abort_callbacks_.empty(); | 864 bool has_abort_callbacks = !abort_callbacks_.empty(); |
| 867 DoUserCallback(); | 865 DoUserCallback(); |
| 868 if (has_abort_callbacks) | 866 if (has_abort_callbacks) |
| 869 DoAbortCallbacks(); | 867 DoAbortCallbacks(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 894 CompletionCallback cb = abort_callbacks_[i]; | 892 CompletionCallback cb = abort_callbacks_[i]; |
| 895 if (i == abort_callbacks_.size() - 1) | 893 if (i == abort_callbacks_.size() - 1) |
| 896 abort_callbacks_.clear(); | 894 abort_callbacks_.clear(); |
| 897 | 895 |
| 898 entry_->Release(); // Don't touch object after this line. | 896 entry_->Release(); // Don't touch object after this line. |
| 899 cb.Run(net::OK); | 897 cb.Run(net::OK); |
| 900 } | 898 } |
| 901 } | 899 } |
| 902 | 900 |
| 903 } // namespace disk_cache | 901 } // namespace disk_cache |
| OLD | NEW |