summaryrefslogtreecommitdiffstats
path: root/net/http/http_cache.cc
diff options
context:
space:
mode:
authorrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-02 19:10:07 +0000
committerrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-02 19:10:07 +0000
commit47b950579d39dc79127c1dc69f44c33c8ad269b0 (patch)
treea12052d4046260987847c3577b90912915431dcc /net/http/http_cache.cc
parent7e5a07433628b7383ed7402bc18bcc6676d8475f (diff)
downloadchromium_src-47b950579d39dc79127c1dc69f44c33c8ad269b0.zip
chromium_src-47b950579d39dc79127c1dc69f44c33c8ad269b0.tar.gz
chromium_src-47b950579d39dc79127c1dc69f44c33c8ad269b0.tar.bz2
Http cache: Expose storing metadata on a given entry.
BUG=32406 TEST=unittests Review URL: http://codereview.chromium.org/660041 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40400 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/http/http_cache.cc')
-rw-r--r--net/http/http_cache.cc88
1 files changed, 87 insertions, 1 deletions
diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc
index cf95c10..32e8e55 100644
--- a/net/http/http_cache.cc
+++ b/net/http/http_cache.cc
@@ -19,6 +19,7 @@
#include "base/ref_counted.h"
#include "base/string_util.h"
#include "net/base/io_buffer.h"
+#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/disk_cache.h"
#include "net/http/http_cache_transaction.h"
@@ -124,6 +125,82 @@ class HttpCache::BackendCallback : public CallbackRunner<Tuple1<int> > {
//-----------------------------------------------------------------------------
+// This class encapsulates a transaction whose only purpose is to write metadata
+// to a given entry.
+class HttpCache::MetadataWriter {
+ public:
+ explicit MetadataWriter(HttpCache::Transaction* trans)
+ : transaction_(trans),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ callback_(this, &MetadataWriter::OnIOComplete)) {}
+ ~MetadataWriter() {}
+
+ // Implementes the bulk of HttpCache::WriteMetadata.
+ void Write(const GURL& url, base::Time expected_response_time, IOBuffer* buf,
+ int buf_len);
+
+ private:
+ void VerifyResponse(int result);
+ void SelfDestroy();
+ void OnIOComplete(int result);
+
+ scoped_ptr<HttpCache::Transaction> transaction_;
+ bool verified_;
+ scoped_refptr<IOBuffer> buf_;
+ int buf_len_;
+ base::Time expected_response_time_;
+ CompletionCallbackImpl<MetadataWriter> callback_;
+ HttpRequestInfo request_info_;
+ DISALLOW_COPY_AND_ASSIGN(MetadataWriter);
+};
+
+void HttpCache::MetadataWriter::Write(const GURL& url,
+ base::Time expected_response_time,
+ IOBuffer* buf, int buf_len) {
+ DCHECK_GT(buf_len, 0);
+ DCHECK(buf);
+ DCHECK(buf->data());
+ request_info_.url = url;
+ request_info_.method = "GET";
+ request_info_.load_flags = LOAD_ONLY_FROM_CACHE;
+
+ expected_response_time_ = expected_response_time;
+ buf_ = buf;
+ buf_len_ = buf_len;
+ verified_ = false;
+
+ int rv = transaction_->Start(&request_info_, &callback_, NULL);
+ if (rv != ERR_IO_PENDING)
+ VerifyResponse(rv);
+}
+
+void HttpCache::MetadataWriter::VerifyResponse(int result) {
+ verified_ = true;
+ if (result != OK)
+ return SelfDestroy();
+
+ const HttpResponseInfo* response_info = transaction_->GetResponseInfo();
+ DCHECK(response_info->was_cached);
+ if (response_info->response_time != expected_response_time_)
+ return SelfDestroy();
+
+ result = transaction_->WriteMetadata(buf_, buf_len_, &callback_);
+ if (result != ERR_IO_PENDING)
+ SelfDestroy();
+}
+
+void HttpCache::MetadataWriter::SelfDestroy() {
+ delete this;
+}
+
+void HttpCache::MetadataWriter::OnIOComplete(int result) {
+ if (!verified_)
+ return VerifyResponse(result);
+ SelfDestroy();
+}
+
+//-----------------------------------------------------------------------------
+
HttpCache::HttpCache(NetworkChangeNotifier* network_change_notifier,
HostResolver* host_resolver,
ProxyService* proxy_service,
@@ -284,7 +361,16 @@ bool HttpCache::WriteResponseInfo(disk_cache::Entry* disk_entry,
void HttpCache::WriteMetadata(const GURL& url,
base::Time expected_response_time, IOBuffer* buf,
int buf_len) {
- // TODO(rvargas): Implement me.
+ if (!buf_len)
+ return;
+
+ GetBackend();
+ HttpCache::Transaction* trans =
+ new HttpCache::Transaction(this, enable_range_support_);
+ MetadataWriter* writer = new MetadataWriter(trans);
+
+ // The writer will self destruct when done.
+ writer->Write(url, expected_response_time, buf, buf_len);
}
// Generate a key that can be used inside the cache.