Index: content/browser/download/url_downloader.cc |
diff --git a/content/browser/download/url_downloader.cc b/content/browser/download/url_downloader.cc |
index ffaea7115086ee20f813405ac72dc133c92f7606..1b05228cffa2397e247b49e545123cc2677f1f7e 100644 |
--- a/content/browser/download/url_downloader.cc |
+++ b/content/browser/download/url_downloader.cc |
@@ -4,18 +4,17 @@ |
#include "content/browser/download/url_downloader.h" |
+#include "base/callback_helpers.h" |
#include "base/location.h" |
-#include "base/thread_task_runner_handle.h" |
-#include "content/browser/appcache/appcache_interceptor.h" |
+#include "base/macros.h" |
+#include "base/threading/sequenced_task_runner_handle.h" |
+#include "content/browser/byte_stream.h" |
+#include "content/browser/download/download_create_info.h" |
#include "content/browser/download/download_manager_impl.h" |
-#include "content/browser/service_worker/service_worker_request_handler.h" |
-#include "content/browser/ssl/ssl_policy.h" |
-#include "content/common/ssl_status_serialization.h" |
-#include "content/public/browser/cert_store.h" |
+#include "content/browser/download/download_request_handle.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/download_interrupt_reasons.h" |
#include "content/public/browser/download_save_info.h" |
-#include "content/public/browser/signed_certificate_timestamp_store.h" |
-#include "content/public/common/process_type.h" |
-#include "content/public/common/security_style.h" |
#include "net/base/io_buffer.h" |
#include "net/base/load_flags.h" |
#include "net/base/net_errors.h" |
@@ -25,14 +24,58 @@ |
namespace content { |
+class UrlDownloader::RequestHandle : public DownloadRequestHandleInterface { |
+ public: |
+ RequestHandle(base::WeakPtr<UrlDownloader> downloader, |
+ base::WeakPtr<DownloadManagerImpl> download_manager_impl, |
+ scoped_refptr<base::SequencedTaskRunner> downloader_task_runner) |
+ : downloader_(downloader), |
+ download_manager_impl_(download_manager_impl), |
+ downloader_task_runner_(downloader_task_runner) {} |
+ RequestHandle(RequestHandle&& other) |
+ : downloader_(std::move(other.downloader_)), |
+ download_manager_impl_(std::move(other.download_manager_impl_)), |
+ downloader_task_runner_(std::move(other.downloader_task_runner_)) {} |
+ RequestHandle& operator=(RequestHandle&& other) { |
+ downloader_ = std::move(other.downloader_); |
+ download_manager_impl_ = std::move(other.download_manager_impl_); |
+ downloader_task_runner_ = std::move(other.downloader_task_runner_); |
+ return *this; |
+ } |
+ |
+ // DownloadRequestHandleInterface |
+ WebContents* GetWebContents() const override { return nullptr; } |
+ DownloadManager* GetDownloadManager() const override { |
+ return download_manager_impl_ ? download_manager_impl_.get() : nullptr; |
+ } |
+ void PauseRequest() const override { |
+ downloader_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&UrlDownloader::PauseRequest, downloader_)); |
+ } |
+ void ResumeRequest() const override { |
+ downloader_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&UrlDownloader::ResumeRequest, downloader_)); |
+ } |
+ void CancelRequest() const override { |
+ downloader_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&UrlDownloader::CancelRequest, downloader_)); |
+ } |
+ std::string DebugString() const override { return std::string(); } |
+ |
+ private: |
+ base::WeakPtr<UrlDownloader> downloader_; |
+ base::WeakPtr<DownloadManagerImpl> download_manager_impl_; |
+ scoped_refptr<base::SequencedTaskRunner> downloader_task_runner_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(RequestHandle); |
+}; |
+ |
// static |
scoped_ptr<UrlDownloader> UrlDownloader::BeginDownload( |
base::WeakPtr<DownloadManagerImpl> download_manager, |
scoped_ptr<net::URLRequest> request, |
const Referrer& referrer, |
- bool is_content_initiated, |
bool prefer_cache, |
- bool do_not_prompt_for_login, |
scoped_ptr<DownloadSaveInfo> save_info, |
uint32 download_id, |
const DownloadUrlParameters::OnStartedCallback& started_callback) { |
@@ -59,32 +102,34 @@ scoped_ptr<UrlDownloader> UrlDownloader::BeginDownload( |
if (request->url().SchemeIs(url::kBlobScheme)) |
return nullptr; |
- scoped_ptr<DownloadRequestCore> handler( |
- new DownloadRequestCore(download_id, request.get(), started_callback, |
- save_info.Pass(), download_manager)); |
- |
// From this point forward, the |UrlDownloader| is responsible for |
// |started_callback|. |
scoped_ptr<UrlDownloader> downloader( |
- new UrlDownloader(request.Pass(), handler.Pass(), download_manager)); |
- |
+ new UrlDownloader(std::move(request), download_manager, |
+ std::move(save_info), download_id, started_callback)); |
downloader->Start(); |
return downloader; |
} |
-UrlDownloader::UrlDownloader(scoped_ptr<net::URLRequest> request, |
- scoped_ptr<DownloadRequestCore> handler, |
- base::WeakPtr<DownloadManagerImpl> manager) |
- : request_(request.Pass()), |
- handler_(handler.Pass()), |
+UrlDownloader::UrlDownloader( |
+ scoped_ptr<net::URLRequest> request, |
+ base::WeakPtr<DownloadManagerImpl> manager, |
+ scoped_ptr<DownloadSaveInfo> save_info, |
+ uint32 download_id, |
+ const DownloadUrlParameters::OnStartedCallback& on_started_callback) |
+ : request_(std::move(request)), |
manager_(manager), |
- weak_ptr_factory_(this) { |
- handler_->set_downloader(this); |
-} |
+ download_id_(download_id), |
+ on_started_callback_(on_started_callback), |
+ handler_( |
+ request_.get(), |
+ std::move(save_info), |
+ base::Bind(&UrlDownloader::ResumeReading, base::Unretained(this))), |
+ weak_ptr_factory_(this) {} |
UrlDownloader::~UrlDownloader() { |
- handler_.reset(); |
+ CallStartedCallbackOnFailure(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); |
} |
void UrlDownloader::Start() { |
@@ -112,7 +157,20 @@ void UrlDownloader::OnResponseStarted(net::URLRequest* request) { |
return; |
} |
- handler_->OnResponseStarted(); |
+ scoped_ptr<DownloadCreateInfo> create_info; |
+ scoped_ptr<ByteStreamReader> stream_reader; |
+ |
+ handler_.OnResponseStarted(&create_info, &stream_reader); |
+ |
+ create_info->download_id = download_id_; |
+ create_info->request_handle.reset( |
+ new RequestHandle(weak_ptr_factory_.GetWeakPtr(), manager_, |
+ base::SequencedTaskRunnerHandle::Get())); |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(&DownloadManagerImpl::StartDownload, manager_, |
+ base::Passed(&create_info), base::Passed(&stream_reader), |
+ base::ResetAndReturn(&on_started_callback_))); |
if (request_->status().is_success()) |
StartReading(false); // Read the first chunk. |
@@ -128,9 +186,9 @@ void UrlDownloader::StartReading(bool is_continuation) { |
// doesn't use the buffer. |
scoped_refptr<net::IOBuffer> buf; |
int buf_size; |
- if (!handler_->OnWillRead(&buf, &buf_size, -1)) { |
+ if (!handler_.OnWillRead(&buf, &buf_size, -1)) { |
request_->CancelWithError(net::ERR_ABORTED); |
- base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ base::SequencedTaskRunnerHandle::Get()->PostTask( |
FROM_HERE, base::Bind(&UrlDownloader::ResponseCompleted, |
weak_ptr_factory_.GetWeakPtr())); |
return; |
@@ -150,7 +208,7 @@ void UrlDownloader::StartReading(bool is_continuation) { |
} else { |
// Else, trigger OnReadCompleted asynchronously to avoid starving the IO |
// thread in case the URLRequest can provide data synchronously. |
- base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ base::SequencedTaskRunnerHandle::Get()->PostTask( |
FROM_HERE, |
base::Bind(&UrlDownloader::OnReadCompleted, |
weak_ptr_factory_.GetWeakPtr(), request_.get(), bytes_read)); |
@@ -171,7 +229,7 @@ void UrlDownloader::OnReadCompleted(net::URLRequest* request, int bytes_read) { |
DCHECK(request_->status().is_success()); |
bool defer = false; |
- if (!handler_->OnReadCompleted(bytes_read, &defer)) { |
+ if (!handler_.OnReadCompleted(bytes_read, &defer)) { |
request_->CancelWithError(net::ERR_ABORTED); |
return; |
} else if (defer) { |
@@ -193,7 +251,7 @@ void UrlDownloader::OnReadCompleted(net::URLRequest* request, int bytes_read) { |
void UrlDownloader::ResponseCompleted() { |
DVLOG(1) << "ResponseCompleted: " << request_->url().spec(); |
- handler_->OnResponseCompleted(request_->status()); |
+ handler_.OnResponseCompleted(request_->status()); |
BrowserThread::PostTask( |
BrowserThread::UI, FROM_HERE, |
base::Bind(&DownloadManagerImpl::RemoveUrlDownloader, manager_, this)); |
@@ -207,4 +265,27 @@ void UrlDownloader::ResumeReading() { |
} |
} |
+void UrlDownloader::CallStartedCallbackOnFailure( |
+ DownloadInterruptReason result) { |
+ if (on_started_callback_.is_null()) |
+ return; |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(base::ResetAndReturn(&on_started_callback_), nullptr, result)); |
+} |
+ |
+void UrlDownloader::PauseRequest() { |
+ handler_.PauseRequest(); |
+} |
+ |
+void UrlDownloader::ResumeRequest() { |
+ handler_.ResumeRequest(); |
+} |
+ |
+void UrlDownloader::CancelRequest() { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(&DownloadManagerImpl::RemoveUrlDownloader, manager_, this)); |
+} |
+ |
} // namespace content |