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