| 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 |
| 11 namespace content { | 11 namespace content { |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 const int kVerboseLevel = 1; | 14 const int kVerboseLevel = 1; |
| 15 | 15 |
| 16 class CompletedByteStreamReader : public ByteStreamReader { |
| 17 public: |
| 18 CompletedByteStreamReader(int status) : status_(status) {}; |
| 19 ~CompletedByteStreamReader() override = default; |
| 20 |
| 21 // ByteStreamReader implementations: |
| 22 ByteStreamReader::StreamState Read(scoped_refptr<net::IOBuffer>* data, |
| 23 size_t* length) override { |
| 24 return ByteStreamReader::STREAM_COMPLETE; |
| 25 } |
| 26 int GetStatus() const override { return status_; } |
| 27 void RegisterCallback(const base::Closure& sink_callback) override {}; |
| 28 |
| 29 private: |
| 30 int status_; |
| 31 }; |
| 32 |
| 16 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> | 33 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> |
| 17 CreateUrlDownloader(std::unique_ptr<DownloadUrlParameters> params, | 34 CreateUrlDownloader(std::unique_ptr<DownloadUrlParameters> params, |
| 18 base::WeakPtr<UrlDownloader::Delegate> delegate) { | 35 base::WeakPtr<UrlDownloader::Delegate> delegate) { |
| 19 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 36 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 20 | 37 |
| 21 // Build the URLRequest, BlobDataHandle is hold in original request for image | 38 // Build the URLRequest, BlobDataHandle is hold in original request for image |
| 22 // download. | 39 // download. |
| 23 std::unique_ptr<net::URLRequest> url_request = | 40 std::unique_ptr<net::URLRequest> url_request = |
| 24 DownloadRequestCore::CreateRequestOnIOThread(DownloadItem::kInvalidId, | 41 DownloadRequestCore::CreateRequestOnIOThread(DownloadItem::kInvalidId, |
| 25 params.get()); | 42 params.get()); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 VLOG(kVerboseLevel) << "Byte stream arrived after user cancel the request."; | 104 VLOG(kVerboseLevel) << "Byte stream arrived after user cancel the request."; |
| 88 create_info->request_handle->CancelRequest(); | 105 create_info->request_handle->CancelRequest(); |
| 89 return; | 106 return; |
| 90 } | 107 } |
| 91 | 108 |
| 92 // TODO(xingliu): Add metric for error handling. | 109 // TODO(xingliu): Add metric for error handling. |
| 93 if (create_info->result != | 110 if (create_info->result != |
| 94 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { | 111 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 95 VLOG(kVerboseLevel) << "Parallel download sub-request failed. reason = " | 112 VLOG(kVerboseLevel) << "Parallel download sub-request failed. reason = " |
| 96 << create_info->result; | 113 << create_info->result; |
| 97 | 114 stream_reader.reset(new CompletedByteStreamReader(create_info->result)); |
| 98 delegate_->OnServerResponseError(this, create_info->result); | |
| 99 return; | |
| 100 } | 115 } |
| 101 | 116 |
| 102 request_handle_ = std::move(create_info->request_handle); | 117 request_handle_ = std::move(create_info->request_handle); |
| 103 | 118 |
| 104 // Pause the stream if user paused, still push the stream reader to the sink. | 119 // Pause the stream if user paused, still push the stream reader to the sink. |
| 105 if (is_paused_) { | 120 if (is_paused_) { |
| 106 VLOG(kVerboseLevel) << "Byte stream arrived after user pause the request."; | 121 VLOG(kVerboseLevel) << "Byte stream arrived after user pause the request."; |
| 107 Pause(); | 122 Pause(); |
| 108 } | 123 } |
| 109 | 124 |
| 110 delegate_->OnByteStreamReady(this, std::move(stream_reader)); | 125 delegate_->OnByteStreamReady(this, std::move(stream_reader)); |
| 111 } | 126 } |
| 112 | 127 |
| 113 void DownloadWorker::OnUrlDownloaderStopped(UrlDownloader* downloader) { | 128 void DownloadWorker::OnUrlDownloaderStopped(UrlDownloader* downloader) { |
| 114 // Release the |url_downloader_|, the object will be deleted on IO thread. | 129 // Release the |url_downloader_|, the object will be deleted on IO thread. |
| 115 url_downloader_.reset(); | 130 url_downloader_.reset(); |
| 116 } | 131 } |
| 117 | 132 |
| 118 void DownloadWorker::AddUrlDownloader( | 133 void DownloadWorker::AddUrlDownloader( |
| 119 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> | 134 std::unique_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> |
| 120 downloader) { | 135 downloader) { |
| 121 url_downloader_ = std::move(downloader); | 136 url_downloader_ = std::move(downloader); |
| 122 } | 137 } |
| 123 | 138 |
| 124 } // namespace content | 139 } // namespace content |
| OLD | NEW |