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.
5 #include "chrome/browser/download/download_service_impl.h"
7 #include "base/callback.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/download/chrome_download_manager_delegate.h"
10 #include "chrome/browser/download/download_history.h"
11 #include "chrome/browser/download/download_status_updater.h"
12 #include "chrome/browser/download/download_ui_controller.h"
13 #include "chrome/browser/history/history_service_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "components/history/core/browser/history_service.h"
16 #include "content/public/browser/download_manager.h"
18 #if defined(ENABLE_EXTENSIONS)
19 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
22 using content::BrowserContext
;
23 using content::DownloadManager
;
24 using content::DownloadManagerDelegate
;
26 DownloadServiceImpl::DownloadServiceImpl(Profile
* profile
)
27 : download_manager_created_(false), profile_(profile
) {
30 DownloadServiceImpl::~DownloadServiceImpl() {
33 ChromeDownloadManagerDelegate
*
34 DownloadServiceImpl::GetDownloadManagerDelegate() {
35 DownloadManager
* manager
= BrowserContext::GetDownloadManager(profile_
);
36 // If we've already created the delegate, just return it.
37 if (download_manager_created_
) {
38 DCHECK(static_cast<DownloadManagerDelegate
*>(manager_delegate_
.get()) ==
39 manager
->GetDelegate());
40 return manager_delegate_
.get();
42 download_manager_created_
= true;
44 // In case the delegate has already been set by
45 // SetDownloadManagerDelegateForTesting.
46 if (!manager_delegate_
.get())
47 manager_delegate_
.reset(new ChromeDownloadManagerDelegate(profile_
));
49 manager_delegate_
->SetDownloadManager(manager
);
51 #if defined(ENABLE_EXTENSIONS)
52 extension_event_router_
.reset(
53 new extensions::ExtensionDownloadsEventRouter(profile_
, manager
));
56 if (!profile_
->IsOffTheRecord()) {
57 history::HistoryService
* history
= HistoryServiceFactory::GetForProfile(
58 profile_
, ServiceAccessType::EXPLICIT_ACCESS
);
59 history
->GetNextDownloadId(
60 manager_delegate_
->GetDownloadIdReceiverCallback());
61 download_history_
.reset(new DownloadHistory(
62 manager
, scoped_ptr
<DownloadHistory::HistoryAdapter
>(
63 new DownloadHistory::HistoryAdapter(history
))));
66 // Pass an empty delegate when constructing the DownloadUIController. The
67 // default delegate does all the notifications we need.
68 download_ui_
.reset(new DownloadUIController(
69 manager
, scoped_ptr
<DownloadUIController::Delegate
>()));
71 // Include this download manager in the set monitored by the
72 // global status updater.
73 g_browser_process
->download_status_updater()->AddManager(manager
);
75 return manager_delegate_
.get();
78 DownloadHistory
* DownloadServiceImpl::GetDownloadHistory() {
79 if (!download_manager_created_
) {
80 GetDownloadManagerDelegate();
82 DCHECK(download_manager_created_
);
83 return download_history_
.get();
86 #if defined(ENABLE_EXTENSIONS)
87 extensions::ExtensionDownloadsEventRouter
*
88 DownloadServiceImpl::GetExtensionEventRouter() {
89 return extension_event_router_
.get();
93 bool DownloadServiceImpl::HasCreatedDownloadManager() {
94 return download_manager_created_
;
97 int DownloadServiceImpl::NonMaliciousDownloadCount() const {
98 if (!download_manager_created_
)
100 return BrowserContext::GetDownloadManager(profile_
)
101 ->NonMaliciousInProgressCount();
104 void DownloadServiceImpl::CancelDownloads() {
105 if (!download_manager_created_
)
108 DownloadManager
* download_manager
=
109 BrowserContext::GetDownloadManager(profile_
);
110 DownloadManager::DownloadVector downloads
;
111 download_manager
->GetAllDownloads(&downloads
);
112 for (DownloadManager::DownloadVector::iterator it
= downloads
.begin();
113 it
!= downloads
.end(); ++it
) {
114 if ((*it
)->GetState() == content::DownloadItem::IN_PROGRESS
)
115 (*it
)->Cancel(false);
119 void DownloadServiceImpl::SetDownloadManagerDelegateForTesting(
120 scoped_ptr
<ChromeDownloadManagerDelegate
> new_delegate
) {
121 manager_delegate_
.swap(new_delegate
);
122 DownloadManager
* dm
= BrowserContext::GetDownloadManager(profile_
);
123 dm
->SetDelegate(manager_delegate_
.get());
124 manager_delegate_
->SetDownloadManager(dm
);
126 new_delegate
->Shutdown();
129 bool DownloadServiceImpl::IsShelfEnabled() {
130 #if defined(OS_ANDROID)
133 return !extension_event_router_
|| extension_event_router_
->IsShelfEnabled();
137 void DownloadServiceImpl::Shutdown() {
138 if (download_manager_created_
) {
139 // Normally the DownloadManager would be shutdown later, after the Profile
140 // goes away and BrowserContext's destructor runs. But that would be too
141 // late for us since we need to use the profile (indirectly through history
142 // code) when the DownloadManager is shutting down. So we shut it down
143 // manually earlier. See http://crbug.com/131692
144 BrowserContext::GetDownloadManager(profile_
)->Shutdown();
146 #if defined(ENABLE_EXTENSIONS)
147 extension_event_router_
.reset();
149 manager_delegate_
.reset();
150 download_history_
.reset();