Process Alt-Svc headers.
[chromium-blink-merge.git] / content / browser / net / view_http_cache_job_factory.cc
blob6b46b618ea26f20eac1b96411d4c31517080f408
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"
7 #include "base/bind.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"
23 namespace content {
24 namespace {
26 // A job subclass that dumps an HTTP cache entry.
27 class ViewHttpCacheJob : public net::URLRequestJob {
28 public:
29 ViewHttpCacheJob(net::URLRequest* request,
30 net::NetworkDelegate* network_delegate)
31 : net::URLRequestJob(request, network_delegate),
32 core_(new Core),
33 callback_(base::Bind(&ViewHttpCacheJob::OnStartCompleted,
34 base::Unretained(this))),
35 weak_factory_(this) {
38 // net::URLRequestJob implementation.
39 void Start() override;
40 void Kill() 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);
51 private:
52 class Core : public base::RefCounted<Core> {
53 public:
54 Core()
55 : data_offset_(0),
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.
62 void Orphan() {
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);
70 private:
71 friend class base::RefCounted<Core>;
73 ~Core() {}
75 // Called when ViewCacheHelper completes the operation.
76 void OnIOComplete(int result);
78 std::string data_;
79 int data_offset_;
80 net::ViewCacheHelper cache_helper_;
81 net::CompletionCallback callback_;
82 base::Closure user_callback_;
84 DISALLOW_COPY_AND_ASSIGN(Core);
87 ~ViewHttpCacheJob() override {}
89 void StartAsync();
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(
102 FROM_HERE,
103 base::Bind(&ViewHttpCacheJob::StartAsync, weak_factory_.GetWeakPtr()));
106 void ViewHttpCacheJob::Kill() {
107 weak_factory_.InvalidateWeakPtrs();
108 if (core_.get()) {
109 core_->Orphan();
110 core_ = NULL;
112 net::URLRequestJob::Kill();
115 void ViewHttpCacheJob::StartAsync() {
116 DCHECK(request());
118 if (!request())
119 return;
121 int rv = core_->Start(*request(), callback_);
122 if (rv != net::ERR_IO_PENDING) {
123 DCHECK_EQ(net::OK, rv);
124 OnStartCompleted();
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));
141 int rv;
142 if (cache_key.empty()) {
143 rv = cache_helper_.GetContentsHTML(request.context(),
144 kChromeUINetworkViewCacheURL,
145 &data_, callback_);
146 } else {
147 rv = cache_helper_.GetEntryInfoHTML(cache_key, request.context(),
148 &data_, callback_);
151 if (rv == net::ERR_IO_PENDING)
152 user_callback_ = callback;
154 return rv;
157 bool ViewHttpCacheJob::Core::GetMimeType(std::string* mime_type) const {
158 mime_type->assign("text/html");
159 return true;
162 bool ViewHttpCacheJob::Core::GetCharset(std::string* charset) {
163 charset->assign("UTF-8");
164 return true;
167 bool ViewHttpCacheJob::Core::ReadRawData(net::IOBuffer* buf,
168 int buf_size,
169 int* bytes_read) {
170 DCHECK(bytes_read);
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;
177 return true;
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|
187 // after Release().
188 Release(); // Acquired on Start().
191 } // namespace.
193 // Static.
194 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) {
195 return url.SchemeIs(kChromeUIScheme) &&
196 url.host() == kChromeUINetworkViewCacheHost;
199 // Static.
200 net::URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest(
201 net::URLRequest* request, net::NetworkDelegate* network_delegate) {
202 return new ViewHttpCacheJob(request, network_delegate);
205 } // namespace content