Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_resource_handler.h" | 5 #include "content/browser/download/download_resource_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 using content::DownloadManager; | 37 using content::DownloadManager; |
| 38 using content::ResourceDispatcherHostImpl; | 38 using content::ResourceDispatcherHostImpl; |
| 39 using content::ResourceRequestInfoImpl; | 39 using content::ResourceRequestInfoImpl; |
| 40 | 40 |
| 41 namespace { | 41 namespace { |
| 42 | 42 |
| 43 static const int kDownloadByteStreamSize = 100 * 1024; | 43 static const int kDownloadByteStreamSize = 100 * 1024; |
| 44 | 44 |
| 45 void CallStartedCBOnUIThread( | 45 void CallStartedCBOnUIThread( |
| 46 const DownloadResourceHandler::OnStartedCallback& started_cb, | 46 const DownloadResourceHandler::OnStartedCallback& started_cb, |
| 47 DownloadId id, | 47 DownloadItem* item, |
| 48 net::Error error) { | 48 net::Error error) { |
| 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 50 | 50 |
| 51 if (started_cb.is_null()) | 51 if (started_cb.is_null()) |
| 52 return; | 52 return; |
| 53 started_cb.Run(id, error); | 53 started_cb.Run(item, error); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Static function in order to prevent any accidental accesses to | 56 // Static function in order to prevent any accidental accesses to |
| 57 // DownloadResourceHandler members from the UI thread. | 57 // DownloadResourceHandler members from the UI thread. |
| 58 static void StartOnUIThread( | 58 static void StartOnUIThread( |
| 59 scoped_ptr<DownloadCreateInfo> info, | 59 scoped_ptr<DownloadCreateInfo> info, |
| 60 scoped_ptr<content::ByteStreamReader> stream, | 60 scoped_ptr<content::ByteStreamReader> stream, |
| 61 const DownloadResourceHandler::OnStartedCallback& started_cb) { | 61 const DownloadResourceHandler::OnStartedCallback& started_cb) { |
| 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 63 | 63 |
| 64 DownloadManager* download_manager = info->request_handle.GetDownloadManager(); | 64 DownloadManager* download_manager = info->request_handle.GetDownloadManager(); |
| 65 if (!download_manager) { | 65 if (!download_manager) { |
| 66 // NULL in unittests or if the page closed right after starting the | 66 // NULL in unittests or if the page closed right after starting the |
| 67 // download. | 67 // download. |
| 68 if (!started_cb.is_null()) | 68 if (!started_cb.is_null()) |
| 69 started_cb.Run(DownloadId(), net::ERR_ACCESS_DENIED); | 69 started_cb.Run(NULL, net::ERR_ACCESS_DENIED); |
| 70 return; | 70 return; |
| 71 } | 71 } |
| 72 | 72 |
| 73 DownloadId download_id = | 73 DownloadItem* item = download_manager->StartDownload( |
|
Randy Smith (Not in Mondays)
2012/10/08 19:20:52
What would you think of just passing the started_c
benjhayden
2012/10/08 19:49:45
Hm, maybe in another CL.
I have no strong objectio
| |
| 74 download_manager->StartDownload(info.Pass(), stream.Pass()); | 74 info.Pass(), stream.Pass()); |
| 75 | 75 |
| 76 if (!started_cb.is_null()) | 76 if (!started_cb.is_null()) |
| 77 started_cb.Run(download_id, net::OK); | 77 started_cb.Run(item, net::OK); |
| 78 } | 78 } |
| 79 | 79 |
| 80 } // namespace | 80 } // namespace |
| 81 | 81 |
| 82 DownloadResourceHandler::DownloadResourceHandler( | 82 DownloadResourceHandler::DownloadResourceHandler( |
| 83 net::URLRequest* request, | 83 net::URLRequest* request, |
| 84 const DownloadResourceHandler::OnStartedCallback& started_cb, | 84 const DownloadResourceHandler::OnStartedCallback& started_cb, |
| 85 const content::DownloadSaveInfo& save_info) | 85 const content::DownloadSaveInfo& save_info) |
| 86 : render_view_id_(0), // Actually initialized below. | 86 : render_view_id_(0), // Actually initialized below. |
| 87 content_length_(0), | 87 content_length_(0), |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 // Pass to StartOnUIThread so that variable | 208 // Pass to StartOnUIThread so that variable |
| 209 // access is always on IO thread but function | 209 // access is always on IO thread but function |
| 210 // is called on UI thread. | 210 // is called on UI thread. |
| 211 started_cb_)); | 211 started_cb_)); |
| 212 // Guaranteed to be called in StartOnUIThread | 212 // Guaranteed to be called in StartOnUIThread |
| 213 started_cb_.Reset(); | 213 started_cb_.Reset(); |
| 214 | 214 |
| 215 return true; | 215 return true; |
| 216 } | 216 } |
| 217 | 217 |
| 218 void DownloadResourceHandler::CallStartedCB(DownloadId id, net::Error error) { | 218 void DownloadResourceHandler::CallStartedCB( |
| 219 DownloadItem* item, net::Error error) { | |
| 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 220 if (started_cb_.is_null()) | 221 if (started_cb_.is_null()) |
| 221 return; | 222 return; |
| 222 BrowserThread::PostTask( | 223 BrowserThread::PostTask( |
| 223 BrowserThread::UI, FROM_HERE, | 224 BrowserThread::UI, FROM_HERE, |
| 224 base::Bind(&CallStartedCBOnUIThread, started_cb_, id, error)); | 225 base::Bind(&CallStartedCBOnUIThread, started_cb_, item, error)); |
| 225 started_cb_.Reset(); | 226 started_cb_.Reset(); |
| 226 } | 227 } |
| 227 | 228 |
| 228 bool DownloadResourceHandler::OnWillStart(int request_id, | 229 bool DownloadResourceHandler::OnWillStart(int request_id, |
| 229 const GURL& url, | 230 const GURL& url, |
| 230 bool* defer) { | 231 bool* defer) { |
| 231 return true; | 232 return true; |
| 232 } | 233 } |
| 233 | 234 |
| 234 // Create a new buffer, which will be handed to the download thread for file | 235 // Create a new buffer, which will be handed to the download thread for file |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 336 reason = content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; | 337 reason = content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; |
| 337 break; | 338 break; |
| 338 } | 339 } |
| 339 } | 340 } |
| 340 } | 341 } |
| 341 | 342 |
| 342 download_stats::RecordAcceptsRanges(accept_ranges_, bytes_read_); | 343 download_stats::RecordAcceptsRanges(accept_ranges_, bytes_read_); |
| 343 download_stats::RecordNetworkBlockage( | 344 download_stats::RecordNetworkBlockage( |
| 344 base::TimeTicks::Now() - download_start_time_, total_pause_time_); | 345 base::TimeTicks::Now() - download_start_time_, total_pause_time_); |
| 345 | 346 |
| 346 CallStartedCB(DownloadId(), error_code); | 347 CallStartedCB(NULL, error_code); |
| 347 | 348 |
| 348 // Send the info down the stream. Conditional is in case we get | 349 // Send the info down the stream. Conditional is in case we get |
| 349 // OnResponseCompleted without OnResponseStarted. | 350 // OnResponseCompleted without OnResponseStarted. |
| 350 if (stream_writer_.get()) | 351 if (stream_writer_.get()) |
| 351 stream_writer_->Close(reason); | 352 stream_writer_->Close(reason); |
| 352 | 353 |
| 353 stream_writer_.reset(); // We no longer need the stream. | 354 stream_writer_.reset(); // We no longer need the stream. |
| 354 read_buffer_ = NULL; | 355 read_buffer_ = NULL; |
| 355 | 356 |
| 356 return true; | 357 return true; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 425 render_view_id_, | 426 render_view_id_, |
| 426 save_info_.file_path.value().c_str()); | 427 save_info_.file_path.value().c_str()); |
| 427 } | 428 } |
| 428 | 429 |
| 429 DownloadResourceHandler::~DownloadResourceHandler() { | 430 DownloadResourceHandler::~DownloadResourceHandler() { |
| 430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 431 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 431 | 432 |
| 432 // This won't do anything if the callback was called before. | 433 // This won't do anything if the callback was called before. |
| 433 // If it goes through, it will likely be because OnWillStart() returned | 434 // If it goes through, it will likely be because OnWillStart() returned |
| 434 // false somewhere in the chain of resource handlers. | 435 // false somewhere in the chain of resource handlers. |
| 435 CallStartedCB(DownloadId(), net::ERR_ACCESS_DENIED); | 436 CallStartedCB(NULL, net::ERR_ACCESS_DENIED); |
| 436 | 437 |
| 437 // Remove output stream callback if a stream exists. | 438 // Remove output stream callback if a stream exists. |
| 438 if (stream_writer_.get()) | 439 if (stream_writer_.get()) |
| 439 stream_writer_->RegisterCallback(base::Closure()); | 440 stream_writer_->RegisterCallback(base::Closure()); |
| 440 | 441 |
| 441 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", | 442 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", |
| 442 base::TimeTicks::Now() - download_start_time_); | 443 base::TimeTicks::Now() - download_start_time_); |
| 443 } | 444 } |
| OLD | NEW |