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
3 // found in the LICENSE file.
5 #include "chrome/browser/download/download_service.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_service_factory.h"
12 #include "chrome/browser/download/download_status_updater.h"
13 #include "chrome/browser/download/download_ui_controller.h"
14 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
15 #include "chrome/browser/history/history_service.h"
16 #include "chrome/browser/history/history_service_factory.h"
17 #include "chrome/browser/net/chrome_net_log.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "content/public/browser/download_manager.h"
22 using content::BrowserContext
;
23 using content::DownloadManager
;
24 using content::DownloadManagerDelegate
;
26 DownloadService::DownloadService(Profile
* profile
)
27 : download_manager_created_(false),
31 DownloadService::~DownloadService() {}
33 ChromeDownloadManagerDelegate
* DownloadService::GetDownloadManagerDelegate() {
34 DownloadManager
* manager
= BrowserContext::GetDownloadManager(profile_
);
35 // If we've already created the delegate, just return it.
36 if (download_manager_created_
) {
37 DCHECK(static_cast<DownloadManagerDelegate
*>(manager_delegate_
.get()) ==
38 manager
->GetDelegate());
39 return manager_delegate_
.get();
41 download_manager_created_
= true;
43 // In case the delegate has already been set by
44 // SetDownloadManagerDelegateForTesting.
45 if (!manager_delegate_
.get())
46 manager_delegate_
.reset(new ChromeDownloadManagerDelegate(profile_
));
48 manager_delegate_
->SetDownloadManager(manager
);
50 #if !defined(OS_ANDROID)
51 extension_event_router_
.reset(
52 new extensions::ExtensionDownloadsEventRouter(profile_
, manager
));
55 if (!profile_
->IsOffTheRecord()) {
56 HistoryService
* history
= HistoryServiceFactory::GetForProfile(
57 profile_
, Profile::EXPLICIT_ACCESS
);
58 history
->GetNextDownloadId(
59 manager_delegate_
->GetDownloadIdReceiverCallback());
60 download_history_
.reset(new DownloadHistory(
62 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
* DownloadService::GetDownloadHistory() {
79 if (!download_manager_created_
) {
80 GetDownloadManagerDelegate();
82 DCHECK(download_manager_created_
);
83 return download_history_
.get();
86 bool DownloadService::HasCreatedDownloadManager() {
87 return download_manager_created_
;
90 int DownloadService::NonMaliciousDownloadCount() const {
91 if (!download_manager_created_
)
93 return BrowserContext::GetDownloadManager(profile_
)->
94 NonMaliciousInProgressCount();
97 void DownloadService::CancelDownloads() {
98 if (!download_manager_created_
)
101 DownloadManager
* download_manager
=
102 BrowserContext::GetDownloadManager(profile_
);
103 DownloadManager::DownloadVector downloads
;
104 download_manager
->GetAllDownloads(&downloads
);
105 for (DownloadManager::DownloadVector::iterator it
= downloads
.begin();
106 it
!= downloads
.end();
108 if ((*it
)->GetState() == content::DownloadItem::IN_PROGRESS
)
109 (*it
)->Cancel(false);
114 int DownloadService::NonMaliciousDownloadCountAllProfiles() {
115 std::vector
<Profile
*> profiles(
116 g_browser_process
->profile_manager()->GetLoadedProfiles());
119 for (std::vector
<Profile
*>::iterator it
= profiles
.begin();
120 it
< profiles
.end(); ++it
) {
121 count
+= DownloadServiceFactory::GetForBrowserContext(*it
)->
122 NonMaliciousDownloadCount();
123 if ((*it
)->HasOffTheRecordProfile())
124 count
+= DownloadServiceFactory::GetForBrowserContext(
125 (*it
)->GetOffTheRecordProfile())->NonMaliciousDownloadCount();
132 void DownloadService::CancelAllDownloads() {
133 std::vector
<Profile
*> profiles(
134 g_browser_process
->profile_manager()->GetLoadedProfiles());
135 for (std::vector
<Profile
*>::iterator it
= profiles
.begin();
138 DownloadService
* service
=
139 DownloadServiceFactory::GetForBrowserContext(*it
);
140 service
->CancelDownloads();
144 void DownloadService::SetDownloadManagerDelegateForTesting(
145 scoped_ptr
<ChromeDownloadManagerDelegate
> new_delegate
) {
146 manager_delegate_
.swap(new_delegate
);
147 DownloadManager
* dm
= BrowserContext::GetDownloadManager(profile_
);
148 dm
->SetDelegate(manager_delegate_
.get());
149 manager_delegate_
->SetDownloadManager(dm
);
151 new_delegate
->Shutdown();
154 bool DownloadService::IsShelfEnabled() {
155 #if defined(OS_ANDROID)
158 return !extension_event_router_
||
159 extension_event_router_
->IsShelfEnabled();
163 void DownloadService::Shutdown() {
164 if (download_manager_created_
) {
165 // Normally the DownloadManager would be shutdown later, after the Profile
166 // goes away and BrowserContext's destructor runs. But that would be too
167 // late for us since we need to use the profile (indirectly through history
168 // code) when the DownloadManager is shutting down. So we shut it down
169 // manually earlier. See http://crbug.com/131692
170 BrowserContext::GetDownloadManager(profile_
)->Shutdown();
172 #if !defined(OS_ANDROID)
173 extension_event_router_
.reset();
175 manager_delegate_
.reset();
176 download_history_
.reset();