| 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/extension_switch_utils.h" | 42 #include "chrome/common/extensions/extension_switch_utils.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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 : profile_(profile), | 149 : profile_(profile), |
| 146 next_download_id_(0), | 150 next_download_id_(0), |
| 147 download_prefs_(new DownloadPrefs(profile)) { | 151 download_prefs_(new DownloadPrefs(profile)) { |
| 148 } | 152 } |
| 149 | 153 |
| 150 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 154 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
| 151 } | 155 } |
| 152 | 156 |
| 153 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { | 157 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { |
| 154 download_manager_ = dm; | 158 download_manager_ = dm; |
| 155 download_history_.reset(new DownloadHistory(profile_)); | |
| 156 if (!profile_->IsOffTheRecord()) { | |
| 157 // DownloadManager should not be RefCountedThreadSafe. | |
| 158 // ChromeDownloadManagerDelegate outlives DownloadManager, and | |
| 159 // DownloadHistory uses a scoped canceller to cancel tasks when it is | |
| 160 // deleted. Almost all callbacks to DownloadManager should use weak pointers | |
| 161 // or bounce off a container object that uses ManagerGoingDown() to simulate | |
| 162 // a weak pointer. | |
| 163 download_history_->Load( | |
| 164 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete, | |
| 165 download_manager_)); | |
| 166 } | |
| 167 #if !defined(OS_ANDROID) | 159 #if !defined(OS_ANDROID) |
| 168 extension_event_router_.reset(new ExtensionDownloadsEventRouter( | 160 extension_event_router_.reset(new ExtensionDownloadsEventRouter( |
| 169 profile_, download_manager_)); | 161 profile_, download_manager_)); |
| 170 #endif | 162 #endif |
| 163 |
| 164 if (!profile_->IsOffTheRecord()) { |
| 165 HistoryService* hs = HistoryServiceFactory::GetForProfile( |
| 166 profile_, Profile::EXPLICIT_ACCESS); |
| 167 if (hs) |
| 168 download_history_.reset(new DownloadHistory(download_manager_, hs)); |
| 169 } |
| 170 } |
| 171 |
| 172 void ChromeDownloadManagerDelegate::AddHistoryObserver( |
| 173 DownloadHistory::Observer* observer) { |
| 174 if (download_history_.get()) |
| 175 download_history_->AddObserver(observer); |
| 176 } |
| 177 |
| 178 void ChromeDownloadManagerDelegate::RemoveHistoryObserver( |
| 179 DownloadHistory::Observer* observer) { |
| 180 if (download_history_.get()) |
| 181 download_history_->RemoveObserver(observer); |
| 171 } | 182 } |
| 172 | 183 |
| 173 void ChromeDownloadManagerDelegate::Shutdown() { | 184 void ChromeDownloadManagerDelegate::Shutdown() { |
| 174 download_history_.reset(); | 185 download_history_.reset(); |
| 175 download_prefs_.reset(); | 186 download_prefs_.reset(); |
| 176 #if !defined(OS_ANDROID) | 187 #if !defined(OS_ANDROID) |
| 177 extension_event_router_.reset(); | 188 extension_event_router_.reset(); |
| 178 #endif | 189 #endif |
| 179 } | 190 } |
| 180 | 191 |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 | 494 |
| 484 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 495 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
| 485 #if defined(ENABLE_SAFE_BROWSING) | 496 #if defined(ENABLE_SAFE_BROWSING) |
| 486 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 497 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
| 487 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 498 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
| 488 #else | 499 #else |
| 489 return false; | 500 return false; |
| 490 #endif | 501 #endif |
| 491 } | 502 } |
| 492 | 503 |
| 493 void ChromeDownloadManagerDelegate::AddItemToPersistentStore( | |
| 494 DownloadItem* item) { | |
| 495 if (profile_->IsOffTheRecord()) { | |
| 496 OnItemAddedToPersistentStore( | |
| 497 item->GetId(), download_history_->GetNextFakeDbHandle()); | |
| 498 return; | |
| 499 } | |
| 500 download_history_->AddEntry(item, | |
| 501 base::Bind(&ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore, | |
| 502 this)); | |
| 503 } | |
| 504 | |
| 505 void ChromeDownloadManagerDelegate::UpdateItemInPersistentStore( | |
| 506 DownloadItem* item) { | |
| 507 download_history_->UpdateEntry(item); | |
| 508 } | |
| 509 | |
| 510 void ChromeDownloadManagerDelegate::UpdatePathForItemInPersistentStore( | |
| 511 DownloadItem* item, | |
| 512 const FilePath& new_path) { | |
| 513 download_history_->UpdateDownloadPath(item, new_path); | |
| 514 } | |
| 515 | |
| 516 void ChromeDownloadManagerDelegate::RemoveItemFromPersistentStore( | |
| 517 DownloadItem* item) { | |
| 518 download_history_->RemoveEntry(item); | |
| 519 } | |
| 520 | |
| 521 void ChromeDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween( | |
| 522 base::Time remove_begin, | |
| 523 base::Time remove_end) { | |
| 524 if (profile_->IsOffTheRecord()) | |
| 525 return; | |
| 526 download_history_->RemoveEntriesBetween(remove_begin, remove_end); | |
| 527 } | |
| 528 | |
| 529 void ChromeDownloadManagerDelegate::GetSaveDir(BrowserContext* browser_context, | 504 void ChromeDownloadManagerDelegate::GetSaveDir(BrowserContext* browser_context, |
| 530 FilePath* website_save_dir, | 505 FilePath* website_save_dir, |
| 531 FilePath* download_save_dir, | 506 FilePath* download_save_dir, |
| 532 bool* skip_dir_check) { | 507 bool* skip_dir_check) { |
| 533 Profile* profile = Profile::FromBrowserContext(browser_context); | 508 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 534 PrefService* prefs = profile->GetPrefs(); | 509 PrefService* prefs = profile->GetPrefs(); |
| 535 | 510 |
| 536 // Check whether the preference has the preferred directory for saving file. | 511 // Check whether the preference has the preferred directory for saving file. |
| 537 // If not, initialize it with default directory. | 512 // If not, initialize it with default directory. |
| 538 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { | 513 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 DownloadItem* download = download_manager_->GetDownload(download_id); | 616 DownloadItem* download = download_manager_->GetDownload(download_id); |
| 642 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) | 617 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) |
| 643 return; | 618 return; |
| 644 | 619 |
| 645 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) | 620 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) |
| 646 << " verdict = " << result; | 621 << " verdict = " << result; |
| 647 content::DownloadDangerType danger_type = download->GetDangerType(); | 622 content::DownloadDangerType danger_type = download->GetDangerType(); |
| 648 if (result != DownloadProtectionService::SAFE) | 623 if (result != DownloadProtectionService::SAFE) |
| 649 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; | 624 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; |
| 650 | 625 |
| 651 download_history_->CheckVisitedReferrerBefore( | 626 DownloadHistory* history = download_history_.get(); |
| 652 download_id, download->GetReferrerUrl(), | 627 if (!history && profile_->IsOffTheRecord()) { |
| 653 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, | 628 // If there's no DownloadHistory because this profile IsOffTheRecord, then |
| 654 this, download_id, callback, danger_type)); | 629 // go to the original profile and ask its ChromeDownloadManagerDelegate to |
| 630 // check the on-record visited db. |
| 631 history = DownloadServiceFactory::GetForProfile( |
| 632 profile_->GetOriginalProfile())->GetDownloadManagerDelegate() |
| 633 ->download_history_.get(); |
| 634 } |
| 635 if (history) { |
| 636 history->CheckVisitedReferrerBefore( |
| 637 download->GetReferrerUrl(), base::Bind( |
| 638 &ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, |
| 639 this, download_id, callback, danger_type)); |
| 640 return; |
| 641 } |
| 642 // If there's no history but the profile is on-record, or if this profile is |
| 643 // off-record and the original profile doesn't have a DownloadHistory, then |
| 644 // give up and assume the referrer has not been visited before. |
| 645 CheckVisitedReferrerBeforeDone(download_id, callback, danger_type, false); |
| 655 } | 646 } |
| 656 | 647 |
| 657 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( | 648 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( |
| 658 int32 download_id, | 649 int32 download_id, |
| 659 DownloadProtectionService::DownloadCheckResult result) { | 650 DownloadProtectionService::DownloadCheckResult result) { |
| 660 DownloadItem* item = download_manager_->GetDownload(download_id); | 651 DownloadItem* item = download_manager_->GetDownload(download_id); |
| 661 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) | 652 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) |
| 662 return; | 653 return; |
| 663 | 654 |
| 664 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) | 655 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 889 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a | 880 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a |
| 890 // directory to persist. Or perhaps, if the Drive path | 881 // directory to persist. Or perhaps, if the Drive path |
| 891 // substitution logic is moved here, then we would have a | 882 // substitution logic is moved here, then we would have a |
| 892 // persistable path after the DownloadFilePicker is done. | 883 // persistable path after the DownloadFilePicker is done. |
| 893 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && | 884 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && |
| 894 !download->IsTemporary()) | 885 !download->IsTemporary()) |
| 895 last_download_path_ = target_path.DirName(); | 886 last_download_path_ = target_path.DirName(); |
| 896 } | 887 } |
| 897 callback.Run(target_path, disposition, danger_type, intermediate_path); | 888 callback.Run(target_path, disposition, danger_type, intermediate_path); |
| 898 } | 889 } |
| 899 | |
| 900 void ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore( | |
| 901 int32 download_id, int64 db_handle) { | |
| 902 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | |
| 903 // call this function with an invalid |db_handle|. For instance, this can | |
| 904 // happen when the history database is offline. We cannot have multiple | |
| 905 // DownloadItems with the same invalid db_handle, so we need to assign a | |
| 906 // unique |db_handle| here. | |
| 907 if (db_handle == DownloadItem::kUninitializedHandle) | |
| 908 db_handle = download_history_->GetNextFakeDbHandle(); | |
| 909 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | |
| 910 } | |
| OLD | NEW |