| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_worker.h" | 5 #include "content/browser/download/download_worker.h" |
| 6 | 6 |
| 7 #include "content/browser/download/download_create_info.h" | 7 #include "content/browser/download/download_create_info.h" |
| 8 #include "content/public/browser/download_interrupt_reasons.h" | 8 #include "content/public/browser/download_interrupt_reasons.h" |
| 9 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 } | 31 } |
| 32 | 32 |
| 33 } // namespace | 33 } // namespace |
| 34 | 34 |
| 35 DownloadWorker::DownloadWorker(DownloadWorker::Delegate* delegate, | 35 DownloadWorker::DownloadWorker(DownloadWorker::Delegate* delegate, |
| 36 int64_t offset, | 36 int64_t offset, |
| 37 int64_t length) | 37 int64_t length) |
| 38 : delegate_(delegate), | 38 : delegate_(delegate), |
| 39 offset_(offset), | 39 offset_(offset), |
| 40 length_(length), | 40 length_(length), |
| 41 is_paused_(false), |
| 42 is_canceled_(false), |
| 41 weak_factory_(this) { | 43 weak_factory_(this) { |
| 42 DCHECK(delegate_); | 44 DCHECK(delegate_); |
| 43 } | 45 } |
| 44 | 46 |
| 45 DownloadWorker::~DownloadWorker() = default; | 47 DownloadWorker::~DownloadWorker() = default; |
| 46 | 48 |
| 47 void DownloadWorker::SendRequest( | 49 void DownloadWorker::SendRequest( |
| 48 std::unique_ptr<DownloadUrlParameters> params) { | 50 std::unique_ptr<DownloadUrlParameters> params) { |
| 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 51 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 50 BrowserThread::PostTaskAndReplyWithResult( | 52 BrowserThread::PostTaskAndReplyWithResult( |
| 51 BrowserThread::IO, FROM_HERE, | 53 BrowserThread::IO, FROM_HERE, |
| 52 base::Bind(&CreateUrlDownloader, base::Passed(¶ms), | 54 base::Bind(&CreateUrlDownloader, base::Passed(¶ms), |
| 53 weak_factory_.GetWeakPtr()), | 55 weak_factory_.GetWeakPtr()), |
| 54 base::Bind(&DownloadWorker::AddUrlDownloader, | 56 base::Bind(&DownloadWorker::AddUrlDownloader, |
| 55 weak_factory_.GetWeakPtr())); | 57 weak_factory_.GetWeakPtr())); |
| 56 } | 58 } |
| 57 | 59 |
| 58 void DownloadWorker::Pause() { | 60 void DownloadWorker::Pause() { |
| 61 is_paused_ = true; |
| 59 if (request_handle_) | 62 if (request_handle_) |
| 60 request_handle_->PauseRequest(); | 63 request_handle_->PauseRequest(); |
| 61 } | 64 } |
| 62 | 65 |
| 63 void DownloadWorker::Resume() { | 66 void DownloadWorker::Resume() { |
| 67 is_paused_ = false; |
| 64 if (request_handle_) | 68 if (request_handle_) |
| 65 request_handle_->ResumeRequest(); | 69 request_handle_->ResumeRequest(); |
| 66 } | 70 } |
| 67 | 71 |
| 68 void DownloadWorker::Cancel() { | 72 void DownloadWorker::Cancel() { |
| 73 is_canceled_ = true; |
| 69 if (request_handle_) | 74 if (request_handle_) |
| 70 request_handle_->CancelRequest(); | 75 request_handle_->CancelRequest(); |
| 71 } | 76 } |
| 72 | 77 |
| 73 void DownloadWorker::OnUrlDownloaderStarted( | 78 void DownloadWorker::OnUrlDownloaderStarted( |
| 74 std::unique_ptr<DownloadCreateInfo> create_info, | 79 std::unique_ptr<DownloadCreateInfo> create_info, |
| 75 std::unique_ptr<ByteStreamReader> stream_reader, | 80 std::unique_ptr<ByteStreamReader> stream_reader, |
| 76 const DownloadUrlParameters::OnStartedCallback& callback) { | 81 const DownloadUrlParameters::OnStartedCallback& callback) { |
| 77 // |callback| is not used in subsequent requests. | 82 // |callback| is not used in subsequent requests. |
| 78 DCHECK(callback.is_null()); | 83 DCHECK(callback.is_null()); |
| 79 | 84 |
| 85 // Destroy the request if user canceled. |
| 86 if (is_canceled_) { |
| 87 VLOG(kVerboseLevel) << "Byte stream arrived after user cancel the request."; |
| 88 create_info->request_handle->CancelRequest(); |
| 89 return; |
| 90 } |
| 91 |
| 80 // TODO(xingliu): Add the interrupt reason and metric data for precondition | 92 // TODO(xingliu): Add the interrupt reason and metric data for precondition |
| 81 // failure. Make DownloadRequestCore know if it should return error if the | 93 // failure. Make DownloadRequestCore know if it should return error if the |
| 82 // the server gives a different part of the content, e.g. "If-Match" return | 94 // the server gives a different part of the content, e.g. "If-Match" return |
| 83 // http 200. | 95 // http 200. |
| 84 if (create_info->result != | 96 if (create_info->result != |
| 85 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { | 97 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 86 VLOG(kVerboseLevel) << "Parallel download sub-request failed. reason = " | 98 VLOG(kVerboseLevel) << "Parallel download sub-request failed. reason = " |
| 87 << create_info->result; | 99 << create_info->result; |
| 88 delegate_->OnServerResponseError(this, create_info->result); | 100 delegate_->OnServerResponseError(this, create_info->result); |
| 89 return; | 101 return; |
| 90 } | 102 } |
| 91 | 103 |
| 92 request_handle_ = std::move(create_info->request_handle); | 104 request_handle_ = std::move(create_info->request_handle); |
| 93 if (delegate_) | 105 |
| 94 delegate_->OnByteStreamReady(this, std::move(stream_reader)); | 106 // Pause the stream if user paused, still push the stream reader to the sink. |
| 107 if (is_paused_) { |
| 108 VLOG(kVerboseLevel) << "Byte stream arrived after user pause the request."; |
| 109 Pause(); |
| 110 } |
| 111 |
| 112 delegate_->OnByteStreamReady(this, std::move(stream_reader)); |
| 95 } | 113 } |
| 96 | 114 |
| 97 void DownloadWorker::OnUrlDownloaderStopped(UrlDownloader* downloader) { | 115 void DownloadWorker::OnUrlDownloaderStopped(UrlDownloader* downloader) { |
| 98 // Release the |url_downloader_|, the object will be deleted on IO thread. | 116 // Release the |url_downloader_|, the object will be deleted on IO thread. |
| 99 url_downloader_.reset(); | 117 url_downloader_.reset(); |
| 100 } | 118 } |
| 101 | 119 |
| 102 void DownloadWorker::AddUrlDownloader( | 120 void DownloadWorker::AddUrlDownloader( |
| 103 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> | 121 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> |
| 104 downloader) { | 122 downloader) { |
| 105 url_downloader_ = std::move(downloader); | 123 url_downloader_ = std::move(downloader); |
| 106 } | 124 } |
| 107 | 125 |
| 108 } // namespace content | 126 } // namespace content |
| OLD | NEW |