Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: chrome/browser/download/chrome_download_manager_delegate.cc

Issue 10915180: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r165669 Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 : profile_(profile), 150 : profile_(profile),
147 next_download_id_(0), 151 next_download_id_(0),
148 download_prefs_(new DownloadPrefs(profile)) { 152 download_prefs_(new DownloadPrefs(profile)) {
149 } 153 }
150 154
151 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { 155 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() {
152 } 156 }
153 157
154 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { 158 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) {
155 download_manager_ = dm; 159 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) 160 #if !defined(OS_ANDROID)
169 extension_event_router_.reset(new ExtensionDownloadsEventRouter( 161 extension_event_router_.reset(new ExtensionDownloadsEventRouter(
170 profile_, download_manager_)); 162 profile_, download_manager_));
171 #endif 163 #endif
164
165 if (!profile_->IsOffTheRecord()) {
166 HistoryService* hs = HistoryServiceFactory::GetForProfile(
167 profile_, Profile::EXPLICIT_ACCESS);
168 if (hs)
169 download_history_.reset(new DownloadHistory(download_manager_, hs));
170 }
171 }
172
173 void ChromeDownloadManagerDelegate::AddHistoryObserver(
174 DownloadHistory::Observer* observer) {
175 if (download_history_.get())
176 download_history_->AddObserver(observer);
177 }
178
179 void ChromeDownloadManagerDelegate::RemoveHistoryObserver(
180 DownloadHistory::Observer* observer) {
181 if (download_history_.get())
182 download_history_->RemoveObserver(observer);
172 } 183 }
173 184
174 void ChromeDownloadManagerDelegate::Shutdown() { 185 void ChromeDownloadManagerDelegate::Shutdown() {
175 download_history_.reset(); 186 download_history_.reset();
176 download_prefs_.reset(); 187 download_prefs_.reset();
177 #if !defined(OS_ANDROID) 188 #if !defined(OS_ANDROID)
178 extension_event_router_.reset(); 189 extension_event_router_.reset();
179 #endif 190 #endif
180 } 191 }
181 192
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 496
486 bool ChromeDownloadManagerDelegate::GenerateFileHash() { 497 bool ChromeDownloadManagerDelegate::GenerateFileHash() {
487 #if defined(ENABLE_SAFE_BROWSING) 498 #if defined(ENABLE_SAFE_BROWSING)
488 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && 499 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) &&
489 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); 500 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded();
490 #else 501 #else
491 return false; 502 return false;
492 #endif 503 #endif
493 } 504 }
494 505
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, 506 void ChromeDownloadManagerDelegate::GetSaveDir(BrowserContext* browser_context,
532 FilePath* website_save_dir, 507 FilePath* website_save_dir,
533 FilePath* download_save_dir, 508 FilePath* download_save_dir,
534 bool* skip_dir_check) { 509 bool* skip_dir_check) {
535 Profile* profile = Profile::FromBrowserContext(browser_context); 510 Profile* profile = Profile::FromBrowserContext(browser_context);
536 PrefService* prefs = profile->GetPrefs(); 511 PrefService* prefs = profile->GetPrefs();
537 512
538 // Check whether the preference has the preferred directory for saving file. 513 // Check whether the preference has the preferred directory for saving file.
539 // If not, initialize it with default directory. 514 // If not, initialize it with default directory.
540 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { 515 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 DownloadItem* download = download_manager_->GetDownload(download_id); 618 DownloadItem* download = download_manager_->GetDownload(download_id);
644 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS)) 619 if (!download || (download->GetState() != DownloadItem::IN_PROGRESS))
645 return; 620 return;
646 621
647 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) 622 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false)
648 << " verdict = " << result; 623 << " verdict = " << result;
649 content::DownloadDangerType danger_type = download->GetDangerType(); 624 content::DownloadDangerType danger_type = download->GetDangerType();
650 if (result != DownloadProtectionService::SAFE) 625 if (result != DownloadProtectionService::SAFE)
651 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; 626 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL;
652 627
653 download_history_->CheckVisitedReferrerBefore( 628 DownloadHistory* history = download_history_.get();
654 download_id, download->GetReferrerUrl(), 629 if (!history && profile_->IsOffTheRecord()) {
655 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, 630 // If there's no DownloadHistory because this profile IsOffTheRecord, then
656 this, download_id, callback, danger_type)); 631 // go to the original profile and ask its ChromeDownloadManagerDelegate to
632 // check the on-record visited db.
633 history = DownloadServiceFactory::GetForProfile(
634 profile_->GetOriginalProfile())->GetDownloadManagerDelegate()
635 ->download_history_.get();
636 }
637 DCHECK(history);
638 if (!history) {
639 // If there's no history but the profile is on-record, or if this profile is
640 // off-record and the original profile doesn't have a DownloadHistory, then
641 // give up and assume the referrer has not been visited before.
642 CheckVisitedReferrerBeforeDone(download_id, callback, danger_type, false);
643 return;
644 }
645 history->CheckVisitedReferrerBefore(
646 download->GetReferrerUrl(), base::Bind(
647 &ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone,
648 this, download_id, callback, danger_type));
657 } 649 }
658 650
659 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( 651 void ChromeDownloadManagerDelegate::CheckClientDownloadDone(
660 int32 download_id, 652 int32 download_id,
661 DownloadProtectionService::DownloadCheckResult result) { 653 DownloadProtectionService::DownloadCheckResult result) {
662 DownloadItem* item = download_manager_->GetDownload(download_id); 654 DownloadItem* item = download_manager_->GetDownload(download_id);
663 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) 655 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS))
664 return; 656 return;
665 657
666 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) 658 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false)
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a 886 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a
895 // directory to persist. Or perhaps, if the Drive path 887 // directory to persist. Or perhaps, if the Drive path
896 // substitution logic is moved here, then we would have a 888 // substitution logic is moved here, then we would have a
897 // persistable path after the DownloadFilePicker is done. 889 // persistable path after the DownloadFilePicker is done.
898 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && 890 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT &&
899 !download->IsTemporary()) 891 !download->IsTemporary())
900 last_download_path_ = target_path.DirName(); 892 last_download_path_ = target_path.DirName();
901 } 893 }
902 callback.Run(target_path, disposition, danger_type, intermediate_path); 894 callback.Run(target_path, disposition, danger_type, intermediate_path);
903 } 895 }
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698