| 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/sequenced_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" | |
| 14 #include "content/browser/download/download_request_handle.h" | 13 #include "content/browser/download/download_request_handle.h" |
| 15 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/download_interrupt_reasons.h" | 15 #include "content/public/browser/download_interrupt_reasons.h" |
| 17 #include "content/public/browser/download_save_info.h" | 16 #include "content/public/browser/download_save_info.h" |
| 18 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 19 #include "net/base/load_flags.h" | 18 #include "net/base/load_flags.h" |
| 20 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 21 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
| 22 #include "net/http/http_status_code.h" | 21 #include "net/http/http_status_code.h" |
| 23 #include "ui/base/page_transition_types.h" | 22 #include "ui/base/page_transition_types.h" |
| 24 | 23 |
| 25 namespace content { | 24 namespace content { |
| 26 | 25 |
| 27 class UrlDownloader::RequestHandle : public DownloadRequestHandleInterface { | 26 class UrlDownloader::RequestHandle : public DownloadRequestHandleInterface { |
| 28 public: | 27 public: |
| 29 RequestHandle(base::WeakPtr<UrlDownloader> downloader, | 28 RequestHandle(base::WeakPtr<UrlDownloader> downloader, |
| 30 base::WeakPtr<DownloadManagerImpl> download_manager_impl, | |
| 31 scoped_refptr<base::SequencedTaskRunner> downloader_task_runner) | 29 scoped_refptr<base::SequencedTaskRunner> downloader_task_runner) |
| 32 : downloader_(downloader), | 30 : downloader_(downloader), |
| 33 download_manager_impl_(download_manager_impl), | |
| 34 downloader_task_runner_(downloader_task_runner) {} | 31 downloader_task_runner_(downloader_task_runner) {} |
| 35 RequestHandle(RequestHandle&& other) | 32 RequestHandle(RequestHandle&& other) |
| 36 : downloader_(std::move(other.downloader_)), | 33 : downloader_(std::move(other.downloader_)), |
| 37 download_manager_impl_(std::move(other.download_manager_impl_)), | |
| 38 downloader_task_runner_(std::move(other.downloader_task_runner_)) {} | 34 downloader_task_runner_(std::move(other.downloader_task_runner_)) {} |
| 39 RequestHandle& operator=(RequestHandle&& other) { | 35 RequestHandle& operator=(RequestHandle&& other) { |
| 40 downloader_ = std::move(other.downloader_); | 36 downloader_ = std::move(other.downloader_); |
| 41 download_manager_impl_ = std::move(other.download_manager_impl_); | |
| 42 downloader_task_runner_ = std::move(other.downloader_task_runner_); | 37 downloader_task_runner_ = std::move(other.downloader_task_runner_); |
| 43 return *this; | 38 return *this; |
| 44 } | 39 } |
| 45 | 40 |
| 46 // DownloadRequestHandleInterface | 41 // DownloadRequestHandleInterface |
| 47 WebContents* GetWebContents() const override { return nullptr; } | 42 WebContents* GetWebContents() const override { return nullptr; } |
| 48 DownloadManager* GetDownloadManager() const override { | 43 DownloadManager* GetDownloadManager() const override { return nullptr; } |
| 49 return download_manager_impl_ ? download_manager_impl_.get() : nullptr; | |
| 50 } | |
| 51 void PauseRequest() const override { | 44 void PauseRequest() const override { |
| 52 downloader_task_runner_->PostTask( | 45 downloader_task_runner_->PostTask( |
| 53 FROM_HERE, base::Bind(&UrlDownloader::PauseRequest, downloader_)); | 46 FROM_HERE, base::Bind(&UrlDownloader::PauseRequest, downloader_)); |
| 54 } | 47 } |
| 55 void ResumeRequest() const override { | 48 void ResumeRequest() const override { |
| 56 downloader_task_runner_->PostTask( | 49 downloader_task_runner_->PostTask( |
| 57 FROM_HERE, base::Bind(&UrlDownloader::ResumeRequest, downloader_)); | 50 FROM_HERE, base::Bind(&UrlDownloader::ResumeRequest, downloader_)); |
| 58 } | 51 } |
| 59 void CancelRequest() const override { | 52 void CancelRequest() const override { |
| 60 downloader_task_runner_->PostTask( | 53 downloader_task_runner_->PostTask( |
| 61 FROM_HERE, base::Bind(&UrlDownloader::CancelRequest, downloader_)); | 54 FROM_HERE, base::Bind(&UrlDownloader::CancelRequest, downloader_)); |
| 62 } | 55 } |
| 63 | 56 |
| 64 private: | 57 private: |
| 65 base::WeakPtr<UrlDownloader> downloader_; | 58 base::WeakPtr<UrlDownloader> downloader_; |
| 66 base::WeakPtr<DownloadManagerImpl> download_manager_impl_; | |
| 67 scoped_refptr<base::SequencedTaskRunner> downloader_task_runner_; | 59 scoped_refptr<base::SequencedTaskRunner> downloader_task_runner_; |
| 68 | 60 |
| 69 DISALLOW_COPY_AND_ASSIGN(RequestHandle); | 61 DISALLOW_COPY_AND_ASSIGN(RequestHandle); |
| 70 }; | 62 }; |
| 71 | 63 |
| 72 // static | 64 // static |
| 73 std::unique_ptr<UrlDownloader> UrlDownloader::BeginDownload( | 65 std::unique_ptr<UrlDownloader> UrlDownloader::BeginDownload( |
| 74 base::WeakPtr<DownloadManagerImpl> download_manager, | 66 base::WeakPtr<UrlDownloader::Delegate> delegate, |
| 75 std::unique_ptr<net::URLRequest> request, | 67 std::unique_ptr<net::URLRequest> request, |
| 76 const Referrer& referrer) { | 68 const Referrer& referrer) { |
| 77 Referrer::SetReferrerForRequest(request.get(), referrer); | 69 Referrer::SetReferrerForRequest(request.get(), referrer); |
| 78 | 70 |
| 79 if (request->url().SchemeIs(url::kBlobScheme)) | 71 if (request->url().SchemeIs(url::kBlobScheme)) |
| 80 return nullptr; | 72 return nullptr; |
| 81 | 73 |
| 82 // From this point forward, the |UrlDownloader| is responsible for | 74 // From this point forward, the |UrlDownloader| is responsible for |
| 83 // |started_callback|. | 75 // |started_callback|. |
| 84 std::unique_ptr<UrlDownloader> downloader( | 76 std::unique_ptr<UrlDownloader> downloader( |
| 85 new UrlDownloader(std::move(request), download_manager)); | 77 new UrlDownloader(std::move(request), delegate)); |
| 86 downloader->Start(); | 78 downloader->Start(); |
| 87 | 79 |
| 88 return downloader; | 80 return downloader; |
| 89 } | 81 } |
| 90 | 82 |
| 91 UrlDownloader::UrlDownloader(std::unique_ptr<net::URLRequest> request, | 83 UrlDownloader::UrlDownloader(std::unique_ptr<net::URLRequest> request, |
| 92 base::WeakPtr<DownloadManagerImpl> manager) | 84 base::WeakPtr<Delegate> delegate) |
| 93 : request_(std::move(request)), | 85 : request_(std::move(request)), |
| 94 manager_(manager), | 86 delegate_(delegate), |
| 95 core_(request_.get(), this), | 87 core_(request_.get(), this), |
| 96 weak_ptr_factory_(this) {} | 88 weak_ptr_factory_(this) {} |
| 97 | 89 |
| 98 UrlDownloader::~UrlDownloader() { | 90 UrlDownloader::~UrlDownloader() { |
| 99 } | 91 } |
| 100 | 92 |
| 101 void UrlDownloader::Start() { | 93 void UrlDownloader::Start() { |
| 102 DCHECK(!request_->is_pending()); | 94 DCHECK(!request_->is_pending()); |
| 103 | 95 |
| 104 request_->set_delegate(this); | 96 request_->set_delegate(this); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 DVLOG(1) << "ResponseCompleted: " << request_->url().spec(); | 198 DVLOG(1) << "ResponseCompleted: " << request_->url().spec(); |
| 207 | 199 |
| 208 core_.OnResponseCompleted(net::URLRequestStatus::FromError(net_error)); | 200 core_.OnResponseCompleted(net::URLRequestStatus::FromError(net_error)); |
| 209 Destroy(); | 201 Destroy(); |
| 210 } | 202 } |
| 211 | 203 |
| 212 void UrlDownloader::OnStart( | 204 void UrlDownloader::OnStart( |
| 213 std::unique_ptr<DownloadCreateInfo> create_info, | 205 std::unique_ptr<DownloadCreateInfo> create_info, |
| 214 std::unique_ptr<ByteStreamReader> stream_reader, | 206 std::unique_ptr<ByteStreamReader> stream_reader, |
| 215 const DownloadUrlParameters::OnStartedCallback& callback) { | 207 const DownloadUrlParameters::OnStartedCallback& callback) { |
| 216 create_info->request_handle.reset( | 208 create_info->request_handle.reset(new RequestHandle( |
| 217 new RequestHandle(weak_ptr_factory_.GetWeakPtr(), manager_, | 209 weak_ptr_factory_.GetWeakPtr(), base::SequencedTaskRunnerHandle::Get())); |
| 218 base::SequencedTaskRunnerHandle::Get())); | 210 |
| 219 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 211 BrowserThread::PostTask( |
| 220 base::Bind(&DownloadManagerImpl::StartDownload, | 212 BrowserThread::UI, FROM_HERE, |
| 221 manager_, base::Passed(&create_info), | 213 base::Bind(&UrlDownloader::Delegate::OnUrlDownloaderStarted, delegate_, |
| 222 base::Passed(&stream_reader), callback)); | 214 base::Passed(&create_info), base::Passed(&stream_reader), |
| 215 callback)); |
| 223 } | 216 } |
| 224 | 217 |
| 225 void UrlDownloader::OnReadyToRead() { | 218 void UrlDownloader::OnReadyToRead() { |
| 226 StartReading(false); // Read the next chunk (OK to complete synchronously). | 219 StartReading(false); // Read the next chunk (OK to complete synchronously). |
| 227 } | 220 } |
| 228 | 221 |
| 229 void UrlDownloader::PauseRequest() { | 222 void UrlDownloader::PauseRequest() { |
| 230 core_.PauseRequest(); | 223 core_.PauseRequest(); |
| 231 } | 224 } |
| 232 | 225 |
| 233 void UrlDownloader::ResumeRequest() { | 226 void UrlDownloader::ResumeRequest() { |
| 234 core_.ResumeRequest(); | 227 core_.ResumeRequest(); |
| 235 } | 228 } |
| 236 | 229 |
| 237 void UrlDownloader::CancelRequest() { | 230 void UrlDownloader::CancelRequest() { |
| 238 Destroy(); | 231 Destroy(); |
| 239 } | 232 } |
| 240 | 233 |
| 241 void UrlDownloader::Destroy() { | 234 void UrlDownloader::Destroy() { |
| 242 BrowserThread::PostTask( | 235 BrowserThread::PostTask( |
| 243 BrowserThread::UI, FROM_HERE, | 236 BrowserThread::UI, FROM_HERE, |
| 244 base::Bind(&DownloadManagerImpl::RemoveUrlDownloader, manager_, this)); | 237 base::Bind(&UrlDownloader::Delegate::OnUrlDownloaderStopped, delegate_, |
| 238 this)); |
| 245 } | 239 } |
| 246 | 240 |
| 247 } // namespace content | 241 } // namespace content |
| OLD | NEW |