1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/net/view_http_cache_job_factory.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string_util.h"
14 #include "content/public/common/url_constants.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/net_errors.h"
17 #include "net/url_request/url_request.h"
18 #include "net/url_request/url_request_simple_job.h"
19 #include "net/url_request/view_cache_helper.h"
24 // A job subclass that dumps an HTTP cache entry.
25 class ViewHttpCacheJob : public net::URLRequestJob {
27 ViewHttpCacheJob(net::URLRequest* request,
28 net::NetworkDelegate* network_delegate)
29 : net::URLRequestJob(request, network_delegate),
31 callback_(base::Bind(&ViewHttpCacheJob::OnStartCompleted,
32 base::Unretained(this))),
36 // net::URLRequestJob implementation.
37 virtual void Start() OVERRIDE;
38 virtual void Kill() OVERRIDE;
39 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE{
40 return core_->GetMimeType(mime_type);
42 virtual bool GetCharset(std::string* charset) OVERRIDE{
43 return core_->GetCharset(charset);
45 virtual bool ReadRawData(net::IOBuffer* buf,
46 int buf_size, int *bytes_read) OVERRIDE{
47 return core_->ReadRawData(buf, buf_size, bytes_read);
51 class Core : public base::RefCounted<Core> {
55 callback_(base::Bind(&Core::OnIOComplete, this)) {
58 int Start(const net::URLRequest& request, const base::Closure& callback);
60 // Prevents it from invoking its callback. It will self-delete.
62 user_callback_.Reset();
65 bool GetMimeType(std::string* mime_type) const;
66 bool GetCharset(std::string* charset);
67 bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read);
70 friend class base::RefCounted<Core>;
74 // Called when ViewCacheHelper completes the operation.
75 void OnIOComplete(int result);
79 net::ViewCacheHelper cache_helper_;
80 net::CompletionCallback callback_;
81 base::Closure user_callback_;
83 DISALLOW_COPY_AND_ASSIGN(Core);
86 virtual ~ViewHttpCacheJob() {}
89 void OnStartCompleted();
91 scoped_refptr<Core> core_;
92 base::Closure callback_;
94 base::WeakPtrFactory<ViewHttpCacheJob> weak_factory_;
96 DISALLOW_COPY_AND_ASSIGN(ViewHttpCacheJob);
99 void ViewHttpCacheJob::Start() {
100 base::MessageLoop::current()->PostTask(
102 base::Bind(&ViewHttpCacheJob::StartAsync, weak_factory_.GetWeakPtr()));
105 void ViewHttpCacheJob::Kill() {
106 weak_factory_.InvalidateWeakPtrs();
111 net::URLRequestJob::Kill();
114 void ViewHttpCacheJob::StartAsync() {
120 int rv = core_->Start(*request(), callback_);
121 if (rv != net::ERR_IO_PENDING) {
122 DCHECK_EQ(net::OK, rv);
127 void ViewHttpCacheJob::OnStartCompleted() {
128 NotifyHeadersComplete();
131 int ViewHttpCacheJob::Core::Start(const net::URLRequest& request,
132 const base::Closure& callback) {
133 DCHECK(!callback.is_null());
134 DCHECK(user_callback_.is_null());
136 AddRef(); // Released on OnIOComplete().
137 std::string cache_key =
138 request.url().spec().substr(strlen(kChromeUINetworkViewCacheURL));
141 if (cache_key.empty()) {
142 rv = cache_helper_.GetContentsHTML(request.context(),
143 kChromeUINetworkViewCacheURL,
146 rv = cache_helper_.GetEntryInfoHTML(cache_key, request.context(),
150 if (rv == net::ERR_IO_PENDING)
151 user_callback_ = callback;
156 bool ViewHttpCacheJob::Core::GetMimeType(std::string* mime_type) const {
157 mime_type->assign("text/html");
161 bool ViewHttpCacheJob::Core::GetCharset(std::string* charset) {
162 charset->assign("UTF-8");
166 bool ViewHttpCacheJob::Core::ReadRawData(net::IOBuffer* buf,
170 int remaining = static_cast<int>(data_.size()) - data_offset_;
171 if (buf_size > remaining)
172 buf_size = remaining;
173 memcpy(buf->data(), data_.data() + data_offset_, buf_size);
174 data_offset_ += buf_size;
175 *bytes_read = buf_size;
179 void ViewHttpCacheJob::Core::OnIOComplete(int result) {
180 DCHECK_EQ(net::OK, result);
182 if (!user_callback_.is_null())
183 user_callback_.Run();
185 // We may be holding the last reference to this job. Do not access |this|
187 Release(); // Acquired on Start().
193 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) {
194 return url.SchemeIs(kChromeUIScheme) &&
195 url.host() == kChromeUINetworkViewCacheHost;
199 net::URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest(
200 net::URLRequest* request, net::NetworkDelegate* network_delegate) {
201 return new ViewHttpCacheJob(request, network_delegate);
204 } // namespace content