| Index: chrome/browser/download/chrome_download_manager_delegate.cc | 
| diff --git a/chrome/browser/download/chrome_download_manager_delegate.cc b/chrome/browser/download/chrome_download_manager_delegate.cc | 
| index 98bb3f2dc6069be38f0f1b9f6a23ce3ad8112a86..a142d12738a2608964ecf831a313f03909738d22 100644 | 
| --- a/chrome/browser/download/chrome_download_manager_delegate.cc | 
| +++ b/chrome/browser/download/chrome_download_manager_delegate.cc | 
| @@ -29,6 +29,8 @@ | 
| #include "chrome/browser/extensions/api/downloads/downloads_api.h" | 
| #include "chrome/browser/extensions/crx_installer.h" | 
| #include "chrome/browser/extensions/extension_service.h" | 
| +#include "chrome/browser/history/history.h" | 
| +#include "chrome/browser/history/history_service_factory.h" | 
| #include "chrome/browser/intents/web_intents_util.h" | 
| #include "chrome/browser/prefs/pref_service.h" | 
| #include "chrome/browser/profiles/profile.h" | 
| @@ -140,6 +142,21 @@ void OnWebIntentDispatchCompleted( | 
| base::Bind(&DeleteFile, file_path)); | 
| } | 
|  | 
| +typedef base::Callback<void(bool)> VisitedBeforeCallback; | 
| + | 
| +// Condenses the results from HistoryService::GetVisibleVisitCountToHost() to a | 
| +// single bool so that VisitedBeforeCallback can curry up to 5 other parameters | 
| +// without a struct. | 
| +void VisitCountsToVisitedBefore( | 
| +    const VisitedBeforeCallback& callback, | 
| +    HistoryService::Handle unused_handle, | 
| +    bool found_visits, | 
| +    int count, | 
| +    base::Time first_visit) { | 
| +  callback.Run(found_visits && count && | 
| +      (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); | 
| +} | 
| + | 
| }  // namespace | 
|  | 
| ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) | 
| @@ -153,18 +170,6 @@ ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { | 
|  | 
| void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { | 
| download_manager_ = dm; | 
| -  download_history_.reset(new DownloadHistory(profile_)); | 
| -  if (!profile_->IsOffTheRecord()) { | 
| -    // DownloadManager should not be RefCountedThreadSafe. | 
| -    // ChromeDownloadManagerDelegate outlives DownloadManager, and | 
| -    // DownloadHistory uses a scoped canceller to cancel tasks when it is | 
| -    // deleted. Almost all callbacks to DownloadManager should use weak pointers | 
| -    // or bounce off a container object that uses ManagerGoingDown() to simulate | 
| -    // a weak pointer. | 
| -    download_history_->Load( | 
| -        base::Bind(&DownloadManager::OnPersistentStoreQueryComplete, | 
| -                   download_manager_)); | 
| -  } | 
| #if !defined(OS_ANDROID) | 
| extension_event_router_.reset(new ExtensionDownloadsEventRouter( | 
| profile_, download_manager_)); | 
| @@ -172,7 +177,6 @@ void ChromeDownloadManagerDelegate::SetDownloadManager(DownloadManager* dm) { | 
| } | 
|  | 
| void ChromeDownloadManagerDelegate::Shutdown() { | 
| -  download_history_.reset(); | 
| download_prefs_.reset(); | 
| #if !defined(OS_ANDROID) | 
| extension_event_router_.reset(); | 
| @@ -479,42 +483,6 @@ bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 
| #endif | 
| } | 
|  | 
| -void ChromeDownloadManagerDelegate::AddItemToPersistentStore( | 
| -    DownloadItem* item) { | 
| -  if (profile_->IsOffTheRecord()) { | 
| -    OnItemAddedToPersistentStore( | 
| -        item->GetId(), download_history_->GetNextFakeDbHandle()); | 
| -    return; | 
| -  } | 
| -  download_history_->AddEntry(item, | 
| -      base::Bind(&ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore, | 
| -                 this)); | 
| -} | 
| - | 
| -void ChromeDownloadManagerDelegate::UpdateItemInPersistentStore( | 
| -    DownloadItem* item) { | 
| -  download_history_->UpdateEntry(item); | 
| -} | 
| - | 
| -void ChromeDownloadManagerDelegate::UpdatePathForItemInPersistentStore( | 
| -    DownloadItem* item, | 
| -    const FilePath& new_path) { | 
| -  download_history_->UpdateDownloadPath(item, new_path); | 
| -} | 
| - | 
| -void ChromeDownloadManagerDelegate::RemoveItemFromPersistentStore( | 
| -    DownloadItem* item) { | 
| -  download_history_->RemoveEntry(item); | 
| -} | 
| - | 
| -void ChromeDownloadManagerDelegate::RemoveItemsFromPersistentStoreBetween( | 
| -    base::Time remove_begin, | 
| -    base::Time remove_end) { | 
| -  if (profile_->IsOffTheRecord()) | 
| -    return; | 
| -  download_history_->RemoveEntriesBetween(remove_begin, remove_end); | 
| -} | 
| - | 
| void ChromeDownloadManagerDelegate::GetSaveDir(BrowserContext* browser_context, | 
| FilePath* website_save_dir, | 
| FilePath* download_save_dir, | 
| @@ -637,10 +605,22 @@ void ChromeDownloadManagerDelegate::CheckDownloadUrlDone( | 
| if (result != DownloadProtectionService::SAFE) | 
| danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; | 
|  | 
| -  download_history_->CheckVisitedReferrerBefore( | 
| -      download_id, download->GetReferrerUrl(), | 
| -      base::Bind(&ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, | 
| -                 this, download_id, callback, danger_type)); | 
| +  // HistoryServiceFactory redirects incognito profiles to on-record profiles. | 
| +  HistoryService* history = HistoryServiceFactory::GetForProfile( | 
| +      profile_, Profile::EXPLICIT_ACCESS); | 
| +  if (!history || !download->GetReferrerUrl().is_valid()) { | 
| +    // If the original profile doesn't have a HistoryService or the referrer url | 
| +    // is invalid, then give up and assume the referrer has not been visited | 
| +    // before. There's no history for on-record profiles in unit_tests, for | 
| +    // example. | 
| +    CheckVisitedReferrerBeforeDone(download_id, callback, danger_type, false); | 
| +    return; | 
| +  } | 
| +  history->GetVisibleVisitCountToHost( | 
| +      download->GetReferrerUrl(), &history_consumer_, | 
| +      base::Bind(&VisitCountsToVisitedBefore, base::Bind( | 
| +          &ChromeDownloadManagerDelegate::CheckVisitedReferrerBeforeDone, | 
| +          this, download_id, callback, danger_type))); | 
| } | 
|  | 
| void ChromeDownloadManagerDelegate::CheckClientDownloadDone( | 
| @@ -885,15 +865,3 @@ void ChromeDownloadManagerDelegate::OnTargetPathDetermined( | 
| } | 
| callback.Run(target_path, disposition, danger_type, intermediate_path); | 
| } | 
| - | 
| -void ChromeDownloadManagerDelegate::OnItemAddedToPersistentStore( | 
| -    int32 download_id, int64 db_handle) { | 
| -  // It's not immediately obvious, but HistoryBackend::CreateDownload() can | 
| -  // call this function with an invalid |db_handle|. For instance, this can | 
| -  // happen when the history database is offline. We cannot have multiple | 
| -  // DownloadItems with the same invalid db_handle, so we need to assign a | 
| -  // unique |db_handle| here. | 
| -  if (db_handle == DownloadItem::kUninitializedHandle) | 
| -    db_handle = download_history_->GetNextFakeDbHandle(); | 
| -  download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | 
| -} | 
|  |