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