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/download/download_request_core.h" | 5 #include "content/browser/download/download_request_core.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 | 192 |
193 DownloadRequestCore::DownloadRequestCore(net::URLRequest* request, | 193 DownloadRequestCore::DownloadRequestCore(net::URLRequest* request, |
194 Delegate* delegate) | 194 Delegate* delegate) |
195 : delegate_(delegate), | 195 : delegate_(delegate), |
196 request_(request), | 196 request_(request), |
197 download_id_(DownloadItem::kInvalidId), | 197 download_id_(DownloadItem::kInvalidId), |
198 last_buffer_size_(0), | 198 last_buffer_size_(0), |
199 bytes_read_(0), | 199 bytes_read_(0), |
200 pause_count_(0), | 200 pause_count_(0), |
201 was_deferred_(false), | 201 was_deferred_(false), |
| 202 is_partial_request_(false), |
202 started_(false), | 203 started_(false), |
203 abort_reason_(DOWNLOAD_INTERRUPT_REASON_NONE) { | 204 abort_reason_(DOWNLOAD_INTERRUPT_REASON_NONE) { |
204 DCHECK(request_); | 205 DCHECK(request_); |
205 DCHECK(delegate_); | 206 DCHECK(delegate_); |
206 RecordDownloadCount(UNTHROTTLED_COUNT); | 207 RecordDownloadCount(UNTHROTTLED_COUNT); |
207 power_save_blocker_ = PowerSaveBlocker::Create( | 208 power_save_blocker_ = PowerSaveBlocker::Create( |
208 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | 209 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
209 PowerSaveBlocker::kReasonOther, "Download in progress"); | 210 PowerSaveBlocker::kReasonOther, "Download in progress"); |
210 DownloadRequestData* request_data = DownloadRequestData::Get(request_); | 211 DownloadRequestData* request_data = DownloadRequestData::Get(request_); |
211 if (request_data) { | 212 if (request_data) { |
212 save_info_ = request_data->TakeSaveInfo(); | 213 save_info_ = request_data->TakeSaveInfo(); |
213 download_id_ = request_data->download_id(); | 214 download_id_ = request_data->download_id(); |
214 on_started_callback_ = request_data->callback(); | 215 on_started_callback_ = request_data->callback(); |
215 DownloadRequestData::Detach(request_); | 216 DownloadRequestData::Detach(request_); |
| 217 is_partial_request_ = save_info_->offset > 0; |
216 } else { | 218 } else { |
217 save_info_.reset(new DownloadSaveInfo); | 219 save_info_.reset(new DownloadSaveInfo); |
218 } | 220 } |
219 } | 221 } |
220 | 222 |
221 DownloadRequestCore::~DownloadRequestCore() { | 223 DownloadRequestCore::~DownloadRequestCore() { |
222 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 224 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
223 // Remove output stream callback if a stream exists. | 225 // Remove output stream callback if a stream exists. |
224 if (stream_writer_) | 226 if (stream_writer_) |
225 stream_writer_->RegisterCallback(base::Closure()); | 227 stream_writer_->RegisterCallback(base::Closure()); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 create_info->save_info->suggested_name.clear(); | 327 create_info->save_info->suggested_name.clear(); |
326 | 328 |
327 RecordDownloadMimeType(create_info->mime_type); | 329 RecordDownloadMimeType(create_info->mime_type); |
328 RecordDownloadContentDisposition(create_info->content_disposition); | 330 RecordDownloadContentDisposition(create_info->content_disposition); |
329 | 331 |
330 delegate_->OnStart(std::move(create_info), std::move(stream_reader), | 332 delegate_->OnStart(std::move(create_info), std::move(stream_reader), |
331 base::ResetAndReturn(&on_started_callback_)); | 333 base::ResetAndReturn(&on_started_callback_)); |
332 return true; | 334 return true; |
333 } | 335 } |
334 | 336 |
| 337 bool DownloadRequestCore::OnRequestRedirected() { |
| 338 DVLOG(20) << __FUNCTION__ << "() " << DebugString(); |
| 339 if (is_partial_request_) { |
| 340 // A redirect while attempting a partial resumption indicates a potential |
| 341 // middle box. Trigger another interruption so that the DownloadItem can |
| 342 // retry. |
| 343 abort_reason_ = DOWNLOAD_INTERRUPT_REASON_SERVER_UNREACHABLE; |
| 344 return false; |
| 345 } |
| 346 return true; |
| 347 } |
| 348 |
335 // Create a new buffer, which will be handed to the download thread for file | 349 // Create a new buffer, which will be handed to the download thread for file |
336 // writing and deletion. | 350 // writing and deletion. |
337 bool DownloadRequestCore::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 351 bool DownloadRequestCore::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
338 int* buf_size, | 352 int* buf_size, |
339 int min_size) { | 353 int min_size) { |
340 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 354 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
341 DCHECK(buf && buf_size); | 355 DCHECK(buf && buf_size); |
342 DCHECK(!read_buffer_.get()); | 356 DCHECK(!read_buffer_.get()); |
343 | 357 |
344 *buf_size = min_size < 0 ? kReadBufSize : min_size; | 358 *buf_size = min_size < 0 ? kReadBufSize : min_size; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 } | 395 } |
382 | 396 |
383 read_buffer_ = NULL; // Drop our reference. | 397 read_buffer_ = NULL; // Drop our reference. |
384 | 398 |
385 if (pause_count_ > 0) | 399 if (pause_count_ > 0) |
386 *defer = was_deferred_ = true; | 400 *defer = was_deferred_ = true; |
387 | 401 |
388 return true; | 402 return true; |
389 } | 403 } |
390 | 404 |
| 405 void DownloadRequestCore::OnWillAbort(DownloadInterruptReason reason) { |
| 406 DVLOG(20) << __FUNCTION__ << "() reason=" << reason << " " << DebugString(); |
| 407 DCHECK(!started_); |
| 408 abort_reason_ = reason; |
| 409 } |
| 410 |
391 void DownloadRequestCore::OnResponseCompleted( | 411 void DownloadRequestCore::OnResponseCompleted( |
392 const net::URLRequestStatus& status) { | 412 const net::URLRequestStatus& status) { |
393 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 413 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
394 int response_code = status.is_success() ? request()->GetResponseCode() : 0; | 414 int response_code = status.is_success() ? request()->GetResponseCode() : 0; |
395 DVLOG(20) << __FUNCTION__ << "()" << DebugString() | 415 DVLOG(20) << __FUNCTION__ << "()" << DebugString() |
396 << " status.status() = " << status.status() | 416 << " status.status() = " << status.status() |
397 << " status.error() = " << status.error() | 417 << " status.error() = " << status.error() |
398 << " response_code = " << response_code; | 418 << " response_code = " << response_code; |
399 | 419 |
400 DownloadInterruptReason reason = HandleRequestStatus(status); | 420 DownloadInterruptReason reason = HandleRequestStatus(status); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
608 return DOWNLOAD_INTERRUPT_REASON_NONE; | 628 return DOWNLOAD_INTERRUPT_REASON_NONE; |
609 } | 629 } |
610 | 630 |
611 if (http_headers.response_code() == net::HTTP_PARTIAL_CONTENT) | 631 if (http_headers.response_code() == net::HTTP_PARTIAL_CONTENT) |
612 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; | 632 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; |
613 | 633 |
614 return DOWNLOAD_INTERRUPT_REASON_NONE; | 634 return DOWNLOAD_INTERRUPT_REASON_NONE; |
615 } | 635 } |
616 | 636 |
617 } // namespace content | 637 } // namespace content |
OLD | NEW |