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

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 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 421
424 bool ChromeDownloadManagerDelegate::GenerateFileHash() { 422 bool ChromeDownloadManagerDelegate::GenerateFileHash() {
425 #if defined(ENABLE_SAFE_BROWSING) 423 #if defined(ENABLE_SAFE_BROWSING)
426 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && 424 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) &&
427 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); 425 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded();
428 #else 426 #else
429 return false; 427 return false;
430 #endif 428 #endif
431 } 429 }
432 430
433 void ChromeDownloadManagerDelegate::AddItemToPersistentStore(
434 DownloadItem* item) {
435 if (profile_->IsOffTheRecord()) {
436 OnItemAddedToPersistentStore(
437 item->GetId(), download_history_->GetNextFakeDbHandle());
438 return;
439 }
440 download_history_->AddEntry(item,
441 base::Bind(&ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore,
442 this));
443 }
444
445 void ChromeDownloadManagerDelegate::UpdateItemInPersistentStore(
446 DownloadItem* item) {
447 download_history_->UpdateEntry(item);
448 }
449
450 void ChromeDownloadManagerDelegate::UpdatePathForItemInPersistentStore(
451 DownloadItem* item,
452 const FilePath& new_path) {
453 download_history_->UpdateDownloadPath(item, new_path);
454 }
455
456 void ChromeDownloadManagerDelegate::RemoveItemFromPersistentStore(
457 DownloadItem* item) {
458 download_history_->RemoveEntry(item);
459 }
460
461 void ChromeDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween(
462 base::Time remove_begin,
463 base::Time remove_end) {
464 if (profile_->IsOffTheRecord())
465 return;
466 download_history_->RemoveEntriesBetween(remove_begin, remove_end);
467 }
468
469 void ChromeDownloadManagerDelegate::GetSaveDir(WebContents* web_contents, 431 void ChromeDownloadManagerDelegate::GetSaveDir(WebContents* web_contents,
470 FilePath* website_save_dir, 432 FilePath* website_save_dir,
471 FilePath* download_save_dir, 433 FilePath* download_save_dir,
472 bool* skip_dir_check) { 434 bool* skip_dir_check) {
473 Profile* profile = 435 Profile* profile =
474 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 436 Profile::FromBrowserContext(web_contents->GetBrowserContext());
475 PrefService* prefs = profile->GetPrefs(); 437 PrefService* prefs = profile->GetPrefs();
476 438
477 // Check whether the preference has the preferred directory for saving file. 439 // Check whether the preference has the preferred directory for saving file.
478 // If not, initialize it with default directory. 440 // If not, initialize it with default directory.
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 download_manager_->GetActiveDownloadItem(download_id); 545 download_manager_->GetActiveDownloadItem(download_id);
584 if (!download) 546 if (!download)
585 return; 547 return;
586 548
587 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false) 549 VLOG(2) << __FUNCTION__ << "() download = " << download->DebugString(false)
588 << " verdict = " << result; 550 << " verdict = " << result;
589 content::DownloadDangerType danger_type = download->GetDangerType(); 551 content::DownloadDangerType danger_type = download->GetDangerType();
590 if (result != DownloadProtectionService::SAFE) 552 if (result != DownloadProtectionService::SAFE)
591 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; 553 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL;
592 554
593 download_history_->CheckVisitedReferrerBefore( 555 if (download_history_.get()) {
594 download_id, download->GetReferrerUrl(), 556 download_history_->CheckVisitedReferrerBefore(
595 base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, 557 download_id, download->GetReferrerUrl(), base::Bind(
596 this, download_id, callback, danger_type)); 558 &ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone,
559 base::Unretained(this), download_id, callback, danger_type));
560 } else {
561 CheckVisitedReferrerBeforeDone(download_id, callback, danger_type, false);
562 }
597 } 563 }
598 564
599 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( 565 void ChromeDownloadManagerDelegate::CheckClientDownloadDone(
600 int32 download_id, 566 int32 download_id,
601 DownloadProtectionService::DownloadCheckResult result) { 567 DownloadProtectionService::DownloadCheckResult result) {
602 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id); 568 DownloadItem* item = download_manager_->GetActiveDownloadItem(download_id);
603 if (!item) 569 if (!item)
604 return; 570 return;
605 571
606 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) 572 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false)
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
823 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a 789 // TODO(asanka): This logic is a hack. DownloadFilePicker should give us a
824 // directory to persist. Or perhaps, if the GData path 790 // directory to persist. Or perhaps, if the GData path
825 // substitution logic is moved here, then we would have a 791 // substitution logic is moved here, then we would have a
826 // persistable path after the DownloadFilePicker is done. 792 // persistable path after the DownloadFilePicker is done.
827 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT && 793 if (disposition == DownloadItem::TARGET_DISPOSITION_PROMPT &&
828 !download->IsTemporary()) 794 !download->IsTemporary())
829 last_download_path_ = target_path.DirName(); 795 last_download_path_ = target_path.DirName();
830 } 796 }
831 callback.Run(target_path, disposition, danger_type, intermediate_path); 797 callback.Run(target_path, disposition, danger_type, intermediate_path);
832 } 798 }
833
834 void ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore(
835 int32 download_id, int64 db_handle) {
836 // It's not immediately obvious, but HistoryBackend::CreateDownload() can
837 // call this function with an invalid |db_handle|. For instance, this can
838 // happen when the history database is offline. We cannot have multiple
839 // DownloadItems with the same invalid db_handle, so we need to assign a
840 // unique |db_handle| here.
841 if (db_handle == DownloadItem::kUninitializedHandle)
842 db_handle = download_history_->GetNextFakeDbHandle();
843 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle);
844 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698