Chromium Code Reviews| 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 14 matching lines...) Expand all Loading... | |
| 25 params.get()); | 25 params.get()); |
| 26 | 26 |
| 27 return std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>( | 27 return std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>( |
| 28 UrlDownloader::BeginDownload(delegate, std::move(url_request), | 28 UrlDownloader::BeginDownload(delegate, std::move(url_request), |
| 29 params->referrer()) | 29 params->referrer()) |
| 30 .release()); | 30 .release()); |
| 31 } | 31 } |
| 32 | 32 |
| 33 } // namespace | 33 } // namespace |
| 34 | 34 |
| 35 DownloadWorker::DownloadWorker() : weak_factory_(this) {} | 35 DownloadWorker::DownloadWorker(DownloadWorker::Delegate* delegate, |
| 36 int64_t offset, | |
| 37 int64_t length) | |
| 38 : delegate_(delegate), | |
| 39 offset_(offset), | |
| 40 length_(length), | |
| 41 weak_factory_(this) { | |
| 42 DCHECK(delegate_); | |
| 43 } | |
| 36 | 44 |
| 37 DownloadWorker::~DownloadWorker() = default; | 45 DownloadWorker::~DownloadWorker() = default; |
| 38 | 46 |
| 39 void DownloadWorker::SendRequest( | 47 void DownloadWorker::SendRequest( |
| 40 std::unique_ptr<DownloadUrlParameters> params) { | 48 std::unique_ptr<DownloadUrlParameters> params) { |
| 41 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 42 BrowserThread::PostTaskAndReplyWithResult( | 50 BrowserThread::PostTaskAndReplyWithResult( |
| 43 BrowserThread::IO, FROM_HERE, | 51 BrowserThread::IO, FROM_HERE, |
| 44 base::Bind(&CreateUrlDownloader, base::Passed(¶ms), | 52 base::Bind(&CreateUrlDownloader, base::Passed(¶ms), |
| 45 weak_factory_.GetWeakPtr()), | 53 weak_factory_.GetWeakPtr()), |
| 46 base::Bind(&DownloadWorker::AddUrlDownloader, | 54 base::Bind(&DownloadWorker::AddUrlDownloader, |
| 47 weak_factory_.GetWeakPtr())); | 55 weak_factory_.GetWeakPtr())); |
| 48 } | 56 } |
| 49 | 57 |
| 50 void DownloadWorker::Pause() { | 58 void DownloadWorker::Pause() { |
| 51 request_handle_->PauseRequest(); | 59 if (request_handle_) |
| 60 request_handle_->PauseRequest(); | |
| 52 } | 61 } |
| 53 | 62 |
| 54 void DownloadWorker::Resume() { | 63 void DownloadWorker::Resume() { |
| 55 request_handle_->ResumeRequest(); | 64 if (request_handle_) |
| 65 request_handle_->ResumeRequest(); | |
| 56 } | 66 } |
| 57 | 67 |
| 58 void DownloadWorker::Cancel() { | 68 void DownloadWorker::Cancel() { |
| 59 request_handle_->CancelRequest(); | 69 if (request_handle_) |
| 70 request_handle_->CancelRequest(); | |
| 60 } | 71 } |
| 61 | 72 |
| 62 void DownloadWorker::OnUrlDownloaderStarted( | 73 void DownloadWorker::OnUrlDownloaderStarted( |
| 63 std::unique_ptr<DownloadCreateInfo> create_info, | 74 std::unique_ptr<DownloadCreateInfo> create_info, |
| 64 std::unique_ptr<ByteStreamReader> stream_reader, | 75 std::unique_ptr<ByteStreamReader> stream_reader, |
| 65 const DownloadUrlParameters::OnStartedCallback& callback) { | 76 const DownloadUrlParameters::OnStartedCallback& callback) { |
| 66 // |callback| is not used in subsequent requests. | 77 // |callback| is not used in subsequent requests. |
| 67 DCHECK(callback.is_null()); | 78 DCHECK(callback.is_null()); |
| 68 | 79 |
| 69 // TODO(xingliu): Pass the |stream_reader| to parallel job and handle failed | 80 // If the server gave us a different part of the content, due to "If-Match" or |
| 70 // request. | 81 // "If-Unmodified-Since" headers, reports an error, which results in the |
| 82 // download to be restarted. | |
| 83 if (create_info->save_info->offset != offset() || | |
| 84 create_info->save_info->length != length()) { | |
|
asanka
2017/03/17 15:54:22
This condition will trip for any half-open slice.
xingliu
2017/03/17 21:14:36
Good point, It will be cleaner to let DownloadRequ
asanka
2017/03/20 20:50:20
Don't forget the part about explicitly indicating
xingliu
2017/03/21 17:32:15
Yeah, that's on my list.
| |
| 85 // TODO(xingliu): Add the interrupt reason and metric data for precondition | |
| 86 // failure. | |
| 87 VLOG(kVerboseLevel) << "Server response does not match the offset or " | |
| 88 "length in the request."; | |
| 89 create_info->result = DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE; | |
| 90 } | |
| 91 | |
| 71 if (create_info->result != | 92 if (create_info->result != |
| 72 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { | 93 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 73 VLOG(kVerboseLevel) << "Parallel download sub request failed. reason = " | 94 VLOG(kVerboseLevel) << "Parallel download sub-request failed. reason = " |
| 74 << create_info->result; | 95 << create_info->result; |
| 75 NOTIMPLEMENTED(); | 96 delegate_->OnServerResponseError(this, create_info->result); |
| 76 return; | 97 return; |
| 77 } | 98 } |
| 78 | 99 |
| 79 request_handle_ = std::move(create_info->request_handle); | 100 request_handle_ = std::move(create_info->request_handle); |
| 80 } | 101 } |
| 81 | 102 |
| 82 void DownloadWorker::OnUrlDownloaderStopped(UrlDownloader* downloader) { | 103 void DownloadWorker::OnUrlDownloaderStopped(UrlDownloader* downloader) { |
| 83 // Release the |url_downloader_|, the object will be deleted on IO thread. | 104 // Release the |url_downloader_|, the object will be deleted on IO thread. |
| 84 url_downloader_.reset(); | 105 url_downloader_.reset(); |
| 85 } | 106 } |
| 86 | 107 |
| 87 void DownloadWorker::AddUrlDownloader( | 108 void DownloadWorker::AddUrlDownloader( |
| 88 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> | 109 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> |
| 89 downloader) { | 110 downloader) { |
| 90 url_downloader_ = std::move(downloader); | 111 url_downloader_ = std::move(downloader); |
| 91 } | 112 } |
| 92 | 113 |
| 93 } // namespace content | 114 } // namespace content |
| OLD | NEW |