Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Unified Diff: net/http/http_cache.cc

Issue 455623003: stale-while-revalidate experimental implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/http/http_cache.cc
diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc
index bfb21ade1d1edcbba4264b3837cefdc25ac2c9fb..696954dbb33cb317e38c3ebb0c39b79e90f62dbd 100644
--- a/net/http/http_cache.cc
+++ b/net/http/http_cache.cc
@@ -292,12 +292,108 @@ class HttpCache::QuicServerInfoFactoryAdaptor : public QuicServerInfoFactory {
};
//-----------------------------------------------------------------------------
+
+class HttpCache::AsyncValidation {
+ public:
+ AsyncValidation(const HttpRequestInfo& original_request, HttpCache* cache)
+ : request_(original_request), cache_(cache) {}
+ ~AsyncValidation() {}
+
+ void Start(const BoundNetLog& net_log);
+
+ private:
+ void OnStarted(int result);
+ void DoRead();
+ void OnRead(int result);
+
+ // Terminate this request with net error code |result|. Logs the transaction
+ // result and asks HttpCache to delete this object.
+ // If there was a client or server certificate error, it cannot be recovered
+ // asynchronously, so we need to prevent futures attempts to asynchronously
+ // fetch the resource. In this case, the cache entry is doomed.
+ void Terminate(int result);
+
+ HttpRequestInfo request_;
+ scoped_refptr<IOBuffer> buf_;
+ CompletionCallback read_callback_;
+ scoped_ptr<Transaction> transaction_;
+
+ // The HttpCache object owns this object. This object is always deleted before
+ // the pointer to the cache becomes invalid.
+ HttpCache* cache_;
+
+ DISALLOW_COPY_AND_ASSIGN(AsyncValidation);
+};
+
+void HttpCache::AsyncValidation::Start(const BoundNetLog& net_log) {
+ transaction_.reset(new Transaction(IDLE, cache_));
+ request_.load_flags |= LOAD_VALIDATE_CACHE;
+ // This use of base::Unretained is safe because |transaction_| is owned by
+ // this object.
+ read_callback_ = base::Bind(&AsyncValidation::OnRead, base::Unretained(this));
+ // This use of base::Unretained is safe as above.
+ int rv = transaction_->Start(
+ &request_,
+ base::Bind(&AsyncValidation::OnStarted, base::Unretained(this)),
+ net_log);
+
+ if (rv == ERR_IO_PENDING)
+ return;
+
+ OnStarted(rv);
+}
+
+void HttpCache::AsyncValidation::OnStarted(int result) {
+ if (result != OK) {
+ DVLOG(1) << "Asynchronous transaction start failed for " << request_.url;
+ Terminate(result);
+ return;
+ }
+
+ DoRead();
+}
+
+void HttpCache::AsyncValidation::DoRead() {
+ const size_t kBufSize = 4096;
+ if (!buf_)
+ buf_ = new IOBuffer(kBufSize);
+
+ int rv = 0;
+ while (rv > 0) {
yhirano 2014/08/26 07:52:01 dead code :)
Adam Rice 2014/08/26 13:38:41 Amazing! This doesn't seem to make any difference
+ rv = transaction_->Read(buf_.get(), kBufSize, read_callback_);
+ }
+
+ if (rv == ERR_IO_PENDING)
+ return;
+
+ OnRead(rv);
+}
+
+void HttpCache::AsyncValidation::OnRead(int result) {
+ if (result > 0) {
+ DoRead();
+ return;
+ }
+ Terminate(result);
+}
+
+void HttpCache::AsyncValidation::Terminate(int result) {
+ if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED || IsCertificateError(result))
+ cache_->DoomEntry(transaction_->key(), transaction_.get());
+ transaction_->net_log().EndEventWithNetErrorCode(
+ NetLog::TYPE_ASYNC_REVALIDATION, result);
+ cache_->DeleteAsyncValidation(this);
+ // |this| is deleted.
+}
+
+//-----------------------------------------------------------------------------
HttpCache::HttpCache(const net::HttpNetworkSession::Params& params,
BackendFactory* backend_factory)
: net_log_(params.net_log),
backend_factory_(backend_factory),
building_backend_(false),
bypass_lock_for_test_(false),
+ use_stale_while_revalidate_(params.use_stale_while_revalidate),
mode_(NORMAL),
network_layer_(new HttpNetworkLayer(new HttpNetworkSession(params))),
weak_factory_(this) {
@@ -313,6 +409,7 @@ HttpCache::HttpCache(HttpNetworkSession* session,
backend_factory_(backend_factory),
building_backend_(false),
bypass_lock_for_test_(false),
+ use_stale_while_revalidate_(session->params().use_stale_while_revalidate),
mode_(NORMAL),
network_layer_(new HttpNetworkLayer(session)),
weak_factory_(this) {
@@ -325,10 +422,14 @@ HttpCache::HttpCache(HttpTransactionFactory* network_layer,
backend_factory_(backend_factory),
building_backend_(false),
bypass_lock_for_test_(false),
+ use_stale_while_revalidate_(false),
mode_(NORMAL),
network_layer_(network_layer),
weak_factory_(this) {
SetupQuicServerInfoFactory(network_layer_->GetSession());
+ HttpNetworkSession* session = network_layer_->GetSession();
+ if (session)
+ use_stale_while_revalidate_ = session->params().use_stale_while_revalidate;
}
HttpCache::~HttpCache() {
@@ -350,6 +451,7 @@ HttpCache::~HttpCache() {
}
STLDeleteElements(&doomed_entries_);
+ STLDeleteElements(&async_validations_);
// Before deleting pending_ops_, we have to make sure that the disk cache is
// done with said operations, or it will attempt to use deleted data.
@@ -1038,6 +1140,20 @@ void HttpCache::ProcessPendingQueue(ActiveEntry* entry) {
base::Bind(&HttpCache::OnProcessPendingQueue, GetWeakPtr(), entry));
}
+void HttpCache::PerformAsyncValidation(const HttpRequestInfo& original_request,
+ const BoundNetLog& net_log) {
yhirano 2014/08/26 07:52:01 DCHECK(use_stale_while_revalidate_)?
Adam Rice 2014/08/26 13:38:41 Done.
+ scoped_ptr<AsyncValidation> job(new AsyncValidation(original_request, this));
+ job->Start(net_log);
yhirano 2014/08/26 07:52:01 When all operations are done synchronously, |this-
Adam Rice 2014/08/26 13:38:41 Good catch! Thanks! I have added a test (Synchron
+ bool insert_ok = async_validations_.insert(job.release()).second;
+ DCHECK(insert_ok);
+}
+
+void HttpCache::DeleteAsyncValidation(AsyncValidation* async_validation) {
+ size_t erased = async_validations_.erase(async_validation);
+ DCHECK_EQ(1U, erased);
+ delete async_validation;
+}
+
void HttpCache::OnProcessPendingQueue(ActiveEntry* entry) {
entry->will_process_pending_queue = false;
DCHECK(!entry->writer);

Powered by Google App Engine
This is Rietveld 408576698