Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / download / download_service_impl.cc
blob7d0aab05f312906dd244ef9ae3fa12f6a49d097e
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/net/chrome_net_log.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"
19 #if defined(ENABLE_EXTENSIONS)
20 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
21 #endif
23 using content::BrowserContext;
24 using content::DownloadManager;
25 using content::DownloadManagerDelegate;
27 DownloadServiceImpl::DownloadServiceImpl(Profile* profile)
28 : download_manager_created_(false), profile_(profile) {
31 DownloadServiceImpl::~DownloadServiceImpl() {
34 ChromeDownloadManagerDelegate*
35 DownloadServiceImpl::GetDownloadManagerDelegate() {
36 DownloadManager* manager = BrowserContext::GetDownloadManager(profile_);
37 // If we've already created the delegate, just return it.
38 if (download_manager_created_) {
39 DCHECK(static_cast<DownloadManagerDelegate*>(manager_delegate_.get()) ==
40 manager->GetDelegate());
41 return manager_delegate_.get();
43 download_manager_created_ = true;
45 // In case the delegate has already been set by
46 // SetDownloadManagerDelegateForTesting.
47 if (!manager_delegate_.get())
48 manager_delegate_.reset(new ChromeDownloadManagerDelegate(profile_));
50 manager_delegate_->SetDownloadManager(manager);
52 #if defined(ENABLE_EXTENSIONS)
53 extension_event_router_.reset(
54 new extensions::ExtensionDownloadsEventRouter(profile_, manager));
55 #endif
57 if (!profile_->IsOffTheRecord()) {
58 history::HistoryService* history = HistoryServiceFactory::GetForProfile(
59 profile_, ServiceAccessType::EXPLICIT_ACCESS);
60 history->GetNextDownloadId(
61 manager_delegate_->GetDownloadIdReceiverCallback());
62 download_history_.reset(new DownloadHistory(
63 manager, scoped_ptr<DownloadHistory::HistoryAdapter>(
64 new DownloadHistory::HistoryAdapter(history))));
67 // Pass an empty delegate when constructing the DownloadUIController. The
68 // default delegate does all the notifications we need.
69 download_ui_.reset(new DownloadUIController(
70 manager, scoped_ptr<DownloadUIController::Delegate>()));
72 // Include this download manager in the set monitored by the
73 // global status updater.
74 g_browser_process->download_status_updater()->AddManager(manager);
76 return manager_delegate_.get();
79 DownloadHistory* DownloadServiceImpl::GetDownloadHistory() {
80 if (!download_manager_created_) {
81 GetDownloadManagerDelegate();
83 DCHECK(download_manager_created_);
84 return download_history_.get();
87 #if defined(ENABLE_EXTENSIONS)
88 extensions::ExtensionDownloadsEventRouter*
89 DownloadServiceImpl::GetExtensionEventRouter() {
90 return extension_event_router_.get();
92 #endif
94 bool DownloadServiceImpl::HasCreatedDownloadManager() {
95 return download_manager_created_;
98 int DownloadServiceImpl::NonMaliciousDownloadCount() const {
99 if (!download_manager_created_)
100 return 0;
101 return BrowserContext::GetDownloadManager(profile_)
102 ->NonMaliciousInProgressCount();
105 void DownloadServiceImpl::CancelDownloads() {
106 if (!download_manager_created_)
107 return;
109 DownloadManager* download_manager =
110 BrowserContext::GetDownloadManager(profile_);
111 DownloadManager::DownloadVector downloads;
112 download_manager->GetAllDownloads(&downloads);
113 for (DownloadManager::DownloadVector::iterator it = downloads.begin();
114 it != downloads.end(); ++it) {
115 if ((*it)->GetState() == content::DownloadItem::IN_PROGRESS)
116 (*it)->Cancel(false);
120 void DownloadServiceImpl::SetDownloadManagerDelegateForTesting(
121 scoped_ptr<ChromeDownloadManagerDelegate> new_delegate) {
122 manager_delegate_.swap(new_delegate);
123 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_);
124 dm->SetDelegate(manager_delegate_.get());
125 manager_delegate_->SetDownloadManager(dm);
126 if (new_delegate)
127 new_delegate->Shutdown();
130 bool DownloadServiceImpl::IsShelfEnabled() {
131 #if defined(OS_ANDROID)
132 return true;
133 #else
134 return !extension_event_router_ || extension_event_router_->IsShelfEnabled();
135 #endif
138 void DownloadServiceImpl::Shutdown() {
139 if (download_manager_created_) {
140 // Normally the DownloadManager would be shutdown later, after the Profile
141 // goes away and BrowserContext's destructor runs. But that would be too
142 // late for us since we need to use the profile (indirectly through history
143 // code) when the DownloadManager is shutting down. So we shut it down
144 // manually earlier. See http://crbug.com/131692
145 BrowserContext::GetDownloadManager(profile_)->Shutdown();
147 #if defined(ENABLE_EXTENSIONS)
148 extension_event_router_.reset();
149 #endif
150 manager_delegate_.reset();
151 download_history_.reset();