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.h" |
| 33 #include "chrome/browser/history/history_service_factory.h" |
32 #include "chrome/browser/intents/web_intents_util.h" | 34 #include "chrome/browser/intents/web_intents_util.h" |
33 #include "chrome/browser/prefs/pref_service.h" | 35 #include "chrome/browser/prefs/pref_service.h" |
34 #include "chrome/browser/profiles/profile.h" | 36 #include "chrome/browser/profiles/profile.h" |
35 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 37 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
36 #include "chrome/browser/ui/browser_tabstrip.h" | 38 #include "chrome/browser/ui/browser_tabstrip.h" |
37 #include "chrome/common/chrome_notification_types.h" | 39 #include "chrome/common/chrome_notification_types.h" |
38 #include "chrome/common/extensions/feature_switch.h" | 40 #include "chrome/common/extensions/feature_switch.h" |
39 #include "chrome/common/extensions/user_script.h" | 41 #include "chrome/common/extensions/user_script.h" |
40 #include "chrome/common/pref_names.h" | 42 #include "chrome/common/pref_names.h" |
41 #include "content/public/browser/download_item.h" | 43 #include "content/public/browser/download_item.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 } | 135 } |
134 | 136 |
135 // Called when the web intents dispatch has completed. Deletes the |file_path|. | 137 // Called when the web intents dispatch has completed. Deletes the |file_path|. |
136 void OnWebIntentDispatchCompleted( | 138 void OnWebIntentDispatchCompleted( |
137 const FilePath& file_path, | 139 const FilePath& file_path, |
138 webkit_glue::WebIntentReplyType intent_reply) { | 140 webkit_glue::WebIntentReplyType intent_reply) { |
139 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 141 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
140 base::Bind(&DeleteFile, file_path)); | 142 base::Bind(&DeleteFile, file_path)); |
141 } | 143 } |
142 | 144 |
| 145 typedef base::Callback<void(bool)> VisitedBeforeCallback; |
| 146 |
| 147 // Condenses the results from HistoryService::GetVisibleVisitCountToHost() to a |
| 148 // single bool so that VisitedBeforeCallback can curry up to 5 other parameters |
| 149 // without a struct. |
| 150 void VisitCountsToVisitedBefore( |
| 151 const VisitedBeforeCallback& callback, |
| 152 HistoryService::Handle unused_handle, |
| 153 bool found_visits, |
| 154 int count, |
| 155 base::Time first_visit) { |
| 156 callback.Run(found_visits && count && |
| 157 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); |
| 158 } |
| 159 |
143 } // namespace | 160 } // namespace |
144 | 161 |
145 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 162 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) |
146 : profile_(profile), | 163 : profile_(profile), |
147 next_download_id_(0), | 164 next_download_id_(0), |
148 download_prefs_(new DownloadPrefs(profile)) { | 165 download_prefs_(new DownloadPrefs(profile)) { |
149 } | 166 } |
150 | 167 |
151 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 168 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { |
152 } | 169 } |
153 | 170 |
154 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { | 171 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { |
155 download_manager_ = dm; | 172 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) | 173 #if !defined(OS_ANDROID) |
169 extension_event_router_.reset(new ExtensionDownloadsEventRouter( | 174 extension_event_router_.reset(new ExtensionDownloadsEventRouter( |
170 profile_, download_manager_)); | 175 profile_, download_manager_)); |
171 #endif | 176 #endif |
172 } | 177 } |
173 | 178 |
174 void ChromeDownloadManagerDelegate::Shutdown() { | 179 void ChromeDownloadManagerDelegate::Shutdown() { |
175 download_history_.reset(); | |
176 download_prefs_.reset(); | 180 download_prefs_.reset(); |
177 #if !defined(OS_ANDROID) | 181 #if !defined(OS_ANDROID) |
178 extension_event_router_.reset(); | 182 extension_event_router_.reset(); |
179 #endif | 183 #endif |
180 } | 184 } |
181 | 185 |
182 DownloadId ChromeDownloadManagerDelegate::GetNextId() { | 186 DownloadId ChromeDownloadManagerDelegate::GetNextId() { |
183 if (!profile_->IsOffTheRecord()) | 187 if (!profile_->IsOffTheRecord()) |
184 return DownloadId(this, next_download_id_++); | 188 return DownloadId(this, next_download_id_++); |
185 | 189 |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 | 476 |
473 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 477 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
474 #if defined(ENABLE_SAFE_BROWSING) | 478 #if defined(ENABLE_SAFE_BROWSING) |
475 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 479 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
476 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 480 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
477 #else | 481 #else |
478 return false; | 482 return false; |
479 #endif | 483 #endif |
480 } | 484 } |
481 | 485 |
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, | 486 void ChromeDownloadManagerDelegate::GetSaveDir(BrowserContext* browser_context, |
519 FilePath* website_save_dir, | 487 FilePath* website_save_dir, |
520 FilePath* download_save_dir, | 488 FilePath* download_save_dir, |
521 bool* skip_dir_check) { | 489 bool* skip_dir_check) { |
522 Profile* profile = Profile::FromBrowserContext(browser_context); | 490 Profile* profile = Profile::FromBrowserContext(browser_context); |
523 PrefService* prefs = profile->GetPrefs(); | 491 PrefService* prefs = profile->GetPrefs(); |
524 | 492 |
525 // Check whether the preference has the preferred directory for saving file. | 493 // Check whether the preference has the preferred directory for saving file. |
526 // If not, initialize it with default directory. | 494 // If not, initialize it with default directory. |
527 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { | 495 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); | 598 DownloadItem* download = download_manager_->GetDownload(download_id); |
631 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) | 599 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) |
632 return; | 600 return; |
633 | 601 |
634 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) | 602 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) |
635 << " verdict = " << result; | 603 << " verdict = " << result; |
636 content::DownloadDangerType danger_type = download->GetDangerType(); | 604 content::DownloadDangerType danger_type = download->GetDangerType(); |
637 if (result != DownloadProtectionService::SAFE) | 605 if (result != DownloadProtectionService::SAFE) |
638 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; | 606 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; |
639 | 607 |
640 download_history_->CheckVisitedReferrerBefore( | 608 // HistoryServiceFactory redirects incognito profiles to on-record profiles. |
641 download_id, download->GetReferrerUrl(), | 609 HistoryService* history = HistoryServiceFactory::GetForProfile( |
642 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, | 610 profile_, Profile::EXPLICIT_ACCESS); |
643 this, download_id, callback, danger_type)); | 611 if (!history || !download->GetReferrerUrl().is_valid()) { |
| 612 // If the original profile doesn't have a HistoryService or the referrer url |
| 613 // is invalid, then give up and assume the referrer has not been visited |
| 614 // before. There's no history for on-record profiles in unit_tests, for |
| 615 // example. |
| 616 CheckVisitedReferrerBeforeDone(download_id, callback, danger_type, false); |
| 617 return; |
| 618 } |
| 619 history->GetVisibleVisitCountToHost( |
| 620 download->GetReferrerUrl(), &history_consumer_, |
| 621 base::Bind(&VisitCountsToVisitedBefore, base::Bind( |
| 622 &ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, |
| 623 this, download_id, callback, danger_type))); |
644 } | 624 } |
645 | 625 |
646 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( | 626 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( |
647 int32 download_id, | 627 int32 download_id, |
648 DownloadProtectionService::DownloadCheckResult result) { | 628 DownloadProtectionService::DownloadCheckResult result) { |
649 DownloadItem* item = download_manager_->GetDownload(download_id); | 629 DownloadItem* item = download_manager_->GetDownload(download_id); |
650 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) | 630 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) |
651 return; | 631 return; |
652 | 632 |
653 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) | 633 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
878 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a | 858 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a |
879 // directory to persist. Or perhaps, if the Drive path | 859 // directory to persist. Or perhaps, if the Drive path |
880 // substitution logic is moved here, then we would have a | 860 // substitution logic is moved here, then we would have a |
881 // persistable path after the DownloadFilePicker is done. | 861 // persistable path after the DownloadFilePicker is done. |
882 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && | 862 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && |
883 !download->IsTemporary()) | 863 !download->IsTemporary()) |
884 last_download_path_ = target_path.DirName(); | 864 last_download_path_ = target_path.DirName(); |
885 } | 865 } |
886 callback.Run(target_path, disposition, danger_type, intermediate_path); | 866 callback.Run(target_path, disposition, danger_type, intermediate_path); |
887 } | 867 } |
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 |