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_request_core.h" | 5 #include "content/browser/download/download_request_core.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 | 195 |
196 DownloadRequestData::Attach(request.get(), params, download_id); | 196 DownloadRequestData::Attach(request.get(), params, download_id); |
197 return request; | 197 return request; |
198 } | 198 } |
199 | 199 |
200 DownloadRequestCore::DownloadRequestCore(net::URLRequest* request, | 200 DownloadRequestCore::DownloadRequestCore(net::URLRequest* request, |
201 Delegate* delegate) | 201 Delegate* delegate) |
202 : delegate_(delegate), | 202 : delegate_(delegate), |
203 request_(request), | 203 request_(request), |
204 download_id_(DownloadItem::kInvalidId), | 204 download_id_(DownloadItem::kInvalidId), |
205 last_buffer_size_(0), | |
206 bytes_read_(0), | 205 bytes_read_(0), |
207 pause_count_(0), | 206 pause_count_(0), |
208 was_deferred_(false), | 207 was_deferred_(false), |
209 is_partial_request_(false), | 208 is_partial_request_(false), |
210 started_(false), | 209 started_(false), |
211 abort_reason_(DOWNLOAD_INTERRUPT_REASON_NONE) { | 210 abort_reason_(DOWNLOAD_INTERRUPT_REASON_NONE) { |
212 DCHECK(request_); | 211 DCHECK(request_); |
213 DCHECK(delegate_); | 212 DCHECK(delegate_); |
214 RecordDownloadCount(UNTHROTTLED_COUNT); | 213 RecordDownloadCount(UNTHROTTLED_COUNT); |
215 power_save_blocker_.reset(new device::PowerSaveBlocker( | 214 power_save_blocker_.reset(new device::PowerSaveBlocker( |
(...skipping 11 matching lines...) Expand all Loading... |
227 } else { | 226 } else { |
228 save_info_.reset(new DownloadSaveInfo); | 227 save_info_.reset(new DownloadSaveInfo); |
229 } | 228 } |
230 } | 229 } |
231 | 230 |
232 DownloadRequestCore::~DownloadRequestCore() { | 231 DownloadRequestCore::~DownloadRequestCore() { |
233 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 232 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
234 // Remove output stream callback if a stream exists. | 233 // Remove output stream callback if a stream exists. |
235 if (stream_writer_) | 234 if (stream_writer_) |
236 stream_writer_->RegisterCallback(base::Closure()); | 235 stream_writer_->RegisterCallback(base::Closure()); |
237 | |
238 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", | |
239 base::TimeTicks::Now() - download_start_time_); | |
240 } | 236 } |
241 | 237 |
242 std::unique_ptr<DownloadCreateInfo> | 238 std::unique_ptr<DownloadCreateInfo> |
243 DownloadRequestCore::CreateDownloadCreateInfo(DownloadInterruptReason result) { | 239 DownloadRequestCore::CreateDownloadCreateInfo(DownloadInterruptReason result) { |
244 DCHECK(!started_); | 240 DCHECK(!started_); |
245 started_ = true; | 241 started_ = true; |
246 std::unique_ptr<DownloadCreateInfo> create_info(new DownloadCreateInfo( | 242 std::unique_ptr<DownloadCreateInfo> create_info(new DownloadCreateInfo( |
247 base::Time::Now(), request()->net_log(), std::move(save_info_))); | 243 base::Time::Now(), request()->net_log(), std::move(save_info_))); |
248 | 244 |
249 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) | 245 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 // Create a new buffer, which will be handed to the download thread for file | 355 // Create a new buffer, which will be handed to the download thread for file |
360 // writing and deletion. | 356 // writing and deletion. |
361 bool DownloadRequestCore::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 357 bool DownloadRequestCore::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
362 int* buf_size, | 358 int* buf_size, |
363 int min_size) { | 359 int min_size) { |
364 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 360 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
365 DCHECK(buf && buf_size); | 361 DCHECK(buf && buf_size); |
366 DCHECK(!read_buffer_.get()); | 362 DCHECK(!read_buffer_.get()); |
367 | 363 |
368 *buf_size = min_size < 0 ? kReadBufSize : min_size; | 364 *buf_size = min_size < 0 ? kReadBufSize : min_size; |
369 last_buffer_size_ = *buf_size; | |
370 read_buffer_ = new net::IOBuffer(*buf_size); | 365 read_buffer_ = new net::IOBuffer(*buf_size); |
371 *buf = read_buffer_.get(); | 366 *buf = read_buffer_.get(); |
372 return true; | 367 return true; |
373 } | 368 } |
374 | 369 |
375 // Pass the buffer to the download file writer. | 370 // Pass the buffer to the download file writer. |
376 bool DownloadRequestCore::OnReadCompleted(int bytes_read, bool* defer) { | 371 bool DownloadRequestCore::OnReadCompleted(int bytes_read, bool* defer) { |
377 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 372 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
378 DCHECK(read_buffer_.get()); | 373 DCHECK(read_buffer_.get()); |
379 | 374 |
380 base::TimeTicks now(base::TimeTicks::Now()); | |
381 if (!last_read_time_.is_null()) { | |
382 double seconds_since_last_read = (now - last_read_time_).InSecondsF(); | |
383 if (now == last_read_time_) | |
384 // Use 1/10 ms as a "very small number" so that we avoid | |
385 // divide-by-zero error and still record a very high potential bandwidth. | |
386 seconds_since_last_read = 0.00001; | |
387 | |
388 double actual_bandwidth = (bytes_read) / seconds_since_last_read; | |
389 double potential_bandwidth = last_buffer_size_ / seconds_since_last_read; | |
390 RecordBandwidth(actual_bandwidth, potential_bandwidth); | |
391 } | |
392 last_read_time_ = now; | |
393 | |
394 if (!bytes_read) | 375 if (!bytes_read) |
395 return true; | 376 return true; |
396 bytes_read_ += bytes_read; | 377 bytes_read_ += bytes_read; |
397 DCHECK(read_buffer_.get()); | 378 DCHECK(read_buffer_.get()); |
398 | 379 |
399 // Take the data ship it down the stream. If the stream is full, pause the | 380 // Take the data ship it down the stream. If the stream is full, pause the |
400 // request; the stream callback will resume it. | 381 // request; the stream callback will resume it. |
401 if (!stream_writer_->Write(read_buffer_, bytes_read)) { | 382 if (!stream_writer_->Write(read_buffer_, bytes_read)) { |
402 PauseRequest(); | 383 PauseRequest(); |
403 *defer = was_deferred_ = true; | 384 *defer = was_deferred_ = true; |
404 last_stream_pause_time_ = now; | 385 last_stream_pause_time_ = base::TimeTicks::Now(); |
405 } | 386 } |
406 | 387 |
407 read_buffer_ = NULL; // Drop our reference. | 388 read_buffer_ = NULL; // Drop our reference. |
408 | 389 |
409 if (pause_count_ > 0) | 390 if (pause_count_ > 0) |
410 *defer = was_deferred_ = true; | 391 *defer = was_deferred_ = true; |
411 | 392 |
412 return true; | 393 return true; |
413 } | 394 } |
414 | 395 |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 return DOWNLOAD_INTERRUPT_REASON_NONE; | 616 return DOWNLOAD_INTERRUPT_REASON_NONE; |
636 } | 617 } |
637 | 618 |
638 if (http_headers.response_code() == net::HTTP_PARTIAL_CONTENT) | 619 if (http_headers.response_code() == net::HTTP_PARTIAL_CONTENT) |
639 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; | 620 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; |
640 | 621 |
641 return DOWNLOAD_INTERRUPT_REASON_NONE; | 622 return DOWNLOAD_INTERRUPT_REASON_NONE; |
642 } | 623 } |
643 | 624 |
644 } // namespace content | 625 } // namespace content |
OLD | NEW |