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

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

Issue 10665049: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 4 months 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/browser_process.h" 17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/download/download_completion_blocker.h" 18 #include "chrome/browser/download/download_completion_blocker.h"
19 #include "chrome/browser/download/download_crx_util.h" 19 #include "chrome/browser/download/download_crx_util.h"
20 #include "chrome/browser/download/download_extensions.h" 20 #include "chrome/browser/download/download_extensions.h"
21 #include "chrome/browser/download/download_file_picker.h" 21 #include "chrome/browser/download/download_file_picker.h"
22 #include "chrome/browser/download/download_history.h" 22 #include "chrome/browser/download/download_history.h"
23 #include "chrome/browser/download/download_path_reservation_tracker.h"
23 #include "chrome/browser/download/download_prefs.h" 24 #include "chrome/browser/download/download_prefs.h"
24 #include "chrome/browser/download/download_path_reservation_tracker.h"
25 #include "chrome/browser/download/download_status_updater.h" 25 #include "chrome/browser/download/download_status_updater.h"
26 #include "chrome/browser/download/download_util.h" 26 #include "chrome/browser/download/download_util.h"
27 #include "chrome/browser/download/save_package_file_picker.h" 27 #include "chrome/browser/download/save_package_file_picker.h"
28 #include "chrome/browser/extensions/api/downloads/downloads_api.h" 28 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
29 #include "chrome/browser/extensions/crx_installer.h" 29 #include "chrome/browser/extensions/crx_installer.h"
30 #include "chrome/browser/extensions/extension_service.h" 30 #include "chrome/browser/extensions/extension_service.h"
31 #include "chrome/browser/history/history_service_factory.h"
31 #include "chrome/browser/prefs/pref_member.h" 32 #include "chrome/browser/prefs/pref_member.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"
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 : profile_(profile), 125 : profile_(profile),
125 next_download_id_(0), 126 next_download_id_(0),
126 download_prefs_(new DownloadPrefs(profile)) { 127 download_prefs_(new DownloadPrefs(profile)) {
127 } 128 }
128 129
129 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { 130 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() {
130 } 131 }
131 132
132 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { 133 void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) {
133 download_manager_ = dm; 134 download_manager_ = dm;
134 download_history_.reset(new DownloadHistory(profile_));
135 if (!profile_->IsOffTheRecord()) {
136 // DownloadManager should not be RefCountedThreadSafe.
137 // ChromeDownloadManagerDelegate outlives DownloadManager, and
138 // DownloadHistory uses a scoped canceller to cancel tasks when it is
139 // deleted. Almost all callbacks to DownloadManager should use weak pointers
140 // or bounce off a container object that uses ManagerGoingDown() to simulate
141 // a weak pointer.
142 download_history_->Load(
143 base::Bind(&DownloadManager::OnPersistentStoreQueryComplete,
144 download_manager_));
145 }
146 #if !defined(OS_ANDROID) 135 #if !defined(OS_ANDROID)
147 extension_event_router_.reset(new ExtensionDownloadsEventRouter( 136 extension_event_router_.reset(new ExtensionDownloadsEventRouter(
148 profile_, download_manager_)); 137 profile_, download_manager_));
149 #endif 138 #endif
139
140 if (!profile_->IsOffTheRecord()) {
141 HistoryService* hs = HistoryServiceFactory::GetForProfile(
142 profile_, Profile::EXPLICIT_ACCESS);
143 if (hs) {
144 download_history_.reset(new DownloadHistory(
145 download_manager_, new HistoryServiceDownloadAdapter(hs)));
146 }
147 }
150 } 148 }
151 149
152 void ChromeDownloadManagerDelegate::Shutdown() { 150 void ChromeDownloadManagerDelegate::Shutdown() {
153 download_history_.reset(); 151 download_history_.reset();
154 download_prefs_.reset(); 152 download_prefs_.reset();
155 #if !defined(OS_ANDROID) 153 #if !defined(OS_ANDROID)
156 extension_event_router_.reset(); 154 extension_event_router_.reset();
157 #endif 155 #endif
158 } 156 }
159 157
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 442
445 bool ChromeDownloadManagerDelegate::GenerateFileHash() { 443 bool ChromeDownloadManagerDelegate::GenerateFileHash() {
446 #if defined(ENABLE_SAFE_BROWSING) 444 #if defined(ENABLE_SAFE_BROWSING)
447 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && 445 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) &&
448 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); 446 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded();
449 #else 447 #else
450 return false; 448 return false;
451 #endif 449 #endif
452 } 450 }
453 451
454 void ChromeDownloadManagerDelegate::AddItemToPersistentStore(
455 DownloadItem* item) {
456 if (profile_->IsOffTheRecord()) {
457 OnItemAddedToPersistentStore(
458 item->GetId(), download_history_->GetNextFakeDbHandle());
459 return;
460 }
461 download_history_->AddEntry(item,
462 base::Bind(&ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore,
463 this));
464 }
465
466 void ChromeDownloadManagerDelegate::UpdateItemInPersistentStore(
467 DownloadItem* item) {
468 download_history_->UpdateEntry(item);
469 }
470
471 void ChromeDownloadManagerDelegate::UpdatePathForItemInPersistentStore(
472 DownloadItem* item,
473 const FilePath& new_path) {
474 download_history_->UpdateDownloadPath(item, new_path);
475 }
476
477 void ChromeDownloadManagerDelegate::RemoveItemFromPersistentStore(
478 DownloadItem* item) {
479 download_history_->RemoveEntry(item);
480 }
481
482 void ChromeDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween(
483 base::Time remove_begin,
484 base::Time remove_end) {
485 if (profile_->IsOffTheRecord())
486 return;
487 download_history_->RemoveEntriesBetween(remove_begin, remove_end);
488 }
489
490 void ChromeDownloadManagerDelegate::GetSaveDir(WebContents* web_contents, 452 void ChromeDownloadManagerDelegate::GetSaveDir(WebContents* web_contents,
491 FilePath* website_save_dir, 453 FilePath* website_save_dir,
492 FilePath* download_save_dir, 454 FilePath* download_save_dir,
493 bool* skip_dir_check) { 455 bool* skip_dir_check) {
494 Profile* profile = 456 Profile* profile =
495 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 457 Profile::FromBrowserContext(web_contents->GetBrowserContext());
496 PrefService* prefs = profile->GetPrefs(); 458 PrefService* prefs = profile->GetPrefs();
497 459
498 // Check whether the preference has the preferred directory for saving file. 460 // Check whether the preference has the preferred directory for saving file.
499 // If not, initialize it with default directory. 461 // If not, initialize it with default directory.
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 download_manager_->GetActiveDownloadItem(download_id); 566 download_manager_->GetActiveDownloadItem(download_id);
605 if (!download) 567 if (!download)
606 return; 568 return;
607 569
608 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) 570 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false)
609 << " verdict = " << result; 571 << " verdict = " << result;
610 content::DownloadDangerType danger_type = download->GetDangerType(); 572 content::DownloadDangerType danger_type = download->GetDangerType();
611 if (result != DownloadProtectionService::SAFE) 573 if (result != DownloadProtectionService::SAFE)
612 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; 574 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL;
613 575
614 download_history_->CheckVisitedReferrerBefore( 576 if (download_history_.get()) {
615 download_id, download->GetReferrerUrl(), 577 download_history_->CheckVisitedReferrerBefore(
616 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, 578 download_id, download->GetReferrerUrl(), base::Bind(
617 this, download_id, callback, danger_type)); 579 &ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone,
580 base::Unretained(this), download_id, callback, danger_type));
581 } else {
582 CheckVisitedReferrerBeforeDone(download_id, callback, danger_type, false);
583 }
618 } 584 }
619 585
620 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( 586 void ChromeDownloadManagerDelegate::CheckClientDownloadDone(
621 int32 download_id, 587 int32 download_id,
622 DownloadProtectionService::DownloadCheckResult result) { 588 DownloadProtectionService::DownloadCheckResult result) {
623 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id); 589 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id);
624 if (!item) 590 if (!item)
625 return; 591 return;
626 592
627 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) 593 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false)
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a 810 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a
845 // directory to persist. Or perhaps, if the GData path 811 // directory to persist. Or perhaps, if the GData path
846 // substitution logic is moved here, then we would have a 812 // substitution logic is moved here, then we would have a
847 // persistable path after the DownloadFilePicker is done. 813 // persistable path after the DownloadFilePicker is done.
848 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && 814 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT &&
849 !download->IsTemporary()) 815 !download->IsTemporary())
850 last_download_path_ = target_path.DirName(); 816 last_download_path_ = target_path.DirName();
851 } 817 }
852 callback.Run(target_path, disposition, danger_type, intermediate_path); 818 callback.Run(target_path, disposition, danger_type, intermediate_path);
853 } 819 }
854
855 void ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore(
856 int32 download_id, int64 db_handle) {
857 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
858 // call this function with an invalid |db_handle|. For instance, this can
859 // happen when the history database is offline. We cannot have multiple
860 // DownloadItems with the same invalid db_handle, so we need to assign a
861 // unique |db_handle| here.
862 if (db_handle == DownloadItem::kUninitializedHandle)
863 db_handle = download_history_->GetNextFakeDbHandle();
864 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle);
865 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698