diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-27 22:06:31 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-27 22:06:31 +0000 |
commit | cec6f9b08dd24625cd40df548810561b5a55e7d6 (patch) | |
tree | b3eb73f30533f31a794bf887d7da0eaaf3f13850 /chrome/browser/net | |
parent | 28191891f2389382e4e53d004e04a74bcb1b152e (diff) | |
download | chromium_src-cec6f9b08dd24625cd40df548810561b5a55e7d6.zip chromium_src-cec6f9b08dd24625cd40df548810561b5a55e7d6.tar.gz chromium_src-cec6f9b08dd24625cd40df548810561b5a55e7d6.tar.bz2 |
view-cache: Refactor ViewCacheHelper and ViewHttpCacheJobFactory
to use asynchronous interfaces.
BUG=26730
TEST=unittest
Review URL: http://codereview.chromium.org/2168004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@48438 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/net')
-rw-r--r-- | chrome/browser/net/view_http_cache_job_factory.cc | 92 |
1 files changed, 69 insertions, 23 deletions
diff --git a/chrome/browser/net/view_http_cache_job_factory.cc b/chrome/browser/net/view_http_cache_job_factory.cc index ce10ff1..5a3cd65 100644 --- a/chrome/browser/net/view_http_cache_job_factory.cc +++ b/chrome/browser/net/view_http_cache_job_factory.cc @@ -4,7 +4,9 @@ #include "chrome/browser/net/view_http_cache_job_factory.h" +#include "base/message_loop.h" #include "chrome/common/url_constants.h" +#include "net/base/net_errors.h" #include "net/url_request/url_request.h" #include "net/url_request/url_request_context.h" #include "net/url_request/url_request_simple_job.h" @@ -13,49 +15,93 @@ namespace { // A job subclass that dumps an HTTP cache entry. -class ViewHttpCacheJob : public URLRequestSimpleJob { +class ViewHttpCacheJob : public URLRequestJob { public: explicit ViewHttpCacheJob(URLRequest* request) - : URLRequestSimpleJob(request) {} + : URLRequestJob(request), data_offset_(0), + ALLOW_THIS_IN_INITIALIZER_LIST( + callback_(this, &ViewHttpCacheJob::OnIOComplete)) {} - // URLRequestSimpleJob methods: - virtual bool GetData(std::string* mime_type, - std::string* charset, - std::string* data) const; + virtual void Start(); + virtual bool GetMimeType(std::string* mime_type) const; + virtual bool GetCharset(std::string* charset); + virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); private: ~ViewHttpCacheJob() {} + + // Called when ViewCacheHelper completes the operation. + void OnIOComplete(int result); + + std::string data_; + int data_offset_; + net::ViewCacheHelper cache_helper_; + net::CompletionCallbackImpl<ViewHttpCacheJob> callback_; }; -bool ViewHttpCacheJob::GetData(std::string* mime_type, - std::string* charset, - std::string* data) const { +void ViewHttpCacheJob::Start() { + if (!request_) + return; + + std::string cache_key = + request_->url().spec().substr(strlen(chrome::kNetworkViewCacheURL)); + + int rv; + if (cache_key.empty()) { + rv = cache_helper_.GetContentsHTML(request_->context(), + chrome::kNetworkViewCacheURL, &data_, + &callback_); + } else { + rv = cache_helper_.GetEntryInfoHTML(cache_key, request_->context(), + &data_, &callback_); + } + + if (rv != net::ERR_IO_PENDING) { + // Start reading asynchronously so that all error reporting and data + // callbacks happen as they would for network requests. + MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( + this, &ViewHttpCacheJob::OnIOComplete, rv)); + } +} + +bool ViewHttpCacheJob::GetMimeType(std::string* mime_type) const { mime_type->assign("text/html"); + return true; +} + +bool ViewHttpCacheJob::GetCharset(std::string* charset) { charset->assign("UTF-8"); + return true; +} - data->clear(); +bool ViewHttpCacheJob::ReadRawData(net::IOBuffer* buf, int buf_size, + int* bytes_read) { + DCHECK(bytes_read); + int remaining = static_cast<int>(data_.size()) - data_offset_; + if (buf_size > remaining) + buf_size = remaining; + memcpy(buf->data(), data_.data() + data_offset_, buf_size); + data_offset_ += buf_size; + *bytes_read = buf_size; + return true; +} - std::string cache_key; - cache_key = - request_->url().spec().substr(strlen(chrome::kNetworkViewCacheURL)); - ViewCacheHelper::GetEntryInfoHTML(cache_key, - request_->context(), - chrome::kNetworkViewCacheURL, - data); +void ViewHttpCacheJob::OnIOComplete(int result) { + DCHECK_EQ(net::OK, result); - return true; + // Notify that the headers are complete. + NotifyHeadersComplete(); } -} // namespace +} // namespace. -// static +// Static. bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) { - return StartsWithASCII(url.spec(), - chrome::kNetworkViewCacheURL, + return StartsWithASCII(url.spec(), chrome::kNetworkViewCacheURL, true /*case_sensitive*/); } -// static +// Static. URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest( URLRequest* request) { return new ViewHttpCacheJob(request); |