| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/view_http_cache_job_factory.h" | 5 #include "chrome/browser/net/view_http_cache_job_factory.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" |
| 7 #include "chrome/common/url_constants.h" | 8 #include "chrome/common/url_constants.h" |
| 9 #include "net/base/net_errors.h" |
| 8 #include "net/url_request/url_request.h" | 10 #include "net/url_request/url_request.h" |
| 9 #include "net/url_request/url_request_context.h" | 11 #include "net/url_request/url_request_context.h" |
| 10 #include "net/url_request/url_request_simple_job.h" | 12 #include "net/url_request/url_request_simple_job.h" |
| 11 #include "net/url_request/view_cache_helper.h" | 13 #include "net/url_request/view_cache_helper.h" |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 15 // A job subclass that dumps an HTTP cache entry. | 17 // A job subclass that dumps an HTTP cache entry. |
| 16 class ViewHttpCacheJob : public URLRequestSimpleJob { | 18 class ViewHttpCacheJob : public URLRequestJob { |
| 17 public: | 19 public: |
| 18 explicit ViewHttpCacheJob(URLRequest* request) | 20 explicit ViewHttpCacheJob(URLRequest* request) |
| 19 : URLRequestSimpleJob(request) {} | 21 : URLRequestJob(request), data_offset_(0), |
| 22 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 23 callback_(this, &ViewHttpCacheJob::OnIOComplete)) {} |
| 20 | 24 |
| 21 // URLRequestSimpleJob methods: | 25 virtual void Start(); |
| 22 virtual bool GetData(std::string* mime_type, | 26 virtual bool GetMimeType(std::string* mime_type) const; |
| 23 std::string* charset, | 27 virtual bool GetCharset(std::string* charset); |
| 24 std::string* data) const; | 28 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); |
| 25 | 29 |
| 26 private: | 30 private: |
| 27 ~ViewHttpCacheJob() {} | 31 ~ViewHttpCacheJob() {} |
| 32 |
| 33 // Called when ViewCacheHelper completes the operation. |
| 34 void OnIOComplete(int result); |
| 35 |
| 36 std::string data_; |
| 37 int data_offset_; |
| 38 net::ViewCacheHelper cache_helper_; |
| 39 net::CompletionCallbackImpl<ViewHttpCacheJob> callback_; |
| 28 }; | 40 }; |
| 29 | 41 |
| 30 bool ViewHttpCacheJob::GetData(std::string* mime_type, | 42 void ViewHttpCacheJob::Start() { |
| 31 std::string* charset, | 43 if (!request_) |
| 32 std::string* data) const { | 44 return; |
| 45 |
| 46 std::string cache_key = |
| 47 request_->url().spec().substr(strlen(chrome::kNetworkViewCacheURL)); |
| 48 |
| 49 int rv; |
| 50 if (cache_key.empty()) { |
| 51 rv = cache_helper_.GetContentsHTML(request_->context(), |
| 52 chrome::kNetworkViewCacheURL, &data_, |
| 53 &callback_); |
| 54 } else { |
| 55 rv = cache_helper_.GetEntryInfoHTML(cache_key, request_->context(), |
| 56 &data_, &callback_); |
| 57 } |
| 58 |
| 59 if (rv != net::ERR_IO_PENDING) { |
| 60 // Start reading asynchronously so that all error reporting and data |
| 61 // callbacks happen as they would for network requests. |
| 62 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 63 this, &ViewHttpCacheJob::OnIOComplete, rv)); |
| 64 } |
| 65 } |
| 66 |
| 67 bool ViewHttpCacheJob::GetMimeType(std::string* mime_type) const { |
| 33 mime_type->assign("text/html"); | 68 mime_type->assign("text/html"); |
| 34 charset->assign("UTF-8"); | |
| 35 | |
| 36 data->clear(); | |
| 37 | |
| 38 std::string cache_key; | |
| 39 cache_key = | |
| 40 request_->url().spec().substr(strlen(chrome::kNetworkViewCacheURL)); | |
| 41 ViewCacheHelper::GetEntryInfoHTML(cache_key, | |
| 42 request_->context(), | |
| 43 chrome::kNetworkViewCacheURL, | |
| 44 data); | |
| 45 | |
| 46 return true; | 69 return true; |
| 47 } | 70 } |
| 48 | 71 |
| 49 } // namespace | 72 bool ViewHttpCacheJob::GetCharset(std::string* charset) { |
| 73 charset->assign("UTF-8"); |
| 74 return true; |
| 75 } |
| 50 | 76 |
| 51 // static | 77 bool ViewHttpCacheJob::ReadRawData(net::IOBuffer* buf, int buf_size, |
| 78 int* bytes_read) { |
| 79 DCHECK(bytes_read); |
| 80 int remaining = static_cast<int>(data_.size()) - data_offset_; |
| 81 if (buf_size > remaining) |
| 82 buf_size = remaining; |
| 83 memcpy(buf->data(), data_.data() + data_offset_, buf_size); |
| 84 data_offset_ += buf_size; |
| 85 *bytes_read = buf_size; |
| 86 return true; |
| 87 } |
| 88 |
| 89 void ViewHttpCacheJob::OnIOComplete(int result) { |
| 90 DCHECK_EQ(net::OK, result); |
| 91 |
| 92 // Notify that the headers are complete. |
| 93 NotifyHeadersComplete(); |
| 94 } |
| 95 |
| 96 } // namespace. |
| 97 |
| 98 // Static. |
| 52 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) { | 99 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) { |
| 53 return StartsWithASCII(url.spec(), | 100 return StartsWithASCII(url.spec(), chrome::kNetworkViewCacheURL, |
| 54 chrome::kNetworkViewCacheURL, | |
| 55 true /*case_sensitive*/); | 101 true /*case_sensitive*/); |
| 56 } | 102 } |
| 57 | 103 |
| 58 // static | 104 // Static. |
| 59 URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest( | 105 URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest( |
| 60 URLRequest* request) { | 106 URLRequest* request) { |
| 61 return new ViewHttpCacheJob(request); | 107 return new ViewHttpCacheJob(request); |
| 62 } | 108 } |
| OLD | NEW |