| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/simple/simple_entry_impl.h" | 5 #include "net/disk_cache/simple/simple_entry_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 SimpleBackendImpl* backend, | 157 SimpleBackendImpl* backend, |
| 158 net::NetLog* net_log) | 158 net::NetLog* net_log) |
| 159 : backend_(backend->AsWeakPtr()), | 159 : backend_(backend->AsWeakPtr()), |
| 160 worker_pool_(backend->worker_pool()), | 160 worker_pool_(backend->worker_pool()), |
| 161 path_(path), | 161 path_(path), |
| 162 entry_hash_(entry_hash), | 162 entry_hash_(entry_hash), |
| 163 use_optimistic_operations_(operations_mode == OPTIMISTIC_OPERATIONS), | 163 use_optimistic_operations_(operations_mode == OPTIMISTIC_OPERATIONS), |
| 164 last_used_(Time::Now()), | 164 last_used_(Time::Now()), |
| 165 last_modified_(last_used_), | 165 last_modified_(last_used_), |
| 166 open_count_(0), | 166 open_count_(0), |
| 167 doomed_(false), |
| 167 state_(STATE_UNINITIALIZED), | 168 state_(STATE_UNINITIALIZED), |
| 168 synchronous_entry_(NULL), | 169 synchronous_entry_(NULL), |
| 169 net_log_(net::BoundNetLog::Make( | 170 net_log_(net::BoundNetLog::Make( |
| 170 net_log, net::NetLog::SOURCE_DISK_CACHE_ENTRY)) { | 171 net_log, net::NetLog::SOURCE_DISK_CACHE_ENTRY)) { |
| 171 COMPILE_ASSERT(arraysize(data_size_) == arraysize(crc32s_end_offset_), | 172 COMPILE_ASSERT(arraysize(data_size_) == arraysize(crc32s_end_offset_), |
| 172 arrays_should_be_same_size); | 173 arrays_should_be_same_size); |
| 173 COMPILE_ASSERT(arraysize(data_size_) == arraysize(crc32s_), | 174 COMPILE_ASSERT(arraysize(data_size_) == arraysize(crc32s_), |
| 174 arrays_should_be_same_size); | 175 arrays_should_be_same_size); |
| 175 COMPILE_ASSERT(arraysize(data_size_) == arraysize(have_written_), | 176 COMPILE_ASSERT(arraysize(data_size_) == arraysize(have_written_), |
| 176 arrays_should_be_same_size); | 177 arrays_should_be_same_size); |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 DCHECK(out_entry); | 521 DCHECK(out_entry); |
| 521 ++open_count_; | 522 ++open_count_; |
| 522 AddRef(); // Balanced in Close() | 523 AddRef(); // Balanced in Close() |
| 523 *out_entry = this; | 524 *out_entry = this; |
| 524 } | 525 } |
| 525 | 526 |
| 526 void SimpleEntryImpl::RemoveSelfFromBackend() { | 527 void SimpleEntryImpl::RemoveSelfFromBackend() { |
| 527 if (!backend_.get()) | 528 if (!backend_.get()) |
| 528 return; | 529 return; |
| 529 backend_->OnDeactivated(this); | 530 backend_->OnDeactivated(this); |
| 530 backend_.reset(); | |
| 531 } | 531 } |
| 532 | 532 |
| 533 void SimpleEntryImpl::MarkAsDoomed() { | 533 void SimpleEntryImpl::MarkAsDoomed() { |
| 534 if (!backend_.get()) | 534 if (!backend_.get()) |
| 535 return; | 535 return; |
| 536 doomed_ = true; |
| 536 backend_->index()->Remove(entry_hash_); | 537 backend_->index()->Remove(entry_hash_); |
| 537 RemoveSelfFromBackend(); | 538 RemoveSelfFromBackend(); |
| 538 } | 539 } |
| 539 | 540 |
| 540 void SimpleEntryImpl::RunNextOperationIfNeeded() { | 541 void SimpleEntryImpl::RunNextOperationIfNeeded() { |
| 541 DCHECK(io_thread_checker_.CalledOnValidThread()); | 542 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 542 UMA_HISTOGRAM_CUSTOM_COUNTS("SimpleCache.EntryOperationsPending", | 543 UMA_HISTOGRAM_CUSTOM_COUNTS("SimpleCache.EntryOperationsPending", |
| 543 pending_operations_.size(), 0, 100, 20); | 544 pending_operations_.size(), 0, 100, 20); |
| 544 if (!pending_operations_.empty() && state_ != STATE_IO_PENDING) { | 545 if (!pending_operations_.empty() && state_ != STATE_IO_PENDING) { |
| 545 scoped_ptr<SimpleEntryOperation> operation( | 546 scoped_ptr<SimpleEntryOperation> operation( |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 772 // STATE_IO_PENDING. | 773 // STATE_IO_PENDING. |
| 773 if (!callback.is_null()) | 774 if (!callback.is_null()) |
| 774 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( | 775 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( |
| 775 callback, 0)); | 776 callback, 0)); |
| 776 return; | 777 return; |
| 777 } | 778 } |
| 778 | 779 |
| 779 buf_len = std::min(buf_len, GetDataSize(stream_index) - offset); | 780 buf_len = std::min(buf_len, GetDataSize(stream_index) - offset); |
| 780 | 781 |
| 781 state_ = STATE_IO_PENDING; | 782 state_ = STATE_IO_PENDING; |
| 782 if (backend_.get()) | 783 if (!doomed_ && backend_.get()) |
| 783 backend_->index()->UseIfExists(entry_hash_); | 784 backend_->index()->UseIfExists(entry_hash_); |
| 784 | 785 |
| 785 scoped_ptr<uint32> read_crc32(new uint32()); | 786 scoped_ptr<uint32> read_crc32(new uint32()); |
| 786 scoped_ptr<int> result(new int()); | 787 scoped_ptr<int> result(new int()); |
| 787 scoped_ptr<base::Time> last_used(new base::Time()); | 788 scoped_ptr<base::Time> last_used(new base::Time()); |
| 788 Closure task = base::Bind( | 789 Closure task = base::Bind( |
| 789 &SimpleSynchronousEntry::ReadData, | 790 &SimpleSynchronousEntry::ReadData, |
| 790 base::Unretained(synchronous_entry_), | 791 base::Unretained(synchronous_entry_), |
| 791 SimpleSynchronousEntry::EntryOperationData(stream_index, offset, buf_len), | 792 SimpleSynchronousEntry::EntryOperationData(stream_index, offset, buf_len), |
| 792 make_scoped_refptr(buf), | 793 make_scoped_refptr(buf), |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 // callback directly. | 833 // callback directly. |
| 833 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( | 834 MessageLoopProxy::current()->PostTask(FROM_HERE, base::Bind( |
| 834 callback, net::ERR_FAILED)); | 835 callback, net::ERR_FAILED)); |
| 835 } | 836 } |
| 836 // |this| may be destroyed after return here. | 837 // |this| may be destroyed after return here. |
| 837 return; | 838 return; |
| 838 } | 839 } |
| 839 | 840 |
| 840 DCHECK_EQ(STATE_READY, state_); | 841 DCHECK_EQ(STATE_READY, state_); |
| 841 state_ = STATE_IO_PENDING; | 842 state_ = STATE_IO_PENDING; |
| 842 if (backend_.get()) | 843 if (!doomed_ && backend_.get()) |
| 843 backend_->index()->UseIfExists(entry_hash_); | 844 backend_->index()->UseIfExists(entry_hash_); |
| 844 // It is easy to incrementally compute the CRC from [0 .. |offset + buf_len|) | 845 // It is easy to incrementally compute the CRC from [0 .. |offset + buf_len|) |
| 845 // if |offset == 0| or we have already computed the CRC for [0 .. offset). | 846 // if |offset == 0| or we have already computed the CRC for [0 .. offset). |
| 846 // We rely on most write operations being sequential, start to end to compute | 847 // We rely on most write operations being sequential, start to end to compute |
| 847 // the crc of the data. When we write to an entry and close without having | 848 // the crc of the data. When we write to an entry and close without having |
| 848 // done a sequential write, we don't check the CRC on read. | 849 // done a sequential write, we don't check the CRC on read. |
| 849 if (offset == 0 || crc32s_end_offset_[stream_index] == offset) { | 850 if (offset == 0 || crc32s_end_offset_[stream_index] == offset) { |
| 850 uint32 initial_crc = (offset != 0) ? crc32s_[stream_index] | 851 uint32 initial_crc = (offset != 0) ? crc32s_[stream_index] |
| 851 : crc32(0, Z_NULL, 0); | 852 : crc32(0, Z_NULL, 0); |
| 852 if (buf_len > 0) { | 853 if (buf_len > 0) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 884 Closure reply = base::Bind(&SimpleEntryImpl::WriteOperationComplete, | 885 Closure reply = base::Bind(&SimpleEntryImpl::WriteOperationComplete, |
| 885 this, | 886 this, |
| 886 stream_index, | 887 stream_index, |
| 887 callback, | 888 callback, |
| 888 base::Passed(&entry_stat), | 889 base::Passed(&entry_stat), |
| 889 base::Passed(&result)); | 890 base::Passed(&result)); |
| 890 worker_pool_->PostTaskAndReply(FROM_HERE, task, reply); | 891 worker_pool_->PostTaskAndReply(FROM_HERE, task, reply); |
| 891 } | 892 } |
| 892 | 893 |
| 893 void SimpleEntryImpl::DoomEntryInternal(const CompletionCallback& callback) { | 894 void SimpleEntryImpl::DoomEntryInternal(const CompletionCallback& callback) { |
| 895 if (backend_) |
| 896 backend_->OnDoomStart(entry_hash_); |
| 894 PostTaskAndReplyWithResult( | 897 PostTaskAndReplyWithResult( |
| 895 worker_pool_, FROM_HERE, | 898 worker_pool_, FROM_HERE, |
| 896 base::Bind(&SimpleSynchronousEntry::DoomEntry, path_, key_, entry_hash_), | 899 base::Bind(&SimpleSynchronousEntry::DoomEntry, path_, key_, entry_hash_), |
| 897 base::Bind(&SimpleEntryImpl::DoomOperationComplete, this, callback, | 900 base::Bind(&SimpleEntryImpl::DoomOperationComplete, this, callback, |
| 898 state_)); | 901 state_)); |
| 899 state_ = STATE_IO_PENDING; | 902 state_ = STATE_IO_PENDING; |
| 900 } | 903 } |
| 901 | 904 |
| 902 void SimpleEntryImpl::CreationOperationComplete( | 905 void SimpleEntryImpl::CreationOperationComplete( |
| 903 const CompletionCallback& completion_callback, | 906 const CompletionCallback& completion_callback, |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1062 RecordWriteResult(WRITE_RESULT_SYNC_WRITE_FAILURE); | 1065 RecordWriteResult(WRITE_RESULT_SYNC_WRITE_FAILURE); |
| 1063 if (net_log_.IsLoggingAllEvents()) { | 1066 if (net_log_.IsLoggingAllEvents()) { |
| 1064 net_log_.AddEvent(net::NetLog::TYPE_SIMPLE_CACHE_ENTRY_WRITE_END, | 1067 net_log_.AddEvent(net::NetLog::TYPE_SIMPLE_CACHE_ENTRY_WRITE_END, |
| 1065 CreateNetLogReadWriteCompleteCallback(*result)); | 1068 CreateNetLogReadWriteCompleteCallback(*result)); |
| 1066 } | 1069 } |
| 1067 | 1070 |
| 1068 EntryOperationComplete( | 1071 EntryOperationComplete( |
| 1069 stream_index, completion_callback, *entry_stat, result.Pass()); | 1072 stream_index, completion_callback, *entry_stat, result.Pass()); |
| 1070 } | 1073 } |
| 1071 | 1074 |
| 1072 void SimpleEntryImpl::DoomOperationComplete(const CompletionCallback& callback, | 1075 void SimpleEntryImpl::DoomOperationComplete( |
| 1073 State state_to_restore, | 1076 const CompletionCallback& callback, |
| 1074 int result) { | 1077 State state_to_restore, |
| 1078 int result) { |
| 1075 state_ = state_to_restore; | 1079 state_ = state_to_restore; |
| 1076 if (!callback.is_null()) | 1080 if (!callback.is_null()) |
| 1077 callback.Run(result); | 1081 callback.Run(result); |
| 1078 RunNextOperationIfNeeded(); | 1082 RunNextOperationIfNeeded(); |
| 1083 if (backend_) |
| 1084 backend_->OnDoomComplete(entry_hash_); |
| 1079 } | 1085 } |
| 1080 | 1086 |
| 1081 void SimpleEntryImpl::ChecksumOperationComplete( | 1087 void SimpleEntryImpl::ChecksumOperationComplete( |
| 1082 int orig_result, | 1088 int orig_result, |
| 1083 int stream_index, | 1089 int stream_index, |
| 1084 const CompletionCallback& completion_callback, | 1090 const CompletionCallback& completion_callback, |
| 1085 scoped_ptr<int> result) { | 1091 scoped_ptr<int> result) { |
| 1086 DCHECK(io_thread_checker_.CalledOnValidThread()); | 1092 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 1087 DCHECK(synchronous_entry_); | 1093 DCHECK(synchronous_entry_); |
| 1088 DCHECK_EQ(STATE_IO_PENDING, state_); | 1094 DCHECK_EQ(STATE_IO_PENDING, state_); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1130 const SimpleEntryStat& entry_stat) { | 1136 const SimpleEntryStat& entry_stat) { |
| 1131 DCHECK(io_thread_checker_.CalledOnValidThread()); | 1137 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 1132 DCHECK(synchronous_entry_); | 1138 DCHECK(synchronous_entry_); |
| 1133 DCHECK_EQ(STATE_READY, state_); | 1139 DCHECK_EQ(STATE_READY, state_); |
| 1134 | 1140 |
| 1135 last_used_ = entry_stat.last_used; | 1141 last_used_ = entry_stat.last_used; |
| 1136 last_modified_ = entry_stat.last_modified; | 1142 last_modified_ = entry_stat.last_modified; |
| 1137 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 1143 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 1138 data_size_[i] = entry_stat.data_size[i]; | 1144 data_size_[i] = entry_stat.data_size[i]; |
| 1139 } | 1145 } |
| 1140 if (backend_.get()) | 1146 if (!doomed_ && backend_.get()) |
| 1141 backend_->index()->UpdateEntrySize(entry_hash_, GetDiskUsage()); | 1147 backend_->index()->UpdateEntrySize(entry_hash_, GetDiskUsage()); |
| 1142 } | 1148 } |
| 1143 | 1149 |
| 1144 int64 SimpleEntryImpl::GetDiskUsage() const { | 1150 int64 SimpleEntryImpl::GetDiskUsage() const { |
| 1145 int64 file_size = 0; | 1151 int64 file_size = 0; |
| 1146 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 1152 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 1147 file_size += | 1153 file_size += |
| 1148 simple_util::GetFileSizeFromKeyAndDataSize(key_, data_size_[i]); | 1154 simple_util::GetFileSizeFromKeyAndDataSize(key_, data_size_[i]); |
| 1149 } | 1155 } |
| 1150 return file_size; | 1156 return file_size; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1213 } else { | 1219 } else { |
| 1214 type = conflicting ? WRITE_FOLLOWS_CONFLICTING_WRITE | 1220 type = conflicting ? WRITE_FOLLOWS_CONFLICTING_WRITE |
| 1215 : WRITE_FOLLOWS_NON_CONFLICTING_WRITE; | 1221 : WRITE_FOLLOWS_NON_CONFLICTING_WRITE; |
| 1216 } | 1222 } |
| 1217 } | 1223 } |
| 1218 UMA_HISTOGRAM_ENUMERATION( | 1224 UMA_HISTOGRAM_ENUMERATION( |
| 1219 "SimpleCache.WriteDependencyType", type, WRITE_DEPENDENCY_TYPE_MAX); | 1225 "SimpleCache.WriteDependencyType", type, WRITE_DEPENDENCY_TYPE_MAX); |
| 1220 } | 1226 } |
| 1221 | 1227 |
| 1222 } // namespace disk_cache | 1228 } // namespace disk_cache |
| OLD | NEW |