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