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/location.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/strings/string_util.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "content/public/common/url_constants.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/net_errors.h"
19 #include "net/url_request/url_request.h"
20 #include "net/url_request/url_request_simple_job.h"
21 #include "net/url_request/view_cache_helper.h"
26 // A job subclass that dumps an HTTP cache entry.
27 class ViewHttpCacheJob
: public net::URLRequestJob
{
29 ViewHttpCacheJob(net::URLRequest
* request
,
30 net::NetworkDelegate
* network_delegate
)
31 : net::URLRequestJob(request
, network_delegate
),
33 callback_(base::Bind(&ViewHttpCacheJob::OnStartCompleted
,
34 base::Unretained(this))),
38 // net::URLRequestJob implementation.
39 void Start() override
;
41 bool GetMimeType(std::string
* mime_type
) const override
{
42 return core_
->GetMimeType(mime_type
);
44 bool GetCharset(std::string
* charset
) override
{
45 return core_
->GetCharset(charset
);
47 bool ReadRawData(net::IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
{
48 return core_
->ReadRawData(buf
, buf_size
, bytes_read
);
52 class Core
: public base::RefCounted
<Core
> {
56 callback_(base::Bind(&Core::OnIOComplete
, this)) {
59 int Start(const net::URLRequest
& request
, const base::Closure
& callback
);
61 // Prevents it from invoking its callback. It will self-delete.
63 user_callback_
.Reset();
66 bool GetMimeType(std::string
* mime_type
) const;
67 bool GetCharset(std::string
* charset
);
68 bool ReadRawData(net::IOBuffer
* buf
, int buf_size
, int *bytes_read
);
71 friend class base::RefCounted
<Core
>;
75 // Called when ViewCacheHelper completes the operation.
76 void OnIOComplete(int result
);
80 net::ViewCacheHelper cache_helper_
;
81 net::CompletionCallback callback_
;
82 base::Closure user_callback_
;
84 DISALLOW_COPY_AND_ASSIGN(Core
);
87 ~ViewHttpCacheJob() override
{}
90 void OnStartCompleted();
92 scoped_refptr
<Core
> core_
;
93 base::Closure callback_
;
95 base::WeakPtrFactory
<ViewHttpCacheJob
> weak_factory_
;
97 DISALLOW_COPY_AND_ASSIGN(ViewHttpCacheJob
);
100 void ViewHttpCacheJob::Start() {
101 base::ThreadTaskRunnerHandle::Get()->PostTask(
103 base::Bind(&ViewHttpCacheJob::StartAsync
, weak_factory_
.GetWeakPtr()));
106 void ViewHttpCacheJob::Kill() {
107 weak_factory_
.InvalidateWeakPtrs();
112 net::URLRequestJob::Kill();
115 void ViewHttpCacheJob::StartAsync() {
121 int rv
= core_
->Start(*request(), callback_
);
122 if (rv
!= net::ERR_IO_PENDING
) {
123 DCHECK_EQ(net::OK
, rv
);
128 void ViewHttpCacheJob::OnStartCompleted() {
129 NotifyHeadersComplete();
132 int ViewHttpCacheJob::Core::Start(const net::URLRequest
& request
,
133 const base::Closure
& callback
) {
134 DCHECK(!callback
.is_null());
135 DCHECK(user_callback_
.is_null());
137 AddRef(); // Released on OnIOComplete().
138 std::string cache_key
=
139 request
.url().spec().substr(strlen(kChromeUINetworkViewCacheURL
));
142 if (cache_key
.empty()) {
143 rv
= cache_helper_
.GetContentsHTML(request
.context(),
144 kChromeUINetworkViewCacheURL
,
147 rv
= cache_helper_
.GetEntryInfoHTML(cache_key
, request
.context(),
151 if (rv
== net::ERR_IO_PENDING
)
152 user_callback_
= callback
;
157 bool ViewHttpCacheJob::Core::GetMimeType(std::string
* mime_type
) const {
158 mime_type
->assign("text/html");
162 bool ViewHttpCacheJob::Core::GetCharset(std::string
* charset
) {
163 charset
->assign("UTF-8");
167 bool ViewHttpCacheJob::Core::ReadRawData(net::IOBuffer
* buf
,
171 int remaining
= static_cast<int>(data_
.size()) - data_offset_
;
172 if (buf_size
> remaining
)
173 buf_size
= remaining
;
174 memcpy(buf
->data(), data_
.data() + data_offset_
, buf_size
);
175 data_offset_
+= buf_size
;
176 *bytes_read
= buf_size
;
180 void ViewHttpCacheJob::Core::OnIOComplete(int result
) {
181 DCHECK_EQ(net::OK
, result
);
183 if (!user_callback_
.is_null())
184 user_callback_
.Run();
186 // We may be holding the last reference to this job. Do not access |this|
188 Release(); // Acquired on Start().
194 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL
& url
) {
195 return url
.SchemeIs(kChromeUIScheme
) &&
196 url
.host() == kChromeUINetworkViewCacheHost
;
200 net::URLRequestJob
* ViewHttpCacheJobFactory::CreateJobForRequest(
201 net::URLRequest
* request
, net::NetworkDelegate
* network_delegate
) {
202 return new ViewHttpCacheJob(request
, network_delegate
);
205 } // namespace content