Chromium Code Reviews| 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, | 169 int remaining = base::checked_cast<int>(data_.size()) - data_offset_; |
|
davidben
2015/11/17 19:54:06
Oh oops. I switched data_offset_ to size_t but int
davidben
2015/11/17 19:55:02
(Er, I guess negative buf_size would have already
xunjieli
2015/11/17 20:28:35
Acknowledged. Thanks, David.
xunjieli
2015/11/17 20:37:55
Done.
| |
| 177 size_t* bytes_read) { | |
| 178 DCHECK(bytes_read); | |
| 179 DCHECK_LE(data_offset_, data_.size()); | |
| 180 size_t remaining = data_.size() - data_offset_; | |
| 181 if (buf_size > remaining) | 170 if (buf_size > remaining) |
| 182 buf_size = remaining; | 171 buf_size = remaining; |
| 183 memcpy(buf->data(), data_.data() + data_offset_, buf_size); | 172 memcpy(buf->data(), data_.data() + data_offset_, buf_size); |
| 184 data_offset_ += buf_size; | 173 data_offset_ += buf_size; |
| 185 *bytes_read = buf_size; | 174 return buf_size; |
| 186 return true; | |
| 187 } | 175 } |
| 188 | 176 |
| 189 void ViewHttpCacheJob::Core::OnIOComplete(int result) { | 177 void ViewHttpCacheJob::Core::OnIOComplete(int result) { |
| 190 DCHECK_EQ(net::OK, result); | 178 DCHECK_EQ(net::OK, result); |
| 191 | 179 |
| 192 if (!user_callback_.is_null()) | 180 if (!user_callback_.is_null()) |
| 193 user_callback_.Run(); | 181 user_callback_.Run(); |
| 194 | 182 |
| 195 // We may be holding the last reference to this job. Do not access |this| | 183 // We may be holding the last reference to this job. Do not access |this| |
| 196 // after Release(). | 184 // after Release(). |
| 197 Release(); // Acquired on Start(). | 185 Release(); // Acquired on Start(). |
| 198 } | 186 } |
| 199 | 187 |
| 200 } // namespace. | 188 } // namespace. |
| 201 | 189 |
| 202 // Static. | 190 // Static. |
| 203 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) { | 191 bool ViewHttpCacheJobFactory::IsSupportedURL(const GURL& url) { |
| 204 return url.SchemeIs(kChromeUIScheme) && | 192 return url.SchemeIs(kChromeUIScheme) && |
| 205 url.host() == kChromeUINetworkViewCacheHost; | 193 url.host() == kChromeUINetworkViewCacheHost; |
| 206 } | 194 } |
| 207 | 195 |
| 208 // Static. | 196 // Static. |
| 209 net::URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest( | 197 net::URLRequestJob* ViewHttpCacheJobFactory::CreateJobForRequest( |
| 210 net::URLRequest* request, net::NetworkDelegate* network_delegate) { | 198 net::URLRequest* request, net::NetworkDelegate* network_delegate) { |
| 211 return new ViewHttpCacheJob(request, network_delegate); | 199 return new ViewHttpCacheJob(request, network_delegate); |
| 212 } | 200 } |
| 213 | 201 |
| 214 } // namespace content | 202 } // namespace content |
| OLD | NEW |