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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 69 |
70 const int DownloadResourceHandler::kDownloadByteStreamSize = 100 * 1024; | 70 const int DownloadResourceHandler::kDownloadByteStreamSize = 100 * 1024; |
71 | 71 |
72 DownloadResourceHandler::DownloadResourceHandler( | 72 DownloadResourceHandler::DownloadResourceHandler( |
73 uint32 id, | 73 uint32 id, |
74 net::URLRequest* request, | 74 net::URLRequest* request, |
75 const DownloadUrlParameters::OnStartedCallback& started_cb, | 75 const DownloadUrlParameters::OnStartedCallback& started_cb, |
76 scoped_ptr<DownloadSaveInfo> save_info) | 76 scoped_ptr<DownloadSaveInfo> save_info) |
77 : ResourceHandler(request), | 77 : ResourceHandler(request), |
78 download_id_(id), | 78 download_id_(id), |
79 content_length_(0), | |
80 started_cb_(started_cb), | 79 started_cb_(started_cb), |
81 save_info_(save_info.Pass()), | 80 save_info_(save_info.Pass()), |
82 last_buffer_size_(0), | 81 last_buffer_size_(0), |
83 bytes_read_(0), | 82 bytes_read_(0), |
84 pause_count_(0), | 83 pause_count_(0), |
85 was_deferred_(false), | 84 was_deferred_(false), |
86 on_response_started_called_(false) { | 85 on_response_started_called_(false) { |
87 RecordDownloadCount(UNTHROTTLED_COUNT); | 86 RecordDownloadCount(UNTHROTTLED_COUNT); |
88 } | 87 } |
89 | 88 |
(...skipping 28 matching lines...) Expand all Loading... |
118 << " request_id = " << request_id; | 117 << " request_id = " << request_id; |
119 download_start_time_ = base::TimeTicks::Now(); | 118 download_start_time_ = base::TimeTicks::Now(); |
120 | 119 |
121 // If it's a download, we don't want to poison the cache with it. | 120 // If it's a download, we don't want to poison the cache with it. |
122 request()->StopCaching(); | 121 request()->StopCaching(); |
123 | 122 |
124 // Lower priority as well, so downloads don't contend for resources | 123 // Lower priority as well, so downloads don't contend for resources |
125 // with main frames. | 124 // with main frames. |
126 request()->SetPriority(net::IDLE); | 125 request()->SetPriority(net::IDLE); |
127 | 126 |
128 std::string content_disposition; | 127 // If the content-length header is not present (or contains something other |
129 request()->GetResponseHeaderByName("content-disposition", | 128 // than numbers), the incoming content_length is -1 (unknown size). |
130 &content_disposition); | 129 // Set the content length to 0 to indicate unknown size to DownloadManager. |
131 SetContentDisposition(content_disposition); | 130 int64 content_length = |
132 SetContentLength(response->head.content_length); | 131 response->head.content_length > 0 ? response->head.content_length : 0; |
133 | 132 |
134 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); | 133 const ResourceRequestInfoImpl* request_info = GetRequestInfo(); |
135 | 134 |
136 // Deleted in DownloadManager. | 135 // Deleted in DownloadManager. |
137 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo( | 136 scoped_ptr<DownloadCreateInfo> info( |
138 base::Time::Now(), content_length_, | 137 new DownloadCreateInfo(base::Time::Now(), |
139 request()->net_log(), request_info->HasUserGesture(), | 138 content_length, |
140 request_info->GetPageTransition())); | 139 request()->net_log(), |
| 140 request_info->HasUserGesture(), |
| 141 request_info->GetPageTransition(), |
| 142 save_info_.Pass())); |
141 | 143 |
142 // Create the ByteStream for sending data to the download sink. | 144 // Create the ByteStream for sending data to the download sink. |
143 scoped_ptr<ByteStreamReader> stream_reader; | 145 scoped_ptr<ByteStreamReader> stream_reader; |
144 CreateByteStream( | 146 CreateByteStream( |
145 base::MessageLoopProxy::current(), | 147 base::MessageLoopProxy::current(), |
146 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | 148 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), |
147 kDownloadByteStreamSize, &stream_writer_, &stream_reader); | 149 kDownloadByteStreamSize, &stream_writer_, &stream_reader); |
148 stream_writer_->RegisterCallback( | 150 stream_writer_->RegisterCallback( |
149 base::Bind(&DownloadResourceHandler::ResumeRequest, AsWeakPtr())); | 151 base::Bind(&DownloadResourceHandler::ResumeRequest, AsWeakPtr())); |
150 | 152 |
151 info->download_id = download_id_; | 153 info->download_id = download_id_; |
152 info->url_chain = request()->url_chain(); | 154 info->url_chain = request()->url_chain(); |
153 info->referrer_url = GURL(request()->referrer()); | 155 info->referrer_url = GURL(request()->referrer()); |
154 info->start_time = base::Time::Now(); | |
155 info->total_bytes = content_length_; | |
156 info->has_user_gesture = request_info->HasUserGesture(); | |
157 info->content_disposition = content_disposition_; | |
158 info->mime_type = response->head.mime_type; | 156 info->mime_type = response->head.mime_type; |
159 info->remote_address = request()->GetSocketAddress().host(); | 157 info->remote_address = request()->GetSocketAddress().host(); |
| 158 request()->GetResponseHeaderByName("content-disposition", |
| 159 &info->content_disposition); |
160 RecordDownloadMimeType(info->mime_type); | 160 RecordDownloadMimeType(info->mime_type); |
161 RecordDownloadContentDisposition(info->content_disposition); | 161 RecordDownloadContentDisposition(info->content_disposition); |
162 | 162 |
163 info->request_handle = | 163 info->request_handle = |
164 DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(), | 164 DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(), |
165 request_info->GetRouteID(), | 165 request_info->GetRouteID(), |
166 request_info->GetRequestID()); | 166 request_info->GetRequestID()); |
167 | 167 |
168 // Get the last modified time and etag. | 168 // Get the last modified time and etag. |
169 const net::HttpResponseHeaders* headers = request()->response_headers(); | 169 const net::HttpResponseHeaders* headers = request()->response_headers(); |
170 if (headers) { | 170 if (headers) { |
171 std::string last_modified_hdr; | 171 // TODO(asanka): Only store these if headers->HasStrongValidators() is true. |
172 if (headers->EnumerateHeader(NULL, "Last-Modified", &last_modified_hdr)) | 172 // See RFC 2616 section 13.3.3. |
173 info->last_modified = last_modified_hdr; | 173 if (!headers->EnumerateHeader(NULL, "Last-Modified", &info->last_modified)) |
174 if (headers->EnumerateHeader(NULL, "ETag", &etag_)) | 174 info->last_modified.clear(); |
175 info->etag = etag_; | 175 if (!headers->EnumerateHeader(NULL, "ETag", &info->etag)) |
| 176 info->etag.clear(); |
176 | 177 |
177 int status = headers->response_code(); | 178 int status = headers->response_code(); |
178 if (2 == status / 100 && status != net::HTTP_PARTIAL_CONTENT) { | 179 if (2 == status / 100 && status != net::HTTP_PARTIAL_CONTENT) { |
179 // Success & not range response; if we asked for a range, we didn't | 180 // Success & not range response; if we asked for a range, we didn't |
180 // get it--reset the file pointers to reflect that. | 181 // get it--reset the file pointers to reflect that. |
181 save_info_->offset = 0; | 182 info->save_info->offset = 0; |
182 save_info_->hash_state = ""; | 183 info->save_info->hash_state = ""; |
183 } | 184 } |
| 185 |
| 186 if (!headers->GetMimeType(&info->original_mime_type)) |
| 187 info->original_mime_type.clear(); |
184 } | 188 } |
185 | 189 |
186 std::string content_type_header; | |
187 if (!response->head.headers.get() || | |
188 !response->head.headers->GetMimeType(&content_type_header)) | |
189 content_type_header = ""; | |
190 info->original_mime_type = content_type_header; | |
191 | |
192 if (!response->head.headers.get() || | |
193 !response->head.headers->EnumerateHeader( | |
194 NULL, "Accept-Ranges", &accept_ranges_)) { | |
195 accept_ranges_ = ""; | |
196 } | |
197 | |
198 info->save_info = save_info_.Pass(); | |
199 | |
200 BrowserThread::PostTask( | 190 BrowserThread::PostTask( |
201 BrowserThread::UI, FROM_HERE, | 191 BrowserThread::UI, FROM_HERE, |
202 base::Bind(&StartOnUIThread, | 192 base::Bind(&StartOnUIThread, |
203 base::Passed(&info), | 193 base::Passed(&info), |
204 base::Passed(&stream_reader), | 194 base::Passed(&stream_reader), |
205 // Pass to StartOnUIThread so that variable | 195 // Pass to StartOnUIThread so that variable |
206 // access is always on IO thread but function | 196 // access is always on IO thread but function |
207 // is called on UI thread. | 197 // is called on UI thread. |
208 started_cb_)); | 198 started_cb_)); |
209 // Guaranteed to be called in StartOnUIThread | 199 // Guaranteed to be called in StartOnUIThread |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 default: // All other errors. | 356 default: // All other errors. |
367 // Redirection and informational codes should have been handled earlier | 357 // Redirection and informational codes should have been handled earlier |
368 // in the stack. | 358 // in the stack. |
369 DCHECK_NE(3, response_code / 100); | 359 DCHECK_NE(3, response_code / 100); |
370 DCHECK_NE(1, response_code / 100); | 360 DCHECK_NE(1, response_code / 100); |
371 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; | 361 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; |
372 break; | 362 break; |
373 } | 363 } |
374 } | 364 } |
375 | 365 |
376 RecordAcceptsRanges(accept_ranges_, bytes_read_, etag_); | 366 std::string accept_ranges; |
377 RecordNetworkBlockage( | 367 bool has_strong_validators = false; |
378 base::TimeTicks::Now() - download_start_time_, total_pause_time_); | 368 if (request()->response_headers()) { |
| 369 request()->response_headers()->EnumerateHeader( |
| 370 NULL, "Accept-Ranges", &accept_ranges); |
| 371 has_strong_validators = |
| 372 request()->response_headers()->HasStrongValidators(); |
| 373 } |
| 374 RecordAcceptsRanges(accept_ranges, bytes_read_, has_strong_validators); |
| 375 RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_, |
| 376 total_pause_time_); |
379 | 377 |
380 CallStartedCB(NULL, error_code); | 378 CallStartedCB(NULL, error_code); |
381 | 379 |
382 // Send the info down the stream. Conditional is in case we get | 380 // Send the info down the stream. Conditional is in case we get |
383 // OnResponseCompleted without OnResponseStarted. | 381 // OnResponseCompleted without OnResponseStarted. |
384 if (stream_writer_) | 382 if (stream_writer_) |
385 stream_writer_->Close(reason); | 383 stream_writer_->Close(reason); |
386 | 384 |
387 // If the error mapped to something unknown, record it so that | 385 // If the error mapped to something unknown, record it so that |
388 // we can drill down. | 386 // we can drill down. |
389 if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) { | 387 if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) { |
390 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Download.MapErrorNetworkFailed", | 388 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Download.MapErrorNetworkFailed", |
391 std::abs(status.error()), | 389 std::abs(status.error()), |
392 net::GetAllErrorCodesForUma()); | 390 net::GetAllErrorCodesForUma()); |
393 } | 391 } |
394 | 392 |
395 stream_writer_.reset(); // We no longer need the stream. | 393 stream_writer_.reset(); // We no longer need the stream. |
396 read_buffer_ = NULL; | 394 read_buffer_ = NULL; |
397 } | 395 } |
398 | 396 |
399 void DownloadResourceHandler::OnDataDownloaded( | 397 void DownloadResourceHandler::OnDataDownloaded( |
400 int request_id, | 398 int request_id, |
401 int bytes_downloaded) { | 399 int bytes_downloaded) { |
402 NOTREACHED(); | 400 NOTREACHED(); |
403 } | 401 } |
404 | 402 |
405 // If the content-length header is not present (or contains something other | |
406 // than numbers), the incoming content_length is -1 (unknown size). | |
407 // Set the content length to 0 to indicate unknown size to DownloadManager. | |
408 void DownloadResourceHandler::SetContentLength(const int64& content_length) { | |
409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
410 content_length_ = 0; | |
411 if (content_length > 0) | |
412 content_length_ = content_length; | |
413 } | |
414 | |
415 void DownloadResourceHandler::SetContentDisposition( | |
416 const std::string& content_disposition) { | |
417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
418 content_disposition_ = content_disposition; | |
419 } | |
420 | |
421 void DownloadResourceHandler::PauseRequest() { | 403 void DownloadResourceHandler::PauseRequest() { |
422 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
423 | 405 |
424 ++pause_count_; | 406 ++pause_count_; |
425 } | 407 } |
426 | 408 |
427 void DownloadResourceHandler::ResumeRequest() { | 409 void DownloadResourceHandler::ResumeRequest() { |
428 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
429 DCHECK_LT(0, pause_count_); | 411 DCHECK_LT(0, pause_count_); |
430 | 412 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 | 464 |
483 // Remove output stream callback if a stream exists. | 465 // Remove output stream callback if a stream exists. |
484 if (stream_writer_) | 466 if (stream_writer_) |
485 stream_writer_->RegisterCallback(base::Closure()); | 467 stream_writer_->RegisterCallback(base::Closure()); |
486 | 468 |
487 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", | 469 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", |
488 base::TimeTicks::Now() - download_start_time_); | 470 base::TimeTicks::Now() - download_start_time_); |
489 } | 471 } |
490 | 472 |
491 } // namespace content | 473 } // namespace content |
OLD | NEW |