Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: content/browser/download/download_item_impl.cc

Issue 2689373003: Introduce ParallelDownloadJob. (Closed)
Patch Set: Polish some details. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // File method ordering: Methods in this file are in the same order as 5 // File method ordering: Methods in this file are in the same order as
6 // in download_item_impl.h, with the following exception: The public 6 // in download_item_impl.h, with the following exception: The public
7 // interface Start is placed in chronological order with the other 7 // interface Start is placed in chronological order with the other
8 // (private) routines that together define a DownloadItem's state 8 // (private) routines that together define a DownloadItem's state
9 // transitions as the download progresses. See "Download progression 9 // transitions as the download progresses. See "Download progression
10 // cascade" later in this file. 10 // cascade" later in this file.
(...skipping 22 matching lines...) Expand all
33 #include "base/logging.h" 33 #include "base/logging.h"
34 #include "base/metrics/histogram_macros.h" 34 #include "base/metrics/histogram_macros.h"
35 #include "base/stl_util.h" 35 #include "base/stl_util.h"
36 #include "base/strings/string_util.h" 36 #include "base/strings/string_util.h"
37 #include "base/strings/stringprintf.h" 37 #include "base/strings/stringprintf.h"
38 #include "base/strings/utf_string_conversions.h" 38 #include "base/strings/utf_string_conversions.h"
39 #include "content/browser/download/download_create_info.h" 39 #include "content/browser/download/download_create_info.h"
40 #include "content/browser/download/download_file.h" 40 #include "content/browser/download/download_file.h"
41 #include "content/browser/download/download_interrupt_reasons_impl.h" 41 #include "content/browser/download/download_interrupt_reasons_impl.h"
42 #include "content/browser/download/download_item_impl_delegate.h" 42 #include "content/browser/download/download_item_impl_delegate.h"
43 #include "content/browser/download/download_job_factory.h"
44 #include "content/browser/download/download_job_impl.h"
43 #include "content/browser/download/download_net_log_parameters.h" 45 #include "content/browser/download/download_net_log_parameters.h"
44 #include "content/browser/download/download_request_handle.h" 46 #include "content/browser/download/download_request_handle.h"
45 #include "content/browser/download/download_stats.h" 47 #include "content/browser/download/download_stats.h"
46 #include "content/browser/renderer_host/render_view_host_impl.h" 48 #include "content/browser/renderer_host/render_view_host_impl.h"
47 #include "content/browser/web_contents/web_contents_impl.h" 49 #include "content/browser/web_contents/web_contents_impl.h"
48 #include "content/public/browser/browser_context.h" 50 #include "content/public/browser/browser_context.h"
49 #include "content/public/browser/browser_thread.h" 51 #include "content/public/browser/browser_thread.h"
50 #include "content/public/browser/content_browser_client.h" 52 #include "content/public/browser/content_browser_client.h"
51 #include "content/public/browser/download_danger_type.h" 53 #include "content/public/browser/download_danger_type.h"
52 #include "content/public/browser/download_interrupt_reasons.h" 54 #include "content/public/browser/download_interrupt_reasons.h"
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 // Constructing for the "Save Page As..." feature: 232 // Constructing for the "Save Page As..." feature:
231 DownloadItemImpl::DownloadItemImpl( 233 DownloadItemImpl::DownloadItemImpl(
232 DownloadItemImplDelegate* delegate, 234 DownloadItemImplDelegate* delegate,
233 uint32_t download_id, 235 uint32_t download_id,
234 const base::FilePath& path, 236 const base::FilePath& path,
235 const GURL& url, 237 const GURL& url,
236 const std::string& mime_type, 238 const std::string& mime_type,
237 std::unique_ptr<DownloadRequestHandleInterface> request_handle, 239 std::unique_ptr<DownloadRequestHandleInterface> request_handle,
238 const net::NetLogWithSource& net_log) 240 const net::NetLogWithSource& net_log)
239 : is_save_package_download_(true), 241 : is_save_package_download_(true),
240 request_handle_(std::move(request_handle)),
241 guid_(base::ToUpperASCII(base::GenerateGUID())), 242 guid_(base::ToUpperASCII(base::GenerateGUID())),
242 download_id_(download_id), 243 download_id_(download_id),
243 target_path_(path), 244 target_path_(path),
244 url_chain_(1, url), 245 url_chain_(1, url),
245 mime_type_(mime_type), 246 mime_type_(mime_type),
246 original_mime_type_(mime_type), 247 original_mime_type_(mime_type),
247 start_tick_(base::TimeTicks::Now()), 248 start_tick_(base::TimeTicks::Now()),
248 state_(IN_PROGRESS_INTERNAL), 249 state_(IN_PROGRESS_INTERNAL),
249 start_time_(base::Time::Now()), 250 start_time_(base::Time::Now()),
250 delegate_(delegate), 251 delegate_(delegate),
251 current_path_(path), 252 current_path_(path),
252 net_log_(net_log), 253 net_log_(net_log),
253 weak_ptr_factory_(this) { 254 weak_ptr_factory_(this) {
255 job_ = base::MakeUnique<DownloadJobImpl>(this, std::move(request_handle));
254 delegate_->Attach(); 256 delegate_->Attach();
255 Init(true /* actively downloading */, SRC_SAVE_PAGE_AS); 257 Init(true /* actively downloading */, SRC_SAVE_PAGE_AS);
256 } 258 }
257 259
258 DownloadItemImpl::~DownloadItemImpl() { 260 DownloadItemImpl::~DownloadItemImpl() {
259 DCHECK_CURRENTLY_ON(BrowserThread::UI); 261 DCHECK_CURRENTLY_ON(BrowserThread::UI);
260 262
261 // Should always have been nuked before now, at worst in 263 // Should always have been nuked before now, at worst in
262 // DownloadManager shutdown. 264 // DownloadManager shutdown.
263 DCHECK(!download_file_.get()); 265 DCHECK(!download_file_.get());
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 base::Bind(&MakeCopyOfDownloadFile, download_file_.get()), callback); 342 base::Bind(&MakeCopyOfDownloadFile, download_file_.get()), callback);
341 } else { 343 } else {
342 callback.Run(current_path_); 344 callback.Run(current_path_);
343 } 345 }
344 } 346 }
345 347
346 void DownloadItemImpl::Pause() { 348 void DownloadItemImpl::Pause() {
347 DCHECK_CURRENTLY_ON(BrowserThread::UI); 349 DCHECK_CURRENTLY_ON(BrowserThread::UI);
348 350
349 // Ignore irrelevant states. 351 // Ignore irrelevant states.
350 if (is_paused_) 352 if (IsPaused())
351 return; 353 return;
352 354
353 switch (state_) { 355 switch (state_) {
354 case CANCELLED_INTERNAL: 356 case CANCELLED_INTERNAL:
355 case COMPLETE_INTERNAL: 357 case COMPLETE_INTERNAL:
356 case COMPLETING_INTERNAL: 358 case COMPLETING_INTERNAL:
357 case INITIAL_INTERNAL: 359 case INITIAL_INTERNAL:
358 case INTERRUPTED_INTERNAL: 360 case INTERRUPTED_INTERNAL:
359 case INTERRUPTED_TARGET_PENDING_INTERNAL: 361 case INTERRUPTED_TARGET_PENDING_INTERNAL:
360 case RESUMING_INTERNAL: 362 case RESUMING_INTERNAL:
361 // No active request. 363 // No active request.
362 // TODO(asanka): In the case of RESUMING_INTERNAL, consider setting 364 // TODO(asanka): In the case of RESUMING_INTERNAL, consider setting
363 // is_paused_ even if there's no request currently associated with this 365 // |DownloadJob::is_paused_| even if there's no request currently
364 // DII. When a request is assigned (due to a resumption, for example) we 366 // associated with this DII. When a request is assigned (due to a
365 // can honor the is_paused_ setting. 367 // resumption, for example) we can honor the |DownloadJob::is_paused_|
368 // setting.
366 return; 369 return;
367 370
368 case IN_PROGRESS_INTERNAL: 371 case IN_PROGRESS_INTERNAL:
369 case TARGET_PENDING_INTERNAL: 372 case TARGET_PENDING_INTERNAL:
370 request_handle_->PauseRequest(); 373 job_->Pause();
371 is_paused_ = true;
372 UpdateObservers(); 374 UpdateObservers();
373 return; 375 return;
374 376
375 case MAX_DOWNLOAD_INTERNAL_STATE: 377 case MAX_DOWNLOAD_INTERNAL_STATE:
376 case TARGET_RESOLVED_INTERNAL: 378 case TARGET_RESOLVED_INTERNAL:
377 NOTREACHED(); 379 NOTREACHED();
378 } 380 }
379 } 381 }
380 382
381 void DownloadItemImpl::Resume() { 383 void DownloadItemImpl::Resume() {
382 DCHECK_CURRENTLY_ON(BrowserThread::UI); 384 DCHECK_CURRENTLY_ON(BrowserThread::UI);
383 DVLOG(20) << __func__ << "() download = " << DebugString(true); 385 DVLOG(20) << __func__ << "() download = " << DebugString(true);
384 switch (state_) { 386 switch (state_) {
385 case CANCELLED_INTERNAL: // Nothing to resume. 387 case CANCELLED_INTERNAL: // Nothing to resume.
386 case COMPLETE_INTERNAL: 388 case COMPLETE_INTERNAL:
387 case COMPLETING_INTERNAL: 389 case COMPLETING_INTERNAL:
388 case INITIAL_INTERNAL: 390 case INITIAL_INTERNAL:
389 case INTERRUPTED_TARGET_PENDING_INTERNAL: 391 case INTERRUPTED_TARGET_PENDING_INTERNAL:
390 case RESUMING_INTERNAL: // Resumption in progress. 392 case RESUMING_INTERNAL: // Resumption in progress.
391 return; 393 return;
392 394
393 case TARGET_PENDING_INTERNAL: 395 case TARGET_PENDING_INTERNAL:
394 case IN_PROGRESS_INTERNAL: 396 case IN_PROGRESS_INTERNAL:
395 if (!is_paused_) 397 if (!IsPaused())
396 return; 398 return;
397 request_handle_->ResumeRequest(); 399 job_->Resume();
qinmin 2017/02/22 19:51:23 job_ can be null here, right?
xingliu 2017/02/22 21:22:41 Done, it should have returned in previous IsPause(
398 is_paused_ = false;
399 UpdateObservers(); 400 UpdateObservers();
400 return; 401 return;
401 402
402 case INTERRUPTED_INTERNAL: 403 case INTERRUPTED_INTERNAL:
403 auto_resume_count_ = 0; // User input resets the counter. 404 auto_resume_count_ = 0; // User input resets the counter.
404 ResumeInterruptedDownload(ResumptionRequestSource::USER); 405 ResumeInterruptedDownload(ResumptionRequestSource::USER);
405 UpdateObservers(); 406 UpdateObservers();
406 return; 407 return;
407 408
408 case MAX_DOWNLOAD_INTERNAL_STATE: 409 case MAX_DOWNLOAD_INTERNAL_STATE:
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 477
477 DownloadItem::DownloadState DownloadItemImpl::GetState() const { 478 DownloadItem::DownloadState DownloadItemImpl::GetState() const {
478 return InternalToExternalState(state_); 479 return InternalToExternalState(state_);
479 } 480 }
480 481
481 DownloadInterruptReason DownloadItemImpl::GetLastReason() const { 482 DownloadInterruptReason DownloadItemImpl::GetLastReason() const {
482 return last_reason_; 483 return last_reason_;
483 } 484 }
484 485
485 bool DownloadItemImpl::IsPaused() const { 486 bool DownloadItemImpl::IsPaused() const {
486 return is_paused_; 487 return job_ ? job_->is_paused() : false;
487 } 488 }
488 489
489 bool DownloadItemImpl::IsTemporary() const { 490 bool DownloadItemImpl::IsTemporary() const {
490 return is_temporary_; 491 return is_temporary_;
491 } 492 }
492 493
493 bool DownloadItemImpl::CanResume() const { 494 bool DownloadItemImpl::CanResume() const {
494 DCHECK_CURRENTLY_ON(BrowserThread::UI); 495 DCHECK_CURRENTLY_ON(BrowserThread::UI);
495 switch (state_) { 496 switch (state_) {
496 case INITIAL_INTERNAL: 497 case INITIAL_INTERNAL:
497 case COMPLETING_INTERNAL: 498 case COMPLETING_INTERNAL:
498 case COMPLETE_INTERNAL: 499 case COMPLETE_INTERNAL:
499 case CANCELLED_INTERNAL: 500 case CANCELLED_INTERNAL:
500 case RESUMING_INTERNAL: 501 case RESUMING_INTERNAL:
501 case INTERRUPTED_TARGET_PENDING_INTERNAL: 502 case INTERRUPTED_TARGET_PENDING_INTERNAL:
502 return false; 503 return false;
503 504
504 case TARGET_PENDING_INTERNAL: 505 case TARGET_PENDING_INTERNAL:
505 case TARGET_RESOLVED_INTERNAL: 506 case TARGET_RESOLVED_INTERNAL:
506 case IN_PROGRESS_INTERNAL: 507 case IN_PROGRESS_INTERNAL:
507 return is_paused_; 508 return IsPaused();
508 509
509 case INTERRUPTED_INTERNAL: { 510 case INTERRUPTED_INTERNAL: {
510 ResumeMode resume_mode = GetResumeMode(); 511 ResumeMode resume_mode = GetResumeMode();
511 // Only allow Resume() calls if the resumption mode requires a user 512 // Only allow Resume() calls if the resumption mode requires a user
512 // action. 513 // action.
513 return resume_mode == RESUME_MODE_USER_RESTART || 514 return resume_mode == RESUME_MODE_USER_RESTART ||
514 resume_mode == RESUME_MODE_USER_CONTINUE; 515 resume_mode == RESUME_MODE_USER_CONTINUE;
515 } 516 }
516 517
517 case MAX_DOWNLOAD_INTERNAL_STATE: 518 case MAX_DOWNLOAD_INTERNAL_STATE:
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
691 int64_t speed = CurrentSpeed(); 692 int64_t speed = CurrentSpeed();
692 if (speed == 0) 693 if (speed == 0)
693 return false; 694 return false;
694 695
695 *remaining = base::TimeDelta::FromSeconds( 696 *remaining = base::TimeDelta::FromSeconds(
696 (total_bytes_ - received_bytes_) / speed); 697 (total_bytes_ - received_bytes_) / speed);
697 return true; 698 return true;
698 } 699 }
699 700
700 int64_t DownloadItemImpl::CurrentSpeed() const { 701 int64_t DownloadItemImpl::CurrentSpeed() const {
701 if (is_paused_) 702 if (IsPaused())
702 return 0; 703 return 0;
703 return bytes_per_sec_; 704 return bytes_per_sec_;
704 } 705 }
705 706
706 int DownloadItemImpl::PercentComplete() const { 707 int DownloadItemImpl::PercentComplete() const {
707 // If the delegate is delaying completion of the download, then we have no 708 // If the delegate is delaying completion of the download, then we have no
708 // idea how long it will take. 709 // idea how long it will take.
709 if (delegate_delayed_complete_ || total_bytes_ <= 0) 710 if (delegate_delayed_complete_ || total_bytes_ <= 0)
710 return -1; 711 return -1;
711 712
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 766
766 BrowserContext* DownloadItemImpl::GetBrowserContext() const { 767 BrowserContext* DownloadItemImpl::GetBrowserContext() const {
767 return delegate_->GetBrowserContext(); 768 return delegate_->GetBrowserContext();
768 } 769 }
769 770
770 WebContents* DownloadItemImpl::GetWebContents() const { 771 WebContents* DownloadItemImpl::GetWebContents() const {
771 // TODO(rdsmith): Remove null check after removing GetWebContents() from 772 // TODO(rdsmith): Remove null check after removing GetWebContents() from
772 // paths that might be used by DownloadItems created from history import. 773 // paths that might be used by DownloadItems created from history import.
773 // Currently such items have null request_handle_s, where other items 774 // Currently such items have null request_handle_s, where other items
774 // (regular and SavePackage downloads) have actual objects off the pointer. 775 // (regular and SavePackage downloads) have actual objects off the pointer.
775 if (request_handle_) 776 if (job_)
776 return request_handle_->GetWebContents(); 777 return job_->GetWebContents();
777 return NULL; 778 return nullptr;
778 } 779 }
779 780
780 void DownloadItemImpl::OnContentCheckCompleted(DownloadDangerType danger_type) { 781 void DownloadItemImpl::OnContentCheckCompleted(DownloadDangerType danger_type) {
781 DCHECK_CURRENTLY_ON(BrowserThread::UI); 782 DCHECK_CURRENTLY_ON(BrowserThread::UI);
782 DCHECK(AllDataSaved()); 783 DCHECK(AllDataSaved());
783 784
784 // Danger type is only allowed to be set on an active download after all data 785 // Danger type is only allowed to be set on an active download after all data
785 // has been saved. This excludes all other states. In particular, 786 // has been saved. This excludes all other states. In particular,
786 // OnContentCheckCompleted() isn't allowed on an INTERRUPTED download since 787 // OnContentCheckCompleted() isn't allowed on an INTERRUPTED download since
787 // such an interruption would need to happen between OnAllDataSaved() and 788 // such an interruption would need to happen between OnAllDataSaved() and
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 873
873 // We can't continue without a handle on the intermediate file. 874 // We can't continue without a handle on the intermediate file.
874 // We also can't continue if we don't have some verifier to make sure 875 // We also can't continue if we don't have some verifier to make sure
875 // we're getting the same file. 876 // we're getting the same file.
876 bool restart_required = 877 bool restart_required =
877 (current_path_.empty() || (etag_.empty() && last_modified_time_.empty())); 878 (current_path_.empty() || (etag_.empty() && last_modified_time_.empty()));
878 879
879 // We won't auto-restart if we've used up our attempts or the 880 // We won't auto-restart if we've used up our attempts or the
880 // download has been paused by user action. 881 // download has been paused by user action.
881 bool user_action_required = 882 bool user_action_required =
882 (auto_resume_count_ >= kMaxAutoResumeAttempts || is_paused_); 883 (auto_resume_count_ >= kMaxAutoResumeAttempts || IsPaused());
883 884
884 switch(last_reason_) { 885 switch(last_reason_) {
885 case DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR: 886 case DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR:
886 case DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT: 887 case DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT:
887 break; 888 break;
888 889
889 case DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE: 890 case DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE:
890 // The server disagreed with the file offset that we sent. 891 // The server disagreed with the file offset that we sent.
891 892
892 case DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH: 893 case DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH:
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
1165 // We're starting the download. 1166 // We're starting the download.
1166 void DownloadItemImpl::Start( 1167 void DownloadItemImpl::Start(
1167 std::unique_ptr<DownloadFile> file, 1168 std::unique_ptr<DownloadFile> file,
1168 std::unique_ptr<DownloadRequestHandleInterface> req_handle, 1169 std::unique_ptr<DownloadRequestHandleInterface> req_handle,
1169 const DownloadCreateInfo& new_create_info) { 1170 const DownloadCreateInfo& new_create_info) {
1170 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1171 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1171 DCHECK(!download_file_.get()); 1172 DCHECK(!download_file_.get());
1172 DVLOG(20) << __func__ << "() this=" << DebugString(true); 1173 DVLOG(20) << __func__ << "() this=" << DebugString(true);
1173 1174
1174 download_file_ = std::move(file); 1175 download_file_ = std::move(file);
1175 request_handle_ = std::move(req_handle); 1176 job_ = DownloadJobFactory::CreateJob(this, std::move(req_handle),
1177 new_create_info);
1176 destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE; 1178 destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE;
1177 1179
1178 if (state_ == CANCELLED_INTERNAL) { 1180 if (state_ == CANCELLED_INTERNAL) {
1179 // The download was in the process of resuming when it was cancelled. Don't 1181 // The download was in the process of resuming when it was cancelled. Don't
1180 // proceed. 1182 // proceed.
1181 ReleaseDownloadFile(true); 1183 ReleaseDownloadFile(true);
1182 if (request_handle_) 1184 job_->Cancel(true);
1183 request_handle_->CancelRequest();
1184 return; 1185 return;
1185 } 1186 }
1186 1187
1187 // The state could be one of the following: 1188 // The state could be one of the following:
1188 // 1189 //
1189 // INITIAL_INTERNAL: A normal download attempt. 1190 // INITIAL_INTERNAL: A normal download attempt.
1190 // 1191 //
1191 // RESUMING_INTERNAL: A resumption attempt. May or may not have been 1192 // RESUMING_INTERNAL: A resumption attempt. May or may not have been
1192 // successful. 1193 // successful.
1193 DCHECK(state_ == INITIAL_INTERNAL || state_ == RESUMING_INTERNAL); 1194 DCHECK(state_ == INITIAL_INTERNAL || state_ == RESUMING_INTERNAL);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1225 // interruption. Continue with current target path. 1226 // interruption. Continue with current target path.
1226 TransitionTo(TARGET_RESOLVED_INTERNAL); 1227 TransitionTo(TARGET_RESOLVED_INTERNAL);
1227 InterruptWithPartialState( 1228 InterruptWithPartialState(
1228 offset, std::move(hash_state), new_create_info.result); 1229 offset, std::move(hash_state), new_create_info.result);
1229 UpdateObservers(); 1230 UpdateObservers();
1230 return; 1231 return;
1231 } 1232 }
1232 1233
1233 // Successful download start. 1234 // Successful download start.
1234 DCHECK(download_file_.get()); 1235 DCHECK(download_file_.get());
1235 DCHECK(request_handle_.get()); 1236 DCHECK(job_.get());
1236 1237
1237 if (state_ == RESUMING_INTERNAL) 1238 if (state_ == RESUMING_INTERNAL)
1238 UpdateValidatorsOnResumption(new_create_info); 1239 UpdateValidatorsOnResumption(new_create_info);
1239 1240
1240 TransitionTo(TARGET_PENDING_INTERNAL); 1241 TransitionTo(TARGET_PENDING_INTERNAL);
1241 1242
1243 job_->Start();
1244 }
1245
1246 void DownloadItemImpl::StartDownloadProgress() {
1242 BrowserThread::PostTask( 1247 BrowserThread::PostTask(
1243 BrowserThread::FILE, FROM_HERE, 1248 BrowserThread::FILE, FROM_HERE,
1244 base::Bind(&DownloadFile::Initialize, 1249 base::Bind(&DownloadFile::Initialize,
1245 // Safe because we control download file lifetime. 1250 // Safe because we control download file lifetime.
1246 base::Unretained(download_file_.get()), 1251 base::Unretained(download_file_.get()),
1247 base::Bind(&DownloadItemImpl::OnDownloadFileInitialized, 1252 base::Bind(&DownloadItemImpl::OnDownloadFileInitialized,
1248 weak_ptr_factory_.GetWeakPtr()))); 1253 weak_ptr_factory_.GetWeakPtr())));
1249 } 1254 }
1250 1255
1251 void DownloadItemImpl::OnDownloadFileInitialized( 1256 void DownloadItemImpl::OnDownloadFileInitialized(
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
1628 1633
1629 if (current_path_.empty()) { 1634 if (current_path_.empty()) {
1630 hash_state_.reset(); 1635 hash_state_.reset();
1631 hash_.clear(); 1636 hash_.clear();
1632 received_bytes_ = 0; 1637 received_bytes_ = 0;
1633 } else { 1638 } else {
1634 UpdateProgress(bytes_so_far, 0); 1639 UpdateProgress(bytes_so_far, 0);
1635 SetHashState(std::move(hash_state)); 1640 SetHashState(std::move(hash_state));
1636 } 1641 }
1637 1642
1638 if (request_handle_) 1643 if (job_.get())
1639 request_handle_->CancelRequest(); 1644 job_->Cancel(false);
1640 1645
1641 if (reason == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED || 1646 if (reason == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED ||
1642 reason == DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN) { 1647 reason == DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN) {
1643 if (IsDangerous()) { 1648 if (IsDangerous()) {
1644 RecordDangerousDownloadDiscard( 1649 RecordDangerousDownloadDiscard(
1645 reason == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED 1650 reason == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
1646 ? DOWNLOAD_DISCARD_DUE_TO_USER_ACTION 1651 ? DOWNLOAD_DISCARD_DUE_TO_USER_ACTION
1647 : DOWNLOAD_DISCARD_DUE_TO_SHUTDOWN, 1652 : DOWNLOAD_DISCARD_DUE_TO_SHUTDOWN,
1648 GetDangerType(), GetTargetFilePath()); 1653 GetDangerType(), GetTargetFilePath());
1649 } 1654 }
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
1769 case TARGET_RESOLVED_INTERNAL: 1774 case TARGET_RESOLVED_INTERNAL:
1770 case INTERRUPTED_TARGET_PENDING_INTERNAL: 1775 case INTERRUPTED_TARGET_PENDING_INTERNAL:
1771 break; 1776 break;
1772 1777
1773 case IN_PROGRESS_INTERNAL: 1778 case IN_PROGRESS_INTERNAL:
1774 DCHECK(!current_path_.empty()) << "Current output path must be known."; 1779 DCHECK(!current_path_.empty()) << "Current output path must be known.";
1775 DCHECK(!target_path_.empty()) << "Target path must be known."; 1780 DCHECK(!target_path_.empty()) << "Target path must be known.";
1776 DCHECK(current_path_.DirName() == target_path_.DirName()) 1781 DCHECK(current_path_.DirName() == target_path_.DirName())
1777 << "Current output directory must match target directory."; 1782 << "Current output directory must match target directory.";
1778 DCHECK(download_file_) << "Output file must be owned by download item."; 1783 DCHECK(download_file_) << "Output file must be owned by download item.";
1779 DCHECK(request_handle_) << "Download source must be active."; 1784 DCHECK(job_) << "Must have active download job.";
1780 DCHECK(!is_paused_) << "At the time a download enters IN_PROGRESS state, " 1785 DCHECK(!job_->is_paused())
1781 "it must not be paused."; 1786 << "At the time a download enters IN_PROGRESS state, "
1787 "it must not be paused.";
1782 break; 1788 break;
1783 1789
1784 case COMPLETING_INTERNAL: 1790 case COMPLETING_INTERNAL:
1785 DCHECK(all_data_saved_) << "All data must be saved prior to completion."; 1791 DCHECK(all_data_saved_) << "All data must be saved prior to completion.";
1786 DCHECK(!download_file_) 1792 DCHECK(!download_file_)
1787 << "Download file must be released prior to completion."; 1793 << "Download file must be released prior to completion.";
1788 DCHECK(!target_path_.empty()) << "Target path must be known."; 1794 DCHECK(!target_path_.empty()) << "Target path must be known.";
1789 DCHECK(current_path_ == target_path_) 1795 DCHECK(current_path_ == target_path_)
1790 << "Current output path must match target path."; 1796 << "Current output path must match target path.";
1791 1797
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
1939 // (which is the contents of the Referer header for the last download request) 1945 // (which is the contents of the Referer header for the last download request)
1940 // will only be sent to the URL returned by GetURL(). 1946 // will only be sent to the URL returned by GetURL().
1941 download_params->set_referrer( 1947 download_params->set_referrer(
1942 Referrer(GetReferrerUrl(), blink::WebReferrerPolicyAlways)); 1948 Referrer(GetReferrerUrl(), blink::WebReferrerPolicyAlways));
1943 1949
1944 TransitionTo(RESUMING_INTERNAL); 1950 TransitionTo(RESUMING_INTERNAL);
1945 RecordDownloadSource(source == ResumptionRequestSource::USER 1951 RecordDownloadSource(source == ResumptionRequestSource::USER
1946 ? INITIATED_BY_MANUAL_RESUMPTION 1952 ? INITIATED_BY_MANUAL_RESUMPTION
1947 : INITIATED_BY_AUTOMATIC_RESUMPTION); 1953 : INITIATED_BY_AUTOMATIC_RESUMPTION);
1948 delegate_->ResumeInterruptedDownload(std::move(download_params), GetId()); 1954 delegate_->ResumeInterruptedDownload(std::move(download_params), GetId());
1949 // Just in case we were interrupted while paused. 1955 // Just in case we were interrupted while paused. |job_| may be null
1950 is_paused_ = false; 1956 // when no WebContents is associated with a download resumption.
1957 if (job_)
1958 job_->set_is_paused(false);
1951 } 1959 }
1952 1960
1953 // static 1961 // static
1954 DownloadItem::DownloadState DownloadItemImpl::InternalToExternalState( 1962 DownloadItem::DownloadState DownloadItemImpl::InternalToExternalState(
1955 DownloadInternalState internal_state) { 1963 DownloadInternalState internal_state) {
1956 switch (internal_state) { 1964 switch (internal_state) {
1957 case INITIAL_INTERNAL: 1965 case INITIAL_INTERNAL:
1958 case TARGET_PENDING_INTERNAL: 1966 case TARGET_PENDING_INTERNAL:
1959 case TARGET_RESOLVED_INTERNAL: 1967 case TARGET_RESOLVED_INTERNAL:
1960 case INTERRUPTED_TARGET_PENDING_INTERNAL: 1968 case INTERRUPTED_TARGET_PENDING_INTERNAL:
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
2120 case RESUME_MODE_USER_CONTINUE: 2128 case RESUME_MODE_USER_CONTINUE:
2121 return "USER_CONTINUE"; 2129 return "USER_CONTINUE";
2122 case RESUME_MODE_USER_RESTART: 2130 case RESUME_MODE_USER_RESTART:
2123 return "USER_RESTART"; 2131 return "USER_RESTART";
2124 } 2132 }
2125 NOTREACHED() << "Unknown resume mode " << mode; 2133 NOTREACHED() << "Unknown resume mode " << mode;
2126 return "unknown"; 2134 return "unknown";
2127 } 2135 }
2128 2136
2129 } // namespace content 2137 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698