OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/net/view_http_cache_job_factory.h" | 5 #include "content/browser/net/view_http_cache_job_factory.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/numerics/safe_conversions.h" | |
14 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
15 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
16 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
17 #include "content/public/common/url_constants.h" | 16 #include "content/public/common/url_constants.h" |
18 #include "net/base/completion_callback.h" | 17 #include "net/base/completion_callback.h" |
19 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
20 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
21 #include "net/url_request/url_request_simple_job.h" | 20 #include "net/url_request/url_request_simple_job.h" |
22 #include "net/url_request/view_cache_helper.h" | 21 #include "net/url_request/view_cache_helper.h" |
23 | 22 |
(...skipping 14 matching lines...) Expand all Loading... |
38 | 37 |
39 // net::URLRequestJob implementation. | 38 // net::URLRequestJob implementation. |
40 void Start() override; | 39 void Start() override; |
41 void Kill() override; | 40 void Kill() override; |
42 bool GetMimeType(std::string* mime_type) const override { | 41 bool GetMimeType(std::string* mime_type) const override { |
43 return core_->GetMimeType(mime_type); | 42 return core_->GetMimeType(mime_type); |
44 } | 43 } |
45 bool GetCharset(std::string* charset) override { | 44 bool GetCharset(std::string* charset) override { |
46 return core_->GetCharset(charset); | 45 return core_->GetCharset(charset); |
47 } | 46 } |
48 int ReadRawData(net::IOBuffer* buf, int buf_size) override { | 47 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override { |
49 return core_->ReadRawData(buf, buf_size); | 48 return core_->ReadRawData(buf, buf_size, bytes_read); |
50 } | 49 } |
51 | 50 |
52 private: | 51 private: |
53 class Core : public base::RefCounted<Core> { | 52 class Core : public base::RefCounted<Core> { |
54 public: | 53 public: |
55 Core() | 54 Core() |
56 : data_offset_(0), | 55 : data_offset_(0), |
57 callback_(base::Bind(&Core::OnIOComplete, this)) { | 56 callback_(base::Bind(&Core::OnIOComplete, this)) { |
58 } | 57 } |
59 | 58 |
60 int Start(const net::URLRequest& request, const base::Closure& callback); | 59 int Start(const net::URLRequest& request, const base::Closure& callback); |
61 | 60 |
62 // Prevents it from invoking its callback. It will self-delete. | 61 // Prevents it from invoking its callback. It will self-delete. |
63 void Orphan() { | 62 void Orphan() { |
64 user_callback_.Reset(); | 63 user_callback_.Reset(); |
65 } | 64 } |
66 | 65 |
67 bool GetMimeType(std::string* mime_type) const; | 66 bool GetMimeType(std::string* mime_type) const; |
68 bool GetCharset(std::string* charset); | 67 bool GetCharset(std::string* charset); |
69 int ReadRawData(net::IOBuffer* buf, int buf_size); | 68 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read); |
70 | 69 |
71 private: | 70 private: |
72 friend class base::RefCounted<Core>; | 71 friend class base::RefCounted<Core>; |
73 | 72 |
74 ~Core() {} | 73 ~Core() {} |
75 | 74 |
76 // Called when ViewCacheHelper completes the operation. | 75 // Called when ViewCacheHelper completes the operation. |
77 void OnIOComplete(int result); | 76 void OnIOComplete(int result); |
78 | 77 |
79 std::string data_; | 78 std::string data_; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 bool ViewHttpCacheJob::Core::GetMimeType(std::string* mime_type) const { | 157 bool ViewHttpCacheJob::Core::GetMimeType(std::string* mime_type) const { |
159 mime_type->assign("text/html"); | 158 mime_type->assign("text/html"); |
160 return true; | 159 return true; |
161 } | 160 } |
162 | 161 |
163 bool ViewHttpCacheJob::Core::GetCharset(std::string* charset) { | 162 bool ViewHttpCacheJob::Core::GetCharset(std::string* charset) { |
164 charset->assign("UTF-8"); | 163 charset->assign("UTF-8"); |
165 return true; | 164 return true; |
166 } | 165 } |
167 | 166 |
168 int ViewHttpCacheJob::Core::ReadRawData(net::IOBuffer* buf, int buf_size) { | 167 bool ViewHttpCacheJob::Core::ReadRawData(net::IOBuffer* buf, |
169 int remaining = base::checked_cast<int>(data_.size()) - data_offset_; | 168 int buf_size, |
| 169 int* bytes_read) { |
| 170 DCHECK(bytes_read); |
| 171 int remaining = static_cast<int>(data_.size()) - data_offset_; |
170 if (buf_size > remaining) | 172 if (buf_size > remaining) |
171 buf_size = remaining; | 173 buf_size = remaining; |
172 memcpy(buf->data(), data_.data() + data_offset_, buf_size); | 174 memcpy(buf->data(), data_.data() + data_offset_, buf_size); |
173 data_offset_ += buf_size; | 175 data_offset_ += buf_size; |
174 return buf_size; | 176 *bytes_read = buf_size; |
| 177 return true; |
175 } | 178 } |
176 | 179 |
177 void ViewHttpCacheJob::Core::OnIOComplete(int result) { | 180 void ViewHttpCacheJob::Core::OnIOComplete(int result) { |
178 DCHECK_EQ(net::OK, result); | 181 DCHECK_EQ(net::OK, result); |
179 | 182 |
180 if (!user_callback_.is_null()) | 183 if (!user_callback_.is_null()) |
181 user_callback_.Run(); | 184 user_callback_.Run(); |
182 | 185 |
183 // We may be holding the last reference to this job. Do not access |this| | 186 // We may be holding the last reference to this job. Do not access |this| |
184 // after Release(). | 187 // after Release(). |
185 Release(); // Acquired on Start(). | 188 Release(); // Acquired on Start(). |
186 } | 189 } |
187 | 190 |
188 } // namespace. | 191 } // namespace. |
189 | 192 |
190 // Static. | 193 // Static. |
191 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) { | 194 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) { |
192 return url.SchemeIs(kChromeUIScheme) && | 195 return url.SchemeIs(kChromeUIScheme) && |
193 url.host() == kChromeUINetworkViewCacheHost; | 196 url.host() == kChromeUINetworkViewCacheHost; |
194 } | 197 } |
195 | 198 |
196 // Static. | 199 // Static. |
197 net::URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest( | 200 net::URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest( |
198 net::URLRequest* request, net::NetworkDelegate* network_delegate) { | 201 net::URLRequest* request, net::NetworkDelegate* network_delegate) { |
199 return new ViewHttpCacheJob(request, network_delegate); | 202 return new ViewHttpCacheJob(request, network_delegate); |
200 } | 203 } |
201 | 204 |
202 } // namespace content | 205 } // namespace content |
OLD | NEW |