| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/download/download_worker.h" |
| 6 |
| 7 #include "content/browser/download/download_create_info.h" |
| 8 #include "content/public/browser/download_interrupt_reasons.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 10 |
| 11 namespace content { |
| 12 namespace { |
| 13 |
| 14 const int kVerboseLevel = 1; |
| 15 |
| 16 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> |
| 17 CreateUrlDownloader(std::unique_ptr<DownloadUrlParameters> params, |
| 18 base::WeakPtr<UrlDownloader::Delegate> delegate) { |
| 19 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 20 |
| 21 // Build the URLRequest, BlobDataHandle is hold in original request for image |
| 22 // download. |
| 23 std::unique_ptr<net::URLRequest> url_request = |
| 24 DownloadRequestCore::CreateRequestOnIOThread(DownloadItem::kInvalidId, |
| 25 params.get()); |
| 26 |
| 27 return std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>( |
| 28 UrlDownloader::BeginDownload(delegate, std::move(url_request), |
| 29 params->referrer()) |
| 30 .release()); |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 DownloadWorker::DownloadWorker() : weak_factory_(this) {} |
| 36 |
| 37 DownloadWorker::~DownloadWorker() = default; |
| 38 |
| 39 void DownloadWorker::SendRequest( |
| 40 std::unique_ptr<DownloadUrlParameters> params) { |
| 41 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 42 BrowserThread::PostTaskAndReplyWithResult( |
| 43 BrowserThread::IO, FROM_HERE, |
| 44 base::Bind(&CreateUrlDownloader, base::Passed(¶ms), |
| 45 weak_factory_.GetWeakPtr()), |
| 46 base::Bind(&DownloadWorker::AddUrlDownloader, |
| 47 weak_factory_.GetWeakPtr())); |
| 48 } |
| 49 |
| 50 void DownloadWorker::Pause() { |
| 51 request_handle_->PauseRequest(); |
| 52 } |
| 53 |
| 54 void DownloadWorker::Resume() { |
| 55 request_handle_->ResumeRequest(); |
| 56 } |
| 57 |
| 58 void DownloadWorker::Cancel() { |
| 59 request_handle_->CancelRequest(); |
| 60 } |
| 61 |
| 62 void DownloadWorker::OnUrlDownloaderStarted( |
| 63 std::unique_ptr<DownloadCreateInfo> create_info, |
| 64 std::unique_ptr<ByteStreamReader> stream_reader, |
| 65 const DownloadUrlParameters::OnStartedCallback& callback) { |
| 66 // |callback| is not used in subsequent requests. |
| 67 DCHECK(callback.is_null()); |
| 68 |
| 69 // TODO(xingliu): Pass the |stream_reader| to parallel job and handle failed |
| 70 // request. |
| 71 if (create_info->result != |
| 72 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 73 VLOG(kVerboseLevel) << "Parallel download sub request failed. reason = " |
| 74 << create_info->result; |
| 75 NOTIMPLEMENTED(); |
| 76 return; |
| 77 } |
| 78 |
| 79 request_handle_ = std::move(create_info->request_handle); |
| 80 } |
| 81 |
| 82 void DownloadWorker::OnUrlDownloaderStopped(UrlDownloader* downloader) { |
| 83 // Release the |url_downloader_|, the object will be deleted on IO thread. |
| 84 url_downloader_.reset(); |
| 85 } |
| 86 |
| 87 void DownloadWorker::AddUrlDownloader( |
| 88 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> |
| 89 downloader) { |
| 90 url_downloader_ = std::move(downloader); |
| 91 } |
| 92 |
| 93 } // namespace content |
| OLD | NEW |