Chromium Code Reviews| Index: net/http/http_cache_transaction.cc |
| diff --git a/net/http/http_cache_transaction.cc b/net/http/http_cache_transaction.cc |
| index 50e9da10ea8da4c3747b3e33011a6949aee72dca..23742a5dbf602d052c7036bbdc06d831e3a76d7c 100644 |
| --- a/net/http/http_cache_transaction.cc |
| +++ b/net/http/http_cache_transaction.cc |
| @@ -27,6 +27,7 @@ |
| #include "base/strings/string_piece.h" |
| #include "base/strings/string_util.h" |
| #include "base/strings/stringprintf.h" |
| +#include "base/time/clock.h" |
| #include "base/time/time.h" |
| #include "base/values.h" |
| #include "net/base/completion_callback.h" |
| @@ -741,7 +742,9 @@ int HttpCache::Transaction::HandleResult(int rv) { |
| return rv; |
| } |
| -// A few common patterns: (Foo* means Foo -> FooComplete) |
| +// A few common patterns: Foo* means Foo -> FooComplete |
| +// [Foo] means state Foo may be skipped |
|
rvargas (doing something else)
2015/01/09 02:25:34
Instead of adding an optional state pairs to known
jkarlin
2015/01/09 14:03:50
Done.
|
| +// |
| // |
| // 1. Not-cached entry: |
| // Start(): |
| @@ -756,7 +759,8 @@ int HttpCache::Transaction::HandleResult(int rv) { |
| // 2. Cached entry, no validation: |
| // Start(): |
| // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
| -// -> BeginPartialCacheValidation() -> BeginCacheValidation() -> |
| +// -> [CacheToggleUnusedSincePrefetch*] -> CacheReadResponseContinue -> |
| +// BeginPartialCacheValidation() -> BeginCacheValidation() -> |
| // SetupEntryForRead() |
| // |
| // Read(): |
| @@ -765,10 +769,11 @@ int HttpCache::Transaction::HandleResult(int rv) { |
| // 3. Cached entry, validation (304): |
| // Start(): |
| // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
| -// -> BeginPartialCacheValidation() -> BeginCacheValidation() -> |
| -// SendRequest* -> SuccessfulSendRequest -> UpdateCachedResponse -> |
| -// CacheWriteResponse* -> UpdateCachedResponseComplete -> |
| -// OverwriteCachedResponse -> PartialHeadersReceived |
| +// -> [CacheToggleUnusedSincePrefetch*] -> CacheReadResponseContinue -> |
| +// BeginPartialCacheValidation() -> BeginCacheValidation() -> SendRequest* -> |
| +// SuccessfulSendRequest -> UpdateCachedResponse -> CacheWriteResponse* -> |
| +// UpdateCachedResponseComplete -> OverwriteCachedResponse -> |
| +// PartialHeadersReceived |
| // |
| // Read(): |
| // CacheReadData* |
| @@ -776,10 +781,10 @@ int HttpCache::Transaction::HandleResult(int rv) { |
| // 4. Cached entry, validation and replace (200): |
| // Start(): |
| // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
| -// -> BeginPartialCacheValidation() -> BeginCacheValidation() -> |
| -// SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> |
| -// CacheWriteResponse* -> DoTruncateCachedData* -> TruncateCachedMetadata* -> |
| -// PartialHeadersReceived |
| +// -> [CacheToggleUnusedSincePrefetch*] -> CacheReadResponseContinue -> |
| +// BeginPartialCacheValidation() -> BeginCacheValidation() -> SendRequest* -> |
| +// SuccessfulSendRequest -> OverwriteCachedResponse -> CacheWriteResponse* -> |
| +// DoTruncateCachedData* -> TruncateCachedMetadata* -> PartialHeadersReceived |
| // |
| // Read(): |
| // NetworkRead* -> CacheWriteData* |
| @@ -787,7 +792,8 @@ int HttpCache::Transaction::HandleResult(int rv) { |
| // 5. Sparse entry, partially cached, byte range request: |
| // Start(): |
| // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
| -// -> BeginPartialCacheValidation() -> CacheQueryData* -> |
| +// -> [CacheToggleUnusedSincePrefetch*] -> CacheReadResponseContinue -> |
| +// BeginPartialCacheValidation() -> CacheQueryData* -> |
| // ValidateEntryHeadersAndContinue() -> StartPartialCacheValidation -> |
| // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* -> |
| // SuccessfulSendRequest -> UpdateCachedResponse -> CacheWriteResponse* -> |
| @@ -831,8 +837,9 @@ int HttpCache::Transaction::HandleResult(int rv) { |
| // itself. |
| // Start(): |
| // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
| -// -> BeginPartialCacheValidation() -> BeginCacheValidation() -> |
| -// SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse |
| +// -> [CacheToggleUnusedSincePrefetch*] -> CacheReadResponseContinue -> |
| +// BeginPartialCacheValidation() -> BeginCacheValidation() -> SendRequest* -> |
| +// SuccessfulSendRequest -> OverwriteCachedResponse |
| // |
| // 10. HEAD. Sparse entry, partially cached: |
| // Serve the request from the cache, as long as it doesn't require |
| @@ -842,7 +849,7 @@ int HttpCache::Transaction::HandleResult(int rv) { |
| // |
| // Start(): Basically the same as example 7, as we never create a partial_ |
| // object for this request. |
| -// |
| + |
| int HttpCache::Transaction::DoLoop(int result) { |
| DCHECK(next_state_ != STATE_NONE); |
| @@ -951,6 +958,16 @@ int HttpCache::Transaction::DoLoop(int result) { |
| case STATE_CACHE_READ_RESPONSE_COMPLETE: |
| rv = DoCacheReadResponseComplete(rv); |
| break; |
| + case STATE_CACHE_READ_RESPONSE_CONTINUE: |
| + rv = DoCacheReadResponseContinue(); |
|
rvargas (doing something else)
2015/01/09 02:25:33
dcheck rv == ok
jkarlin
2015/01/09 14:03:50
Done.
|
| + break; |
| + case STATE_TOGGLE_UNUSED_SINCE_PREFETCH: |
| + DCHECK_EQ(OK, rv); |
| + rv = DoCacheToggleUnusedSincePrefetch(); |
| + break; |
| + case STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE: |
| + rv = DoCacheToggleUnusedSincePrefetchComplete(rv); |
| + break; |
| case STATE_CACHE_WRITE_RESPONSE: |
| DCHECK_EQ(OK, rv); |
| rv = DoCacheWriteResponse(); |
| @@ -1630,6 +1647,7 @@ int HttpCache::Transaction::DoUpdateCachedResponse() { |
| response_.response_time = new_response_->response_time; |
| response_.request_time = new_response_->request_time; |
| response_.network_accessed = new_response_->network_accessed; |
| + response_.unused_since_prefetch = new_response_->unused_since_prefetch; |
| if (response_.headers->HasHeaderValue("cache-control", "no-store")) { |
| if (!entry_->doomed) { |
| @@ -1848,6 +1866,42 @@ int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { |
| if (response_.headers->GetContentLength() == current_size) |
| truncated_ = false; |
| + if ((response_.unused_since_prefetch && |
| + !(request_->load_flags & LOAD_PREFETCH)) || |
| + (!response_.unused_since_prefetch && |
| + (request_->load_flags & LOAD_PREFETCH))) { |
| + // Either this is the first use of an entry since it was prefetched or |
| + // this is a prefetch. The value of response.unused_since_prefetch is valid |
| + // for this transaction but the bit needs to be flipped in storage. |
| + next_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH; |
| + return OK; |
| + } |
| + |
| + next_state_ = STATE_CACHE_READ_RESPONSE_CONTINUE; |
| + return OK; |
| +} |
| + |
| +int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetch() { |
| + // Write back the toggled value for the next use of this entry. |
| + response_.unused_since_prefetch = !response_.unused_since_prefetch; |
| + |
| + // TODO(jkarlin): If DoUpdateCachedResponse is also called for this |
| + // transaction then metadata will be written to cache twice. If prefetching |
| + // becomes more common, consider combining the writes. |
| + target_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE; |
| + next_state_ = STATE_CACHE_WRITE_RESPONSE; |
| + return OK; |
| +} |
| + |
| +int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetchComplete( |
| + int result) { |
| + // Restore the original value for this transaction. |
| + response_.unused_since_prefetch = !response_.unused_since_prefetch; |
| + next_state_ = STATE_CACHE_READ_RESPONSE_CONTINUE; |
| + return OK; |
| +} |
| + |
| +int HttpCache::Transaction::DoCacheReadResponseContinue() { |
|
rvargas (doing something else)
2015/01/09 02:25:34
Ideally states should be named by what is done in
jkarlin
2015/01/09 14:03:50
Yeah, I had a hell of a time coming up with an acc
|
| // We now have access to the cache entry. |
| // |
| // o if we are a reader for the transaction, then we can start reading the |
| @@ -1861,6 +1915,7 @@ int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { |
| // conditionalized request (if-modified-since / if-none-match). We check |
| // if the request headers define a validation request. |
| // |
| + int result = ERR_FAILED; |
| switch (mode_) { |
| case READ: |
| UpdateTransactionPattern(PATTERN_ENTRY_USED); |
| @@ -1875,7 +1930,6 @@ int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { |
| case WRITE: |
| default: |
| NOTREACHED(); |
| - result = ERR_FAILED; |
| } |
| return result; |
| } |
| @@ -2545,6 +2599,16 @@ ValidationType HttpCache::Transaction::RequiresValidation() { |
| if (effective_load_flags_ & LOAD_PREFERRING_CACHE) |
| return VALIDATION_NONE; |
| + if (response_.unused_since_prefetch && |
| + !(effective_load_flags_ & LOAD_PREFETCH) && |
| + response_.headers->GetCurrentAge( |
| + response_.request_time, response_.response_time, |
| + cache_->clock_->Now()) < TimeDelta::FromMinutes(kPrefetchReuseMins)) { |
| + // The first use of a resource after prefetch within a short window skips |
| + // validation. |
| + return VALIDATION_NONE; |
| + } |
| + |
| if (effective_load_flags_ & (LOAD_VALIDATE_CACHE | LOAD_ASYNC_REVALIDATION)) |
| return VALIDATION_SYNCHRONOUS; |
| @@ -2552,8 +2616,9 @@ ValidationType HttpCache::Transaction::RequiresValidation() { |
| return VALIDATION_SYNCHRONOUS; |
| ValidationType validation_required_by_headers = |
| - response_.headers->RequiresValidation( |
| - response_.request_time, response_.response_time, Time::Now()); |
| + response_.headers->RequiresValidation(response_.request_time, |
| + response_.response_time, |
| + cache_->clock_->Now()); |
| if (validation_required_by_headers == VALIDATION_ASYNCHRONOUS) { |
| // Asynchronous revalidation is only supported for GET and HEAD methods. |
| @@ -2614,7 +2679,8 @@ bool HttpCache::Transaction::ConditionalizeRequest() { |
| response_.headers->GetFreshnessLifetimes(response_.response_time); |
| if (lifetimes.staleness > TimeDelta()) { |
| TimeDelta current_age = response_.headers->GetCurrentAge( |
| - response_.request_time, response_.response_time, Time::Now()); |
| + response_.request_time, response_.response_time, |
| + cache_->clock_->Now()); |
| custom_request_->extra_headers.SetHeader( |
| kFreshnessHeader, |