Index: chrome/browser/renderer_host/download_resource_handler.cc |
diff --git a/chrome/browser/renderer_host/download_resource_handler.cc b/chrome/browser/renderer_host/download_resource_handler.cc |
index f35d5e4166ce7a127ce62a813137ecc2081b24e6..b4d522d8f0e0151884edf5a20aee9adcdaeb3c77 100644 |
--- a/chrome/browser/renderer_host/download_resource_handler.cc |
+++ b/chrome/browser/renderer_host/download_resource_handler.cc |
@@ -13,6 +13,8 @@ |
#include "chrome/browser/download/download_create_info.h" |
#include "chrome/browser/download/download_item.h" |
#include "chrome/browser/download/download_file_manager.h" |
+#include "chrome/browser/download/download_manager.h" |
+#include "chrome/browser/download/download_prefs.h" |
#include "chrome/browser/download/download_request_handle.h" |
#include "chrome/browser/download/download_util.h" |
#include "content/browser/browser_thread.h" |
@@ -33,6 +35,7 @@ DownloadResourceHandler::DownloadResourceHandler( |
DownloadFileManager* download_file_manager, |
net::URLRequest* request, |
bool save_as, |
+ OnStartedCallback started_cb, |
const DownloadSaveInfo& save_info) |
: download_id_(-1), |
global_id_(render_process_host_id, request_id), |
@@ -41,6 +44,7 @@ DownloadResourceHandler::DownloadResourceHandler( |
download_file_manager_(download_file_manager), |
request_(request), |
save_as_(save_as), |
+ started_cb_(started_cb), |
save_info_(save_info), |
buffer_(new DownloadBuffer), |
rdh_(rdh), |
@@ -77,8 +81,6 @@ bool DownloadResourceHandler::OnResponseStarted(int request_id, |
const ResourceDispatcherHostRequestInfo* request_info = |
ResourceDispatcherHost::InfoForRequest(request_); |
- download_id_ = download_file_manager_->GetNextId(); |
- |
// Deleted in DownloadManager. |
DownloadCreateInfo* info = new DownloadCreateInfo; |
info->url_chain = request_->url_chain(); |
@@ -87,7 +89,6 @@ bool DownloadResourceHandler::OnResponseStarted(int request_id, |
info->received_bytes = 0; |
info->total_bytes = content_length_; |
info->state = DownloadItem::IN_PROGRESS; |
- info->download_id = download_id_; |
info->has_user_gesture = request_info->has_user_gesture(); |
info->request_handle = DownloadRequestHandle(rdh_, |
global_id_.child_id, |
@@ -108,10 +109,11 @@ bool DownloadResourceHandler::OnResponseStarted(int request_id, |
save_as_ && save_info_.file_path.empty(); |
info->referrer_charset = request_->context()->referrer_charset(); |
info->save_info = save_info_; |
+ |
BrowserThread::PostTask( |
BrowserThread::UI, FROM_HERE, |
NewRunnableMethod( |
- download_file_manager_, &DownloadFileManager::StartDownload, info)); |
+ this, &DownloadResourceHandler::OnResponseStartedOnUIThread, info)); |
// We can't start saving the data before we create the file on disk. |
// The request will be un-paused in DownloadFileManager::CreateDownloadFile. |
@@ -120,6 +122,25 @@ bool DownloadResourceHandler::OnResponseStarted(int request_id, |
return true; |
} |
+void DownloadResourceHandler::OnResponseStartedOnUIThread( |
+ DownloadCreateInfo* info) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ download_id_ = info->request_handle.GetDownloadManager()-> |
+ download_prefs()->GetNextId(); |
+ VLOG(1) << __PRETTY_FUNCTION__ << " " << download_id_; |
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, NewRunnableMethod( |
+ this, &DownloadResourceHandler::CallStartedCB, 0)); |
+ info->download_id = download_id_; |
+ download_file_manager_->StartDownload(info); |
+} |
+ |
+void DownloadResourceHandler::CallStartedCB(int error) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ if (started_cb_.is_null()) return; |
+ started_cb_.Run(download_id_, error); |
+ started_cb_.Reset(); |
+} |
+ |
bool DownloadResourceHandler::OnWillStart(int request_id, |
const GURL& url, |
bool* defer) { |
@@ -178,6 +199,9 @@ bool DownloadResourceHandler::OnResponseCompleted( |
<< " status.os_error() = " << status.os_error(); |
int error_code = (status.status() == net::URLRequestStatus::FAILED) ? |
status.os_error() : 0; |
+ if (download_id_ == -1) { |
+ CallStartedCB(error_code); |
+ } |
// We transfer ownership to |DownloadFileManager| to delete |buffer_|, |
// so that any functions queued up on the FILE thread are executed |
// before deletion. |