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( |
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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 reason = content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; | 350 reason = content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; |
350 break; | 351 break; |
351 } | 352 } |
352 } | 353 } |
353 } | 354 } |
354 | 355 |
355 download_stats::RecordAcceptsRanges(accept_ranges_, bytes_read_); | 356 download_stats::RecordAcceptsRanges(accept_ranges_, bytes_read_); |
356 download_stats::RecordNetworkBlockage( | 357 download_stats::RecordNetworkBlockage( |
357 base::TimeTicks::Now() - download_start_time_, total_pause_time_); | 358 base::TimeTicks::Now() - download_start_time_, total_pause_time_); |
358 | 359 |
359 CallStartedCB(DownloadId(), error_code); | 360 CallStartedCB(NULL, error_code); |
360 | 361 |
361 // Send the info down the stream. Conditional is in case we get | 362 // Send the info down the stream. Conditional is in case we get |
362 // OnResponseCompleted without OnResponseStarted. | 363 // OnResponseCompleted without OnResponseStarted. |
363 if (stream_writer_.get()) | 364 if (stream_writer_.get()) |
364 stream_writer_->Close(reason); | 365 stream_writer_->Close(reason); |
365 | 366 |
366 stream_writer_.reset(); // We no longer need the stream. | 367 stream_writer_.reset(); // We no longer need the stream. |
367 read_buffer_ = NULL; | 368 read_buffer_ = NULL; |
368 | 369 |
369 return true; | 370 return true; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 render_view_id_, | 439 render_view_id_, |
439 save_info_.file_path.value().c_str()); | 440 save_info_.file_path.value().c_str()); |
440 } | 441 } |
441 | 442 |
442 DownloadResourceHandler::~DownloadResourceHandler() { | 443 DownloadResourceHandler::~DownloadResourceHandler() { |
443 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 444 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
444 | 445 |
445 // This won't do anything if the callback was called before. | 446 // This won't do anything if the callback was called before. |
446 // If it goes through, it will likely be because OnWillStart() returned | 447 // If it goes through, it will likely be because OnWillStart() returned |
447 // false somewhere in the chain of resource handlers. | 448 // false somewhere in the chain of resource handlers. |
448 CallStartedCB(DownloadId(), net::ERR_ACCESS_DENIED); | 449 CallStartedCB(NULL, net::ERR_ACCESS_DENIED); |
449 | 450 |
450 // Remove output stream callback if a stream exists. | 451 // Remove output stream callback if a stream exists. |
451 if (stream_writer_.get()) | 452 if (stream_writer_.get()) |
452 stream_writer_->RegisterCallback(base::Closure()); | 453 stream_writer_->RegisterCallback(base::Closure()); |
453 | 454 |
454 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", | 455 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", |
455 base::TimeTicks::Now() - download_start_time_); | 456 base::TimeTicks::Now() - download_start_time_); |
456 } | 457 } |
OLD | NEW |