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