Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / net / view_http_cache_job_factory.cc
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.
4
5 #include "content/browser/net/view_http_cache_job_factory.h"
6
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/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"
20
21 namespace content {
22 namespace {
23
24 // A job subclass that dumps an HTTP cache entry.
25 class ViewHttpCacheJob : public net::URLRequestJob {
26  public:
27   ViewHttpCacheJob(net::URLRequest* request,
28                    net::NetworkDelegate* network_delegate)
29       : net::URLRequestJob(request, network_delegate),
30         core_(new Core),
31         callback_(base::Bind(&ViewHttpCacheJob::OnStartCompleted,
32                              base::Unretained(this))),
33         weak_factory_(this) {
34   }
35
36   // net::URLRequestJob implementation.
37   void Start() override;
38   void Kill() override;
39   bool GetMimeType(std::string* mime_type) const override {
40     return core_->GetMimeType(mime_type);
41   }
42   bool GetCharset(std::string* charset) override {
43     return core_->GetCharset(charset);
44   }
45   bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override {
46     return core_->ReadRawData(buf, buf_size, bytes_read);
47   }
48
49  private:
50   class Core : public base::RefCounted<Core> {
51    public:
52     Core()
53         : data_offset_(0),
54           callback_(base::Bind(&Core::OnIOComplete, this)) {
55     }
56
57     int Start(const net::URLRequest& request, const base::Closure& callback);
58
59     // Prevents it from invoking its callback. It will self-delete.
60     void Orphan() {
61       user_callback_.Reset();
62     }
63
64     bool GetMimeType(std::string* mime_type) const;
65     bool GetCharset(std::string* charset);
66     bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read);
67
68    private:
69     friend class base::RefCounted<Core>;
70
71     ~Core() {}
72
73     // Called when ViewCacheHelper completes the operation.
74     void OnIOComplete(int result);
75
76     std::string data_;
77     int data_offset_;
78     net::ViewCacheHelper cache_helper_;
79     net::CompletionCallback callback_;
80     base::Closure user_callback_;
81
82     DISALLOW_COPY_AND_ASSIGN(Core);
83   };
84
85   ~ViewHttpCacheJob() override {}
86
87   void StartAsync();
88   void OnStartCompleted();
89
90   scoped_refptr<Core> core_;
91   base::Closure callback_;
92
93   base::WeakPtrFactory<ViewHttpCacheJob> weak_factory_;
94
95   DISALLOW_COPY_AND_ASSIGN(ViewHttpCacheJob);
96 };
97
98 void ViewHttpCacheJob::Start() {
99   base::MessageLoop::current()->PostTask(
100       FROM_HERE,
101       base::Bind(&ViewHttpCacheJob::StartAsync, weak_factory_.GetWeakPtr()));
102 }
103
104 void ViewHttpCacheJob::Kill() {
105   weak_factory_.InvalidateWeakPtrs();
106   if (core_.get()) {
107     core_->Orphan();
108     core_ = NULL;
109   }
110   net::URLRequestJob::Kill();
111 }
112
113 void ViewHttpCacheJob::StartAsync() {
114   DCHECK(request());
115
116   if (!request())
117     return;
118
119   int rv = core_->Start(*request(), callback_);
120   if (rv != net::ERR_IO_PENDING) {
121     DCHECK_EQ(net::OK, rv);
122     OnStartCompleted();
123   }
124 }
125
126 void ViewHttpCacheJob::OnStartCompleted() {
127   NotifyHeadersComplete();
128 }
129
130 int ViewHttpCacheJob::Core::Start(const net::URLRequest& request,
131                                   const base::Closure& callback) {
132   DCHECK(!callback.is_null());
133   DCHECK(user_callback_.is_null());
134
135   AddRef();  // Released on OnIOComplete().
136   std::string cache_key =
137       request.url().spec().substr(strlen(kChromeUINetworkViewCacheURL));
138
139   int rv;
140   if (cache_key.empty()) {
141     rv = cache_helper_.GetContentsHTML(request.context(),
142                                        kChromeUINetworkViewCacheURL,
143                                        &data_, callback_);
144   } else {
145     rv = cache_helper_.GetEntryInfoHTML(cache_key, request.context(),
146                                         &data_, callback_);
147   }
148
149   if (rv == net::ERR_IO_PENDING)
150     user_callback_ = callback;
151
152   return rv;
153 }
154
155 bool ViewHttpCacheJob::Core::GetMimeType(std::string* mime_type) const {
156   mime_type->assign("text/html");
157   return true;
158 }
159
160 bool ViewHttpCacheJob::Core::GetCharset(std::string* charset) {
161   charset->assign("UTF-8");
162   return true;
163 }
164
165 bool ViewHttpCacheJob::Core::ReadRawData(net::IOBuffer* buf,
166                                          int buf_size,
167                                          int* bytes_read) {
168   DCHECK(bytes_read);
169   int remaining = static_cast<int>(data_.size()) - data_offset_;
170   if (buf_size > remaining)
171     buf_size = remaining;
172   memcpy(buf->data(), data_.data() + data_offset_, buf_size);
173   data_offset_ += buf_size;
174   *bytes_read = buf_size;
175   return true;
176 }
177
178 void ViewHttpCacheJob::Core::OnIOComplete(int result) {
179   DCHECK_EQ(net::OK, result);
180
181   if (!user_callback_.is_null())
182     user_callback_.Run();
183
184   // We may be holding the last reference to this job. Do not access |this|
185   // after Release().
186   Release();  // Acquired on Start().
187 }
188
189 }  // namespace.
190
191 // Static.
192 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) {
193   return url.SchemeIs(kChromeUIScheme) &&
194          url.host() == kChromeUINetworkViewCacheHost;
195 }
196
197 // Static.
198 net::URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest(
199     net::URLRequest* request, net::NetworkDelegate* network_delegate) {
200   return new ViewHttpCacheJob(request, network_delegate);
201 }
202
203 }  // namespace content