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" |
(...skipping 11 matching lines...) Expand all Loading... |
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_status_updater.h" | 26 #include "chrome/browser/download/download_status_updater.h" |
27 #include "chrome/browser/download/download_util.h" | 27 #include "chrome/browser/download/download_util.h" |
28 #include "chrome/browser/download/save_package_file_picker.h" | 28 #include "chrome/browser/download/save_package_file_picker.h" |
29 #include "chrome/browser/extensions/api/downloads/downloads_api.h" | 29 #include "chrome/browser/extensions/api/downloads/downloads_api.h" |
30 #include "chrome/browser/extensions/crx_installer.h" | 30 #include "chrome/browser/extensions/crx_installer.h" |
31 #include "chrome/browser/extensions/extension_service.h" | 31 #include "chrome/browser/extensions/extension_service.h" |
| 32 #include "chrome/browser/history/history_service_factory.h" |
32 #include "chrome/browser/prefs/pref_service.h" | 33 #include "chrome/browser/prefs/pref_service.h" |
33 #include "chrome/browser/profiles/profile.h" | 34 #include "chrome/browser/profiles/profile.h" |
34 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 35 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
35 #include "chrome/browser/ui/browser_tabstrip.h" | 36 #include "chrome/browser/ui/browser_tabstrip.h" |
36 #include "chrome/common/chrome_notification_types.h" | 37 #include "chrome/common/chrome_notification_types.h" |
37 #include "chrome/common/extensions/extension_switch_utils.h" | 38 #include "chrome/common/extensions/extension_switch_utils.h" |
38 #include "chrome/common/extensions/user_script.h" | 39 #include "chrome/common/extensions/user_script.h" |
39 #include "chrome/common/pref_names.h" | 40 #include "chrome/common/pref_names.h" |
40 #include "content/public/browser/download_item.h" | 41 #include "content/public/browser/download_item.h" |
41 #include "content/public/browser/download_manager.h" | 42 #include "content/public/browser/download_manager.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 : profile_(profile), | 145 : profile_(profile), |
145 next_download_id_(0), | 146 next_download_id_(0), |
146 download_prefs_(new DownloadPrefs(profile)) { | 147 download_prefs_(new DownloadPrefs(profile)) { |
147 } | 148 } |
148 | 149 |
149 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 150 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
150 } | 151 } |
151 | 152 |
152 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { | 153 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { |
153 download_manager_ = dm; | 154 download_manager_ = dm; |
154 download_history_.reset(new DownloadHistory(profile_)); | |
155 if (!profile_->IsOffTheRecord()) { | |
156 // DownloadManager should not be RefCountedThreadSafe. | |
157 // ChromeDownloadManagerDelegate outlives DownloadManager, and | |
158 // DownloadHistory uses a scoped canceller to cancel tasks when it is | |
159 // deleted. Almost all callbacks to DownloadManager should use weak pointers | |
160 // or bounce off a container object that uses ManagerGoingDown() to simulate | |
161 // a weak pointer. | |
162 download_history_->Load( | |
163 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete, | |
164 download_manager_)); | |
165 } | |
166 #if !defined(OS_ANDROID) | 155 #if !defined(OS_ANDROID) |
167 extension_event_router_.reset(new ExtensionDownloadsEventRouter( | 156 extension_event_router_.reset(new ExtensionDownloadsEventRouter( |
168 profile_, download_manager_)); | 157 profile_, download_manager_)); |
169 #endif | 158 #endif |
| 159 |
| 160 if (!profile_->IsOffTheRecord()) { |
| 161 HistoryService* hs = HistoryServiceFactory::GetForProfile( |
| 162 profile_, Profile::EXPLICIT_ACCESS); |
| 163 if (hs) |
| 164 download_history_.reset(new DownloadHistory(download_manager_, hs)); |
| 165 } |
170 } | 166 } |
171 | 167 |
172 void ChromeDownloadManagerDelegate::Shutdown() { | 168 void ChromeDownloadManagerDelegate::Shutdown() { |
173 download_history_.reset(); | 169 download_history_.reset(); |
174 download_prefs_.reset(); | 170 download_prefs_.reset(); |
175 #if !defined(OS_ANDROID) | 171 #if !defined(OS_ANDROID) |
176 extension_event_router_.reset(); | 172 extension_event_router_.reset(); |
177 #endif | 173 #endif |
178 } | 174 } |
179 | 175 |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 | 464 |
469 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 465 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
470 #if defined(ENABLE_SAFE_BROWSING) | 466 #if defined(ENABLE_SAFE_BROWSING) |
471 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 467 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
472 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 468 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
473 #else | 469 #else |
474 return false; | 470 return false; |
475 #endif | 471 #endif |
476 } | 472 } |
477 | 473 |
478 void ChromeDownloadManagerDelegate::AddItemToPersistentStore( | |
479 DownloadItem* item) { | |
480 if (profile_->IsOffTheRecord()) { | |
481 OnItemAddedToPersistentStore( | |
482 item->GetId(), download_history_->GetNextFakeDbHandle()); | |
483 return; | |
484 } | |
485 download_history_->AddEntry(item, | |
486 base::Bind(&ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore, | |
487 this)); | |
488 } | |
489 | |
490 void ChromeDownloadManagerDelegate::UpdateItemInPersistentStore( | |
491 DownloadItem* item) { | |
492 download_history_->UpdateEntry(item); | |
493 } | |
494 | |
495 void ChromeDownloadManagerDelegate::UpdatePathForItemInPersistentStore( | |
496 DownloadItem* item, | |
497 const FilePath& new_path) { | |
498 download_history_->UpdateDownloadPath(item, new_path); | |
499 } | |
500 | |
501 void ChromeDownloadManagerDelegate::RemoveItemFromPersistentStore( | |
502 DownloadItem* item) { | |
503 download_history_->RemoveEntry(item); | |
504 } | |
505 | |
506 void ChromeDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween( | |
507 base::Time remove_begin, | |
508 base::Time remove_end) { | |
509 if (profile_->IsOffTheRecord()) | |
510 return; | |
511 download_history_->RemoveEntriesBetween(remove_begin, remove_end); | |
512 } | |
513 | |
514 void ChromeDownloadManagerDelegate::GetSaveDir(WebContents* web_contents, | 474 void ChromeDownloadManagerDelegate::GetSaveDir(WebContents* web_contents, |
515 FilePath* website_save_dir, | 475 FilePath* website_save_dir, |
516 FilePath* download_save_dir, | 476 FilePath* download_save_dir, |
517 bool* skip_dir_check) { | 477 bool* skip_dir_check) { |
518 Profile* profile = | 478 Profile* profile = |
519 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 479 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
520 PrefService* prefs = profile->GetPrefs(); | 480 PrefService* prefs = profile->GetPrefs(); |
521 | 481 |
522 // Check whether the preference has the preferred directory for saving file. | 482 // Check whether the preference has the preferred directory for saving file. |
523 // If not, initialize it with default directory. | 483 // If not, initialize it with default directory. |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
628 download_manager_->GetActiveDownloadItem(download_id); | 588 download_manager_->GetActiveDownloadItem(download_id); |
629 if (!download) | 589 if (!download) |
630 return; | 590 return; |
631 | 591 |
632 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) | 592 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) |
633 << " verdict = " << result; | 593 << " verdict = " << result; |
634 content::DownloadDangerType danger_type = download->GetDangerType(); | 594 content::DownloadDangerType danger_type = download->GetDangerType(); |
635 if (result != DownloadProtectionService::SAFE) | 595 if (result != DownloadProtectionService::SAFE) |
636 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; | 596 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; |
637 | 597 |
638 download_history_->CheckVisitedReferrerBefore( | 598 if (download_history_.get()) { |
639 download_id, download->GetReferrerUrl(), | 599 download_history_->CheckVisitedReferrerBefore( |
640 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, | 600 download_id, download->GetReferrerUrl(), base::Bind( |
641 this, download_id, callback, danger_type)); | 601 &ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, |
| 602 this, download_id, callback, danger_type)); |
| 603 } else { |
| 604 CheckVisitedReferrerBeforeDone(download_id, callback, danger_type, false); |
| 605 } |
642 } | 606 } |
643 | 607 |
644 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( | 608 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( |
645 int32 download_id, | 609 int32 download_id, |
646 DownloadProtectionService::DownloadCheckResult result) { | 610 DownloadProtectionService::DownloadCheckResult result) { |
647 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id); | 611 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id); |
648 if (!item) | 612 if (!item) |
649 return; | 613 return; |
650 | 614 |
651 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) | 615 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
876 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a | 840 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a |
877 // directory to persist. Or perhaps, if the Drive path | 841 // directory to persist. Or perhaps, if the Drive path |
878 // substitution logic is moved here, then we would have a | 842 // substitution logic is moved here, then we would have a |
879 // persistable path after the DownloadFilePicker is done. | 843 // persistable path after the DownloadFilePicker is done. |
880 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && | 844 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && |
881 !download->IsTemporary()) | 845 !download->IsTemporary()) |
882 last_download_path_ = target_path.DirName(); | 846 last_download_path_ = target_path.DirName(); |
883 } | 847 } |
884 callback.Run(target_path, disposition, danger_type, intermediate_path); | 848 callback.Run(target_path, disposition, danger_type, intermediate_path); |
885 } | 849 } |
886 | |
887 void ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore( | |
888 int32 download_id, int64 db_handle) { | |
889 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | |
890 // call this function with an invalid |db_handle|. For instance, this can | |
891 // happen when the history database is offline. We cannot have multiple | |
892 // DownloadItems with the same invalid db_handle, so we need to assign a | |
893 // unique |db_handle| here. | |
894 if (db_handle == DownloadItem::kUninitializedHandle) | |
895 db_handle = download_history_->GetNextFakeDbHandle(); | |
896 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | |
897 } | |
OLD | NEW |