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

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

Issue 148133007: [Downloads] Always call DM::StartDownload() for explicit downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Ketchup with upstream. Created 4 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 | Annotate | Revision Log
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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 has_user_gesture_(info.has_user_gesture), 185 has_user_gesture_(info.has_user_gesture),
186 content_disposition_(info.content_disposition), 186 content_disposition_(info.content_disposition),
187 mime_type_(info.mime_type), 187 mime_type_(info.mime_type),
188 original_mime_type_(info.original_mime_type), 188 original_mime_type_(info.original_mime_type),
189 remote_address_(info.remote_address), 189 remote_address_(info.remote_address),
190 total_bytes_(info.total_bytes), 190 total_bytes_(info.total_bytes),
191 received_bytes_(0), 191 received_bytes_(0),
192 bytes_per_sec_(0), 192 bytes_per_sec_(0),
193 last_modified_time_(info.last_modified), 193 last_modified_time_(info.last_modified),
194 etag_(info.etag), 194 etag_(info.etag),
195 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), 195 last_reason_(info.result),
196 start_tick_(base::TimeTicks::Now()), 196 start_tick_(base::TimeTicks::Now()),
197 state_(INITIAL_INTERNAL), 197 state_(INITIAL_INTERNAL),
198 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), 198 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS),
199 start_time_(info.start_time), 199 start_time_(info.start_time),
200 delegate_(delegate), 200 delegate_(delegate),
201 is_paused_(false), 201 is_paused_(false),
202 auto_resume_count_(0), 202 auto_resume_count_(0),
203 open_when_complete_(false), 203 open_when_complete_(false),
204 file_externally_removed_(false), 204 file_externally_removed_(false),
205 auto_opened_(false), 205 auto_opened_(false),
206 is_temporary_(!info.save_info->file_path.empty()), 206 is_temporary_(!info.save_info->file_path.empty()),
207 all_data_saved_(false), 207 all_data_saved_(false),
208 destination_error_(content::DOWNLOAD_INTERRUPT_REASON_NONE), 208 destination_error_(DOWNLOAD_INTERRUPT_REASON_NONE),
209 opened_(false), 209 opened_(false),
210 delegate_delayed_complete_(false), 210 delegate_delayed_complete_(false),
211 bound_net_log_(bound_net_log), 211 bound_net_log_(bound_net_log),
212 weak_ptr_factory_(this) { 212 weak_ptr_factory_(this) {
213 delegate_->Attach(); 213 delegate_->Attach();
214 Init(true /* actively downloading */, SRC_ACTIVE_DOWNLOAD); 214 Init(true /* actively downloading */, SRC_ACTIVE_DOWNLOAD);
215 215
216 // Link the event sources. 216 // Link the event sources.
217 bound_net_log_.AddEvent( 217 bound_net_log_.AddEvent(
218 net::NetLog::TYPE_DOWNLOAD_URL_REQUEST, 218 net::NetLog::TYPE_DOWNLOAD_URL_REQUEST,
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 case INITIAL_INTERNAL: 354 case INITIAL_INTERNAL:
355 case COMPLETING_INTERNAL: 355 case COMPLETING_INTERNAL:
356 case COMPLETE_INTERNAL: 356 case COMPLETE_INTERNAL:
357 case INTERRUPTED_INTERNAL: 357 case INTERRUPTED_INTERNAL:
358 case CANCELLED_INTERNAL: 358 case CANCELLED_INTERNAL:
359 case RESUMING_INTERNAL: 359 case RESUMING_INTERNAL:
360 // No active request. 360 // No active request.
361 return; 361 return;
362 362
363 case TARGET_PENDING_INTERNAL: 363 case TARGET_PENDING_INTERNAL:
364 case INTERRUPTED_TARGET_PENDING_INTERNAL:
364 case TARGET_RESOLVED_INTERNAL: 365 case TARGET_RESOLVED_INTERNAL:
365 case IN_PROGRESS_INTERNAL: 366 case IN_PROGRESS_INTERNAL:
366 request_handle_->PauseRequest(); 367 request_handle_->PauseRequest();
367 is_paused_ = true; 368 is_paused_ = true;
368 UpdateObservers(); 369 UpdateObservers();
369 return; 370 return;
370 371
371 case MAX_DOWNLOAD_INTERNAL_STATE: 372 case MAX_DOWNLOAD_INTERNAL_STATE:
372 NOTREACHED(); 373 NOTREACHED();
373 } 374 }
374 } 375 }
375 376
376 void DownloadItemImpl::Resume() { 377 void DownloadItemImpl::Resume() {
377 DCHECK_CURRENTLY_ON(BrowserThread::UI); 378 DCHECK_CURRENTLY_ON(BrowserThread::UI);
379 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true);
378 switch (state_) { 380 switch (state_) {
379 case INITIAL_INTERNAL: 381 case INITIAL_INTERNAL:
380 case COMPLETING_INTERNAL: 382 case COMPLETING_INTERNAL:
381 case COMPLETE_INTERNAL: 383 case COMPLETE_INTERNAL:
382 case CANCELLED_INTERNAL: 384 case CANCELLED_INTERNAL:
383 // Nothing to resume. 385 // Nothing to resume.
384 case RESUMING_INTERNAL: 386 case RESUMING_INTERNAL:
385 // Resumption in progress. 387 // Resumption in progress.
386 return; 388 return;
387 389
388 case TARGET_PENDING_INTERNAL: 390 case TARGET_PENDING_INTERNAL:
391 case INTERRUPTED_TARGET_PENDING_INTERNAL:
Randy Smith (Not in Mondays) 2016/02/11 22:08:08 This also seems incompatible with mapping to an ex
asanka 2016/02/11 23:57:25 It's worse than that, we can't trigger a resumptio
asanka 2016/02/12 16:06:49 Fine! We'll go with mapping this to IN_PROGRESS :P
Randy Smith (Not in Mondays) 2016/02/12 18:01:19 :-}.
389 case TARGET_RESOLVED_INTERNAL: 392 case TARGET_RESOLVED_INTERNAL:
390 case IN_PROGRESS_INTERNAL: 393 case IN_PROGRESS_INTERNAL:
391 if (!is_paused_) 394 if (!is_paused_)
392 return; 395 return;
393 request_handle_->ResumeRequest(); 396 request_handle_->ResumeRequest();
394 is_paused_ = false; 397 is_paused_ = false;
395 UpdateObservers(); 398 UpdateObservers();
396 return; 399 return;
397 400
398 case INTERRUPTED_INTERNAL: 401 case INTERRUPTED_INTERNAL:
399 auto_resume_count_ = 0; // User input resets the counter. 402 auto_resume_count_ = 0; // User input resets the counter.
400 ResumeInterruptedDownload(); 403 ResumeInterruptedDownload();
404 UpdateObservers();
Randy Smith (Not in Mondays) 2016/02/11 22:08:08 See comment elsewhere about registered observers v
asanka 2016/02/11 23:57:25 Responded there. Basically, UpdateObservers is bes
401 return; 405 return;
402 406
403 case MAX_DOWNLOAD_INTERNAL_STATE: 407 case MAX_DOWNLOAD_INTERNAL_STATE:
404 NOTREACHED(); 408 NOTREACHED();
405 } 409 }
406 } 410 }
407 411
408 void DownloadItemImpl::Cancel(bool user_cancel) { 412 void DownloadItemImpl::Cancel(bool user_cancel) {
409 DCHECK_CURRENTLY_ON(BrowserThread::UI); 413 DCHECK_CURRENTLY_ON(BrowserThread::UI);
410 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); 414 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true);
411 Interrupt(user_cancel ? DOWNLOAD_INTERRUPT_REASON_USER_CANCELED 415 Interrupt(user_cancel ? DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
412 : DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN); 416 : DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN);
417 UpdateObservers();
413 } 418 }
414 419
415 void DownloadItemImpl::Remove() { 420 void DownloadItemImpl::Remove() {
416 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); 421 DVLOG(20) << __FUNCTION__ << "() download = " << DebugString(true);
417 DCHECK_CURRENTLY_ON(BrowserThread::UI); 422 DCHECK_CURRENTLY_ON(BrowserThread::UI);
418 423
419 delegate_->AssertStateConsistent(this); 424 delegate_->AssertStateConsistent(this);
420 Interrupt(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); 425 Interrupt(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
421 delegate_->AssertStateConsistent(this); 426 delegate_->AssertStateConsistent(this);
422 427
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 DCHECK_CURRENTLY_ON(BrowserThread::UI); 485 DCHECK_CURRENTLY_ON(BrowserThread::UI);
481 switch (state_) { 486 switch (state_) {
482 case INITIAL_INTERNAL: 487 case INITIAL_INTERNAL:
483 case COMPLETING_INTERNAL: 488 case COMPLETING_INTERNAL:
484 case COMPLETE_INTERNAL: 489 case COMPLETE_INTERNAL:
485 case CANCELLED_INTERNAL: 490 case CANCELLED_INTERNAL:
486 case RESUMING_INTERNAL: 491 case RESUMING_INTERNAL:
487 return false; 492 return false;
488 493
489 case TARGET_PENDING_INTERNAL: 494 case TARGET_PENDING_INTERNAL:
495 case INTERRUPTED_TARGET_PENDING_INTERNAL:
490 case TARGET_RESOLVED_INTERNAL: 496 case TARGET_RESOLVED_INTERNAL:
491 case IN_PROGRESS_INTERNAL: 497 case IN_PROGRESS_INTERNAL:
492 return is_paused_; 498 return is_paused_;
493 499
494 case INTERRUPTED_INTERNAL: { 500 case INTERRUPTED_INTERNAL: {
495 ResumeMode resume_mode = GetResumeMode(); 501 ResumeMode resume_mode = GetResumeMode();
496 // Only allow Resume() calls if the resumption mode requires a user 502 // Only allow Resume() calls if the resumption mode requires a user
497 // action. 503 // action.
498 return IsDownloadResumptionEnabled() && 504 return IsDownloadResumptionEnabled() &&
499 (resume_mode == RESUME_MODE_USER_RESTART || 505 (resume_mode == RESUME_MODE_USER_RESTART ||
500 resume_mode == RESUME_MODE_USER_CONTINUE); 506 resume_mode == RESUME_MODE_USER_CONTINUE);
501 } 507 }
502 508
503 case MAX_DOWNLOAD_INTERNAL_STATE: 509 case MAX_DOWNLOAD_INTERNAL_STATE:
504 NOTREACHED(); 510 NOTREACHED();
505 } 511 }
506 return false; 512 return false;
507 } 513 }
508 514
509 bool DownloadItemImpl::IsDone() const { 515 bool DownloadItemImpl::IsDone() const {
510 switch (state_) { 516 switch (state_) {
511 case INITIAL_INTERNAL: 517 case INITIAL_INTERNAL:
512 case COMPLETING_INTERNAL: 518 case COMPLETING_INTERNAL:
513 case RESUMING_INTERNAL: 519 case RESUMING_INTERNAL:
514 case TARGET_PENDING_INTERNAL: 520 case TARGET_PENDING_INTERNAL:
521 case INTERRUPTED_TARGET_PENDING_INTERNAL:
Randy Smith (Not in Mondays) 2016/02/11 22:08:09 I'm beginning to think that the mapping of this to
asanka 2016/02/11 23:57:25 ?
asanka 2016/02/12 16:06:49 NM
515 case TARGET_RESOLVED_INTERNAL: 522 case TARGET_RESOLVED_INTERNAL:
516 case IN_PROGRESS_INTERNAL: 523 case IN_PROGRESS_INTERNAL:
517 return false; 524 return false;
518 525
519 case COMPLETE_INTERNAL: 526 case COMPLETE_INTERNAL:
520 case CANCELLED_INTERNAL: 527 case CANCELLED_INTERNAL:
521 return true; 528 return true;
522 529
523 case INTERRUPTED_INTERNAL: 530 case INTERRUPTED_INTERNAL:
524 return !CanResume(); 531 return !CanResume();
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 case DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED: 936 case DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED:
930 case DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM: 937 case DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM:
931 case DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN: 938 case DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN:
932 mode = RESUME_MODE_INVALID; 939 mode = RESUME_MODE_INVALID;
933 break; 940 break;
934 } 941 }
935 942
936 return mode; 943 return mode;
937 } 944 }
938 945
939 void DownloadItemImpl::MergeOriginInfoOnResume( 946 void DownloadItemImpl::UpdateValidatorsOnResumption(
940 const DownloadCreateInfo& new_create_info) { 947 const DownloadCreateInfo& new_create_info) {
941 DCHECK_CURRENTLY_ON(BrowserThread::UI); 948 DCHECK_CURRENTLY_ON(BrowserThread::UI);
942 DCHECK_EQ(RESUMING_INTERNAL, state_); 949 DCHECK_EQ(RESUMING_INTERNAL, state_);
943 DCHECK(!new_create_info.url_chain.empty()); 950 DCHECK(!new_create_info.url_chain.empty());
944 951
945 // We are going to tack on any new redirects to our list of redirects. 952 // We are going to tack on any new redirects to our list of redirects.
946 // When a download is resumed, the URL used for the resumption request is the 953 // When a download is resumed, the URL used for the resumption request is the
947 // one at the end of the previous redirect chain. Tacking additional redirects 954 // one at the end of the previous redirect chain. Tacking additional redirects
948 // to the end of this chain ensures that: 955 // to the end of this chain ensures that:
949 // - If the download needs to be resumed again, the ETag/Last-Modified headers 956 // - If the download needs to be resumed again, the ETag/Last-Modified headers
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 hash_state_ = ""; 1023 hash_state_ = "";
1017 1024
1018 UpdateObservers(); 1025 UpdateObservers();
1019 } 1026 }
1020 1027
1021 void DownloadItemImpl::MarkAsComplete() { 1028 void DownloadItemImpl::MarkAsComplete() {
1022 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1029 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1023 1030
1024 DCHECK(all_data_saved_); 1031 DCHECK(all_data_saved_);
1025 end_time_ = base::Time::Now(); 1032 end_time_ = base::Time::Now();
1026 TransitionTo(COMPLETE_INTERNAL, UPDATE_OBSERVERS); 1033 TransitionTo(COMPLETE_INTERNAL);
1034 UpdateObservers();
1027 } 1035 }
1028 1036
1029 void DownloadItemImpl::DestinationUpdate(int64_t bytes_so_far, 1037 void DownloadItemImpl::DestinationUpdate(int64_t bytes_so_far,
1030 int64_t bytes_per_sec, 1038 int64_t bytes_per_sec,
1031 const std::string& hash_state) { 1039 const std::string& hash_state) {
1032 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1040 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1033 DVLOG(20) << __FUNCTION__ << " so_far=" << bytes_so_far 1041 DVLOG(20) << __FUNCTION__ << " so_far=" << bytes_so_far
1034 << " per_sec=" << bytes_per_sec << " download=" 1042 << " per_sec=" << bytes_per_sec << " download="
1035 << DebugString(true); 1043 << DebugString(true);
1036 1044
(...skipping 29 matching lines...) Expand all
1066 1074
1067 void DownloadItemImpl::DestinationError(DownloadInterruptReason reason) { 1075 void DownloadItemImpl::DestinationError(DownloadInterruptReason reason) {
1068 DCHECK(state_ == TARGET_PENDING_INTERNAL || 1076 DCHECK(state_ == TARGET_PENDING_INTERNAL ||
1069 state_ == TARGET_RESOLVED_INTERNAL || state_ == IN_PROGRESS_INTERNAL); 1077 state_ == TARGET_RESOLVED_INTERNAL || state_ == IN_PROGRESS_INTERNAL);
1070 DVLOG(20) << __FUNCTION__ 1078 DVLOG(20) << __FUNCTION__
1071 << "() reason:" << DownloadInterruptReasonToString(reason); 1079 << "() reason:" << DownloadInterruptReasonToString(reason);
1072 1080
1073 // Postpone recognition of this error until after file name determination 1081 // Postpone recognition of this error until after file name determination
1074 // has completed and the intermediate file has been renamed to simplify 1082 // has completed and the intermediate file has been renamed to simplify
1075 // resumption conditions. 1083 // resumption conditions.
1076 if (state_ != IN_PROGRESS_INTERNAL) 1084 if (state_ != IN_PROGRESS_INTERNAL) {
1077 destination_error_ = reason; 1085 destination_error_ = reason;
1078 else 1086 return;
1079 Interrupt(reason); 1087 }
1088 Interrupt(reason);
1089 UpdateObservers();
1080 } 1090 }
1081 1091
1082 void DownloadItemImpl::DestinationCompleted(const std::string& final_hash) { 1092 void DownloadItemImpl::DestinationCompleted(const std::string& final_hash) {
1083 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); 1093 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true);
1084 OnAllDataSaved(final_hash); 1094 OnAllDataSaved(final_hash);
1085 MaybeCompleteDownload(); 1095 MaybeCompleteDownload();
1086 } 1096 }
1087 1097
1088 // **** Download progression cascade 1098 // **** Download progression cascade
1089 1099
(...skipping 28 matching lines...) Expand all
1118 bound_net_log_.AddEvent( 1128 bound_net_log_.AddEvent(
1119 net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, active_data); 1129 net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, active_data);
1120 } 1130 }
1121 1131
1122 DVLOG(20) << __FUNCTION__ << "() " << DebugString(true); 1132 DVLOG(20) << __FUNCTION__ << "() " << DebugString(true);
1123 } 1133 }
1124 1134
1125 // We're starting the download. 1135 // We're starting the download.
1126 void DownloadItemImpl::Start( 1136 void DownloadItemImpl::Start(
1127 scoped_ptr<DownloadFile> file, 1137 scoped_ptr<DownloadFile> file,
1128 scoped_ptr<DownloadRequestHandleInterface> req_handle) { 1138 scoped_ptr<DownloadRequestHandleInterface> req_handle,
1139 const DownloadCreateInfo& new_create_info) {
1129 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1140 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1130 DCHECK(!download_file_.get()); 1141 DCHECK(!download_file_.get());
1131 DCHECK(file.get());
1132 DCHECK(req_handle.get());
1133 DVLOG(20) << __FUNCTION__ << "() this=" << DebugString(true); 1142 DVLOG(20) << __FUNCTION__ << "() this=" << DebugString(true);
1134 1143
1135 download_file_ = std::move(file); 1144 download_file_ = std::move(file);
1136 request_handle_ = std::move(req_handle); 1145 request_handle_ = std::move(req_handle);
1137 1146
1138 if (state_ == CANCELLED_INTERNAL) { 1147 if (state_ == CANCELLED_INTERNAL) {
1139 // The download was in the process of resuming when it was cancelled. Don't 1148 // The download was in the process of resuming when it was cancelled. Don't
1140 // proceed. 1149 // proceed.
1141 ReleaseDownloadFile(true); 1150 ReleaseDownloadFile(true);
1142 request_handle_->CancelRequest(); 1151 if (request_handle_)
1152 request_handle_->CancelRequest();
1143 return; 1153 return;
1144 } 1154 }
1145 1155
1146 TransitionTo(TARGET_PENDING_INTERNAL, UPDATE_OBSERVERS); 1156 // The state could be one of the following:
1157 //
1158 // INITIAL_INTERNAL: A normal download attempt.
1159 //
1160 // RESUMING_INTERNAL: A resumption attempt. May or may not have been
1161 // successful.
1162 DCHECK(state_ == INITIAL_INTERNAL || state_ == RESUMING_INTERNAL);
1163
1164 // If a resumption attempted failed, or if the download was DOA, then the
1165 // download should go back to being interrupted.
1166 if (new_create_info.result != DOWNLOAD_INTERRUPT_REASON_NONE) {
1167 DCHECK(!download_file_.get());
1168
1169 // For downloads that are interrupted on initiation, invoke target
1170 // determination so that we'll end up with a usable filename.
1171 if (state_ == INITIAL_INTERNAL) {
1172 TransitionTo(INTERRUPTED_TARGET_PENDING_INTERNAL);
1173 destination_error_ = new_create_info.result;
1174 DetermineDownloadTarget();
1175 return;
1176 }
1177
1178 // Otherwise, this was a resumption attempt which ended with an
1179 // interruption.
1180 Interrupt(new_create_info.result);
1181 UpdateObservers();
1182 return;
1183 }
1184
1185 // Successful download start.
1186 DCHECK(download_file_.get());
1187 DCHECK(request_handle_.get());
1188
1189 if (state_ == RESUMING_INTERNAL)
1190 UpdateValidatorsOnResumption(new_create_info);
1191
1192 TransitionTo(TARGET_PENDING_INTERNAL);
1193 UpdateObservers();
1147 1194
1148 BrowserThread::PostTask( 1195 BrowserThread::PostTask(
1149 BrowserThread::FILE, FROM_HERE, 1196 BrowserThread::FILE, FROM_HERE,
1150 base::Bind(&DownloadFile::Initialize, 1197 base::Bind(&DownloadFile::Initialize,
1151 // Safe because we control download file lifetime. 1198 // Safe because we control download file lifetime.
1152 base::Unretained(download_file_.get()), 1199 base::Unretained(download_file_.get()),
1153 base::Bind(&DownloadItemImpl::OnDownloadFileInitialized, 1200 base::Bind(&DownloadItemImpl::OnDownloadFileInitialized,
1154 weak_ptr_factory_.GetWeakPtr()))); 1201 weak_ptr_factory_.GetWeakPtr())));
1155 } 1202 }
1156 1203
1157 void DownloadItemImpl::OnDownloadFileInitialized( 1204 void DownloadItemImpl::OnDownloadFileInitialized(
1158 DownloadInterruptReason result) { 1205 DownloadInterruptReason result) {
1159 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1206 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1160 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL); 1207 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL);
1161 DVLOG(20) << __FUNCTION__ 1208 DVLOG(20) << __FUNCTION__
1162 << "() result:" << DownloadInterruptReasonToString(result); 1209 << "() result:" << DownloadInterruptReasonToString(result);
1163 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) { 1210 if (result != DOWNLOAD_INTERRUPT_REASON_NONE) {
1164 // Transition out to TARGET_RESOLVED_INTERNAL since this DownloadItem is 1211 // Whoops. That didn't work. Proceed as an interrupted download. Update the
1165 // skipping the download target determination process. 1212 // observers since this is an externally visible transition.
1166 TransitionTo(TARGET_RESOLVED_INTERNAL, DONT_UPDATE_OBSERVERS); 1213 destination_error_ = result;
1167 Interrupt(result); 1214 TransitionTo(INTERRUPTED_TARGET_PENDING_INTERNAL);
1168 // TODO(rdsmith/asanka): Arguably we should show this in the UI, but 1215 UpdateObservers();
1169 // it's not at all clear what to show--we haven't done filename
1170 // determination, so we don't know what name to display. OTOH,
1171 // the failure mode of not showing the DI if the file initialization
1172 // fails isn't a good one. Can we hack up a name based on the
1173 // URLRequest? We'll need to make sure that initialization happens
1174 // properly. Possibly the right thing is to have the UI handle
1175 // this case specially.
1176 return;
1177 } 1216 }
1178 1217
1218 DetermineDownloadTarget();
1219 }
1220
1221 void DownloadItemImpl::DetermineDownloadTarget() {
1222 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1223 DVLOG(20) << __FUNCTION__ << "() " << DebugString(true);
1224
1179 delegate_->DetermineDownloadTarget( 1225 delegate_->DetermineDownloadTarget(
1180 this, base::Bind(&DownloadItemImpl::OnDownloadTargetDetermined, 1226 this, base::Bind(&DownloadItemImpl::OnDownloadTargetDetermined,
1181 weak_ptr_factory_.GetWeakPtr())); 1227 weak_ptr_factory_.GetWeakPtr()));
1182 } 1228 }
1183 1229
1184 // Called by delegate_ when the download target path has been 1230 // Called by delegate_ when the download target path has been determined.
1185 // determined.
1186 void DownloadItemImpl::OnDownloadTargetDetermined( 1231 void DownloadItemImpl::OnDownloadTargetDetermined(
1187 const base::FilePath& target_path, 1232 const base::FilePath& target_path,
1188 TargetDisposition disposition, 1233 TargetDisposition disposition,
1189 DownloadDangerType danger_type, 1234 DownloadDangerType danger_type,
1190 const base::FilePath& intermediate_path) { 1235 const base::FilePath& intermediate_path) {
1191 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1236 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1192 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL); 1237 DCHECK(state_ == TARGET_PENDING_INTERNAL ||
1238 state_ == INTERRUPTED_TARGET_PENDING_INTERNAL);
1193 1239
1194 // If the |target_path| is empty, then we consider this download to be 1240 // If the |target_path| is empty, then we consider this download to be
1195 // canceled. 1241 // canceled.
1196 if (target_path.empty()) { 1242 if (target_path.empty()) {
1197 Cancel(true); 1243 Cancel(true);
1198 return; 1244 return;
1199 } 1245 }
1200 1246
1201 // TODO(rdsmith,asanka): We are ignoring the possibility that the download
1202 // has been interrupted at this point until we finish the intermediate
1203 // rename and set the full path. That's dangerous, because we might race
1204 // with resumption, either manual (because the interrupt is visible to the
1205 // UI) or automatic. If we keep the "ignore an error on download until file
1206 // name determination complete" semantics, we need to make sure that the
1207 // error is kept completely invisible until that point.
1208
1209 DVLOG(20) << __FUNCTION__ << " " << target_path.value() << " " << disposition 1247 DVLOG(20) << __FUNCTION__ << " " << target_path.value() << " " << disposition
1210 << " " << danger_type << " " << DebugString(true); 1248 << " " << danger_type << " " << DebugString(true);
1211 1249
1212 target_path_ = target_path; 1250 target_path_ = target_path;
1213 target_disposition_ = disposition; 1251 target_disposition_ = disposition;
1214 SetDangerType(danger_type); 1252 SetDangerType(danger_type);
1215 1253
1254 // This was an interrupted download that was looking for a filename. Now that
1255 // it has one, transition to interrupted.
1256 if (state_ == INTERRUPTED_TARGET_PENDING_INTERNAL) {
1257 Interrupt(destination_error_);
1258 UpdateObservers();
1259 return;
1260 }
1261
1216 // We want the intermediate and target paths to refer to the same directory so 1262 // We want the intermediate and target paths to refer to the same directory so
1217 // that they are both on the same device and subject to same 1263 // that they are both on the same device and subject to same
1218 // space/permission/availability constraints. 1264 // space/permission/availability constraints.
1219 DCHECK(intermediate_path.DirName() == target_path.DirName()); 1265 DCHECK(intermediate_path.DirName() == target_path.DirName());
1220 1266
1221 // During resumption, we may choose to proceed with the same intermediate 1267 // During resumption, we may choose to proceed with the same intermediate
1222 // file. No rename is necessary if our intermediate file already has the 1268 // file. No rename is necessary if our intermediate file already has the
1223 // correct name. 1269 // correct name.
1224 // 1270 //
1225 // The intermediate name may change from its original value during filename 1271 // The intermediate name may change from its original value during filename
(...skipping 22 matching lines...) Expand all
1248 base::Unretained(download_file_.get()), 1294 base::Unretained(download_file_.get()),
1249 intermediate_path, callback)); 1295 intermediate_path, callback));
1250 } 1296 }
1251 1297
1252 void DownloadItemImpl::OnDownloadRenamedToIntermediateName( 1298 void DownloadItemImpl::OnDownloadRenamedToIntermediateName(
1253 DownloadInterruptReason reason, 1299 DownloadInterruptReason reason,
1254 const base::FilePath& full_path) { 1300 const base::FilePath& full_path) {
1255 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1301 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1256 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL); 1302 DCHECK_EQ(state_, TARGET_PENDING_INTERNAL);
1257 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true); 1303 DVLOG(20) << __FUNCTION__ << " download=" << DebugString(true);
1258 TransitionTo(TARGET_RESOLVED_INTERNAL, DONT_UPDATE_OBSERVERS); 1304 TransitionTo(TARGET_RESOLVED_INTERNAL);
Randy Smith (Not in Mondays) 2016/02/11 22:08:08 It looks to me as if this state isn't needed; the
asanka 2016/02/11 23:57:25 I've tried to justify the existence of this state
1259 1305
1260 if (DOWNLOAD_INTERRUPT_REASON_NONE != destination_error_) { 1306 if (DOWNLOAD_INTERRUPT_REASON_NONE != destination_error_) {
1261 // Process destination error. If both |reason| and |destination_error_| 1307 // Process destination error. If both |reason| and |destination_error_|
1262 // refer to actual errors, we want to use the |destination_error_| as the 1308 // refer to actual errors, we want to use the |destination_error_| as the
1263 // argument to the Interrupt() routine, as it happened first. 1309 // argument to the Interrupt() routine, as it happened first.
1264 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) 1310 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE)
1265 SetFullPath(full_path); 1311 SetFullPath(full_path);
1266 Interrupt(destination_error_); 1312 Interrupt(destination_error_);
1267 destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE; 1313 destination_error_ = DOWNLOAD_INTERRUPT_REASON_NONE;
1268 } else if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) { 1314 } else if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) {
1269 Interrupt(reason); 1315 Interrupt(reason);
1270 // All file errors result in file deletion above; no need to cleanup. The 1316 // All file errors result in file deletion above; no need to cleanup. The
1271 // current_path_ should be empty. Resuming this download will force a 1317 // current_path_ should be empty. Resuming this download will force a
1272 // restart and a re-doing of filename determination. 1318 // restart and a re-doing of filename determination.
1273 DCHECK(current_path_.empty()); 1319 DCHECK(current_path_.empty());
1274 } else { 1320 } else {
1275 SetFullPath(full_path); 1321 SetFullPath(full_path);
1276 TransitionTo(IN_PROGRESS_INTERNAL, UPDATE_OBSERVERS); 1322 TransitionTo(IN_PROGRESS_INTERNAL);
1277 MaybeCompleteDownload(); 1323 MaybeCompleteDownload();
1278 } 1324 }
1325 UpdateObservers();
1279 } 1326 }
1280 1327
1281 // When SavePackage downloads MHTML to GData (see 1328 // When SavePackage downloads MHTML to GData (see
1282 // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it 1329 // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it
1283 // does for non-SavePackage downloads, but SavePackage downloads never satisfy 1330 // does for non-SavePackage downloads, but SavePackage downloads never satisfy
1284 // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls 1331 // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls
1285 // DownloadItem::UpdateObservers() when the upload completes so that SavePackage 1332 // DownloadItem::UpdateObservers() when the upload completes so that SavePackage
1286 // notices that the upload has completed and runs its normal Finish() pathway. 1333 // notices that the upload has completed and runs its normal Finish() pathway.
1287 // MaybeCompleteDownload() is never the mechanism by which SavePackage completes 1334 // MaybeCompleteDownload() is never the mechanism by which SavePackage completes
1288 // downloads. SavePackage always uses its own Finish() to mark downloads 1335 // downloads. SavePackage always uses its own Finish() to mark downloads
(...skipping 25 matching lines...) Expand all
1314 return; 1361 return;
1315 1362
1316 DVLOG(20) << __FUNCTION__ << "()" 1363 DVLOG(20) << __FUNCTION__ << "()"
1317 << " " << DebugString(true); 1364 << " " << DebugString(true);
1318 DCHECK(!GetTargetFilePath().empty()); 1365 DCHECK(!GetTargetFilePath().empty());
1319 DCHECK(!IsDangerous()); 1366 DCHECK(!IsDangerous());
1320 1367
1321 // TODO(rdsmith/benjhayden): Remove as part of SavePackage integration. 1368 // TODO(rdsmith/benjhayden): Remove as part of SavePackage integration.
1322 if (is_save_package_download_) { 1369 if (is_save_package_download_) {
1323 // Avoid doing anything on the file thread; there's nothing we control 1370 // Avoid doing anything on the file thread; there's nothing we control
1324 // there. 1371 // there. Strictly speaking, this skips giving the embedder a chance to
1325 // Strictly speaking, this skips giving the embedder a chance to open 1372 // open the download. But on a save package download, there's no real
1326 // the download. But on a save package download, there's no real
1327 // concept of opening. 1373 // concept of opening.
1328 Completed(); 1374 Completed();
1329 return; 1375 return;
1330 } 1376 }
1331 1377
1332 DCHECK(download_file_.get()); 1378 DCHECK(download_file_.get());
1333 // Unilaterally rename; even if it already has the right name, 1379 // Unilaterally rename; even if it already has the right name,
1334 // we need theannotation. 1380 // we need theannotation.
1335 DownloadFile::RenameCompletionCallback callback = 1381 DownloadFile::RenameCompletionCallback callback =
1336 base::Bind(&DownloadItemImpl::OnDownloadRenamedToFinalName, 1382 base::Bind(&DownloadItemImpl::OnDownloadRenamedToFinalName,
(...skipping 20 matching lines...) Expand all
1357 DVLOG(20) << __FUNCTION__ << "()" 1403 DVLOG(20) << __FUNCTION__ << "()"
1358 << " full_path = \"" << full_path.value() << "\"" 1404 << " full_path = \"" << full_path.value() << "\""
1359 << " " << DebugString(false); 1405 << " " << DebugString(false);
1360 1406
1361 if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) { 1407 if (DOWNLOAD_INTERRUPT_REASON_NONE != reason) {
1362 Interrupt(reason); 1408 Interrupt(reason);
1363 1409
1364 // All file errors should have resulted in in file deletion above. On 1410 // All file errors should have resulted in in file deletion above. On
1365 // resumption we will need to re-do filename determination. 1411 // resumption we will need to re-do filename determination.
1366 DCHECK(current_path_.empty()); 1412 DCHECK(current_path_.empty());
1413 UpdateObservers();
Randy Smith (Not in Mondays) 2016/02/11 22:08:08 nit, suggestion: I'd rather have these right next
asanka 2016/02/11 23:57:25 Iv'e tried to move the UpdateObservers() calls as
Randy Smith (Not in Mondays) 2016/02/12 18:01:19 SGTM.
1367 return; 1414 return;
1368 } 1415 }
1369 1416
1370 DCHECK(target_path_ == full_path); 1417 DCHECK(target_path_ == full_path);
1371 1418
1372 if (full_path != current_path_) { 1419 if (full_path != current_path_) {
1373 // full_path is now the current and target file path. 1420 // full_path is now the current and target file path.
1374 DCHECK(!full_path.empty()); 1421 DCHECK(!full_path.empty());
1375 SetFullPath(full_path); 1422 SetFullPath(full_path);
1376 } 1423 }
1377 1424
1378 // Complete the download and release the DownloadFile. 1425 // Complete the download and release the DownloadFile.
1379 DCHECK(download_file_); 1426 DCHECK(download_file_);
1380 ReleaseDownloadFile(false); 1427 ReleaseDownloadFile(false);
1381 1428
1382 // We're not completely done with the download item yet, but at this 1429 // We're not completely done with the download item yet, but at this
1383 // point we're committed to complete the download. Cancels (or Interrupts, 1430 // point we're committed to complete the download. Cancels (or Interrupts,
1384 // though it's not clear how they could happen) after this point will be 1431 // though it's not clear how they could happen) after this point will be
1385 // ignored. 1432 // ignored.
1386 TransitionTo(COMPLETING_INTERNAL, DONT_UPDATE_OBSERVERS); 1433 TransitionTo(COMPLETING_INTERNAL);
1387 1434
1388 if (delegate_->ShouldOpenDownload( 1435 if (delegate_->ShouldOpenDownload(
1389 this, base::Bind(&DownloadItemImpl::DelayedDownloadOpened, 1436 this, base::Bind(&DownloadItemImpl::DelayedDownloadOpened,
1390 weak_ptr_factory_.GetWeakPtr()))) { 1437 weak_ptr_factory_.GetWeakPtr()))) {
1391 Completed(); 1438 Completed();
1392 } else { 1439 } else {
1393 delegate_delayed_complete_ = true; 1440 delegate_delayed_complete_ = true;
1394 UpdateObservers(); 1441 UpdateObservers();
1395 } 1442 }
1396 } 1443 }
1397 1444
1398 void DownloadItemImpl::DelayedDownloadOpened(bool auto_opened) { 1445 void DownloadItemImpl::DelayedDownloadOpened(bool auto_opened) {
1399 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1446 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1400 1447
1401 auto_opened_ = auto_opened; 1448 auto_opened_ = auto_opened;
1402 Completed(); 1449 Completed();
1403 } 1450 }
1404 1451
1405 void DownloadItemImpl::Completed() { 1452 void DownloadItemImpl::Completed() {
1406 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1453 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1407 1454
1408 DVLOG(20) << __FUNCTION__ << "() " << DebugString(false); 1455 DVLOG(20) << __FUNCTION__ << "() " << DebugString(false);
1409 1456
1410 DCHECK(all_data_saved_); 1457 DCHECK(all_data_saved_);
1411 end_time_ = base::Time::Now(); 1458 end_time_ = base::Time::Now();
1412 TransitionTo(COMPLETE_INTERNAL, UPDATE_OBSERVERS); 1459 TransitionTo(COMPLETE_INTERNAL);
1460 UpdateObservers();
1413 RecordDownloadCompleted(start_tick_, received_bytes_); 1461 RecordDownloadCompleted(start_tick_, received_bytes_);
1414 1462
1415 if (auto_opened_) { 1463 if (auto_opened_) {
1416 // If it was already handled by the delegate, do nothing. 1464 // If it was already handled by the delegate, do nothing.
1417 } else if (GetOpenWhenComplete() || 1465 } else if (GetOpenWhenComplete() ||
1418 ShouldOpenFileBasedOnExtension() || 1466 ShouldOpenFileBasedOnExtension() ||
1419 IsTemporary()) { 1467 IsTemporary()) {
1420 // If the download is temporary, like in drag-and-drop, do not open it but 1468 // If the download is temporary, like in drag-and-drop, do not open it but
1421 // we still need to set it auto-opened so that it can be removed from the 1469 // we still need to set it auto-opened so that it can be removed from the
1422 // download shelf. 1470 // download shelf.
1423 if (!IsTemporary()) 1471 if (!IsTemporary())
1424 OpenDownload(); 1472 OpenDownload();
1425 1473
1426 auto_opened_ = true; 1474 auto_opened_ = true;
1427 UpdateObservers(); 1475 UpdateObservers();
1428 } 1476 }
1429 } 1477 }
1430 1478
1431 void DownloadItemImpl::OnResumeRequestStarted(
1432 DownloadItem* item,
1433 DownloadInterruptReason interrupt_reason) {
1434 // If |item| is not NULL, then Start() has been called already, and nothing
1435 // more needs to be done here.
1436 if (item) {
1437 DCHECK_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason);
1438 DCHECK_EQ(static_cast<DownloadItem*>(this), item);
1439 return;
1440 }
1441 // Otherwise, the request failed without passing through
1442 // DownloadResourceHandler::OnResponseStarted.
1443 DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason);
1444 Interrupt(interrupt_reason);
1445 }
1446
1447 // **** End of Download progression cascade 1479 // **** End of Download progression cascade
1448 1480
1449 // An error occurred somewhere. 1481 // An error occurred somewhere.
1450 void DownloadItemImpl::Interrupt(DownloadInterruptReason reason) { 1482 void DownloadItemImpl::Interrupt(DownloadInterruptReason reason) {
1451 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1483 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1452 DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, reason); 1484 DCHECK_NE(DOWNLOAD_INTERRUPT_REASON_NONE, reason);
1453 DVLOG(20) << __FUNCTION__ 1485 DVLOG(20) << __FUNCTION__
1454 << "() reason:" << DownloadInterruptReasonToString(reason) 1486 << "() reason:" << DownloadInterruptReasonToString(reason)
1455 << " this=" << DebugString(true); 1487 << " this=" << DebugString(true);
1456 1488
(...skipping 13 matching lines...) Expand all
1470 case COMPLETE_INTERNAL: 1502 case COMPLETE_INTERNAL:
1471 // Already complete. 1503 // Already complete.
1472 return; 1504 return;
1473 1505
1474 case TARGET_PENDING_INTERNAL: 1506 case TARGET_PENDING_INTERNAL:
1475 case TARGET_RESOLVED_INTERNAL: 1507 case TARGET_RESOLVED_INTERNAL:
1476 case IN_PROGRESS_INTERNAL: 1508 case IN_PROGRESS_INTERNAL:
1477 break; 1509 break;
1478 1510
1479 case RESUMING_INTERNAL: 1511 case RESUMING_INTERNAL:
1512 case INTERRUPTED_TARGET_PENDING_INTERNAL:
1480 case INTERRUPTED_INTERNAL: 1513 case INTERRUPTED_INTERNAL:
1481 // The first non-cancel interrupt reason wins in cases where multiple 1514 // The first non-cancel interrupt reason wins in cases where multiple
1482 // things go wrong. 1515 // things go wrong.
1483 if (reason != DOWNLOAD_INTERRUPT_REASON_USER_CANCELED && 1516 if (reason != DOWNLOAD_INTERRUPT_REASON_USER_CANCELED &&
1484 reason != DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN) 1517 reason != DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN)
1485 return; 1518 return;
1486 1519
1487 if (!current_path_.empty()) { 1520 if (!current_path_.empty()) {
1488 BrowserThread::PostTask( 1521 BrowserThread::PostTask(
1489 BrowserThread::FILE, FROM_HERE, 1522 BrowserThread::FILE, FROM_HERE,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1524 reason == DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN) { 1557 reason == DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN) {
1525 if (IsDangerous()) { 1558 if (IsDangerous()) {
1526 RecordDangerousDownloadDiscard( 1559 RecordDangerousDownloadDiscard(
1527 reason == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED 1560 reason == DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
1528 ? DOWNLOAD_DISCARD_DUE_TO_USER_ACTION 1561 ? DOWNLOAD_DISCARD_DUE_TO_USER_ACTION
1529 : DOWNLOAD_DISCARD_DUE_TO_SHUTDOWN, 1562 : DOWNLOAD_DISCARD_DUE_TO_SHUTDOWN,
1530 GetDangerType(), GetTargetFilePath()); 1563 GetDangerType(), GetTargetFilePath());
1531 } 1564 }
1532 1565
1533 RecordDownloadCount(CANCELLED_COUNT); 1566 RecordDownloadCount(CANCELLED_COUNT);
1534 TransitionTo(CANCELLED_INTERNAL, DONT_UPDATE_OBSERVERS); 1567 TransitionTo(CANCELLED_INTERNAL);
1535 } else { 1568 return;
1536 RecordDownloadInterrupted(reason, received_bytes_, total_bytes_);
1537 if (!GetWebContents())
1538 RecordDownloadCount(INTERRUPTED_WITHOUT_WEBCONTENTS);
1539 TransitionTo(INTERRUPTED_INTERNAL, DONT_UPDATE_OBSERVERS);
1540 AutoResumeIfValid();
1541 } 1569 }
1542 1570
1543 UpdateObservers(); 1571 RecordDownloadInterrupted(reason, received_bytes_, total_bytes_);
1572 if (!GetWebContents())
1573 RecordDownloadCount(INTERRUPTED_WITHOUT_WEBCONTENTS);
1574
1575 TransitionTo(INTERRUPTED_INTERNAL);
1576 AutoResumeIfValid();
1544 } 1577 }
1545 1578
1546 void DownloadItemImpl::ReleaseDownloadFile(bool destroy_file) { 1579 void DownloadItemImpl::ReleaseDownloadFile(bool destroy_file) {
1547 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1580 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1548 DVLOG(20) << __FUNCTION__ << "() destroy_file:" << destroy_file; 1581 DVLOG(20) << __FUNCTION__ << "() destroy_file:" << destroy_file;
1549 1582
1550 if (destroy_file) { 1583 if (destroy_file) {
1551 BrowserThread::PostTask( 1584 BrowserThread::PostTask(
1552 BrowserThread::FILE, FROM_HERE, 1585 BrowserThread::FILE, FROM_HERE,
1553 // Will be deleted at end of task execution. 1586 // Will be deleted at end of task execution.
(...skipping 25 matching lines...) Expand all
1579 // If we don't have all the data, the download is not ready for 1612 // If we don't have all the data, the download is not ready for
1580 // completion. 1613 // completion.
1581 if (!AllDataSaved()) 1614 if (!AllDataSaved())
1582 return false; 1615 return false;
1583 1616
1584 // If the download is dangerous, but not yet validated, it's not ready for 1617 // If the download is dangerous, but not yet validated, it's not ready for
1585 // completion. 1618 // completion.
1586 if (IsDangerous()) 1619 if (IsDangerous())
1587 return false; 1620 return false;
1588 1621
1589 // Invariants for the IN_PROGRESS state. DCHECKs here verify that the 1622 // Check for consistency before invoking delegate. Since there are no pending
1590 // invariants are still true. 1623 // target determination calls and the download is in progress, both the target
1624 // and current paths should be non-empty and they should point to the same
1625 // directory.
1591 DCHECK(!target_path_.empty()); 1626 DCHECK(!target_path_.empty());
1592 DCHECK(!current_path_.empty()); 1627 DCHECK(!current_path_.empty());
1593 DCHECK(target_path_.DirName() == current_path_.DirName()); 1628 DCHECK(target_path_.DirName() == current_path_.DirName());
1594 1629
1595 // Give the delegate a chance to hold up a stop sign. It'll call 1630 // Give the delegate a chance to hold up a stop sign. It'll call
1596 // use back through the passed callback if it does and that state changes. 1631 // use back through the passed callback if it does and that state changes.
1597 if (!delegate_->ShouldCompleteDownload(this, state_change_notification)) 1632 if (!delegate_->ShouldCompleteDownload(this, state_change_notification))
1598 return false; 1633 return false;
1599 1634
1600 return true; 1635 return true;
1601 } 1636 }
1602 1637
1603 void DownloadItemImpl::TransitionTo(DownloadInternalState new_state, 1638 void DownloadItemImpl::TransitionTo(DownloadInternalState new_state) {
1604 ShouldUpdateObservers notify_action) {
1605 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1639 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1606 1640
1607 if (state_ == new_state) 1641 if (state_ == new_state)
1608 return; 1642 return;
1609 1643
1610 DownloadInternalState old_state = state_; 1644 DownloadInternalState old_state = state_;
1611 state_ = new_state; 1645 state_ = new_state;
1612 1646
1613 DCHECK(is_save_package_download_ 1647 DCHECK(is_save_package_download_
1614 ? IsValidSavePackageStateTransition(old_state, new_state) 1648 ? IsValidSavePackageStateTransition(old_state, new_state)
1615 : IsValidStateTransition(old_state, new_state)) 1649 : IsValidStateTransition(old_state, new_state))
1616 << "Invalid state transition from:" << DebugDownloadStateString(old_state) 1650 << "Invalid state transition from:" << DebugDownloadStateString(old_state)
1617 << " to:" << DebugDownloadStateString(new_state); 1651 << " to:" << DebugDownloadStateString(new_state);
1618 1652
1619 switch (state_) { 1653 switch (state_) {
1620 case INITIAL_INTERNAL: 1654 case INITIAL_INTERNAL:
1621 NOTREACHED(); 1655 NOTREACHED();
1622 break; 1656 break;
1623 1657
1624 case TARGET_PENDING_INTERNAL: 1658 case TARGET_PENDING_INTERNAL:
1625 case TARGET_RESOLVED_INTERNAL: 1659 case TARGET_RESOLVED_INTERNAL:
1660 case INTERRUPTED_TARGET_PENDING_INTERNAL:
1626 break; 1661 break;
1627 1662
1628 case IN_PROGRESS_INTERNAL: 1663 case IN_PROGRESS_INTERNAL:
1629 DCHECK(!current_path_.empty()) << "Current output path must be known."; 1664 DCHECK(!current_path_.empty()) << "Current output path must be known.";
1630 DCHECK(!target_path_.empty()) << "Target path must be known."; 1665 DCHECK(!target_path_.empty()) << "Target path must be known.";
1631 DCHECK(current_path_.DirName() == target_path_.DirName()) 1666 DCHECK(current_path_.DirName() == target_path_.DirName())
1632 << "Current output directory must match target directory."; 1667 << "Current output directory must match target directory.";
1633 DCHECK(download_file_) << "Output file must be owned by download item."; 1668 DCHECK(download_file_) << "Output file must be owned by download item.";
1634 DCHECK(request_handle_) << "Download source must be active."; 1669 DCHECK(request_handle_) << "Download source must be active.";
1635 DCHECK(!is_paused_) << "At the time a download enters IN_PROGRESS state, " 1670 DCHECK(!is_paused_) << "At the time a download enters IN_PROGRESS state, "
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
1698 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE); 1733 bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE);
1699 1734
1700 // Resumption 1735 // Resumption
1701 if (was_done && !is_done) { 1736 if (was_done && !is_done) {
1702 std::string file_name(target_path_.BaseName().AsUTF8Unsafe()); 1737 std::string file_name(target_path_.BaseName().AsUTF8Unsafe());
1703 bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE, 1738 bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_ITEM_ACTIVE,
1704 base::Bind(&ItemActivatedNetLogCallback, 1739 base::Bind(&ItemActivatedNetLogCallback,
1705 this, SRC_ACTIVE_DOWNLOAD, 1740 this, SRC_ACTIVE_DOWNLOAD,
1706 &file_name)); 1741 &file_name));
1707 } 1742 }
1708
1709 if (notify_action == UPDATE_OBSERVERS)
1710 UpdateObservers();
1711 } 1743 }
1712 1744
1713 void DownloadItemImpl::SetDangerType(DownloadDangerType danger_type) { 1745 void DownloadItemImpl::SetDangerType(DownloadDangerType danger_type) {
1714 if (danger_type != danger_type_) { 1746 if (danger_type != danger_type_) {
1715 bound_net_log_.AddEvent( 1747 bound_net_log_.AddEvent(
1716 net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED, 1748 net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED,
1717 base::Bind(&ItemCheckedNetLogCallback, danger_type)); 1749 base::Bind(&ItemCheckedNetLogCallback, danger_type));
1718 } 1750 }
1719 // Only record the Malicious UMA stat if it's going from {not malicious} -> 1751 // Only record the Malicious UMA stat if it's going from {not malicious} ->
1720 // {malicious}. 1752 // {malicious}.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1762 1794
1763 void DownloadItemImpl::ResumeInterruptedDownload() { 1795 void DownloadItemImpl::ResumeInterruptedDownload() {
1764 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1796 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1765 if (!IsDownloadResumptionEnabled()) 1797 if (!IsDownloadResumptionEnabled())
1766 return; 1798 return;
1767 1799
1768 // If we're not interrupted, ignore the request; our caller is drunk. 1800 // If we're not interrupted, ignore the request; our caller is drunk.
1769 if (state_ != INTERRUPTED_INTERNAL) 1801 if (state_ != INTERRUPTED_INTERNAL)
1770 return; 1802 return;
1771 1803
1804 // We are starting a new request. Shake off all pending operations.
1805 DCHECK(!download_file_);
1806 weak_ptr_factory_.InvalidateWeakPtrs();
Randy Smith (Not in Mondays) 2016/02/11 22:08:09 Why doesn't this solve the race that the earlier C
asanka 2016/02/11 23:57:25 The race was that a DestinationError() or Destinat
Randy Smith (Not in Mondays) 2016/02/12 18:01:19 Right, sorry, still confused. My image was that t
asanka 2016/02/12 18:31:47 It could be the same. The workflow is: - initia
Randy Smith (Not in Mondays) 2016/02/12 18:58:10 Ah, got it. Thanks!
1807
1772 // Reset the appropriate state if restarting. 1808 // Reset the appropriate state if restarting.
1773 ResumeMode mode = GetResumeMode(); 1809 ResumeMode mode = GetResumeMode();
1774 if (mode == RESUME_MODE_IMMEDIATE_RESTART || 1810 if (mode == RESUME_MODE_IMMEDIATE_RESTART ||
1775 mode == RESUME_MODE_USER_RESTART) { 1811 mode == RESUME_MODE_USER_RESTART) {
1776 received_bytes_ = 0; 1812 received_bytes_ = 0;
1777 hash_state_ = ""; 1813 hash_state_ = "";
1778 last_modified_time_ = ""; 1814 last_modified_time_ = "";
1779 etag_ = ""; 1815 etag_ = "";
1780 } 1816 }
1781 1817
1782 scoped_ptr<DownloadUrlParameters> download_params; 1818 scoped_ptr<DownloadUrlParameters> download_params;
1783 if (GetWebContents()) { 1819 if (GetWebContents()) {
1784 download_params = 1820 download_params =
1785 DownloadUrlParameters::FromWebContents(GetWebContents(), GetURL()); 1821 DownloadUrlParameters::FromWebContents(GetWebContents(), GetURL());
1786 } else { 1822 } else {
1787 download_params = make_scoped_ptr(new DownloadUrlParameters( 1823 download_params = make_scoped_ptr(new DownloadUrlParameters(
1788 GetURL(), -1, -1, -1, GetBrowserContext()->GetResourceContext())); 1824 GetURL(), -1, -1, -1, GetBrowserContext()->GetResourceContext()));
1789 } 1825 }
1790 1826
1791 download_params->set_file_path(GetFullPath()); 1827 download_params->set_file_path(GetFullPath());
1792 download_params->set_offset(GetReceivedBytes()); 1828 download_params->set_offset(GetReceivedBytes());
1793 download_params->set_hash_state(GetHashState()); 1829 download_params->set_hash_state(GetHashState());
1794 download_params->set_last_modified(GetLastModifiedTime()); 1830 download_params->set_last_modified(GetLastModifiedTime());
1795 download_params->set_etag(GetETag()); 1831 download_params->set_etag(GetETag());
1796 download_params->set_callback(
1797 base::Bind(&DownloadItemImpl::OnResumeRequestStarted,
1798 weak_ptr_factory_.GetWeakPtr()));
1799 1832
1800 TransitionTo(RESUMING_INTERNAL, DONT_UPDATE_OBSERVERS); 1833 TransitionTo(RESUMING_INTERNAL);
1801 delegate_->ResumeInterruptedDownload(std::move(download_params), GetId()); 1834 delegate_->ResumeInterruptedDownload(std::move(download_params), GetId());
1802 // Just in case we were interrupted while paused. 1835 // Just in case we were interrupted while paused.
1803 is_paused_ = false; 1836 is_paused_ = false;
1804 } 1837 }
1805 1838
1806 // static 1839 // static
1807 DownloadItem::DownloadState DownloadItemImpl::InternalToExternalState( 1840 DownloadItem::DownloadState DownloadItemImpl::InternalToExternalState(
1808 DownloadInternalState internal_state) { 1841 DownloadInternalState internal_state) {
1809 switch (internal_state) { 1842 switch (internal_state) {
1810 case INITIAL_INTERNAL: 1843 case INITIAL_INTERNAL:
1811 case TARGET_PENDING_INTERNAL: 1844 case TARGET_PENDING_INTERNAL:
1812 case TARGET_RESOLVED_INTERNAL: 1845 case TARGET_RESOLVED_INTERNAL:
1813 // TODO(asanka): Introduce an externally visible state to distinguish 1846 // TODO(asanka): Introduce an externally visible state to distinguish
1814 // between the above states and IN_PROGRESS_INTERNAL. The latter (the 1847 // between the above states and IN_PROGRESS_INTERNAL. The latter (the
1815 // state where the download is active and has a known target) is the state 1848 // state where the download is active and has a known target) is the state
1816 // that most external users are interested in. 1849 // that most external users are interested in.
1817 case IN_PROGRESS_INTERNAL: 1850 case IN_PROGRESS_INTERNAL:
1818 return IN_PROGRESS; 1851 return IN_PROGRESS;
1819 case COMPLETING_INTERNAL: 1852 case COMPLETING_INTERNAL:
1820 return IN_PROGRESS; 1853 return IN_PROGRESS;
1821 case COMPLETE_INTERNAL: 1854 case COMPLETE_INTERNAL:
1822 return COMPLETE; 1855 return COMPLETE;
1823 case CANCELLED_INTERNAL: 1856 case CANCELLED_INTERNAL:
1824 return CANCELLED; 1857 return CANCELLED;
1858 case INTERRUPTED_TARGET_PENDING_INTERNAL:
Randy Smith (Not in Mondays) 2016/02/11 22:08:09 Doesn't mapping this to interrupted leave you open
asanka 2016/02/11 23:57:25 Resume() already doesn't work on an interrupted do
asanka 2016/02/12 16:06:49 Retracted.
1825 case INTERRUPTED_INTERNAL: 1859 case INTERRUPTED_INTERNAL:
1826 return INTERRUPTED; 1860 return INTERRUPTED;
1827 case RESUMING_INTERNAL: 1861 case RESUMING_INTERNAL:
1828 return IN_PROGRESS; 1862 return IN_PROGRESS;
1829 case MAX_DOWNLOAD_INTERNAL_STATE: 1863 case MAX_DOWNLOAD_INTERNAL_STATE:
1830 break; 1864 break;
1831 } 1865 }
1832 NOTREACHED(); 1866 NOTREACHED();
1833 return MAX_DOWNLOAD_STATE; 1867 return MAX_DOWNLOAD_STATE;
1834 } 1868 }
(...skipping 18 matching lines...) Expand all
1853 } 1887 }
1854 1888
1855 #if DCHECK_IS_ON() 1889 #if DCHECK_IS_ON()
1856 // static 1890 // static
1857 bool DownloadItemImpl::IsValidSavePackageStateTransition( 1891 bool DownloadItemImpl::IsValidSavePackageStateTransition(
1858 DownloadInternalState from, 1892 DownloadInternalState from,
1859 DownloadInternalState to) { 1893 DownloadInternalState to) {
1860 switch (from) { 1894 switch (from) {
1861 case INITIAL_INTERNAL: 1895 case INITIAL_INTERNAL:
1862 case TARGET_PENDING_INTERNAL: 1896 case TARGET_PENDING_INTERNAL:
1897 case INTERRUPTED_TARGET_PENDING_INTERNAL:
1863 case TARGET_RESOLVED_INTERNAL: 1898 case TARGET_RESOLVED_INTERNAL:
1864 case COMPLETING_INTERNAL: 1899 case COMPLETING_INTERNAL:
1865 case COMPLETE_INTERNAL: 1900 case COMPLETE_INTERNAL:
1866 case INTERRUPTED_INTERNAL: 1901 case INTERRUPTED_INTERNAL:
1867 case RESUMING_INTERNAL: 1902 case RESUMING_INTERNAL:
1868 case CANCELLED_INTERNAL: 1903 case CANCELLED_INTERNAL:
1869 return false; 1904 return false;
1870 1905
1871 case IN_PROGRESS_INTERNAL: 1906 case IN_PROGRESS_INTERNAL:
1872 return to == CANCELLED_INTERNAL || COMPLETE_INTERNAL; 1907 return to == CANCELLED_INTERNAL || COMPLETE_INTERNAL;
1873 1908
1874 case MAX_DOWNLOAD_INTERNAL_STATE: 1909 case MAX_DOWNLOAD_INTERNAL_STATE:
1875 NOTREACHED(); 1910 NOTREACHED();
1876 } 1911 }
1877 return false; 1912 return false;
1878 } 1913 }
1879 1914
1880 // static 1915 // static
1881 bool DownloadItemImpl::IsValidStateTransition(DownloadInternalState from, 1916 bool DownloadItemImpl::IsValidStateTransition(DownloadInternalState from,
1882 DownloadInternalState to) { 1917 DownloadInternalState to) {
1883 switch (from) { 1918 switch (from) {
1884 case INITIAL_INTERNAL: 1919 case INITIAL_INTERNAL:
1885 return to == TARGET_PENDING_INTERNAL || to == INTERRUPTED_INTERNAL; 1920 return to == TARGET_PENDING_INTERNAL ||
1921 to == INTERRUPTED_TARGET_PENDING_INTERNAL;
1886 1922
1887 case TARGET_PENDING_INTERNAL: 1923 case TARGET_PENDING_INTERNAL:
1888 return to == TARGET_RESOLVED_INTERNAL || to == CANCELLED_INTERNAL; 1924 return to == INTERRUPTED_TARGET_PENDING_INTERNAL ||
1925 to == TARGET_RESOLVED_INTERNAL || to == CANCELLED_INTERNAL;
1926
1927 case INTERRUPTED_TARGET_PENDING_INTERNAL:
1928 return to == INTERRUPTED_INTERNAL || to == CANCELLED_INTERNAL;
1889 1929
1890 case TARGET_RESOLVED_INTERNAL: 1930 case TARGET_RESOLVED_INTERNAL:
1891 return to == IN_PROGRESS_INTERNAL || to == INTERRUPTED_INTERNAL || 1931 return to == IN_PROGRESS_INTERNAL || to == INTERRUPTED_INTERNAL ||
1892 to == CANCELLED_INTERNAL; 1932 to == CANCELLED_INTERNAL;
1893 1933
1894 case IN_PROGRESS_INTERNAL: 1934 case IN_PROGRESS_INTERNAL:
1895 return to == COMPLETING_INTERNAL || to == CANCELLED_INTERNAL || 1935 return to == COMPLETING_INTERNAL || to == CANCELLED_INTERNAL ||
1896 to == INTERRUPTED_INTERNAL; 1936 to == INTERRUPTED_INTERNAL;
1897 1937
1898 case COMPLETING_INTERNAL: 1938 case COMPLETING_INTERNAL:
1899 return to == COMPLETE_INTERNAL; 1939 return to == COMPLETE_INTERNAL;
1900 1940
1901 case COMPLETE_INTERNAL: 1941 case COMPLETE_INTERNAL:
1902 return false; 1942 return false;
1903 1943
1904 case INTERRUPTED_INTERNAL: 1944 case INTERRUPTED_INTERNAL:
1905 return to == RESUMING_INTERNAL || to == CANCELLED_INTERNAL; 1945 return to == RESUMING_INTERNAL || to == CANCELLED_INTERNAL;
1906 1946
1907 case RESUMING_INTERNAL: 1947 case RESUMING_INTERNAL:
1908 return to == TARGET_PENDING_INTERNAL || to == CANCELLED_INTERNAL; 1948 return to == TARGET_PENDING_INTERNAL ||
1949 to == INTERRUPTED_TARGET_PENDING_INTERNAL ||
1950 to == CANCELLED_INTERNAL;
1909 1951
1910 case CANCELLED_INTERNAL: 1952 case CANCELLED_INTERNAL:
1911 return false; 1953 return false;
1912 1954
1913 case MAX_DOWNLOAD_INTERNAL_STATE: 1955 case MAX_DOWNLOAD_INTERNAL_STATE:
1914 NOTREACHED(); 1956 NOTREACHED();
1915 } 1957 }
1916 return false; 1958 return false;
1917 } 1959 }
1918 #endif // DCHECK_IS_ON() 1960 #endif // DCHECK_IS_ON()
1919 1961
1920 const char* DownloadItemImpl::DebugDownloadStateString( 1962 const char* DownloadItemImpl::DebugDownloadStateString(
1921 DownloadInternalState state) { 1963 DownloadInternalState state) {
1922 switch (state) { 1964 switch (state) {
1923 case INITIAL_INTERNAL: 1965 case INITIAL_INTERNAL:
1924 return "INITIAL"; 1966 return "INITIAL";
1925 case TARGET_PENDING_INTERNAL: 1967 case TARGET_PENDING_INTERNAL:
1926 return "TARGET_PENDING"; 1968 return "TARGET_PENDING";
1969 case INTERRUPTED_TARGET_PENDING_INTERNAL:
1970 return "INTERRUPTED_TARGET_PENDING";
1927 case TARGET_RESOLVED_INTERNAL: 1971 case TARGET_RESOLVED_INTERNAL:
1928 return "TARGET_RESOLVED"; 1972 return "TARGET_RESOLVED";
1929 case IN_PROGRESS_INTERNAL: 1973 case IN_PROGRESS_INTERNAL:
1930 return "IN_PROGRESS"; 1974 return "IN_PROGRESS";
1931 case COMPLETING_INTERNAL: 1975 case COMPLETING_INTERNAL:
1932 return "COMPLETING"; 1976 return "COMPLETING";
1933 case COMPLETE_INTERNAL: 1977 case COMPLETE_INTERNAL:
1934 return "COMPLETE"; 1978 return "COMPLETE";
1935 case CANCELLED_INTERNAL: 1979 case CANCELLED_INTERNAL:
1936 return "CANCELLED"; 1980 return "CANCELLED";
(...skipping 19 matching lines...) Expand all
1956 case RESUME_MODE_USER_CONTINUE: 2000 case RESUME_MODE_USER_CONTINUE:
1957 return "USER_CONTINUE"; 2001 return "USER_CONTINUE";
1958 case RESUME_MODE_USER_RESTART: 2002 case RESUME_MODE_USER_RESTART:
1959 return "USER_RESTART"; 2003 return "USER_RESTART";
1960 } 2004 }
1961 NOTREACHED() << "Unknown resume mode " << mode; 2005 NOTREACHED() << "Unknown resume mode " << mode;
1962 return "unknown"; 2006 return "unknown";
1963 } 2007 }
1964 2008
1965 } // namespace content 2009 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698