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 19 matching lines...) Expand all Loading... |
30 #include "net/http/http_response_headers.h" | 30 #include "net/http/http_response_headers.h" |
31 #include "net/http/http_status_code.h" | 31 #include "net/http/http_status_code.h" |
32 #include "net/url_request/url_request_context.h" | 32 #include "net/url_request/url_request_context.h" |
33 | 33 |
34 namespace content { | 34 namespace content { |
35 namespace { | 35 namespace { |
36 | 36 |
37 void CallStartedCBOnUIThread( | 37 void CallStartedCBOnUIThread( |
38 const DownloadUrlParameters::OnStartedCallback& started_cb, | 38 const DownloadUrlParameters::OnStartedCallback& started_cb, |
39 DownloadItem* item, | 39 DownloadItem* item, |
40 net::Error error) { | 40 DownloadInterruptReason interrupt_reason) { |
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
42 | 42 |
43 if (started_cb.is_null()) | 43 if (started_cb.is_null()) |
44 return; | 44 return; |
45 started_cb.Run(item, error); | 45 started_cb.Run(item, interrupt_reason); |
46 } | 46 } |
47 | 47 |
48 // Static function in order to prevent any accidental accesses to | 48 // Static function in order to prevent any accidental accesses to |
49 // DownloadResourceHandler members from the UI thread. | 49 // DownloadResourceHandler members from the UI thread. |
50 static void StartOnUIThread( | 50 static void StartOnUIThread( |
51 scoped_ptr<DownloadCreateInfo> info, | 51 scoped_ptr<DownloadCreateInfo> info, |
52 scoped_ptr<ByteStreamReader> stream, | 52 scoped_ptr<ByteStreamReader> stream, |
53 const DownloadUrlParameters::OnStartedCallback& started_cb) { | 53 const DownloadUrlParameters::OnStartedCallback& started_cb) { |
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
55 | 55 |
56 DownloadManager* download_manager = info->request_handle.GetDownloadManager(); | 56 DownloadManager* download_manager = info->request_handle.GetDownloadManager(); |
57 if (!download_manager) { | 57 if (!download_manager) { |
58 // NULL in unittests or if the page closed right after starting the | 58 // NULL in unittests or if the page closed right after starting the |
59 // download. | 59 // download. |
60 if (!started_cb.is_null()) | 60 if (!started_cb.is_null()) |
61 started_cb.Run(NULL, net::ERR_ACCESS_DENIED); | 61 started_cb.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); |
62 return; | 62 return; |
63 } | 63 } |
64 | 64 |
65 download_manager->StartDownload(info.Pass(), stream.Pass(), started_cb); | 65 download_manager->StartDownload(info.Pass(), stream.Pass(), started_cb); |
66 } | 66 } |
67 | 67 |
68 } // namespace | 68 } // namespace |
69 | 69 |
70 const int DownloadResourceHandler::kDownloadByteStreamSize = 100 * 1024; | 70 const int DownloadResourceHandler::kDownloadByteStreamSize = 100 * 1024; |
71 | 71 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 // access is always on IO thread but function | 199 // access is always on IO thread but function |
200 // is called on UI thread. | 200 // is called on UI thread. |
201 started_cb_)); | 201 started_cb_)); |
202 // Guaranteed to be called in StartOnUIThread | 202 // Guaranteed to be called in StartOnUIThread |
203 started_cb_.Reset(); | 203 started_cb_.Reset(); |
204 | 204 |
205 return true; | 205 return true; |
206 } | 206 } |
207 | 207 |
208 void DownloadResourceHandler::CallStartedCB( | 208 void DownloadResourceHandler::CallStartedCB( |
209 DownloadItem* item, net::Error error) { | 209 DownloadItem* item, |
| 210 DownloadInterruptReason interrupt_reason) { |
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
211 if (started_cb_.is_null()) | 212 if (started_cb_.is_null()) |
212 return; | 213 return; |
213 BrowserThread::PostTask( | 214 BrowserThread::PostTask( |
214 BrowserThread::UI, FROM_HERE, | 215 BrowserThread::UI, |
215 base::Bind(&CallStartedCBOnUIThread, started_cb_, item, error)); | 216 FROM_HERE, |
| 217 base::Bind( |
| 218 &CallStartedCBOnUIThread, started_cb_, item, interrupt_reason)); |
216 started_cb_.Reset(); | 219 started_cb_.Reset(); |
217 } | 220 } |
218 | 221 |
219 bool DownloadResourceHandler::OnWillStart(int request_id, | 222 bool DownloadResourceHandler::OnWillStart(int request_id, |
220 const GURL& url, | 223 const GURL& url, |
221 bool* defer) { | 224 bool* defer) { |
222 return true; | 225 return true; |
223 } | 226 } |
224 | 227 |
225 // Create a new buffer, which will be handed to the download thread for file | 228 // Create a new buffer, which will be handed to the download thread for file |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 if (request()->response_headers()) { | 374 if (request()->response_headers()) { |
372 request()->response_headers()->EnumerateHeader( | 375 request()->response_headers()->EnumerateHeader( |
373 NULL, "Accept-Ranges", &accept_ranges); | 376 NULL, "Accept-Ranges", &accept_ranges); |
374 has_strong_validators = | 377 has_strong_validators = |
375 request()->response_headers()->HasStrongValidators(); | 378 request()->response_headers()->HasStrongValidators(); |
376 } | 379 } |
377 RecordAcceptsRanges(accept_ranges, bytes_read_, has_strong_validators); | 380 RecordAcceptsRanges(accept_ranges, bytes_read_, has_strong_validators); |
378 RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_, | 381 RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_, |
379 total_pause_time_); | 382 total_pause_time_); |
380 | 383 |
381 CallStartedCB(NULL, error_code); | 384 CallStartedCB(NULL, reason); |
382 | 385 |
383 // Send the info down the stream. Conditional is in case we get | 386 // Send the info down the stream. Conditional is in case we get |
384 // OnResponseCompleted without OnResponseStarted. | 387 // OnResponseCompleted without OnResponseStarted. |
385 if (stream_writer_) | 388 if (stream_writer_) |
386 stream_writer_->Close(reason); | 389 stream_writer_->Close(reason); |
387 | 390 |
388 // If the error mapped to something unknown, record it so that | 391 // If the error mapped to something unknown, record it so that |
389 // we can drill down. | 392 // we can drill down. |
390 if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) { | 393 if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) { |
391 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Download.MapErrorNetworkFailed", | 394 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Download.MapErrorNetworkFailed", |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 info->GetRequestID(), | 459 info->GetRequestID(), |
457 info->GetRouteID()); | 460 info->GetRouteID()); |
458 } | 461 } |
459 | 462 |
460 DownloadResourceHandler::~DownloadResourceHandler() { | 463 DownloadResourceHandler::~DownloadResourceHandler() { |
461 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 464 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
462 | 465 |
463 // This won't do anything if the callback was called before. | 466 // This won't do anything if the callback was called before. |
464 // If it goes through, it will likely be because OnWillStart() returned | 467 // If it goes through, it will likely be because OnWillStart() returned |
465 // false somewhere in the chain of resource handlers. | 468 // false somewhere in the chain of resource handlers. |
466 CallStartedCB(NULL, net::ERR_ACCESS_DENIED); | 469 CallStartedCB(NULL, DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); |
467 | 470 |
468 // Remove output stream callback if a stream exists. | 471 // Remove output stream callback if a stream exists. |
469 if (stream_writer_) | 472 if (stream_writer_) |
470 stream_writer_->RegisterCallback(base::Closure()); | 473 stream_writer_->RegisterCallback(base::Closure()); |
471 | 474 |
472 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", | 475 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", |
473 base::TimeTicks::Now() - download_start_time_); | 476 base::TimeTicks::Now() - download_start_time_); |
474 } | 477 } |
475 | 478 |
476 } // namespace content | 479 } // namespace content |
OLD | NEW |