| 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 // Condenses the results from HistoryService::GetVisibleVisitCountToHost() to a |
| 150 // single bool so that VisitedBeforeCallback can curry up to 5 other parameters |
| 151 // without a struct. |
| 152 void VisitCountsToVisitedBefore( |
| 153 const VisitedBeforeCallback& callback, |
| 154 HistoryService::Handle unused_handle, |
| 155 bool found_visits, |
| 156 int count, |
| 157 base::Time first_visit) { |
| 158 callback.Run(found_visits && count && |
| 159 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); |
| 160 } |
| 161 |
| 143 } // namespace | 162 } // namespace |
| 144 | 163 |
| 145 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 164 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
| 146 : profile_(profile), | 165 : profile_(profile), |
| 147 next_download_id_(0), | 166 next_download_id_(0), |
| 148 download_prefs_(new DownloadPrefs(profile)) { | 167 download_prefs_(new DownloadPrefs(profile)) { |
| 149 } | 168 } |
| 150 | 169 |
| 151 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 170 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
| 152 } | 171 } |
| 153 | 172 |
| 154 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { | 173 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { |
| 155 download_manager_ = dm; | 174 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) | 175 #if !defined(OS_ANDROID) |
| 169 extension_event_router_.reset(new ExtensionDownloadsEventRouter( | 176 extension_event_router_.reset(new ExtensionDownloadsEventRouter( |
| 170 profile_, download_manager_)); | 177 profile_, download_manager_)); |
| 171 #endif | 178 #endif |
| 172 } | 179 } |
| 173 | 180 |
| 174 void ChromeDownloadManagerDelegate::Shutdown() { | 181 void ChromeDownloadManagerDelegate::Shutdown() { |
| 175 download_history_.reset(); | |
| 176 download_prefs_.reset(); | 182 download_prefs_.reset(); |
| 177 #if !defined(OS_ANDROID) | 183 #if !defined(OS_ANDROID) |
| 178 extension_event_router_.reset(); | 184 extension_event_router_.reset(); |
| 179 #endif | 185 #endif |
| 180 } | 186 } |
| 181 | 187 |
| 182 DownloadId ChromeDownloadManagerDelegate::GetNextId() { | 188 DownloadId ChromeDownloadManagerDelegate::GetNextId() { |
| 183 if (!profile_->IsOffTheRecord()) | 189 if (!profile_->IsOffTheRecord()) |
| 184 return DownloadId(this, next_download_id_++); | 190 return DownloadId(this, next_download_id_++); |
| 185 | 191 |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 | 478 |
| 473 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 479 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
| 474 #if defined(ENABLE_SAFE_BROWSING) | 480 #if defined(ENABLE_SAFE_BROWSING) |
| 475 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 481 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
| 476 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 482 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
| 477 #else | 483 #else |
| 478 return false; | 484 return false; |
| 479 #endif | 485 #endif |
| 480 } | 486 } |
| 481 | 487 |
| 482 void ChromeDownloadManagerDelegate::AddItemToPersistentStore( | |
| 483 DownloadItem* item) { | |
| 484 if (profile_->IsOffTheRecord()) { | |
| 485 OnItemAddedToPersistentStore( | |
| 486 item->GetId(), download_history_->GetNextFakeDbHandle()); | |
| 487 return; | |
| 488 } | |
| 489 download_history_->AddEntry(item, | |
| 490 base::Bind(&ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore, | |
| 491 this)); | |
| 492 } | |
| 493 | |
| 494 void ChromeDownloadManagerDelegate::UpdateItemInPersistentStore( | |
| 495 DownloadItem* item) { | |
| 496 download_history_->UpdateEntry(item); | |
| 497 } | |
| 498 | |
| 499 void ChromeDownloadManagerDelegate::UpdatePathForItemInPersistentStore( | |
| 500 DownloadItem* item, | |
| 501 const FilePath& new_path) { | |
| 502 download_history_->UpdateDownloadPath(item, new_path); | |
| 503 } | |
| 504 | |
| 505 void ChromeDownloadManagerDelegate::RemoveItemFromPersistentStore( | |
| 506 DownloadItem* item) { | |
| 507 download_history_->RemoveEntry(item); | |
| 508 } | |
| 509 | |
| 510 void ChromeDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween( | |
| 511 base::Time remove_begin, | |
| 512 base::Time remove_end) { | |
| 513 if (profile_->IsOffTheRecord()) | |
| 514 return; | |
| 515 download_history_->RemoveEntriesBetween(remove_begin, remove_end); | |
| 516 } | |
| 517 | |
| 518 void ChromeDownloadManagerDelegate::GetSaveDir(BrowserContext* browser_context, | 488 void ChromeDownloadManagerDelegate::GetSaveDir(BrowserContext* browser_context, |
| 519 FilePath* website_save_dir, | 489 FilePath* website_save_dir, |
| 520 FilePath* download_save_dir, | 490 FilePath* download_save_dir, |
| 521 bool* skip_dir_check) { | 491 bool* skip_dir_check) { |
| 522 Profile* profile = Profile::FromBrowserContext(browser_context); | 492 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 523 PrefService* prefs = profile->GetPrefs(); | 493 PrefService* prefs = profile->GetPrefs(); |
| 524 | 494 |
| 525 // Check whether the preference has the preferred directory for saving file. | 495 // Check whether the preference has the preferred directory for saving file. |
| 526 // If not, initialize it with default directory. | 496 // If not, initialize it with default directory. |
| 527 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { | 497 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 DownloadItem* download = download_manager_->GetDownload(download_id); | 600 DownloadItem* download = download_manager_->GetDownload(download_id); |
| 631 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) | 601 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) |
| 632 return; | 602 return; |
| 633 | 603 |
| 634 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) | 604 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) |
| 635 << " verdict = " << result; | 605 << " verdict = " << result; |
| 636 content::DownloadDangerType danger_type = download->GetDangerType(); | 606 content::DownloadDangerType danger_type = download->GetDangerType(); |
| 637 if (result != DownloadProtectionService::SAFE) | 607 if (result != DownloadProtectionService::SAFE) |
| 638 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; | 608 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; |
| 639 | 609 |
| 640 download_history_->CheckVisitedReferrerBefore( | 610 // HistoryServiceFactory redirects incognito profiles to on-record profiles. |
| 641 download_id, download->GetReferrerUrl(), | 611 HistoryService* history = HistoryServiceFactory::GetForProfile( |
| 642 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, | 612 profile_, Profile::EXPLICIT_ACCESS); |
| 643 this, download_id, callback, danger_type)); | 613 if (!history || !download->GetReferrerUrl().is_valid()) { |
| 614 // If the original profile doesn't have a HistoryService or the referrer url |
| 615 // is invalid, then give up and assume the referrer has not been visited |
| 616 // before. There's no history for on-record profiles in unit_tests, for |
| 617 // example. |
| 618 CheckVisitedReferrerBeforeDone(download_id, callback, danger_type, false); |
| 619 return; |
| 620 } |
| 621 history->GetVisibleVisitCountToHost( |
| 622 download->GetReferrerUrl(), &history_consumer_, |
| 623 base::Bind(&VisitCountsToVisitedBefore, base::Bind( |
| 624 &ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, |
| 625 this, download_id, callback, danger_type))); |
| 644 } | 626 } |
| 645 | 627 |
| 646 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( | 628 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( |
| 647 int32 download_id, | 629 int32 download_id, |
| 648 DownloadProtectionService::DownloadCheckResult result) { | 630 DownloadProtectionService::DownloadCheckResult result) { |
| 649 DownloadItem* item = download_manager_->GetDownload(download_id); | 631 DownloadItem* item = download_manager_->GetDownload(download_id); |
| 650 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) | 632 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) |
| 651 return; | 633 return; |
| 652 | 634 |
| 653 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) | 635 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 785 } else { | 767 } else { |
| 786 // Currently we only expect this case. | 768 // Currently we only expect this case. |
| 787 DCHECK_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, danger_type); | 769 DCHECK_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, danger_type); |
| 788 } | 770 } |
| 789 | 771 |
| 790 #if defined (OS_CHROMEOS) | 772 #if defined (OS_CHROMEOS) |
| 791 drive::DriveDownloadObserver::SubstituteDriveDownloadPath( | 773 drive::DriveDownloadObserver::SubstituteDriveDownloadPath( |
| 792 profile_, suggested_path, download, | 774 profile_, suggested_path, download, |
| 793 base::Bind( | 775 base::Bind( |
| 794 &ChromeDownloadManagerDelegate::SubstituteDriveDownloadPathCallback, | 776 &ChromeDownloadManagerDelegate::SubstituteDriveDownloadPathCallback, |
| 795 this, download->GetId(), callback, should_prompt, is_forced_path, | 777 this, download->GetId(), callback, should_prompt, |
| 796 danger_type)); | 778 is_forced_path, danger_type)); |
| 797 #else | 779 #else |
| 798 GetReservedPath( | 780 GetReservedPath( |
| 799 *download, suggested_path, download_prefs_->DownloadPath(), | 781 *download, suggested_path, download_prefs_->DownloadPath(), |
| 800 !is_forced_path, | 782 !is_forced_path, |
| 801 base::Bind(&ChromeDownloadManagerDelegate::OnPathReservationAvailable, | 783 base::Bind(&ChromeDownloadManagerDelegate::OnPathReservationAvailable, |
| 802 this, download->GetId(), callback, should_prompt, | 784 this, download->GetId(), callback, should_prompt, |
| 803 danger_type)); | 785 danger_type)); |
| 804 #endif | 786 #endif |
| 805 } | 787 } |
| 806 | 788 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a | 860 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a |
| 879 // directory to persist. Or perhaps, if the Drive path | 861 // directory to persist. Or perhaps, if the Drive path |
| 880 // substitution logic is moved here, then we would have a | 862 // substitution logic is moved here, then we would have a |
| 881 // persistable path after the DownloadFilePicker is done. | 863 // persistable path after the DownloadFilePicker is done. |
| 882 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && | 864 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && |
| 883 !download->IsTemporary()) | 865 !download->IsTemporary()) |
| 884 last_download_path_ = target_path.DirName(); | 866 last_download_path_ = target_path.DirName(); |
| 885 } | 867 } |
| 886 callback.Run(target_path, disposition, danger_type, intermediate_path); | 868 callback.Run(target_path, disposition, danger_type, intermediate_path); |
| 887 } | 869 } |
| 888 | |
| 889 void ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore( | |
| 890 int32 download_id, int64 db_handle) { | |
| 891 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | |
| 892 // call this function with an invalid |db_handle|. For instance, this can | |
| 893 // happen when the history database is offline. We cannot have multiple | |
| 894 // DownloadItems with the same invalid db_handle, so we need to assign a | |
| 895 // unique |db_handle| here. | |
| 896 if (db_handle == DownloadItem::kUninitializedHandle) | |
| 897 db_handle = download_history_->GetNextFakeDbHandle(); | |
| 898 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | |
| 899 } | |
| OLD | NEW |