Index: net/disk_cache/simple/simple_entry_impl.cc |
diff --git a/net/disk_cache/simple/simple_entry_impl.cc b/net/disk_cache/simple/simple_entry_impl.cc |
index 3a54a1fa4c0e79d2dd1f7173b5f4aef272fbc4a5..92f600c8275db52fd2de97fc966aa2ff7806d08b 100644 |
--- a/net/disk_cache/simple/simple_entry_impl.cc |
+++ b/net/disk_cache/simple/simple_entry_impl.cc |
@@ -335,7 +335,7 @@ int SimpleEntryImpl::ReadData(int stream_index, |
false)); |
} |
- if (stream_index < 0 || stream_index >= kSimpleEntryFileCount || |
+ if (stream_index < 0 || stream_index >= kSimpleEntryStreamCount || |
buf_len < 0) { |
if (net_log_.IsLoggingAllEvents()) { |
net_log_.AddEvent(net::NetLog::TYPE_SIMPLE_CACHE_ENTRY_READ_END, |
@@ -381,8 +381,8 @@ int SimpleEntryImpl::WriteData(int stream_index, |
truncate)); |
} |
- if (stream_index < 0 || stream_index >= kSimpleEntryFileCount || offset < 0 || |
- buf_len < 0) { |
+ if (stream_index < 0 || stream_index >= kSimpleEntryStreamCount || |
+ offset < 0 || buf_len < 0) { |
if (net_log_.IsLoggingAllEvents()) { |
net_log_.AddEvent( |
net::NetLog::TYPE_SIMPLE_CACHE_ENTRY_WRITE_END, |
@@ -402,17 +402,6 @@ int SimpleEntryImpl::WriteData(int stream_index, |
} |
ScopedOperationRunner operation_runner(this); |
- // Currently, Simple Cache is only used for HTTP, which stores the headers in |
- // stream 0 and always writes them with a single, truncating write. Detect |
- // these writes and record the size and size changes of the headers. Also, |
- // note writes to stream 0 that violate those assumptions. |
- if (stream_index == 0) { |
- if (offset == 0 && truncate) |
- RecordHeaderSizeChange(cache_type_, data_size_[0], buf_len); |
- else |
- RecordUnexpectedStream0Write(cache_type_); |
- } |
- |
// We can only do optimistic Write if there is no pending operations, so |
// that we are sure that the next call to RunNextOperationIfNeeded will |
// actually run the write operation that sets the stream size. It also |
@@ -692,7 +681,7 @@ void SimpleEntryImpl::CreateEntryInternal(bool have_index, |
last_used_ = last_modified_ = base::Time::Now(); |
// If creation succeeds, we should mark all streams to be saved on close. |
- for (int i = 0; i < kSimpleEntryFileCount; ++i) |
+ for (int i = 0; i < kSimpleEntryStreamCount; ++i) |
have_written_[i] = true; |
const base::TimeTicks start_time = base::TimeTicks::Now(); |
@@ -727,7 +716,7 @@ void SimpleEntryImpl::CloseInternal() { |
if (state_ == STATE_READY) { |
DCHECK(synchronous_entry_); |
state_ = STATE_IO_PENDING; |
- for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
+ for (int i = 0; i < kSimpleEntryStreamCount; ++i) { |
if (have_written_[i]) { |
if (GetDataSize(i) == crc32s_end_offset_[i]) { |
int32 crc = GetDataSize(i) == 0 ? crc32(0, Z_NULL, 0) : crc32s_[i]; |
@@ -746,12 +735,13 @@ void SimpleEntryImpl::CloseInternal() { |
base::Bind(&SimpleSynchronousEntry::Close, |
base::Unretained(synchronous_entry_), |
SimpleEntryStat(last_used_, last_modified_, data_size_), |
- base::Passed(&crc32s_to_write)); |
+ base::Passed(&crc32s_to_write), |
+ stream_0_data_); |
Closure reply = base::Bind(&SimpleEntryImpl::CloseOperationComplete, this); |
synchronous_entry_ = NULL; |
worker_pool_->PostTaskAndReply(FROM_HERE, task, reply); |
- for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
+ for (int i = 0; i < kSimpleEntryStreamCount; ++i) { |
if (!have_written_[i]) { |
SIMPLE_CACHE_UMA(ENUMERATION, |
"CheckCRCResult", cache_type_, |
@@ -804,6 +794,16 @@ void SimpleEntryImpl::ReadDataInternal(int stream_index, |
return; |
} |
+ // Since stream 0 data is kept in memory, it is read immediately. |
+ if (stream_index == 0) { |
+ int ret_value = ReadStream0Data(buf, offset, buf_len); |
+ if (!callback.is_null()) { |
+ MessageLoopProxy::current()->PostTask(FROM_HERE, |
+ base::Bind(callback, ret_value)); |
+ } |
+ return; |
+ } |
+ |
buf_len = std::min(buf_len, GetDataSize(stream_index) - offset); |
pasko
2013/09/17 14:17:36
we recompute the buf_len here, and in ReadStream0D
clamy
2013/09/18 16:17:14
Done.
|
state_ = STATE_IO_PENDING; |
@@ -812,14 +812,15 @@ void SimpleEntryImpl::ReadDataInternal(int stream_index, |
scoped_ptr<uint32> read_crc32(new uint32()); |
scoped_ptr<int> result(new int()); |
- scoped_ptr<base::Time> last_used(new base::Time()); |
+ scoped_ptr<SimpleEntryStat> entry_stat( |
+ new SimpleEntryStat(last_used_, last_modified_, data_size_)); |
Closure task = base::Bind( |
&SimpleSynchronousEntry::ReadData, |
base::Unretained(synchronous_entry_), |
SimpleSynchronousEntry::EntryOperationData(stream_index, offset, buf_len), |
make_scoped_refptr(buf), |
read_crc32.get(), |
- last_used.get(), |
+ entry_stat.get(), |
result.get()); |
Closure reply = base::Bind(&SimpleEntryImpl::ReadOperationComplete, |
this, |
@@ -827,7 +828,7 @@ void SimpleEntryImpl::ReadDataInternal(int stream_index, |
offset, |
callback, |
base::Passed(&read_crc32), |
- base::Passed(&last_used), |
+ base::Passed(&entry_stat), |
base::Passed(&result)); |
worker_pool_->PostTaskAndReply(FROM_HERE, task, reply); |
} |
@@ -864,6 +865,17 @@ void SimpleEntryImpl::WriteDataInternal(int stream_index, |
} |
DCHECK_EQ(STATE_READY, state_); |
+ // Since stream 0 data is kept in memory, it will be written immediatly. |
+ if (stream_index == 0) { |
+ int ret_value = CopyStream0Data(buf, offset, buf_len, truncate); |
+ if (!callback.is_null()) { |
+ // PostTask prevents creating a loop when calling the callback directly. |
pasko
2013/09/17 14:17:36
nit:
this comment is not necessary, we consistent
clamy
2013/09/18 16:17:14
Done.
|
+ MessageLoopProxy::current()->PostTask(FROM_HERE, |
+ base::Bind(callback, ret_value)); |
+ } |
+ return; |
+ } |
+ |
state_ = STATE_IO_PENDING; |
if (!doomed_ && backend_.get()) |
backend_->index()->UseIfExists(entry_hash_); |
@@ -898,6 +910,9 @@ void SimpleEntryImpl::WriteDataInternal(int stream_index, |
last_used_ = last_modified_ = base::Time::Now(); |
have_written_[stream_index] = true; |
+ // Writing on stream 1 affects the placement of stream 0 in the file. |
+ if (stream_index == 1) |
+ have_written_[0] = true; |
pasko
2013/09/17 14:17:36
this means we would loose the CRC on stream 0 afte
clamy
2013/09/18 16:17:14
Done.
|
scoped_ptr<int> result(new int()); |
Closure task = base::Bind(&SimpleSynchronousEntry::WriteData, |
@@ -956,6 +971,9 @@ void SimpleEntryImpl::CreationOperationComplete( |
state_ = STATE_READY; |
synchronous_entry_ = in_results->sync_entry; |
+ stream_0_data_ = in_results->stream_0_data; |
+ // The crc was read in SimpleSynchronousEntry. |
+ crc_check_state_[0] = CRC_CHECK_DONE; |
pasko
2013/09/17 14:17:36
this should also update the crc32s_end_offset_[0],
clamy
2013/09/18 16:17:14
Done.
|
if (key_.empty()) { |
SetKey(synchronous_entry_->key()); |
} else { |
@@ -1003,7 +1021,7 @@ void SimpleEntryImpl::ReadOperationComplete( |
int offset, |
const CompletionCallback& completion_callback, |
scoped_ptr<uint32> read_crc32, |
- scoped_ptr<base::Time> last_used, |
+ scoped_ptr<SimpleEntryStat> entry_stat, |
scoped_ptr<int> result) { |
DCHECK(io_thread_checker_.CalledOnValidThread()); |
DCHECK(synchronous_entry_); |
@@ -1039,7 +1057,7 @@ void SimpleEntryImpl::ReadOperationComplete( |
Closure task = base::Bind(&SimpleSynchronousEntry::CheckEOFRecord, |
base::Unretained(synchronous_entry_), |
stream_index, |
- data_size_[stream_index], |
+ *entry_stat, |
crc32s_[stream_index], |
new_result.get()); |
Closure reply = base::Bind(&SimpleEntryImpl::ChecksumOperationComplete, |
@@ -1068,10 +1086,7 @@ void SimpleEntryImpl::ReadOperationComplete( |
} |
EntryOperationComplete( |
- stream_index, |
- completion_callback, |
- SimpleEntryStat(*last_used, last_modified_, data_size_), |
- result.Pass()); |
+ stream_index, completion_callback, *entry_stat, result.Pass()); |
} |
void SimpleEntryImpl::WriteOperationComplete( |
@@ -1160,7 +1175,7 @@ void SimpleEntryImpl::UpdateDataFromEntryStat( |
last_used_ = entry_stat.last_used; |
last_modified_ = entry_stat.last_modified; |
- for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
+ for (int i = 0; i < kSimpleEntryStreamCount; ++i) { |
data_size_[i] = entry_stat.data_size[i]; |
} |
if (!doomed_ && backend_.get()) |
@@ -1169,7 +1184,7 @@ void SimpleEntryImpl::UpdateDataFromEntryStat( |
int64 SimpleEntryImpl::GetDiskUsage() const { |
int64 file_size = 0; |
- for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
+ for (int i = 0; i < kSimpleEntryStreamCount; ++i) { |
file_size += |
simple_util::GetFileSizeFromKeyAndDataSize(key_, data_size_[i]); |
} |
@@ -1247,4 +1262,65 @@ void SimpleEntryImpl::RecordWriteDependencyType( |
type, WRITE_DEPENDENCY_TYPE_MAX); |
} |
+int SimpleEntryImpl::ReadStream0Data(net::IOBuffer* buf, |
+ int offset, |
+ int buf_len) { |
+ int read_size = std::min(data_size_[0] - offset, buf_len); |
+ if (read_size < 0) { |
+ RecordReadResult(cache_type_, READ_RESULT_SYNC_READ_FAILURE); |
+ return 0; |
+ } |
+ memcpy(buf->data(), stream_0_data_->data() + offset, read_size); |
+ UpdateDataFromEntryStat( |
+ SimpleEntryStat(base::Time::Now(), last_modified_, data_size_)); |
+ RecordReadResult(cache_type_, READ_RESULT_SUCCESS); |
+ return read_size; |
+} |
+ |
+int SimpleEntryImpl::CopyStream0Data(net::IOBuffer* buf, |
+ int offset, |
+ int buf_len, |
+ bool truncate) { |
+ // Currently, stream 0 is only used for HTTP headers, and always writes them |
+ // with a single, truncating write. Detect these writes and record the size |
+ // and size changes of the headers. Also, supports writes to stream 0 that |
pasko
2013/09/17 14:17:36
nit: "the size and size changes"?
I do not see th
clamy
2013/09/18 16:17:14
Done.
|
+ // violate those assumptions. All other clients of the Simple Cache are |
+ // encouraged to use stream 1. |
+ if (!stream_0_data_) |
+ stream_0_data_ = new net::GrowableIOBuffer(); |
+ have_written_[0] = true; |
+ int data_size = data_size_[0]; |
pasko
2013/09/17 14:17:36
In this file we tried to remain consistent on alwa
clamy
2013/09/18 16:17:14
Done.
|
+ if (offset == 0 && truncate) { |
+ RecordHeaderSizeChange(cache_type_, data_size, buf_len); |
+ stream_0_data_->SetCapacity(buf_len); |
+ memcpy(stream_0_data_->data(), buf->data(), buf_len); |
+ data_size_[0] = buf_len; |
+ } else { |
+ RecordUnexpectedStream0Write(cache_type_); |
+ const int buffer_size = |
+ truncate ? offset + buf_len : std::max(offset + buf_len, data_size); |
+ stream_0_data_->SetCapacity(buffer_size); |
+ // If |stream_0_data_| was extended. the extension until offset need to be |
+ // zeroed. |
+ const int fill_size = offset <= data_size ? 0 : offset - data_size; |
+ if (fill_size > 0) |
+ memset(stream_0_data_->data() + data_size, 0, fill_size); |
+ if (buf) |
+ memcpy(stream_0_data_->data() + offset, buf->data(), buf_len); |
+ data_size_[0] = buffer_size; |
+ } |
+ base::Time modification_time = base::Time::Now(); |
+ UpdateDataFromEntryStat( |
+ SimpleEntryStat(modification_time, modification_time, data_size_)); |
+ if (stream_0_data_) { |
+ crc32s_[0] = crc32(crc32(0L, Z_NULL, 0), |
+ reinterpret_cast<const Bytef*>(stream_0_data_->data()), |
+ data_size_[0]); |
+ } else { |
+ crc32s_[0] = crc32(0L, Z_NULL, 0); |
+ } |
+ RecordWriteResult(cache_type_, WRITE_RESULT_SUCCESS); |
+ return buf_len; |
+} |
+ |
} // namespace disk_cache |