| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/download/download_service_impl.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "build/build_config.h" | |
| 9 #include "chrome/browser/browser_process.h" | |
| 10 #include "chrome/browser/download/chrome_download_manager_delegate.h" | |
| 11 #include "chrome/browser/download/download_history.h" | |
| 12 #include "chrome/browser/download/download_status_updater.h" | |
| 13 #include "chrome/browser/download/download_ui_controller.h" | |
| 14 #include "chrome/browser/history/history_service_factory.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "components/history/core/browser/history_service.h" | |
| 17 #include "content/public/browser/download_manager.h" | |
| 18 #include "extensions/features/features.h" | |
| 19 | |
| 20 #if BUILDFLAG(ENABLE_EXTENSIONS) | |
| 21 #include "chrome/browser/extensions/api/downloads/downloads_api.h" | |
| 22 #endif | |
| 23 | |
| 24 using content::BrowserContext; | |
| 25 using content::DownloadManager; | |
| 26 using content::DownloadManagerDelegate; | |
| 27 | |
| 28 DownloadServiceImpl::DownloadServiceImpl(Profile* profile) | |
| 29 : download_manager_created_(false), profile_(profile) { | |
| 30 } | |
| 31 | |
| 32 DownloadServiceImpl::~DownloadServiceImpl() { | |
| 33 } | |
| 34 | |
| 35 ChromeDownloadManagerDelegate* | |
| 36 DownloadServiceImpl::GetDownloadManagerDelegate() { | |
| 37 DownloadManager* manager = BrowserContext::GetDownloadManager(profile_); | |
| 38 // If we've already created the delegate, just return it. | |
| 39 if (download_manager_created_) { | |
| 40 DCHECK(static_cast<DownloadManagerDelegate*>(manager_delegate_.get()) == | |
| 41 manager->GetDelegate()); | |
| 42 return manager_delegate_.get(); | |
| 43 } | |
| 44 download_manager_created_ = true; | |
| 45 | |
| 46 // In case the delegate has already been set by | |
| 47 // SetDownloadManagerDelegateForTesting. | |
| 48 if (!manager_delegate_.get()) | |
| 49 manager_delegate_.reset(new ChromeDownloadManagerDelegate(profile_)); | |
| 50 | |
| 51 manager_delegate_->SetDownloadManager(manager); | |
| 52 | |
| 53 #if BUILDFLAG(ENABLE_EXTENSIONS) | |
| 54 extension_event_router_.reset( | |
| 55 new extensions::ExtensionDownloadsEventRouter(profile_, manager)); | |
| 56 #endif | |
| 57 | |
| 58 if (!profile_->IsOffTheRecord()) { | |
| 59 history::HistoryService* history = HistoryServiceFactory::GetForProfile( | |
| 60 profile_, ServiceAccessType::EXPLICIT_ACCESS); | |
| 61 history->GetNextDownloadId( | |
| 62 manager_delegate_->GetDownloadIdReceiverCallback()); | |
| 63 download_history_.reset(new DownloadHistory( | |
| 64 manager, std::unique_ptr<DownloadHistory::HistoryAdapter>( | |
| 65 new DownloadHistory::HistoryAdapter(history)))); | |
| 66 } | |
| 67 | |
| 68 // Pass an empty delegate when constructing the DownloadUIController. The | |
| 69 // default delegate does all the notifications we need. | |
| 70 download_ui_.reset(new DownloadUIController( | |
| 71 manager, std::unique_ptr<DownloadUIController::Delegate>())); | |
| 72 | |
| 73 // Include this download manager in the set monitored by the | |
| 74 // global status updater. | |
| 75 g_browser_process->download_status_updater()->AddManager(manager); | |
| 76 | |
| 77 return manager_delegate_.get(); | |
| 78 } | |
| 79 | |
| 80 DownloadHistory* DownloadServiceImpl::GetDownloadHistory() { | |
| 81 if (!download_manager_created_) { | |
| 82 GetDownloadManagerDelegate(); | |
| 83 } | |
| 84 DCHECK(download_manager_created_); | |
| 85 return download_history_.get(); | |
| 86 } | |
| 87 | |
| 88 #if BUILDFLAG(ENABLE_EXTENSIONS) | |
| 89 extensions::ExtensionDownloadsEventRouter* | |
| 90 DownloadServiceImpl::GetExtensionEventRouter() { | |
| 91 return extension_event_router_.get(); | |
| 92 } | |
| 93 #endif | |
| 94 | |
| 95 bool DownloadServiceImpl::HasCreatedDownloadManager() { | |
| 96 return download_manager_created_; | |
| 97 } | |
| 98 | |
| 99 int DownloadServiceImpl::NonMaliciousDownloadCount() const { | |
| 100 if (!download_manager_created_) | |
| 101 return 0; | |
| 102 return BrowserContext::GetDownloadManager(profile_) | |
| 103 ->NonMaliciousInProgressCount(); | |
| 104 } | |
| 105 | |
| 106 void DownloadServiceImpl::CancelDownloads() { | |
| 107 if (!download_manager_created_) | |
| 108 return; | |
| 109 | |
| 110 DownloadManager* download_manager = | |
| 111 BrowserContext::GetDownloadManager(profile_); | |
| 112 DownloadManager::DownloadVector downloads; | |
| 113 download_manager->GetAllDownloads(&downloads); | |
| 114 for (DownloadManager::DownloadVector::iterator it = downloads.begin(); | |
| 115 it != downloads.end(); ++it) { | |
| 116 if ((*it)->GetState() == content::DownloadItem::IN_PROGRESS) | |
| 117 (*it)->Cancel(false); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void DownloadServiceImpl::SetDownloadManagerDelegateForTesting( | |
| 122 std::unique_ptr<ChromeDownloadManagerDelegate> new_delegate) { | |
| 123 manager_delegate_.swap(new_delegate); | |
| 124 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); | |
| 125 dm->SetDelegate(manager_delegate_.get()); | |
| 126 manager_delegate_->SetDownloadManager(dm); | |
| 127 if (new_delegate) | |
| 128 new_delegate->Shutdown(); | |
| 129 } | |
| 130 | |
| 131 bool DownloadServiceImpl::IsShelfEnabled() { | |
| 132 #if defined(OS_ANDROID) | |
| 133 return true; | |
| 134 #else | |
| 135 return !extension_event_router_ || extension_event_router_->IsShelfEnabled(); | |
| 136 #endif | |
| 137 } | |
| 138 | |
| 139 void DownloadServiceImpl::Shutdown() { | |
| 140 if (download_manager_created_) { | |
| 141 // Normally the DownloadManager would be shutdown later, after the Profile | |
| 142 // goes away and BrowserContext's destructor runs. But that would be too | |
| 143 // late for us since we need to use the profile (indirectly through history | |
| 144 // code) when the DownloadManager is shutting down. So we shut it down | |
| 145 // manually earlier. See http://crbug.com/131692 | |
| 146 BrowserContext::GetDownloadManager(profile_)->Shutdown(); | |
| 147 } | |
| 148 #if BUILDFLAG(ENABLE_EXTENSIONS) | |
| 149 extension_event_router_.reset(); | |
| 150 #endif | |
| 151 manager_delegate_.reset(); | |
| 152 download_history_.reset(); | |
| 153 } | |
| OLD | NEW |