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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 int status = headers->response_code(); | 185 int status = headers->response_code(); |
186 if (2 == status / 100 && status != net::HTTP_PARTIAL_CONTENT) { | 186 if (2 == status / 100 && status != net::HTTP_PARTIAL_CONTENT) { |
187 // Success & not range response; if we asked for a range, we didn't | 187 // Success & not range response; if we asked for a range, we didn't |
188 // get it--reset the file pointers to reflect that. | 188 // get it--reset the file pointers to reflect that. |
189 save_info_->offset = 0; | 189 save_info_->offset = 0; |
190 save_info_->hash_state = ""; | 190 save_info_->hash_state = ""; |
191 } | 191 } |
192 } | 192 } |
193 | 193 |
194 std::string content_type_header; | 194 std::string content_type_header; |
195 if (!response->head.headers || | 195 if (!response->head.headers.get() || |
196 !response->head.headers->GetMimeType(&content_type_header)) | 196 !response->head.headers->GetMimeType(&content_type_header)) |
197 content_type_header = ""; | 197 content_type_header = ""; |
198 info->original_mime_type = content_type_header; | 198 info->original_mime_type = content_type_header; |
199 | 199 |
200 if (!response->head.headers || | 200 if (!response->head.headers.get() || |
201 !response->head.headers->EnumerateHeader( | 201 !response->head.headers->EnumerateHeader( |
202 NULL, "Accept-Ranges", &accept_ranges_)) { | 202 NULL, "Accept-Ranges", &accept_ranges_)) { |
203 accept_ranges_ = ""; | 203 accept_ranges_ = ""; |
204 } | 204 } |
205 | 205 |
206 info->save_info = save_info_.Pass(); | 206 info->save_info = save_info_.Pass(); |
207 | 207 |
208 BrowserThread::PostTask( | 208 BrowserThread::PostTask( |
209 BrowserThread::UI, FROM_HERE, | 209 BrowserThread::UI, FROM_HERE, |
210 base::Bind(&StartOnUIThread, | 210 base::Bind(&StartOnUIThread, |
(...skipping 25 matching lines...) Expand all Loading... |
236 bool* defer) { | 236 bool* defer) { |
237 return true; | 237 return true; |
238 } | 238 } |
239 | 239 |
240 // Create a new buffer, which will be handed to the download thread for file | 240 // Create a new buffer, which will be handed to the download thread for file |
241 // writing and deletion. | 241 // writing and deletion. |
242 bool DownloadResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | 242 bool DownloadResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, |
243 int* buf_size, int min_size) { | 243 int* buf_size, int min_size) { |
244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
245 DCHECK(buf && buf_size); | 245 DCHECK(buf && buf_size); |
246 DCHECK(!read_buffer_); | 246 DCHECK(!read_buffer_.get()); |
247 | 247 |
248 *buf_size = min_size < 0 ? kReadBufSize : min_size; | 248 *buf_size = min_size < 0 ? kReadBufSize : min_size; |
249 last_buffer_size_ = *buf_size; | 249 last_buffer_size_ = *buf_size; |
250 read_buffer_ = new net::IOBuffer(*buf_size); | 250 read_buffer_ = new net::IOBuffer(*buf_size); |
251 *buf = read_buffer_.get(); | 251 *buf = read_buffer_.get(); |
252 return true; | 252 return true; |
253 } | 253 } |
254 | 254 |
255 // Pass the buffer to the download file writer. | 255 // Pass the buffer to the download file writer. |
256 bool DownloadResourceHandler::OnReadCompleted(int request_id, int bytes_read, | 256 bool DownloadResourceHandler::OnReadCompleted(int request_id, int bytes_read, |
257 bool* defer) { | 257 bool* defer) { |
258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
259 DCHECK(read_buffer_); | 259 DCHECK(read_buffer_.get()); |
260 | 260 |
261 base::TimeTicks now(base::TimeTicks::Now()); | 261 base::TimeTicks now(base::TimeTicks::Now()); |
262 if (!last_read_time_.is_null()) { | 262 if (!last_read_time_.is_null()) { |
263 double seconds_since_last_read = (now - last_read_time_).InSecondsF(); | 263 double seconds_since_last_read = (now - last_read_time_).InSecondsF(); |
264 if (now == last_read_time_) | 264 if (now == last_read_time_) |
265 // Use 1/10 ms as a "very small number" so that we avoid | 265 // Use 1/10 ms as a "very small number" so that we avoid |
266 // divide-by-zero error and still record a very high potential bandwidth. | 266 // divide-by-zero error and still record a very high potential bandwidth. |
267 seconds_since_last_read = 0.00001; | 267 seconds_since_last_read = 0.00001; |
268 | 268 |
269 double actual_bandwidth = (bytes_read)/seconds_since_last_read; | 269 double actual_bandwidth = (bytes_read)/seconds_since_last_read; |
270 double potential_bandwidth = last_buffer_size_/seconds_since_last_read; | 270 double potential_bandwidth = last_buffer_size_/seconds_since_last_read; |
271 RecordBandwidth(actual_bandwidth, potential_bandwidth); | 271 RecordBandwidth(actual_bandwidth, potential_bandwidth); |
272 } | 272 } |
273 last_read_time_ = now; | 273 last_read_time_ = now; |
274 | 274 |
275 if (!bytes_read) | 275 if (!bytes_read) |
276 return true; | 276 return true; |
277 bytes_read_ += bytes_read; | 277 bytes_read_ += bytes_read; |
278 DCHECK(read_buffer_); | 278 DCHECK(read_buffer_.get()); |
279 | 279 |
280 // Take the data ship it down the stream. If the stream is full, pause the | 280 // Take the data ship it down the stream. If the stream is full, pause the |
281 // request; the stream callback will resume it. | 281 // request; the stream callback will resume it. |
282 if (!stream_writer_->Write(read_buffer_, bytes_read)) { | 282 if (!stream_writer_->Write(read_buffer_, bytes_read)) { |
283 PauseRequest(); | 283 PauseRequest(); |
284 *defer = was_deferred_ = true; | 284 *defer = was_deferred_ = true; |
285 last_stream_pause_time_ = now; | 285 last_stream_pause_time_ = now; |
286 } | 286 } |
287 | 287 |
288 read_buffer_ = NULL; // Drop our reference. | 288 read_buffer_ = NULL; // Drop our reference. |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 | 485 |
486 // Remove output stream callback if a stream exists. | 486 // Remove output stream callback if a stream exists. |
487 if (stream_writer_) | 487 if (stream_writer_) |
488 stream_writer_->RegisterCallback(base::Closure()); | 488 stream_writer_->RegisterCallback(base::Closure()); |
489 | 489 |
490 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", | 490 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", |
491 base::TimeTicks::Now() - download_start_time_); | 491 base::TimeTicks::Now() - download_start_time_); |
492 } | 492 } |
493 | 493 |
494 } // namespace content | 494 } // namespace content |
OLD | NEW |