Index: net/http/http_cache.cc |
=================================================================== |
--- net/http/http_cache.cc (revision 25867) |
+++ net/http/http_cache.cc (working copy) |
@@ -192,7 +192,10 @@ |
network_read_callback_(this, &Transaction::OnNetworkReadCompleted)), |
ALLOW_THIS_IN_INITIALIZER_LIST( |
cache_read_callback_(new CancelableCompletionCallback<Transaction>( |
- this, &Transaction::OnCacheReadCompleted))) { |
+ this, &Transaction::OnCacheReadCompleted))), |
+ ALLOW_THIS_IN_INITIALIZER_LIST( |
+ cache_write_callback_(new CancelableCompletionCallback<Transaction>( |
+ this, &Transaction::OnCacheWriteCompleted))) { |
} |
// Clean up the transaction. |
@@ -333,15 +336,18 @@ |
// Called to write data to the cache entry. If the write fails, then the |
// cache entry is destroyed. Future calls to this function will just do |
- // nothing without side-effect. |
- void WriteToEntry(int index, int offset, IOBuffer* data, int data_len); |
+ // nothing without side-effect. Returns a network error code. |
+ int WriteToEntry(int index, int offset, IOBuffer* data, int data_len, |
+ CompletionCallback* callback); |
// Called to write response_ to the cache entry. |truncated| indicates if the |
// entry should be marked as incomplete. |
void WriteResponseInfoToEntry(bool truncated); |
- // Called to append response data to the cache entry. |
- void AppendResponseDataToEntry(IOBuffer* data, int data_len); |
+ // Called to append response data to the cache entry. Returns a network error |
+ // code. |
+ int AppendResponseDataToEntry(IOBuffer* data, int data_len, |
+ CompletionCallback* callback); |
// Called to truncate response content in the entry. |
void TruncateResponseData(); |
@@ -367,6 +373,9 @@ |
// working with range requests. |
int DoPartialCacheReadCompleted(int result); |
+ // Performs the needed work after writing data to the cache. |
+ int DoCacheWriteCompleted(int result); |
+ |
// Called to signal completion of the network transaction's Start method: |
void OnNetworkInfoAvailable(int result); |
@@ -376,6 +385,9 @@ |
// Called to signal completion of the cache's ReadData method: |
void OnCacheReadCompleted(int result); |
+ // Called to signal completion of the cache's WriteData method: |
+ void OnCacheWriteCompleted(int result); |
+ |
scoped_refptr<LoadLog> load_log_; |
const HttpRequestInfo* request_; |
scoped_ptr<HttpRequestInfo> custom_request_; |
@@ -404,6 +416,8 @@ |
CompletionCallbackImpl<Transaction> network_read_callback_; |
scoped_refptr<CancelableCompletionCallback<Transaction> > |
cache_read_callback_; |
+ scoped_refptr<CancelableCompletionCallback<Transaction> > |
+ cache_write_callback_; |
}; |
HttpCache::Transaction::~Transaction() { |
@@ -422,9 +436,10 @@ |
// If there is an outstanding callback, mark it as cancelled so running it |
// does nothing. |
cache_read_callback_->Cancel(); |
+ cache_write_callback_->Cancel(); |
- // We could still have a cache read in progress, so we just null the cache_ |
- // pointer to signal that we are dead. See OnCacheReadCompleted. |
+ // We could still have a cache read or write in progress, so we just null the |
+ // cache_ pointer to signal that we are dead. See DoCacheReadCompleted. |
cache_ = NULL; |
} |
@@ -1272,7 +1287,7 @@ |
int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) { |
DCHECK(entry_); |
int rv; |
- cache_read_callback_->AddRef(); // Balanced in DoCacheReadCompleted. |
+ cache_read_callback_->AddRef(); // Balanced in OnCacheReadCompleted. |
if (partial_.get()) { |
rv = partial_->CacheRead(entry_->disk_entry, data, data_len, |
cache_read_callback_); |
@@ -1282,11 +1297,12 @@ |
} |
read_buf_ = data; |
read_buf_len_ = data_len; |
- if (rv >= 0) { |
+ if (rv != ERR_IO_PENDING) |
+ cache_read_callback_->Release(); |
+ |
+ if (rv >= 0) |
rv = DoCacheReadCompleted(rv); |
- } else if (rv != ERR_IO_PENDING) { |
- cache_read_callback_->Release(); |
- } |
+ |
return rv; |
} |
@@ -1301,22 +1317,25 @@ |
return read_ok ? OK : ERR_CACHE_READ_FAILURE; |
} |
-void HttpCache::Transaction::WriteToEntry(int index, int offset, |
- IOBuffer* data, int data_len) { |
+int HttpCache::Transaction::WriteToEntry(int index, int offset, |
+ IOBuffer* data, int data_len, |
+ CompletionCallback* callback) { |
if (!entry_) |
- return; |
+ return data_len; |
int rv = 0; |
if (!partial_.get() || !data_len) { |
- rv = entry_->disk_entry->WriteData(index, offset, data, data_len, NULL, |
+ rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback, |
true); |
} else { |
- rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, NULL); |
+ rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback); |
} |
- if (rv != data_len) { |
+ |
+ if (rv >= 0 && rv != data_len) { |
DLOG(ERROR) << "failed to write response data to cache"; |
DoneWritingToEntry(false); |
} |
+ return rv; |
} |
void HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) { |
@@ -1354,13 +1373,14 @@ |
} |
} |
-void HttpCache::Transaction::AppendResponseDataToEntry(IOBuffer* data, |
- int data_len) { |
+int HttpCache::Transaction::AppendResponseDataToEntry( |
+ IOBuffer* data, int data_len, CompletionCallback* callback) { |
if (!entry_ || !data_len) |
- return; |
+ return data_len; |
int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); |
- WriteToEntry(kResponseContentIndex, current_size, data, data_len); |
+ return WriteToEntry(kResponseContentIndex, current_size, data, data_len, |
+ callback); |
} |
void HttpCache::Transaction::TruncateResponseData() { |
@@ -1368,7 +1388,8 @@ |
return; |
// Truncate the stream. |
- WriteToEntry(kResponseContentIndex, 0, NULL, 0); |
+ int rv = WriteToEntry(kResponseContentIndex, 0, NULL, 0, NULL); |
+ DCHECK(rv != ERR_IO_PENDING); |
} |
void HttpCache::Transaction::DoneWritingToEntry(bool success) { |
@@ -1398,15 +1419,13 @@ |
if (revoked()) |
return HandleResult(ERR_UNEXPECTED); |
- AppendResponseDataToEntry(read_buf_, result); |
+ cache_write_callback_->AddRef(); // Balanced in DoCacheWriteCompleted. |
- if (partial_.get()) |
- return DoPartialNetworkReadCompleted(result); |
+ result = AppendResponseDataToEntry(read_buf_, result, cache_write_callback_); |
+ if (result == ERR_IO_PENDING) |
+ return result; |
- if (result == 0) // End of file. |
- DoneWritingToEntry(true); |
- |
- return HandleResult(result); |
+ return DoCacheWriteCompleted(result); |
} |
int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) { |
@@ -1428,7 +1447,6 @@ |
int HttpCache::Transaction::DoCacheReadCompleted(int result) { |
DCHECK(cache_); |
- cache_read_callback_->Release(); // Balance the AddRef() from Start(). |
if (revoked()) |
return HandleResult(ERR_UNEXPECTED); |
@@ -1463,6 +1481,25 @@ |
return HandleResult(result); |
} |
+int HttpCache::Transaction::DoCacheWriteCompleted(int result) { |
+ DCHECK(cache_); |
+ // Balance the AddRef from DoNetworkReadCompleted. |
+ cache_write_callback_->Release(); |
+ if (revoked()) |
+ return HandleResult(ERR_UNEXPECTED); |
+ |
+ if (result < 0) |
+ return HandleResult(result); |
+ |
+ if (partial_.get()) |
+ return DoPartialNetworkReadCompleted(result); |
+ |
+ if (result == 0) // End of file. |
+ DoneWritingToEntry(true); |
+ |
+ return HandleResult(result); |
+} |
+ |
void HttpCache::Transaction::OnNetworkInfoAvailable(int result) { |
DCHECK(result != ERR_IO_PENDING); |
@@ -1577,9 +1614,14 @@ |
} |
void HttpCache::Transaction::OnCacheReadCompleted(int result) { |
+ cache_read_callback_->Release(); // Balance the AddRef from ReadFromEntry. |
DoCacheReadCompleted(result); |
} |
+void HttpCache::Transaction::OnCacheWriteCompleted(int result) { |
+ DoCacheWriteCompleted(result); |
+} |
+ |
//----------------------------------------------------------------------------- |
HttpCache::HttpCache(HostResolver* host_resolver, |