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