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" |
(...skipping 27 matching lines...) Expand all Loading... |
38 | 38 |
39 // net::URLRequestJob implementation. | 39 // net::URLRequestJob implementation. |
40 void Start() override; | 40 void Start() override; |
41 void Kill() override; | 41 void Kill() override; |
42 bool GetMimeType(std::string* mime_type) const override { | 42 bool GetMimeType(std::string* mime_type) const override { |
43 return core_->GetMimeType(mime_type); | 43 return core_->GetMimeType(mime_type); |
44 } | 44 } |
45 bool GetCharset(std::string* charset) override { | 45 bool GetCharset(std::string* charset) override { |
46 return core_->GetCharset(charset); | 46 return core_->GetCharset(charset); |
47 } | 47 } |
48 int ReadRawData(net::IOBuffer* buf, int buf_size) override { | 48 bool ReadRawData(net::IOBuffer* buf, |
49 return core_->ReadRawData(buf, buf_size); | 49 int buf_size, |
| 50 int* out_bytes_read) override { |
| 51 size_t bytes_read; |
| 52 if (!core_->ReadRawData(buf, base::checked_cast<size_t>(buf_size), |
| 53 &bytes_read)) |
| 54 return false; |
| 55 *out_bytes_read = base::checked_cast<int>(bytes_read); |
| 56 return true; |
50 } | 57 } |
51 | 58 |
52 private: | 59 private: |
53 class Core : public base::RefCounted<Core> { | 60 class Core : public base::RefCounted<Core> { |
54 public: | 61 public: |
55 Core() | 62 Core() |
56 : data_offset_(0), | 63 : data_offset_(0), |
57 callback_(base::Bind(&Core::OnIOComplete, this)) { | 64 callback_(base::Bind(&Core::OnIOComplete, this)) { |
58 } | 65 } |
59 | 66 |
60 int Start(const net::URLRequest& request, const base::Closure& callback); | 67 int Start(const net::URLRequest& request, const base::Closure& callback); |
61 | 68 |
62 // Prevents it from invoking its callback. It will self-delete. | 69 // Prevents it from invoking its callback. It will self-delete. |
63 void Orphan() { | 70 void Orphan() { |
64 user_callback_.Reset(); | 71 user_callback_.Reset(); |
65 } | 72 } |
66 | 73 |
67 bool GetMimeType(std::string* mime_type) const; | 74 bool GetMimeType(std::string* mime_type) const; |
68 bool GetCharset(std::string* charset); | 75 bool GetCharset(std::string* charset); |
69 int ReadRawData(net::IOBuffer* buf, int buf_size); | 76 bool ReadRawData(net::IOBuffer* buf, size_t buf_size, size_t* bytes_read); |
70 | 77 |
71 private: | 78 private: |
72 friend class base::RefCounted<Core>; | 79 friend class base::RefCounted<Core>; |
73 | 80 |
74 ~Core() {} | 81 ~Core() {} |
75 | 82 |
76 // Called when ViewCacheHelper completes the operation. | 83 // Called when ViewCacheHelper completes the operation. |
77 void OnIOComplete(int result); | 84 void OnIOComplete(int result); |
78 | 85 |
79 std::string data_; | 86 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 { | 165 bool ViewHttpCacheJob::Core::GetMimeType(std::string* mime_type) const { |
159 mime_type->assign("text/html"); | 166 mime_type->assign("text/html"); |
160 return true; | 167 return true; |
161 } | 168 } |
162 | 169 |
163 bool ViewHttpCacheJob::Core::GetCharset(std::string* charset) { | 170 bool ViewHttpCacheJob::Core::GetCharset(std::string* charset) { |
164 charset->assign("UTF-8"); | 171 charset->assign("UTF-8"); |
165 return true; | 172 return true; |
166 } | 173 } |
167 | 174 |
168 int ViewHttpCacheJob::Core::ReadRawData(net::IOBuffer* buf, int buf_size) { | 175 bool ViewHttpCacheJob::Core::ReadRawData(net::IOBuffer* buf, |
| 176 size_t buf_size, |
| 177 size_t* bytes_read) { |
| 178 DCHECK(bytes_read); |
169 DCHECK_LE(data_offset_, data_.size()); | 179 DCHECK_LE(data_offset_, data_.size()); |
170 int remaining = base::checked_cast<int>(data_.size() - data_offset_); | 180 size_t remaining = data_.size() - data_offset_; |
171 if (buf_size > remaining) | 181 if (buf_size > remaining) |
172 buf_size = remaining; | 182 buf_size = remaining; |
173 memcpy(buf->data(), data_.data() + data_offset_, buf_size); | 183 memcpy(buf->data(), data_.data() + data_offset_, buf_size); |
174 data_offset_ += buf_size; | 184 data_offset_ += buf_size; |
175 return buf_size; | 185 *bytes_read = buf_size; |
| 186 return true; |
176 } | 187 } |
177 | 188 |
178 void ViewHttpCacheJob::Core::OnIOComplete(int result) { | 189 void ViewHttpCacheJob::Core::OnIOComplete(int result) { |
179 DCHECK_EQ(net::OK, result); | 190 DCHECK_EQ(net::OK, result); |
180 | 191 |
181 if (!user_callback_.is_null()) | 192 if (!user_callback_.is_null()) |
182 user_callback_.Run(); | 193 user_callback_.Run(); |
183 | 194 |
184 // We may be holding the last reference to this job. Do not access |this| | 195 // We may be holding the last reference to this job. Do not access |this| |
185 // after Release(). | 196 // after Release(). |
186 Release(); // Acquired on Start(). | 197 Release(); // Acquired on Start(). |
187 } | 198 } |
188 | 199 |
189 } // namespace. | 200 } // namespace. |
190 | 201 |
191 // Static. | 202 // Static. |
192 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) { | 203 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) { |
193 return url.SchemeIs(kChromeUIScheme) && | 204 return url.SchemeIs(kChromeUIScheme) && |
194 url.host() == kChromeUINetworkViewCacheHost; | 205 url.host() == kChromeUINetworkViewCacheHost; |
195 } | 206 } |
196 | 207 |
197 // Static. | 208 // Static. |
198 net::URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest( | 209 net::URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest( |
199 net::URLRequest* request, net::NetworkDelegate* network_delegate) { | 210 net::URLRequest* request, net::NetworkDelegate* network_delegate) { |
200 return new ViewHttpCacheJob(request, network_delegate); | 211 return new ViewHttpCacheJob(request, network_delegate); |
201 } | 212 } |
202 | 213 |
203 } // namespace content | 214 } // namespace content |
OLD | NEW |