Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "chrome/browser/download/chrome_download_manager_delegate.h" | 5 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "chrome/browser/api/prefs/pref_member.h" | 17 #include "chrome/browser/api/prefs/pref_member.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/download/download_completion_blocker.h" | 19 #include "chrome/browser/download/download_completion_blocker.h" |
| 20 #include "chrome/browser/download/download_crx_util.h" | 20 #include "chrome/browser/download/download_crx_util.h" |
| 21 #include "chrome/browser/download/download_extensions.h" | 21 #include "chrome/browser/download/download_extensions.h" |
| 22 #include "chrome/browser/download/download_file_picker.h" | 22 #include "chrome/browser/download/download_file_picker.h" |
| 23 #include "chrome/browser/download/download_history.h" | 23 #include "chrome/browser/download/download_history.h" |
| 24 #include "chrome/browser/download/download_path_reservation_tracker.h" | 24 #include "chrome/browser/download/download_path_reservation_tracker.h" |
| 25 #include "chrome/browser/download/download_prefs.h" | 25 #include "chrome/browser/download/download_prefs.h" |
| 26 #include "chrome/browser/download/download_service.h" | |
| 27 #include "chrome/browser/download/download_service_factory.h" | |
| 26 #include "chrome/browser/download/download_status_updater.h" | 28 #include "chrome/browser/download/download_status_updater.h" |
| 27 #include "chrome/browser/download/download_util.h" | 29 #include "chrome/browser/download/download_util.h" |
| 28 #include "chrome/browser/download/save_package_file_picker.h" | 30 #include "chrome/browser/download/save_package_file_picker.h" |
| 29 #include "chrome/browser/extensions/api/downloads/downloads_api.h" | 31 #include "chrome/browser/extensions/api/downloads/downloads_api.h" |
| 30 #include "chrome/browser/extensions/crx_installer.h" | 32 #include "chrome/browser/extensions/crx_installer.h" |
| 31 #include "chrome/browser/extensions/extension_service.h" | 33 #include "chrome/browser/extensions/extension_service.h" |
| 34 #include "chrome/browser/history/history.h" | |
| 35 #include "chrome/browser/history/history_service_factory.h" | |
| 32 #include "chrome/browser/intents/web_intents_util.h" | 36 #include "chrome/browser/intents/web_intents_util.h" |
| 33 #include "chrome/browser/prefs/pref_service.h" | 37 #include "chrome/browser/prefs/pref_service.h" |
| 34 #include "chrome/browser/profiles/profile.h" | 38 #include "chrome/browser/profiles/profile.h" |
| 35 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 39 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 36 #include "chrome/browser/ui/browser_tabstrip.h" | 40 #include "chrome/browser/ui/browser_tabstrip.h" |
| 37 #include "chrome/common/chrome_notification_types.h" | 41 #include "chrome/common/chrome_notification_types.h" |
| 38 #include "chrome/common/extensions/feature_switch.h" | 42 #include "chrome/common/extensions/feature_switch.h" |
| 39 #include "chrome/common/extensions/user_script.h" | 43 #include "chrome/common/extensions/user_script.h" |
| 40 #include "chrome/common/pref_names.h" | 44 #include "chrome/common/pref_names.h" |
| 41 #include "content/public/browser/download_item.h" | 45 #include "content/public/browser/download_item.h" |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 } | 137 } |
| 134 | 138 |
| 135 // Called when the web intents dispatch has completed. Deletes the |file_path|. | 139 // Called when the web intents dispatch has completed. Deletes the |file_path|. |
| 136 void OnWebIntentDispatchCompleted( | 140 void OnWebIntentDispatchCompleted( |
| 137 const FilePath& file_path, | 141 const FilePath& file_path, |
| 138 webkit_glue::WebIntentReplyType intent_reply) { | 142 webkit_glue::WebIntentReplyType intent_reply) { |
| 139 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 143 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 140 base::Bind(&DeleteFile, file_path)); | 144 base::Bind(&DeleteFile, file_path)); |
| 141 } | 145 } |
| 142 | 146 |
| 147 typedef base::Callback<void(bool)> VisitedBeforeCallback; | |
| 148 | |
| 149 void VisitCountsToVisitedBefore( | |
|
Randy Smith (Not in Mondays)
2012/11/09 21:36:39
*snort* No problem can't be solved by another lay
benjhayden
2012/11/12 18:44:16
Done.
| |
| 150 const VisitedBeforeCallback& callback, | |
| 151 HistoryService::Handle unused_handle, | |
| 152 bool found_visits, | |
| 153 int count, | |
| 154 base::Time first_visit) { | |
| 155 callback.Run(found_visits && count && | |
| 156 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); | |
| 157 } | |
| 158 | |
| 143 } // namespace | 159 } // namespace |
| 144 | 160 |
| 145 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 161 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
| 146 : profile_(profile), | 162 : profile_(profile), |
| 147 next_download_id_(0), | 163 next_download_id_(0), |
| 148 download_prefs_(new DownloadPrefs(profile)) { | 164 download_prefs_(new DownloadPrefs(profile)) { |
| 149 } | 165 } |
| 150 | 166 |
| 151 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 167 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
| 152 } | 168 } |
| 153 | 169 |
| 154 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { | 170 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { |
| 155 download_manager_ = dm; | 171 download_manager_ = dm; |
| 156 download_history_.reset(new DownloadHistory(profile_)); | |
| 157 if (!profile_->IsOffTheRecord()) { | |
| 158 // DownloadManager should not be RefCountedThreadSafe. | |
| 159 // ChromeDownloadManagerDelegate outlives DownloadManager, and | |
| 160 // DownloadHistory uses a scoped canceller to cancel tasks when it is | |
| 161 // deleted. Almost all callbacks to DownloadManager should use weak pointers | |
| 162 // or bounce off a container object that uses ManagerGoingDown() to simulate | |
| 163 // a weak pointer. | |
| 164 download_history_->Load( | |
| 165 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete, | |
| 166 download_manager_)); | |
| 167 } | |
| 168 #if !defined(OS_ANDROID) | 172 #if !defined(OS_ANDROID) |
| 169 extension_event_router_.reset(new ExtensionDownloadsEventRouter( | 173 extension_event_router_.reset(new ExtensionDownloadsEventRouter( |
| 170 profile_, download_manager_)); | 174 profile_, download_manager_)); |
| 171 #endif | 175 #endif |
| 172 } | 176 } |
| 173 | 177 |
| 174 void ChromeDownloadManagerDelegate::Shutdown() { | 178 void ChromeDownloadManagerDelegate::Shutdown() { |
| 175 download_history_.reset(); | |
| 176 download_prefs_.reset(); | 179 download_prefs_.reset(); |
| 177 #if !defined(OS_ANDROID) | 180 #if !defined(OS_ANDROID) |
| 178 extension_event_router_.reset(); | 181 extension_event_router_.reset(); |
| 179 #endif | 182 #endif |
| 180 } | 183 } |
| 181 | 184 |
| 182 DownloadId ChromeDownloadManagerDelegate::GetNextId() { | 185 DownloadId ChromeDownloadManagerDelegate::GetNextId() { |
| 183 if (!profile_->IsOffTheRecord()) | 186 if (!profile_->IsOffTheRecord()) |
| 184 return DownloadId(this, next_download_id_++); | 187 return DownloadId(this, next_download_id_++); |
| 185 | 188 |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 485 | 488 |
| 486 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 489 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
| 487 #if defined(ENABLE_SAFE_BROWSING) | 490 #if defined(ENABLE_SAFE_BROWSING) |
| 488 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 491 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
| 489 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 492 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
| 490 #else | 493 #else |
| 491 return false; | 494 return false; |
| 492 #endif | 495 #endif |
| 493 } | 496 } |
| 494 | 497 |
| 495 void ChromeDownloadManagerDelegate::AddItemToPersistentStore( | |
| 496 DownloadItem* item) { | |
| 497 if (profile_->IsOffTheRecord()) { | |
| 498 OnItemAddedToPersistentStore( | |
| 499 item->GetId(), download_history_->GetNextFakeDbHandle()); | |
| 500 return; | |
| 501 } | |
| 502 download_history_->AddEntry(item, | |
| 503 base::Bind(&ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore, | |
| 504 this)); | |
| 505 } | |
| 506 | |
| 507 void ChromeDownloadManagerDelegate::UpdateItemInPersistentStore( | |
| 508 DownloadItem* item) { | |
| 509 download_history_->UpdateEntry(item); | |
| 510 } | |
| 511 | |
| 512 void ChromeDownloadManagerDelegate::UpdatePathForItemInPersistentStore( | |
| 513 DownloadItem* item, | |
| 514 const FilePath& new_path) { | |
| 515 download_history_->UpdateDownloadPath(item, new_path); | |
| 516 } | |
| 517 | |
| 518 void ChromeDownloadManagerDelegate::RemoveItemFromPersistentStore( | |
| 519 DownloadItem* item) { | |
| 520 download_history_->RemoveEntry(item); | |
| 521 } | |
| 522 | |
| 523 void ChromeDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween( | |
| 524 base::Time remove_begin, | |
| 525 base::Time remove_end) { | |
| 526 if (profile_->IsOffTheRecord()) | |
| 527 return; | |
| 528 download_history_->RemoveEntriesBetween(remove_begin, remove_end); | |
| 529 } | |
| 530 | |
| 531 void ChromeDownloadManagerDelegate::GetSaveDir(BrowserContext* browser_context, | 498 void ChromeDownloadManagerDelegate::GetSaveDir(BrowserContext* browser_context, |
| 532 FilePath* website_save_dir, | 499 FilePath* website_save_dir, |
| 533 FilePath* download_save_dir, | 500 FilePath* download_save_dir, |
| 534 bool* skip_dir_check) { | 501 bool* skip_dir_check) { |
| 535 Profile* profile = Profile::FromBrowserContext(browser_context); | 502 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 536 PrefService* prefs = profile->GetPrefs(); | 503 PrefService* prefs = profile->GetPrefs(); |
| 537 | 504 |
| 538 // Check whether the preference has the preferred directory for saving file. | 505 // Check whether the preference has the preferred directory for saving file. |
| 539 // If not, initialize it with default directory. | 506 // If not, initialize it with default directory. |
| 540 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { | 507 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 643 DownloadItem* download = download_manager_->GetDownload(download_id); | 610 DownloadItem* download = download_manager_->GetDownload(download_id); |
| 644 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) | 611 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) |
| 645 return; | 612 return; |
| 646 | 613 |
| 647 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) | 614 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) |
| 648 << " verdict = " << result; | 615 << " verdict = " << result; |
| 649 content::DownloadDangerType danger_type = download->GetDangerType(); | 616 content::DownloadDangerType danger_type = download->GetDangerType(); |
| 650 if (result != DownloadProtectionService::SAFE) | 617 if (result != DownloadProtectionService::SAFE) |
| 651 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; | 618 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; |
| 652 | 619 |
| 653 download_history_->CheckVisitedReferrerBefore( | 620 // HistoryServiceFactory redirects incognito profiles to on-record profiles. |
| 654 download_id, download->GetReferrerUrl(), | 621 HistoryService* history = HistoryServiceFactory::GetForProfile( |
| 655 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, | 622 profile_, Profile::EXPLICIT_ACCESS); |
| 656 this, download_id, callback, danger_type)); | 623 if (!history || !download->GetReferrerUrl().is_valid()) { |
| 624 // If the original profile doesn't have a HistoryService or the referrer url | |
| 625 // is invalid, then give up and assume the referrer has not been visited | |
| 626 // before. There's no history for on-record profiles in unit_tests, for | |
| 627 // example. | |
| 628 CheckVisitedReferrerBeforeDone(download_id, callback, danger_type, false); | |
| 629 return; | |
| 630 } | |
| 631 history->GetVisibleVisitCountToHost( | |
| 632 download->GetReferrerUrl(), &history_consumer_, | |
| 633 base::Bind(&VisitCountsToVisitedBefore, base::Bind( | |
| 634 &ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, | |
| 635 this, download_id, callback, danger_type))); | |
| 657 } | 636 } |
| 658 | 637 |
| 659 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( | 638 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( |
| 660 int32 download_id, | 639 int32 download_id, |
| 661 DownloadProtectionService::DownloadCheckResult result) { | 640 DownloadProtectionService::DownloadCheckResult result) { |
| 662 DownloadItem* item = download_manager_->GetDownload(download_id); | 641 DownloadItem* item = download_manager_->GetDownload(download_id); |
| 663 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) | 642 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) |
| 664 return; | 643 return; |
| 665 | 644 |
| 666 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) | 645 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 801 } else { | 780 } else { |
| 802 // Currently we only expect this case. | 781 // Currently we only expect this case. |
| 803 DCHECK_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, danger_type); | 782 DCHECK_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, danger_type); |
| 804 } | 783 } |
| 805 | 784 |
| 806 #if defined (OS_CHROMEOS) | 785 #if defined (OS_CHROMEOS) |
| 807 drive::DriveDownloadObserver::SubstituteDriveDownloadPath( | 786 drive::DriveDownloadObserver::SubstituteDriveDownloadPath( |
| 808 profile_, suggested_path, download, | 787 profile_, suggested_path, download, |
| 809 base::Bind( | 788 base::Bind( |
| 810 &ChromeDownloadManagerDelegate::SubstituteDriveDownloadPathCallback, | 789 &ChromeDownloadManagerDelegate::SubstituteDriveDownloadPathCallback, |
| 811 this, download->GetId(), callback, should_prompt, is_forced_path, | 790 this, download->GetId(), callback, should_prompt, |
| 812 danger_type)); | 791 is_forced_path, danger_type)); |
| 813 #else | 792 #else |
| 814 GetReservedPath( | 793 GetReservedPath( |
| 815 *download, suggested_path, download_prefs_->DownloadPath(), | 794 *download, suggested_path, download_prefs_->DownloadPath(), |
| 816 !is_forced_path, | 795 !is_forced_path, |
| 817 base::Bind(&ChromeDownloadManagerDelegate::OnPathReservationAvailable, | 796 base::Bind(&ChromeDownloadManagerDelegate::OnPathReservationAvailable, |
| 818 this, download->GetId(), callback, should_prompt, | 797 this, download->GetId(), callback, should_prompt, |
| 819 danger_type)); | 798 danger_type)); |
| 820 #endif | 799 #endif |
| 821 } | 800 } |
| 822 | 801 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 894 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a | 873 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a |
| 895 // directory to persist. Or perhaps, if the Drive path | 874 // directory to persist. Or perhaps, if the Drive path |
| 896 // substitution logic is moved here, then we would have a | 875 // substitution logic is moved here, then we would have a |
| 897 // persistable path after the DownloadFilePicker is done. | 876 // persistable path after the DownloadFilePicker is done. |
| 898 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && | 877 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && |
| 899 !download->IsTemporary()) | 878 !download->IsTemporary()) |
| 900 last_download_path_ = target_path.DirName(); | 879 last_download_path_ = target_path.DirName(); |
| 901 } | 880 } |
| 902 callback.Run(target_path, disposition, danger_type, intermediate_path); | 881 callback.Run(target_path, disposition, danger_type, intermediate_path); |
| 903 } | 882 } |
| 904 | |
| 905 void ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore( | |
| 906 int32 download_id, int64 db_handle) { | |
| 907 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | |
| 908 // call this function with an invalid |db_handle|. For instance, this can | |
| 909 // happen when the history database is offline. We cannot have multiple | |
| 910 // DownloadItems with the same invalid db_handle, so we need to assign a | |
| 911 // unique |db_handle| here. | |
| 912 if (db_handle == DownloadItem::kUninitializedHandle) | |
| 913 db_handle = download_history_->GetNextFakeDbHandle(); | |
| 914 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | |
| 915 } | |
| OLD | NEW |