OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "content/browser/download/download_item.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 const DownloadPersistentStoreInfo& info) | 122 const DownloadPersistentStoreInfo& info) |
123 : download_id_(-1), | 123 : download_id_(-1), |
124 full_path_(info.path), | 124 full_path_(info.path), |
125 url_chain_(1, info.url), | 125 url_chain_(1, info.url), |
126 referrer_url_(info.referrer_url), | 126 referrer_url_(info.referrer_url), |
127 total_bytes_(info.total_bytes), | 127 total_bytes_(info.total_bytes), |
128 received_bytes_(info.received_bytes), | 128 received_bytes_(info.received_bytes), |
129 start_tick_(base::TimeTicks()), | 129 start_tick_(base::TimeTicks()), |
130 state_(static_cast<DownloadState>(info.state)), | 130 state_(static_cast<DownloadState>(info.state)), |
131 start_time_(info.start_time), | 131 start_time_(info.start_time), |
| 132 end_time_(info.end_time), |
132 db_handle_(info.db_handle), | 133 db_handle_(info.db_handle), |
133 download_manager_(download_manager), | 134 download_manager_(download_manager), |
134 is_paused_(false), | 135 is_paused_(false), |
135 open_when_complete_(false), | 136 open_when_complete_(false), |
136 file_externally_removed_(false), | 137 file_externally_removed_(false), |
137 safety_state_(SAFE), | 138 safety_state_(SAFE), |
138 auto_opened_(false), | 139 auto_opened_(false), |
139 is_otr_(false), | 140 is_otr_(false), |
140 is_temporary_(false), | 141 is_temporary_(false), |
141 all_data_saved_(false), | 142 all_data_saved_(false), |
142 opened_(false), | 143 opened_(info.opened), |
143 open_enabled_(true), | 144 open_enabled_(true), |
144 delegate_delayed_complete_(false) { | 145 delegate_delayed_complete_(false) { |
145 if (IsInProgress()) | 146 if (IsInProgress()) |
146 state_ = CANCELLED; | 147 state_ = CANCELLED; |
147 if (IsComplete()) | 148 if (IsComplete()) |
148 all_data_saved_ = true; | 149 all_data_saved_ = true; |
149 Init(false /* not actively downloading */); | 150 Init(false /* not actively downloading */); |
150 } | 151 } |
151 | 152 |
152 // Constructing for a regular download: | 153 // Constructing for a regular download: |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 } | 278 } |
278 | 279 |
279 if (!IsComplete() || file_externally_removed_) | 280 if (!IsComplete() || file_externally_removed_) |
280 return; | 281 return; |
281 | 282 |
282 // Ideally, we want to detect errors in opening and report them, but we | 283 // Ideally, we want to detect errors in opening and report them, but we |
283 // don't generally have the proper interface for that to the external | 284 // don't generally have the proper interface for that to the external |
284 // program that opens the file. So instead we spawn a check to update | 285 // program that opens the file. So instead we spawn a check to update |
285 // the UI if the file has been deleted in parallel with the open. | 286 // the UI if the file has been deleted in parallel with the open. |
286 download_manager_->CheckForFileRemoval(this); | 287 download_manager_->CheckForFileRemoval(this); |
| 288 download_stats::RecordOpen(end_time(), !opened()); |
287 opened_ = true; | 289 opened_ = true; |
288 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); | 290 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); |
| 291 download_manager_->MarkDownloadOpened(this); |
289 | 292 |
290 // For testing: If download opening is disabled on this item, | 293 // For testing: If download opening is disabled on this item, |
291 // make the rest of the routine a no-op. | 294 // make the rest of the routine a no-op. |
292 if (!open_enabled_) | 295 if (!open_enabled_) |
293 return; | 296 return; |
294 | 297 |
295 if (download_manager_->delegate()->ShouldOpenDownload(this)) | 298 if (download_manager_->delegate()->ShouldOpenDownload(this)) |
296 content::GetContentClient()->browser()->OpenItem(full_path()); | 299 content::GetContentClient()->browser()->OpenItem(full_path()); |
297 } | 300 } |
298 | 301 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 StopProgressTimer(); | 381 StopProgressTimer(); |
379 if (update_history) | 382 if (update_history) |
380 download_manager_->DownloadCancelledInternal(this); | 383 download_manager_->DownloadCancelledInternal(this); |
381 } | 384 } |
382 | 385 |
383 void DownloadItem::MarkAsComplete() { | 386 void DownloadItem::MarkAsComplete() { |
384 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 387 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
385 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 388 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
386 | 389 |
387 DCHECK(all_data_saved_); | 390 DCHECK(all_data_saved_); |
| 391 end_time_ = base::Time::Now(); |
388 TransitionTo(COMPLETE); | 392 TransitionTo(COMPLETE); |
389 } | 393 } |
390 | 394 |
391 void DownloadItem::CompleteDelayedDownload() { | 395 void DownloadItem::CompleteDelayedDownload() { |
392 auto_opened_ = true; | 396 auto_opened_ = true; |
393 Completed(); | 397 Completed(); |
394 } | 398 } |
395 | 399 |
396 void DownloadItem::OnAllDataSaved(int64 size) { | 400 void DownloadItem::OnAllDataSaved(int64 size) { |
397 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 401 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
(...skipping 10 matching lines...) Expand all Loading... |
408 UpdateObservers(); | 412 UpdateObservers(); |
409 } | 413 } |
410 | 414 |
411 void DownloadItem::Completed() { | 415 void DownloadItem::Completed() { |
412 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 416 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
413 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 417 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
414 | 418 |
415 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); | 419 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); |
416 | 420 |
417 DCHECK(all_data_saved_); | 421 DCHECK(all_data_saved_); |
| 422 end_time_ = base::Time::Now(); |
418 TransitionTo(COMPLETE); | 423 TransitionTo(COMPLETE); |
419 download_manager_->DownloadCompleted(id()); | 424 download_manager_->DownloadCompleted(id()); |
420 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); | 425 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); |
421 | 426 |
422 if (auto_opened_) { | 427 if (auto_opened_) { |
423 // If it was already handled by the delegate, do nothing. | 428 // If it was already handled by the delegate, do nothing. |
424 } else if (open_when_complete() || | 429 } else if (open_when_complete() || |
425 ShouldOpenFileBasedOnExtension() || | 430 ShouldOpenFileBasedOnExtension() || |
426 is_temporary()) { | 431 is_temporary()) { |
427 // If the download is temporary, like in drag-and-drop, do not open it but | 432 // If the download is temporary, like in drag-and-drop, do not open it but |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 | 681 |
677 state_info_.is_dangerous_url = true; | 682 state_info_.is_dangerous_url = true; |
678 UpdateSafetyState(); | 683 UpdateSafetyState(); |
679 } | 684 } |
680 | 685 |
681 DownloadPersistentStoreInfo DownloadItem::GetPersistentStoreInfo() const { | 686 DownloadPersistentStoreInfo DownloadItem::GetPersistentStoreInfo() const { |
682 return DownloadPersistentStoreInfo(full_path(), | 687 return DownloadPersistentStoreInfo(full_path(), |
683 GetURL(), | 688 GetURL(), |
684 referrer_url(), | 689 referrer_url(), |
685 start_time(), | 690 start_time(), |
| 691 end_time(), |
686 received_bytes(), | 692 received_bytes(), |
687 total_bytes(), | 693 total_bytes(), |
688 state(), | 694 state(), |
689 db_handle()); | 695 db_handle(), |
| 696 opened()); |
690 } | 697 } |
691 | 698 |
692 FilePath DownloadItem::GetTargetFilePath() const { | 699 FilePath DownloadItem::GetTargetFilePath() const { |
693 return full_path_.DirName().Append(state_info_.target_name); | 700 return full_path_.DirName().Append(state_info_.target_name); |
694 } | 701 } |
695 | 702 |
696 FilePath DownloadItem::GetFileNameToReportUser() const { | 703 FilePath DownloadItem::GetFileNameToReportUser() const { |
697 if (state_info_.path_uniquifier > 0) { | 704 if (state_info_.path_uniquifier > 0) { |
698 FilePath name(state_info_.target_name); | 705 FilePath name(state_info_.target_name); |
699 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier); | 706 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
799 state_info_.target_name.value().c_str(), | 806 state_info_.target_name.value().c_str(), |
800 full_path().value().c_str()); | 807 full_path().value().c_str()); |
801 } else { | 808 } else { |
802 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 809 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
803 } | 810 } |
804 | 811 |
805 description += " }"; | 812 description += " }"; |
806 | 813 |
807 return description; | 814 return description; |
808 } | 815 } |
OLD | NEW |