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

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

Issue 10872102: Revert 153563 - Remove DownloadFileManager in favor of direct ownership of DownloadFiles. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 3 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 #include "content/browser/download/download_item_impl.h" 5 #include "content/browser/download/download_item_impl.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/i18n/case_conversion.h" 13 #include "base/i18n/case_conversion.h"
14 #include "base/i18n/string_search.h" 14 #include "base/i18n/string_search.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/metrics/histogram.h" 16 #include "base/metrics/histogram.h"
17 #include "base/stl_util.h" 17 #include "base/stl_util.h"
18 #include "base/stringprintf.h" 18 #include "base/stringprintf.h"
19 #include "base/utf_string_conversions.h" 19 #include "base/utf_string_conversions.h"
20 #include "content/browser/download/download_create_info.h" 20 #include "content/browser/download/download_create_info.h"
21 #include "content/browser/download/download_file.h" 21 #include "content/browser/download/download_file.h"
22 #include "content/browser/download/download_file_manager.h"
22 #include "content/browser/download/download_interrupt_reasons_impl.h" 23 #include "content/browser/download/download_interrupt_reasons_impl.h"
23 #include "content/browser/download/download_item_impl_delegate.h" 24 #include "content/browser/download/download_item_impl_delegate.h"
24 #include "content/browser/download/download_request_handle.h" 25 #include "content/browser/download/download_request_handle.h"
25 #include "content/browser/download/download_stats.h" 26 #include "content/browser/download/download_stats.h"
26 #include "content/browser/web_contents/web_contents_impl.h" 27 #include "content/browser/web_contents/web_contents_impl.h"
27 #include "content/public/browser/browser_thread.h" 28 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/content_browser_client.h" 29 #include "content/public/browser/content_browser_client.h"
29 #include "content/public/browser/download_persistent_store_info.h" 30 #include "content/public/browser/download_persistent_store_info.h"
30 #include "net/base/net_util.h" 31 #include "net/base/net_util.h"
31 32
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 return NULL; 112 return NULL;
112 } 113 }
113 virtual void PauseRequest() const OVERRIDE {} 114 virtual void PauseRequest() const OVERRIDE {}
114 virtual void ResumeRequest() const OVERRIDE {} 115 virtual void ResumeRequest() const OVERRIDE {}
115 virtual void CancelRequest() const OVERRIDE {} 116 virtual void CancelRequest() const OVERRIDE {}
116 virtual std::string DebugString() const OVERRIDE { 117 virtual std::string DebugString() const OVERRIDE {
117 return "Null DownloadRequestHandle"; 118 return "Null DownloadRequestHandle";
118 } 119 }
119 }; 120 };
120 121
121 // Wrapper around DownloadFile::Detach and DownloadFile::Cancel that
122 // takes ownership of the DownloadFile and hence implicitly destroys it
123 // at the end of the function.
124 static void DownloadFileDetach(scoped_ptr<DownloadFile> download_file) {
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
126 download_file->Detach();
127 }
128
129 static void DownloadFileCancel(scoped_ptr<DownloadFile> download_file) {
130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
131 download_file->Cancel();
132 }
133
134 } // namespace 122 } // namespace
135 123
136 namespace content { 124 namespace content {
137 125
138 // Our download table ID starts at 1, so we use 0 to represent a download that 126 // Our download table ID starts at 1, so we use 0 to represent a download that
139 // has started, but has not yet had its data persisted in the table. We use fake 127 // has started, but has not yet had its data persisted in the table. We use fake
140 // database handles in incognito mode starting at -1 and progressively getting 128 // database handles in incognito mode starting at -1 and progressively getting
141 // more negative. 129 // more negative.
142 // static 130 // static
143 const int DownloadItem::kUninitializedHandle = 0; 131 const int DownloadItem::kUninitializedHandle = 0;
144 132
145 const char DownloadItem::kEmptyFileHash[] = ""; 133 const char DownloadItem::kEmptyFileHash[] = "";
146 134
147 } 135 }
148 136
149 // Our download table ID starts at 1, so we use 0 to represent a download that 137 // Our download table ID starts at 1, so we use 0 to represent a download that
150 // has started, but has not yet had its data persisted in the table. We use fake 138 // has started, but has not yet had its data persisted in the table. We use fake
151 // database handles in incognito mode starting at -1 and progressively getting 139 // database handles in incognito mode starting at -1 and progressively getting
152 // more negative. 140 // more negative.
153 141
154 // Constructor for reading from the history service. 142 // Constructor for reading from the history service.
155 DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate, 143 DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate,
156 DownloadId download_id, 144 DownloadId download_id,
157 const DownloadPersistentStoreInfo& info, 145 const DownloadPersistentStoreInfo& info,
158 const net::BoundNetLog& bound_net_log) 146 const net::BoundNetLog& bound_net_log)
159 : is_save_package_download_(false), 147 : download_id_(download_id),
160 download_id_(download_id),
161 current_path_(info.path), 148 current_path_(info.path),
162 target_path_(info.path), 149 target_path_(info.path),
163 target_disposition_(TARGET_DISPOSITION_OVERWRITE), 150 target_disposition_(TARGET_DISPOSITION_OVERWRITE),
164 url_chain_(1, info.url), 151 url_chain_(1, info.url),
165 referrer_url_(info.referrer_url), 152 referrer_url_(info.referrer_url),
166 transition_type_(content::PAGE_TRANSITION_LINK), 153 transition_type_(content::PAGE_TRANSITION_LINK),
167 has_user_gesture_(false), 154 has_user_gesture_(false),
168 total_bytes_(info.total_bytes), 155 total_bytes_(info.total_bytes),
169 received_bytes_(info.received_bytes), 156 received_bytes_(info.received_bytes),
170 bytes_per_sec_(0), 157 bytes_per_sec_(0),
(...skipping 26 matching lines...) Expand all
197 Init(false /* not actively downloading */, 184 Init(false /* not actively downloading */,
198 download_net_logs::SRC_HISTORY_IMPORT); 185 download_net_logs::SRC_HISTORY_IMPORT);
199 } 186 }
200 187
201 // Constructing for a regular download: 188 // Constructing for a regular download:
202 DownloadItemImpl::DownloadItemImpl( 189 DownloadItemImpl::DownloadItemImpl(
203 DownloadItemImplDelegate* delegate, 190 DownloadItemImplDelegate* delegate,
204 const DownloadCreateInfo& info, 191 const DownloadCreateInfo& info,
205 scoped_ptr<DownloadRequestHandleInterface> request_handle, 192 scoped_ptr<DownloadRequestHandleInterface> request_handle,
206 const net::BoundNetLog& bound_net_log) 193 const net::BoundNetLog& bound_net_log)
207 : is_save_package_download_(false), 194 : request_handle_(request_handle.Pass()),
208 request_handle_(request_handle.Pass()),
209 download_id_(info.download_id), 195 download_id_(info.download_id),
210 target_disposition_( 196 target_disposition_(
211 (info.prompt_user_for_save_location) ? 197 (info.prompt_user_for_save_location) ?
212 TARGET_DISPOSITION_PROMPT : TARGET_DISPOSITION_OVERWRITE), 198 TARGET_DISPOSITION_PROMPT : TARGET_DISPOSITION_OVERWRITE),
213 url_chain_(info.url_chain), 199 url_chain_(info.url_chain),
214 referrer_url_(info.referrer_url), 200 referrer_url_(info.referrer_url),
215 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), 201 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)),
216 forced_file_path_(info.save_info.file_path), 202 forced_file_path_(info.save_info.file_path),
217 transition_type_(info.transition_type), 203 transition_type_(info.transition_type),
218 has_user_gesture_(info.has_user_gesture), 204 has_user_gesture_(info.has_user_gesture),
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 bound_net_log_.source().ToEventParametersCallback()); 244 bound_net_log_.source().ToEventParametersCallback());
259 } 245 }
260 246
261 // Constructing for the "Save Page As..." feature: 247 // Constructing for the "Save Page As..." feature:
262 DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate, 248 DownloadItemImpl::DownloadItemImpl(DownloadItemImplDelegate* delegate,
263 const FilePath& path, 249 const FilePath& path,
264 const GURL& url, 250 const GURL& url,
265 DownloadId download_id, 251 DownloadId download_id,
266 const std::string& mime_type, 252 const std::string& mime_type,
267 const net::BoundNetLog& bound_net_log) 253 const net::BoundNetLog& bound_net_log)
268 : is_save_package_download_(true), 254 : request_handle_(new NullDownloadRequestHandle()),
269 request_handle_(new NullDownloadRequestHandle()),
270 download_id_(download_id), 255 download_id_(download_id),
271 current_path_(path), 256 current_path_(path),
272 target_path_(path), 257 target_path_(path),
273 target_disposition_(TARGET_DISPOSITION_OVERWRITE), 258 target_disposition_(TARGET_DISPOSITION_OVERWRITE),
274 url_chain_(1, url), 259 url_chain_(1, url),
275 referrer_url_(GURL()), 260 referrer_url_(GURL()),
276 transition_type_(content::PAGE_TRANSITION_LINK), 261 transition_type_(content::PAGE_TRANSITION_LINK),
277 has_user_gesture_(false), 262 has_user_gesture_(false),
278 mime_type_(mime_type), 263 mime_type_(mime_type),
279 original_mime_type_(mime_type), 264 original_mime_type_(mime_type),
(...skipping 20 matching lines...) Expand all
300 delegate_delayed_complete_(false), 285 delegate_delayed_complete_(false),
301 bound_net_log_(bound_net_log), 286 bound_net_log_(bound_net_log),
302 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { 287 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
303 delegate_->Attach(); 288 delegate_->Attach();
304 Init(true /* actively downloading */, 289 Init(true /* actively downloading */,
305 download_net_logs::SRC_SAVE_PAGE_AS); 290 download_net_logs::SRC_SAVE_PAGE_AS);
306 } 291 }
307 292
308 DownloadItemImpl::~DownloadItemImpl() { 293 DownloadItemImpl::~DownloadItemImpl() {
309 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
310
311 // Should always have been nuked before now, at worst in
312 // DownloadManager shutdown.
313 DCHECK(!download_file_.get());
314
315 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadDestroyed(this)); 295 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadDestroyed(this));
316 delegate_->AssertStateConsistent(this); 296 delegate_->AssertStateConsistent(this);
317 delegate_->Detach(); 297 delegate_->Detach();
318 } 298 }
319 299
320 base::WeakPtr<content::DownloadDestinationObserver>
321 DownloadItemImpl::DestinationObserverAsWeakPtr() {
322 // Return does private downcast.
323 return weak_ptr_factory_.GetWeakPtr();
324 }
325
326 void DownloadItemImpl::AddObserver(Observer* observer) { 300 void DownloadItemImpl::AddObserver(Observer* observer) {
327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
328 302
329 observers_.AddObserver(observer); 303 observers_.AddObserver(observer);
330 } 304 }
331 305
332 void DownloadItemImpl::RemoveObserver(Observer* observer) { 306 void DownloadItemImpl::RemoveObserver(Observer* observer) {
333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
334 308
335 observers_.RemoveObserver(observer); 309 observers_.RemoveObserver(observer);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 bound_net_log_.AddEvent( 378 bound_net_log_.AddEvent(
405 net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED, 379 net::NetLog::TYPE_DOWNLOAD_ITEM_SAFETY_STATE_UPDATED,
406 base::Bind(&download_net_logs::ItemCheckedCallback, 380 base::Bind(&download_net_logs::ItemCheckedCallback,
407 GetDangerType(), GetSafetyState())); 381 GetDangerType(), GetSafetyState()));
408 382
409 UpdateObservers(); 383 UpdateObservers();
410 384
411 delegate_->MaybeCompleteDownload(this); 385 delegate_->MaybeCompleteDownload(this);
412 } 386 }
413 387
388 void DownloadItemImpl::ProgressComplete(int64 bytes_so_far,
389 const std::string& final_hash) {
390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
391
392 hash_ = final_hash;
393 hash_state_ = "";
394
395 received_bytes_ = bytes_so_far;
396
397 // If we've received more data than we were expecting (bad server info?),
398 // revert to 'unknown size mode'.
399 if (received_bytes_ > total_bytes_)
400 total_bytes_ = 0;
401 }
402
414 // Updates from the download thread may have been posted while this download 403 // Updates from the download thread may have been posted while this download
415 // was being cancelled in the UI thread, so we'll accept them unless we're 404 // was being cancelled in the UI thread, so we'll accept them unless we're
416 // complete. 405 // complete.
417 void DownloadItemImpl::UpdateProgress(int64 bytes_so_far, 406 void DownloadItemImpl::UpdateProgress(int64 bytes_so_far,
418 int64 bytes_per_sec, 407 int64 bytes_per_sec,
419 const std::string& hash_state) { 408 const std::string& hash_state) {
420 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
421 410
422 if (!IsInProgress()) { 411 if (!IsInProgress()) {
423 // Ignore if we're no longer in-progress. This can happen if we race a 412 // Ignore if we're no longer in-progress. This can happen if we race a
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); 449 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true);
461 if (!IsPartialDownload()) { 450 if (!IsPartialDownload()) {
462 // Small downloads might be complete before this method has 451 // Small downloads might be complete before this method has
463 // a chance to run. 452 // a chance to run.
464 return; 453 return;
465 } 454 }
466 455
467 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); 456 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT);
468 457
469 TransitionTo(CANCELLED); 458 TransitionTo(CANCELLED);
470
471 // Cancel and remove the download file.
472 // TODO(rdsmith/benjhayden): Remove condition as part of
473 // SavePackage integration.
474 if (!is_save_package_download_) {
475 CHECK(download_file_.get());
476 BrowserThread::PostTask(
477 BrowserThread::FILE, FROM_HERE,
478 // Will be deleted at end of task execution.
479 base::Bind(&DownloadFileCancel, base::Passed(download_file_.Pass())));
480 }
481
482 // Cancel the originating URL request.
483 request_handle_->CancelRequest();
484
485 if (user_cancel) 459 if (user_cancel)
486 delegate_->DownloadStopped(this); 460 delegate_->DownloadStopped(this);
487 } 461 }
488 462
489 // We're starting the download.
490 void DownloadItemImpl::Start(scoped_ptr<content::DownloadFile> download_file) {
491 DCHECK(!download_file_.get());
492 download_file_ = download_file.Pass();
493
494 BrowserThread::PostTask(
495 BrowserThread::FILE, FROM_HERE,
496 base::Bind(&DownloadFile::Initialize,
497 // Safe because we control download file lifetime.
498 base::Unretained(download_file_.get()),
499 base::Bind(&DownloadItemImpl::OnDownloadFileInitialized,
500 weak_ptr_factory_.GetWeakPtr())));
501 }
502
503 // An error occurred somewhere. 463 // An error occurred somewhere.
504 void DownloadItemImpl::Interrupt(content::DownloadInterruptReason reason) { 464 void DownloadItemImpl::Interrupt(content::DownloadInterruptReason reason) {
505 // Somewhat counter-intuitively, it is possible for us to receive an 465 // Somewhat counter-intuitively, it is possible for us to receive an
506 // interrupt after we've already been interrupted. The generation of 466 // interrupt after we've already been interrupted. The generation of
507 // interrupts from the file thread Renames and the generation of 467 // interrupts from the file thread Renames and the generation of
508 // interrupts from disk writes go through two different mechanisms (driven 468 // interrupts from disk writes go through two different mechanisms (driven
509 // by rename requests from UI thread and by write requests from IO thread, 469 // by rename requests from UI thread and by write requests from IO thread,
510 // respectively), and since we choose not to keep state on the File thread, 470 // respectively), and since we choose not to keep state on the File thread,
511 // this is the place where the races collide. It's also possible for 471 // this is the place where the races collide. It's also possible for
512 // interrupts to race with cancels. 472 // interrupts to race with cancels.
513 473
514 // Whatever happens, the first one to hit the UI thread wins. 474 // Whatever happens, the first one to hit the UI thread wins.
515 if (!IsInProgress()) 475 if (!IsInProgress())
516 return; 476 return;
517 477
518 last_reason_ = reason; 478 last_reason_ = reason;
519 TransitionTo(INTERRUPTED); 479 TransitionTo(INTERRUPTED);
520
521 // Cancel and remove the download file.
522 // TODO(rdsmith/benjhayden): Remove condition as part of
523 // SavePackage integration.
524 if (!is_save_package_download_) {
525 CHECK(download_file_.get());
526 BrowserThread::PostTask(
527 BrowserThread::FILE, FROM_HERE,
528 // Will be deleted at end of task execution.
529 base::Bind(&DownloadFileCancel, base::Passed(download_file_.Pass())));
530 }
531
532 // Cancel the originating URL request.
533 request_handle_->CancelRequest();
534
535 download_stats::RecordDownloadInterrupted( 480 download_stats::RecordDownloadInterrupted(
536 reason, received_bytes_, total_bytes_); 481 reason, received_bytes_, total_bytes_);
537 delegate_->DownloadStopped(this); 482 delegate_->DownloadStopped(this);
538 } 483 }
539 484
540 void DownloadItemImpl::MarkAsComplete() { 485 void DownloadItemImpl::MarkAsComplete() {
541 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 486 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
542 487
543 DCHECK(all_data_saved_); 488 DCHECK(all_data_saved_);
544 end_time_ = base::Time::Now(); 489 end_time_ = base::Time::Now();
545 TransitionTo(COMPLETE); 490 TransitionTo(COMPLETE);
546 } 491 }
547 492
548 void DownloadItemImpl::DelayedDownloadOpened(bool auto_opened) { 493 void DownloadItemImpl::DelayedDownloadOpened(bool auto_opened) {
549 auto_opened_ = auto_opened; 494 auto_opened_ = auto_opened;
550 Completed(); 495 Completed();
551 } 496 }
552 497
553 void DownloadItemImpl::OnAllDataSaved( 498 void DownloadItemImpl::OnAllDataSaved(
554 const std::string& final_hash) { 499 int64 size, const std::string& final_hash) {
555 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 500 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
556 501
557 DCHECK_EQ(IN_PROGRESS, state_);
558 DCHECK(!all_data_saved_); 502 DCHECK(!all_data_saved_);
559 all_data_saved_ = true; 503 all_data_saved_ = true;
560 504 ProgressComplete(size, final_hash);
561 // Store final hash and null out intermediate serialized hash state.
562 hash_ = final_hash;
563 hash_state_ = "";
564
565 UpdateObservers(); 505 UpdateObservers();
566 } 506 }
567 507
568 void DownloadItemImpl::OnDownloadedFileRemoved() { 508 void DownloadItemImpl::OnDownloadedFileRemoved() {
569 file_externally_removed_ = true; 509 file_externally_removed_ = true;
570 UpdateObservers(); 510 UpdateObservers();
571 } 511 }
572 512
573 void DownloadItemImpl::MaybeCompleteDownload() { 513 void DownloadItemImpl::MaybeCompleteDownload() {
574 // TODO(rdsmith): Move logic for this function here. 514 // TODO(rdsmith): Move logic for this function here.
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 659
720 int64 speed = CurrentSpeed(); 660 int64 speed = CurrentSpeed();
721 if (speed == 0) 661 if (speed == 0)
722 return false; 662 return false;
723 663
724 *remaining = base::TimeDelta::FromSeconds( 664 *remaining = base::TimeDelta::FromSeconds(
725 (total_bytes_ - received_bytes_) / speed); 665 (total_bytes_ - received_bytes_) / speed);
726 return true; 666 return true;
727 } 667 }
728 668
729 bool DownloadItemImpl::IsSavePackageDownload() const {
730 return is_save_package_download_;
731 }
732
733 int64 DownloadItemImpl::CurrentSpeed() const { 669 int64 DownloadItemImpl::CurrentSpeed() const {
734 if (is_paused_) 670 if (is_paused_)
735 return 0; 671 return 0;
736 return bytes_per_sec_; 672 return bytes_per_sec_;
737 } 673 }
738 674
739 int DownloadItemImpl::PercentComplete() const { 675 int DownloadItemImpl::PercentComplete() const {
740 // If the delegate is delaying completion of the download, then we have no 676 // If the delegate is delaying completion of the download, then we have no
741 // idea how long it will take. 677 // idea how long it will take.
742 if (delegate_delayed_complete_ || total_bytes_ <= 0) 678 if (delegate_delayed_complete_ || total_bytes_ <= 0)
(...skipping 10 matching lines...) Expand all
753 request_handle_->ResumeRequest(); 689 request_handle_->ResumeRequest();
754 else 690 else
755 request_handle_->PauseRequest(); 691 request_handle_->PauseRequest();
756 is_paused_ = !is_paused_; 692 is_paused_ = !is_paused_;
757 UpdateObservers(); 693 UpdateObservers();
758 } 694 }
759 695
760 void DownloadItemImpl::OnDownloadCompleting() { 696 void DownloadItemImpl::OnDownloadCompleting() {
761 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 697 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
762 698
763 if (!IsInProgress())
764 return;
765
766 VLOG(20) << __FUNCTION__ << "()" 699 VLOG(20) << __FUNCTION__ << "()"
767 << " needs rename = " << NeedsRename() 700 << " needs rename = " << NeedsRename()
768 << " " << DebugString(true); 701 << " " << DebugString(true);
769 DCHECK(!GetTargetName().empty()); 702 DCHECK(!GetTargetName().empty());
770 DCHECK_NE(DANGEROUS, GetSafetyState()); 703 DCHECK_NE(DANGEROUS, GetSafetyState());
771 704
772 // TODO(rdsmith/benjhayden): Remove as part of SavePackage integration.
773 if (is_save_package_download_) {
774 // Avoid doing anything on the file thread; there's nothing we control
775 // there.
776 OnDownloadFileReleased();
777 return;
778 }
779
780 CHECK(download_file_.get());
781 if (NeedsRename()) { 705 if (NeedsRename()) {
782 content::DownloadFile::RenameCompletionCallback callback = 706 DownloadFileManager::RenameCompletionCallback callback =
783 base::Bind(&DownloadItemImpl::OnDownloadRenamedToFinalName, 707 base::Bind(&DownloadItemImpl::OnDownloadRenamedToFinalName,
784 weak_ptr_factory_.GetWeakPtr()); 708 weak_ptr_factory_.GetWeakPtr());
785 BrowserThread::PostTask( 709 BrowserThread::PostTask(
786 BrowserThread::FILE, FROM_HERE, 710 BrowserThread::FILE, FROM_HERE,
787 base::Bind(&DownloadFile::Rename, 711 base::Bind(&DownloadFileManager::RenameDownloadFile,
788 base::Unretained(download_file_.get()), 712 delegate_->GetDownloadFileManager(), GetGlobalId(),
789 GetTargetFilePath(), true, callback)); 713 GetTargetFilePath(), true, callback));
790 } else { 714 } else {
791 // Complete the download and release the DownloadFile. 715 // Complete the download and release the DownloadFile.
792 BrowserThread::PostTaskAndReply( 716 BrowserThread::PostTask(
793 BrowserThread::FILE, FROM_HERE, 717 BrowserThread::FILE, FROM_HERE,
794 base::Bind(&DownloadFileDetach, base::Passed(download_file_.Pass())), 718 base::Bind(&DownloadFileManager::CompleteDownload,
795 base::Bind(&DownloadItemImpl::OnDownloadFileReleased, 719 delegate_->GetDownloadFileManager(), GetGlobalId(),
796 weak_ptr_factory_.GetWeakPtr())); 720 base::Bind(&DownloadItemImpl::OnDownloadFileReleased,
721 weak_ptr_factory_.GetWeakPtr())));
797 } 722 }
798 } 723 }
799 724
800 void DownloadItemImpl::OnDownloadRenamedToFinalName( 725 void DownloadItemImpl::OnDownloadRenamedToFinalName(
801 content::DownloadInterruptReason reason, 726 content::DownloadInterruptReason reason,
802 const FilePath& full_path) { 727 const FilePath& full_path) {
803 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 728 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
804 729
805 if (!IsInProgress())
806 return;
807
808 VLOG(20) << __FUNCTION__ << "()" 730 VLOG(20) << __FUNCTION__ << "()"
809 << " full_path = \"" << full_path.value() << "\"" 731 << " full_path = \"" << full_path.value() << "\""
810 << " needed rename = " << NeedsRename() 732 << " needed rename = " << NeedsRename()
811 << " " << DebugString(false); 733 << " " << DebugString(false);
812 DCHECK(NeedsRename()); 734 DCHECK(NeedsRename());
813 735
814 if (content::DOWNLOAD_INTERRUPT_REASON_NONE != reason) { 736 if (content::DOWNLOAD_INTERRUPT_REASON_NONE != reason) {
815 Interrupt(reason); 737 Interrupt(reason);
816 return; 738 return;
817 } 739 }
818 740
819 // full_path is now the current and target file path. 741 // full_path is now the current and target file path.
820 DCHECK(!full_path.empty()); 742 DCHECK(!full_path.empty());
821 target_path_ = full_path; 743 target_path_ = full_path;
822 SetFullPath(full_path); 744 SetFullPath(full_path);
823 delegate_->DownloadRenamedToFinalName(this); 745 delegate_->DownloadRenamedToFinalName(this);
824 746
825 // Complete the download and release the DownloadFile. 747 // Complete the download and release the DownloadFile.
826 // TODO(rdsmith): Unify this path with the !NeedsRename() path in 748 BrowserThread::PostTask(
827 // OnDownloadCompleting above. This can happen easily after history
828 // is made into an observer and the path accessors are cleaned up;
829 // that should allow OnDownloadCompleting to simply call
830 // OnDownloadRenamedToFinalName directly.
831 DCHECK(!is_save_package_download_);
832 CHECK(download_file_.get());
833 BrowserThread::PostTaskAndReply(
834 BrowserThread::FILE, FROM_HERE, 749 BrowserThread::FILE, FROM_HERE,
835 base::Bind(&DownloadFileDetach, base::Passed(download_file_.Pass())), 750 base::Bind(&DownloadFileManager::CompleteDownload,
836 base::Bind(&DownloadItemImpl::OnDownloadFileReleased, 751 delegate_->GetDownloadFileManager(), GetGlobalId(),
837 weak_ptr_factory_.GetWeakPtr())); 752 base::Bind(&DownloadItemImpl::OnDownloadFileReleased,
838 } 753 weak_ptr_factory_.GetWeakPtr())));
839
840 void DownloadItemImpl::OnDownloadFileInitialized(
841 content::DownloadInterruptReason result) {
842 if (result != content::DOWNLOAD_INTERRUPT_REASON_NONE) {
843 Interrupt(result);
844 // TODO(rdsmith): It makes no sense to continue along the
845 // regular download path after we've gotten an error. But it's
846 // the way the code has historically worked, and this allows us
847 // to get the download persisted and observers of the download manager
848 // notified, so tests work. When we execute all side effects of cancel
849 // (including queue removal) immedately rather than waiting for
850 // persistence we should replace this comment with a "return;".
851 }
852
853 delegate_->DelegateStart(this);
854 } 754 }
855 755
856 void DownloadItemImpl::OnDownloadFileReleased() { 756 void DownloadItemImpl::OnDownloadFileReleased() {
857 if (delegate_->ShouldOpenDownload(this)) 757 if (delegate_->ShouldOpenDownload(this))
858 Completed(); 758 Completed();
859 else 759 else
860 delegate_delayed_complete_ = true; 760 delegate_delayed_complete_ = true;
861 } 761 }
862 762
863 void DownloadItemImpl::OnDownloadRenamedToIntermediateName( 763 void DownloadItemImpl::OnDownloadRenamedToIntermediateName(
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
974 target_path_ = target_path; 874 target_path_ = target_path;
975 target_disposition_ = disposition; 875 target_disposition_ = disposition;
976 SetDangerType(danger_type); 876 SetDangerType(danger_type);
977 // TODO(asanka): SetDangerType() doesn't need to send a notification here. 877 // TODO(asanka): SetDangerType() doesn't need to send a notification here.
978 878
979 // We want the intermediate and target paths to refer to the same directory so 879 // We want the intermediate and target paths to refer to the same directory so
980 // that they are both on the same device and subject to same 880 // that they are both on the same device and subject to same
981 // space/permission/availability constraints. 881 // space/permission/availability constraints.
982 DCHECK(intermediate_path.DirName() == target_path.DirName()); 882 DCHECK(intermediate_path.DirName() == target_path.DirName());
983 883
984 if (!IsInProgress()) {
985 // If we've been cancelled or interrupted while the target was being
986 // determined, continue the cascade with a null name.
987 // The error doesn't matter as the cause of download stoppaged
988 // will already have been recorded.
989 OnDownloadRenamedToIntermediateName(
990 content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, FilePath());
991 return;
992 }
993
994 // Rename to intermediate name. 884 // Rename to intermediate name.
995 // TODO(asanka): Skip this rename if AllDataSaved() is true. This avoids a 885 // TODO(asanka): Skip this rename if AllDataSaved() is true. This avoids a
996 // spurious rename when we can just rename to the final 886 // spurious rename when we can just rename to the final
997 // filename. Unnecessary renames may cause bugs like 887 // filename. Unnecessary renames may cause bugs like
998 // http://crbug.com/74187. 888 // http://crbug.com/74187.
999 DCHECK(!is_save_package_download_); 889 DownloadFileManager::RenameCompletionCallback callback =
1000 CHECK(download_file_.get());
1001 DownloadFile::RenameCompletionCallback callback =
1002 base::Bind(&DownloadItemImpl::OnDownloadRenamedToIntermediateName, 890 base::Bind(&DownloadItemImpl::OnDownloadRenamedToIntermediateName,
1003 weak_ptr_factory_.GetWeakPtr()); 891 weak_ptr_factory_.GetWeakPtr());
1004 BrowserThread::PostTask( 892 BrowserThread::PostTask(
1005 BrowserThread::FILE, FROM_HERE, 893 BrowserThread::FILE, FROM_HERE,
1006 base::Bind(&DownloadFile::Rename, 894 base::Bind(&DownloadFileManager::RenameDownloadFile,
1007 // Safe because we control download file lifetime. 895 delegate_->GetDownloadFileManager(), GetGlobalId(),
1008 base::Unretained(download_file_.get()),
1009 intermediate_path, false, callback)); 896 intermediate_path, false, callback));
1010 } 897 }
1011 898
1012 void DownloadItemImpl::OnContentCheckCompleted( 899 void DownloadItemImpl::OnContentCheckCompleted(
1013 content::DownloadDangerType danger_type) { 900 content::DownloadDangerType danger_type) {
1014 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 901 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1015 DCHECK(AllDataSaved()); 902 DCHECK(AllDataSaved());
1016 SetDangerType(danger_type); 903 SetDangerType(danger_type);
1017 UpdateObservers(); 904 UpdateObservers();
1018 } 905 }
(...skipping 10 matching lines...) Expand all
1029 916
1030 void DownloadItemImpl::SetDisplayName(const FilePath& name) { 917 void DownloadItemImpl::SetDisplayName(const FilePath& name) {
1031 display_name_ = name; 918 display_name_ = name;
1032 } 919 }
1033 920
1034 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const { 921 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const {
1035 return (safety_state_ == DownloadItem::SAFE) ? 922 return (safety_state_ == DownloadItem::SAFE) ?
1036 GetTargetFilePath() : GetFullPath(); 923 GetTargetFilePath() : GetFullPath();
1037 } 924 }
1038 925
1039 void DownloadItemImpl::DestinationUpdate(int64 bytes_so_far, 926 void DownloadItemImpl::OffThreadCancel() {
1040 int64 bytes_per_sec,
1041 const std::string& hash_state) {
1042 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 927 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
928 request_handle_->CancelRequest();
1043 929
1044 if (!IsInProgress()) { 930 BrowserThread::PostTask(
1045 // Ignore if we're no longer in-progress. This can happen if we race a 931 BrowserThread::FILE, FROM_HERE,
1046 // Cancel on the UI thread with an update on the FILE thread. 932 base::Bind(&DownloadFileManager::CancelDownload,
1047 // 933 delegate_->GetDownloadFileManager(), download_id_));
1048 // TODO(rdsmith): Arguably we should let this go through, as this means
1049 // the download really did get further than we know before it was
1050 // cancelled. But the gain isn't very large, and the code is more
1051 // fragile if it has to support in progress updates in a non-in-progress
1052 // state. This issue should be readdressed when we revamp performance
1053 // reporting.
1054 return;
1055 }
1056 bytes_per_sec_ = bytes_per_sec;
1057 hash_state_ = hash_state;
1058 received_bytes_ = bytes_so_far;
1059
1060 // If we've received more data than we were expecting (bad server info?),
1061 // revert to 'unknown size mode'.
1062 if (received_bytes_ > total_bytes_)
1063 total_bytes_ = 0;
1064
1065 if (bound_net_log_.IsLoggingAllEvents()) {
1066 bound_net_log_.AddEvent(
1067 net::NetLog::TYPE_DOWNLOAD_ITEM_UPDATED,
1068 net::NetLog::Int64Callback("bytes_so_far", received_bytes_));
1069 }
1070
1071 UpdateObservers();
1072 }
1073
1074 void DownloadItemImpl::DestinationError(
1075 content::DownloadInterruptReason reason) {
1076 // The DestinationError and Interrupt routines are being kept separate
1077 // to allow for a future merging of the Cancel and Interrupt routines..
1078 Interrupt(reason);
1079 }
1080
1081 void DownloadItemImpl::DestinationCompleted(const std::string& final_hash) {
1082 if (!IsInProgress())
1083 return;
1084 OnAllDataSaved(final_hash);
1085 delegate_->MaybeCompleteDownload(this);
1086 } 934 }
1087 935
1088 void DownloadItemImpl::Init(bool active, 936 void DownloadItemImpl::Init(bool active,
1089 download_net_logs::DownloadType download_type) { 937 download_net_logs::DownloadType download_type) {
1090 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 938 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1091 939
1092 if (active) 940 if (active)
1093 download_stats::RecordDownloadCount(download_stats::START_COUNT); 941 download_stats::RecordDownloadCount(download_stats::START_COUNT);
1094 942
1095 if (target_path_.empty()) 943 if (target_path_.empty())
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
1185 " db_handle = %" PRId64 1033 " db_handle = %" PRId64
1186 " total = %" PRId64 1034 " total = %" PRId64
1187 " received = %" PRId64 1035 " received = %" PRId64
1188 " reason = %s" 1036 " reason = %s"
1189 " paused = %c" 1037 " paused = %c"
1190 " safety = %s" 1038 " safety = %s"
1191 " last_modified = '%s'" 1039 " last_modified = '%s'"
1192 " etag = '%s'" 1040 " etag = '%s'"
1193 " url_chain = \n\t\"%s\"\n\t" 1041 " url_chain = \n\t\"%s\"\n\t"
1194 " full_path = \"%" PRFilePath "\"" 1042 " full_path = \"%" PRFilePath "\""
1195 " target_path = \"%" PRFilePath "\"" 1043 " target_path = \"%" PRFilePath "\"",
1196 " has download file = %s",
1197 GetDbHandle(), 1044 GetDbHandle(),
1198 GetTotalBytes(), 1045 GetTotalBytes(),
1199 GetReceivedBytes(), 1046 GetReceivedBytes(),
1200 InterruptReasonDebugString(last_reason_).c_str(), 1047 InterruptReasonDebugString(last_reason_).c_str(),
1201 IsPaused() ? 'T' : 'F', 1048 IsPaused() ? 'T' : 'F',
1202 DebugSafetyStateString(GetSafetyState()), 1049 DebugSafetyStateString(GetSafetyState()),
1203 GetLastModifiedTime().c_str(), 1050 GetLastModifiedTime().c_str(),
1204 GetETag().c_str(), 1051 GetETag().c_str(),
1205 url_list.c_str(), 1052 url_list.c_str(),
1206 GetFullPath().value().c_str(), 1053 GetFullPath().value().c_str(),
1207 GetTargetFilePath().value().c_str(), 1054 GetTargetFilePath().value().c_str());
1208 download_file_.get() ? "true" : "false");
1209 } else { 1055 } else {
1210 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); 1056 description += base::StringPrintf(" url = \"%s\"", url_list.c_str());
1211 } 1057 }
1212 1058
1213 description += " }"; 1059 description += " }";
1214 1060
1215 return description; 1061 return description;
1216 } 1062 }
1217 1063
1218 bool DownloadItemImpl::AllDataSaved() const { return all_data_saved_; } 1064 bool DownloadItemImpl::AllDataSaved() const { return all_data_saved_; }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
1308 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } 1154 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; }
1309 bool DownloadItemImpl::GetOpened() const { return opened_; } 1155 bool DownloadItemImpl::GetOpened() const { return opened_; }
1310 const std::string& DownloadItemImpl::GetLastModifiedTime() const { 1156 const std::string& DownloadItemImpl::GetLastModifiedTime() const {
1311 return last_modified_time_; 1157 return last_modified_time_;
1312 } 1158 }
1313 const std::string& DownloadItemImpl::GetETag() const { return etag_; } 1159 const std::string& DownloadItemImpl::GetETag() const { return etag_; }
1314 content::DownloadInterruptReason DownloadItemImpl::GetLastReason() const { 1160 content::DownloadInterruptReason DownloadItemImpl::GetLastReason() const {
1315 return last_reason_; 1161 return last_reason_;
1316 } 1162 }
1317 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } 1163 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; }
OLDNEW
« no previous file with comments | « content/browser/download/download_item_impl.h ('k') | content/browser/download/download_item_impl_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698