| 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 weak_factory_(this) {} | 41 weak_factory_(this) { |
| 42 DCHECK(delegate_); |
| 43 } |
| 42 | 44 |
| 43 DownloadWorker::~DownloadWorker() = default; | 45 DownloadWorker::~DownloadWorker() = default; |
| 44 | 46 |
| 45 void DownloadWorker::SendRequest( | 47 void DownloadWorker::SendRequest( |
| 46 std::unique_ptr<DownloadUrlParameters> params) { | 48 std::unique_ptr<DownloadUrlParameters> params) { |
| 47 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 48 BrowserThread::PostTaskAndReplyWithResult( | 50 BrowserThread::PostTaskAndReplyWithResult( |
| 49 BrowserThread::IO, FROM_HERE, | 51 BrowserThread::IO, FROM_HERE, |
| 50 base::Bind(&CreateUrlDownloader, base::Passed(¶ms), | 52 base::Bind(&CreateUrlDownloader, base::Passed(¶ms), |
| 51 weak_factory_.GetWeakPtr()), | 53 weak_factory_.GetWeakPtr()), |
| 52 base::Bind(&DownloadWorker::AddUrlDownloader, | 54 base::Bind(&DownloadWorker::AddUrlDownloader, |
| 53 weak_factory_.GetWeakPtr())); | 55 weak_factory_.GetWeakPtr())); |
| 54 } | 56 } |
| 55 | 57 |
| 56 void DownloadWorker::Pause() { | 58 void DownloadWorker::Pause() { |
| 57 request_handle_->PauseRequest(); | 59 if (request_handle_) |
| 60 request_handle_->PauseRequest(); |
| 58 } | 61 } |
| 59 | 62 |
| 60 void DownloadWorker::Resume() { | 63 void DownloadWorker::Resume() { |
| 61 request_handle_->ResumeRequest(); | 64 if (request_handle_) |
| 65 request_handle_->ResumeRequest(); |
| 62 } | 66 } |
| 63 | 67 |
| 64 void DownloadWorker::Cancel() { | 68 void DownloadWorker::Cancel() { |
| 65 request_handle_->CancelRequest(); | 69 if (request_handle_) |
| 70 request_handle_->CancelRequest(); |
| 66 } | 71 } |
| 67 | 72 |
| 68 void DownloadWorker::OnUrlDownloaderStarted( | 73 void DownloadWorker::OnUrlDownloaderStarted( |
| 69 std::unique_ptr<DownloadCreateInfo> create_info, | 74 std::unique_ptr<DownloadCreateInfo> create_info, |
| 70 std::unique_ptr<ByteStreamReader> stream_reader, | 75 std::unique_ptr<ByteStreamReader> stream_reader, |
| 71 const DownloadUrlParameters::OnStartedCallback& callback) { | 76 const DownloadUrlParameters::OnStartedCallback& callback) { |
| 72 // |callback| is not used in subsequent requests. | 77 // |callback| is not used in subsequent requests. |
| 73 DCHECK(callback.is_null()); | 78 DCHECK(callback.is_null()); |
| 74 | 79 |
| 75 // TODO(xingliu): Pass the |stream_reader| to parallel job and handle failed | 80 // TODO(xingliu): Add the interrupt reason and metric data for precondition |
| 76 // request. | 81 // 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 |
| 83 // http 200. |
| 77 if (create_info->result != | 84 if (create_info->result != |
| 78 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { | 85 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 79 VLOG(kVerboseLevel) << "Parallel download sub request failed. reason = " | 86 VLOG(kVerboseLevel) << "Parallel download sub-request failed. reason = " |
| 80 << create_info->result; | 87 << create_info->result; |
| 81 NOTIMPLEMENTED(); | 88 delegate_->OnServerResponseError(this, create_info->result); |
| 82 return; | 89 return; |
| 83 } | 90 } |
| 84 | 91 |
| 85 request_handle_ = std::move(create_info->request_handle); | 92 request_handle_ = std::move(create_info->request_handle); |
| 86 if (delegate_) | 93 if (delegate_) |
| 87 delegate_->OnByteStreamReady(this, std::move(stream_reader)); | 94 delegate_->OnByteStreamReady(this, std::move(stream_reader)); |
| 88 } | 95 } |
| 89 | 96 |
| 90 void DownloadWorker::OnUrlDownloaderStopped(UrlDownloader* downloader) { | 97 void DownloadWorker::OnUrlDownloaderStopped(UrlDownloader* downloader) { |
| 91 // Release the |url_downloader_|, the object will be deleted on IO thread. | 98 // Release the |url_downloader_|, the object will be deleted on IO thread. |
| 92 url_downloader_.reset(); | 99 url_downloader_.reset(); |
| 93 } | 100 } |
| 94 | 101 |
| 95 void DownloadWorker::AddUrlDownloader( | 102 void DownloadWorker::AddUrlDownloader( |
| 96 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> | 103 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> |
| 97 downloader) { | 104 downloader) { |
| 98 url_downloader_ = std::move(downloader); | 105 url_downloader_ = std::move(downloader); |
| 99 } | 106 } |
| 100 | 107 |
| 101 } // namespace content | 108 } // namespace content |
| OLD | NEW |