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/test/net/url_request_slow_download_job.h" | 5 #include "content/test/net/url_request_slow_download_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 "http://url.handled.by.slow.download/download-error"; | 30 "http://url.handled.by.slow.download/download-error"; |
31 | 31 |
32 const int URLRequestSlowDownloadJob::kFirstDownloadSize = 1024 * 35; | 32 const int URLRequestSlowDownloadJob::kFirstDownloadSize = 1024 * 35; |
33 const int URLRequestSlowDownloadJob::kSecondDownloadSize = 1024 * 10; | 33 const int URLRequestSlowDownloadJob::kSecondDownloadSize = 1024 * 10; |
34 | 34 |
35 // static | 35 // static |
36 base::LazyInstance<URLRequestSlowDownloadJob::SlowJobsSet>::Leaky | 36 base::LazyInstance<URLRequestSlowDownloadJob::SlowJobsSet>::Leaky |
37 URLRequestSlowDownloadJob::pending_requests_ = LAZY_INSTANCE_INITIALIZER; | 37 URLRequestSlowDownloadJob::pending_requests_ = LAZY_INSTANCE_INITIALIZER; |
38 | 38 |
39 void URLRequestSlowDownloadJob::Start() { | 39 void URLRequestSlowDownloadJob::Start() { |
40 MessageLoop::current()->PostTask( | 40 base::MessageLoop::current()->PostTask( |
41 FROM_HERE, | 41 FROM_HERE, |
42 base::Bind(&URLRequestSlowDownloadJob::StartAsync, | 42 base::Bind(&URLRequestSlowDownloadJob::StartAsync, |
43 weak_factory_.GetWeakPtr())); | 43 weak_factory_.GetWeakPtr())); |
44 } | 44 } |
45 | 45 |
46 // static | 46 // static |
47 void URLRequestSlowDownloadJob::AddUrlHandler() { | 47 void URLRequestSlowDownloadJob::AddUrlHandler() { |
48 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 48 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
49 filter->AddUrlHandler(GURL(kUnknownSizeUrl), | 49 filter->AddUrlHandler(GURL(kUnknownSizeUrl), |
50 &URLRequestSlowDownloadJob::Factory); | 50 &URLRequestSlowDownloadJob::Factory); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 VLOG(10) << __FUNCTION__ << " called at position " | 179 VLOG(10) << __FUNCTION__ << " called at position " |
180 << bytes_already_sent_ << " in the stream."; | 180 << bytes_already_sent_ << " in the stream."; |
181 ReadStatus status = FillBufferHelper(buf, buf_size, bytes_read); | 181 ReadStatus status = FillBufferHelper(buf, buf_size, bytes_read); |
182 switch (status) { | 182 switch (status) { |
183 case BUFFER_FILLED: | 183 case BUFFER_FILLED: |
184 return true; | 184 return true; |
185 case REQUEST_BLOCKED: | 185 case REQUEST_BLOCKED: |
186 buffer_ = buf; | 186 buffer_ = buf; |
187 buffer_size_ = buf_size; | 187 buffer_size_ = buf_size; |
188 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | 188 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
189 MessageLoop::current()->PostDelayedTask( | 189 base::MessageLoop::current()->PostDelayedTask( |
190 FROM_HERE, | 190 FROM_HERE, |
191 base::Bind(&URLRequestSlowDownloadJob::CheckDoneStatus, | 191 base::Bind(&URLRequestSlowDownloadJob::CheckDoneStatus, |
192 weak_factory_.GetWeakPtr()), | 192 weak_factory_.GetWeakPtr()), |
193 base::TimeDelta::FromMilliseconds(100)); | 193 base::TimeDelta::FromMilliseconds(100)); |
194 return false; | 194 return false; |
195 case REQUEST_COMPLETE: | 195 case REQUEST_COMPLETE: |
196 *bytes_read = 0; | 196 *bytes_read = 0; |
197 return true; | 197 return true; |
198 } | 198 } |
199 NOTREACHED(); | 199 NOTREACHED(); |
200 return true; | 200 return true; |
201 } | 201 } |
202 | 202 |
203 void URLRequestSlowDownloadJob::CheckDoneStatus() { | 203 void URLRequestSlowDownloadJob::CheckDoneStatus() { |
204 if (should_finish_download_) { | 204 if (should_finish_download_) { |
205 VLOG(10) << __FUNCTION__ << " called w/ should_finish_download_ set."; | 205 VLOG(10) << __FUNCTION__ << " called w/ should_finish_download_ set."; |
206 DCHECK(NULL != buffer_); | 206 DCHECK(NULL != buffer_); |
207 int bytes_written = 0; | 207 int bytes_written = 0; |
208 ReadStatus status = FillBufferHelper(buffer_, buffer_size_, &bytes_written); | 208 ReadStatus status = FillBufferHelper(buffer_, buffer_size_, &bytes_written); |
209 DCHECK_EQ(BUFFER_FILLED, status); | 209 DCHECK_EQ(BUFFER_FILLED, status); |
210 buffer_ = NULL; // Release the reference. | 210 buffer_ = NULL; // Release the reference. |
211 SetStatus(net::URLRequestStatus()); | 211 SetStatus(net::URLRequestStatus()); |
212 NotifyReadComplete(bytes_written); | 212 NotifyReadComplete(bytes_written); |
213 } else if (should_error_download_) { | 213 } else if (should_error_download_) { |
214 VLOG(10) << __FUNCTION__ << " called w/ should_finish_ownload_ set."; | 214 VLOG(10) << __FUNCTION__ << " called w/ should_finish_ownload_ set."; |
215 NotifyDone(net::URLRequestStatus( | 215 NotifyDone(net::URLRequestStatus( |
216 net::URLRequestStatus::FAILED, net::ERR_CONNECTION_RESET)); | 216 net::URLRequestStatus::FAILED, net::ERR_CONNECTION_RESET)); |
217 } else { | 217 } else { |
218 MessageLoop::current()->PostDelayedTask( | 218 base::MessageLoop::current()->PostDelayedTask( |
219 FROM_HERE, | 219 FROM_HERE, |
220 base::Bind(&URLRequestSlowDownloadJob::CheckDoneStatus, | 220 base::Bind(&URLRequestSlowDownloadJob::CheckDoneStatus, |
221 weak_factory_.GetWeakPtr()), | 221 weak_factory_.GetWeakPtr()), |
222 base::TimeDelta::FromMilliseconds(100)); | 222 base::TimeDelta::FromMilliseconds(100)); |
223 } | 223 } |
224 } | 224 } |
225 | 225 |
226 // Public virtual version. | 226 // Public virtual version. |
227 void URLRequestSlowDownloadJob::GetResponseInfo(net::HttpResponseInfo* info) { | 227 void URLRequestSlowDownloadJob::GetResponseInfo(net::HttpResponseInfo* info) { |
228 // Forward to private const version. | 228 // Forward to private const version. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 info->headers = new net::HttpResponseHeaders(raw_headers); | 264 info->headers = new net::HttpResponseHeaders(raw_headers); |
265 } | 265 } |
266 | 266 |
267 bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const { | 267 bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const { |
268 net::HttpResponseInfo info; | 268 net::HttpResponseInfo info; |
269 GetResponseInfoConst(&info); | 269 GetResponseInfoConst(&info); |
270 return info.headers && info.headers->GetMimeType(mime_type); | 270 return info.headers && info.headers->GetMimeType(mime_type); |
271 } | 271 } |
272 | 272 |
273 } // namespace content | 273 } // namespace content |
OLD | NEW |