| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/url_downloader.h" | 5 #include "content/browser/download/url_downloader.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/threading/sequenced_task_runner_handle.h" | 10 #include "base/threading/task_runner_handle.h" |
| 11 #include "content/browser/byte_stream.h" | 11 #include "content/browser/byte_stream.h" |
| 12 #include "content/browser/download/download_create_info.h" | 12 #include "content/browser/download/download_create_info.h" |
| 13 #include "content/browser/download/download_manager_impl.h" | 13 #include "content/browser/download/download_manager_impl.h" |
| 14 #include "content/browser/download/download_request_handle.h" | 14 #include "content/browser/download/download_request_handle.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/download_interrupt_reasons.h" | 16 #include "content/public/browser/download_interrupt_reasons.h" |
| 17 #include "content/public/browser/download_save_info.h" | 17 #include "content/public/browser/download_save_info.h" |
| 18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 19 #include "net/base/load_flags.h" | 19 #include "net/base/load_flags.h" |
| 20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 void UrlDownloader::StartReading(bool is_continuation) { | 143 void UrlDownloader::StartReading(bool is_continuation) { |
| 144 int bytes_read; | 144 int bytes_read; |
| 145 | 145 |
| 146 // Make sure we track the buffer in at least one place. This ensures it gets | 146 // Make sure we track the buffer in at least one place. This ensures it gets |
| 147 // deleted even in the case the request has already finished its job and | 147 // deleted even in the case the request has already finished its job and |
| 148 // doesn't use the buffer. | 148 // doesn't use the buffer. |
| 149 scoped_refptr<net::IOBuffer> buf; | 149 scoped_refptr<net::IOBuffer> buf; |
| 150 int buf_size; | 150 int buf_size; |
| 151 if (!core_.OnWillRead(&buf, &buf_size, -1)) { | 151 if (!core_.OnWillRead(&buf, &buf_size, -1)) { |
| 152 request_->CancelWithError(net::ERR_ABORTED); | 152 request_->CancelWithError(net::ERR_ABORTED); |
| 153 base::SequencedTaskRunnerHandle::Get()->PostTask( | 153 base::TaskRunnerHandle::GetSequenced()->PostTask( |
| 154 FROM_HERE, base::Bind(&UrlDownloader::ResponseCompleted, | 154 FROM_HERE, base::Bind(&UrlDownloader::ResponseCompleted, |
| 155 weak_ptr_factory_.GetWeakPtr())); | 155 weak_ptr_factory_.GetWeakPtr())); |
| 156 return; | 156 return; |
| 157 } | 157 } |
| 158 | 158 |
| 159 DCHECK(buf.get()); | 159 DCHECK(buf.get()); |
| 160 DCHECK(buf_size > 0); | 160 DCHECK(buf_size > 0); |
| 161 | 161 |
| 162 request_->Read(buf.get(), buf_size, &bytes_read); | 162 request_->Read(buf.get(), buf_size, &bytes_read); |
| 163 | 163 |
| 164 // If IO is pending, wait for the URLRequest to call OnReadCompleted. | 164 // If IO is pending, wait for the URLRequest to call OnReadCompleted. |
| 165 if (request_->status().is_io_pending()) | 165 if (request_->status().is_io_pending()) |
| 166 return; | 166 return; |
| 167 | 167 |
| 168 if (!is_continuation || bytes_read <= 0) { | 168 if (!is_continuation || bytes_read <= 0) { |
| 169 OnReadCompleted(request_.get(), bytes_read); | 169 OnReadCompleted(request_.get(), bytes_read); |
| 170 } else { | 170 } else { |
| 171 // Else, trigger OnReadCompleted asynchronously to avoid starving the IO | 171 // Else, trigger OnReadCompleted asynchronously to avoid starving the IO |
| 172 // thread in case the URLRequest can provide data synchronously. | 172 // thread in case the URLRequest can provide data synchronously. |
| 173 base::SequencedTaskRunnerHandle::Get()->PostTask( | 173 base::TaskRunnerHandle::GetSequenced()->PostTask( |
| 174 FROM_HERE, | 174 FROM_HERE, |
| 175 base::Bind(&UrlDownloader::OnReadCompleted, | 175 base::Bind(&UrlDownloader::OnReadCompleted, |
| 176 weak_ptr_factory_.GetWeakPtr(), request_.get(), bytes_read)); | 176 weak_ptr_factory_.GetWeakPtr(), request_.get(), bytes_read)); |
| 177 } | 177 } |
| 178 } | 178 } |
| 179 | 179 |
| 180 void UrlDownloader::OnReadCompleted(net::URLRequest* request, int bytes_read) { | 180 void UrlDownloader::OnReadCompleted(net::URLRequest* request, int bytes_read) { |
| 181 DVLOG(1) << "OnReadCompleted: \"" << request_->url().spec() << "\"" | 181 DVLOG(1) << "OnReadCompleted: \"" << request_->url().spec() << "\"" |
| 182 << " bytes_read = " << bytes_read; | 182 << " bytes_read = " << bytes_read; |
| 183 | 183 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 core_.OnResponseCompleted(request_->status()); | 216 core_.OnResponseCompleted(request_->status()); |
| 217 Destroy(); | 217 Destroy(); |
| 218 } | 218 } |
| 219 | 219 |
| 220 void UrlDownloader::OnStart( | 220 void UrlDownloader::OnStart( |
| 221 std::unique_ptr<DownloadCreateInfo> create_info, | 221 std::unique_ptr<DownloadCreateInfo> create_info, |
| 222 std::unique_ptr<ByteStreamReader> stream_reader, | 222 std::unique_ptr<ByteStreamReader> stream_reader, |
| 223 const DownloadUrlParameters::OnStartedCallback& callback) { | 223 const DownloadUrlParameters::OnStartedCallback& callback) { |
| 224 create_info->request_handle.reset( | 224 create_info->request_handle.reset( |
| 225 new RequestHandle(weak_ptr_factory_.GetWeakPtr(), manager_, | 225 new RequestHandle(weak_ptr_factory_.GetWeakPtr(), manager_, |
| 226 base::SequencedTaskRunnerHandle::Get())); | 226 base::TaskRunnerHandle::GetSequenced())); |
| 227 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 227 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 228 base::Bind(&DownloadManagerImpl::StartDownload, | 228 base::Bind(&DownloadManagerImpl::StartDownload, |
| 229 manager_, base::Passed(&create_info), | 229 manager_, base::Passed(&create_info), |
| 230 base::Passed(&stream_reader), callback)); | 230 base::Passed(&stream_reader), callback)); |
| 231 } | 231 } |
| 232 | 232 |
| 233 void UrlDownloader::OnReadyToRead() { | 233 void UrlDownloader::OnReadyToRead() { |
| 234 if (request_->status().is_success()) | 234 if (request_->status().is_success()) |
| 235 StartReading(false); // Read the next chunk (OK to complete synchronously). | 235 StartReading(false); // Read the next chunk (OK to complete synchronously). |
| 236 else | 236 else |
| (...skipping 12 matching lines...) Expand all Loading... |
| 249 Destroy(); | 249 Destroy(); |
| 250 } | 250 } |
| 251 | 251 |
| 252 void UrlDownloader::Destroy() { | 252 void UrlDownloader::Destroy() { |
| 253 BrowserThread::PostTask( | 253 BrowserThread::PostTask( |
| 254 BrowserThread::UI, FROM_HERE, | 254 BrowserThread::UI, FROM_HERE, |
| 255 base::Bind(&DownloadManagerImpl::RemoveUrlDownloader, manager_, this)); | 255 base::Bind(&DownloadManagerImpl::RemoveUrlDownloader, manager_, this)); |
| 256 } | 256 } |
| 257 | 257 |
| 258 } // namespace content | 258 } // namespace content |
| OLD | NEW |