PageRenderTime 50ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/chrome/browser/download/download_core_service_impl.cc

http://github.com/chromium/chromium
C++ | 172 lines | 131 code | 26 blank | 15 comment | 14 complexity | ee0ff506056ad14bef1346b7e9bf74ee MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, BSD-2-Clause, LGPL-2.1, MPL-2.0, 0BSD, EPL-1.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BitTorrent-1.0, CPL-1.0, LGPL-3.0, Unlicense, BSD-3-Clause, CC0-1.0, JSON, MIT, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0
  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. #include "chrome/browser/download/download_core_service_impl.h"
  5. #include "base/callback.h"
  6. #include "build/build_config.h"
  7. #include "chrome/browser/browser_process.h"
  8. #include "chrome/browser/download/chrome_download_manager_delegate.h"
  9. #include "chrome/browser/download/download_history.h"
  10. #include "chrome/browser/download/download_offline_content_provider.h"
  11. #include "chrome/browser/download/download_offline_content_provider_factory.h"
  12. #include "chrome/browser/download/download_status_updater.h"
  13. #include "chrome/browser/download/download_ui_controller.h"
  14. #include "chrome/browser/download/simple_download_manager_coordinator_factory.h"
  15. #include "chrome/browser/history/history_service_factory.h"
  16. #include "chrome/browser/profiles/profile.h"
  17. #include "chrome/browser/profiles/profile_key.h"
  18. #include "components/download/public/common/simple_download_manager_coordinator.h"
  19. #include "components/history/core/browser/history_service.h"
  20. #include "content/public/browser/download_manager.h"
  21. #include "extensions/buildflags/buildflags.h"
  22. #if BUILDFLAG(ENABLE_EXTENSIONS)
  23. #include "chrome/browser/extensions/api/downloads/downloads_api.h"
  24. #endif
  25. #if defined(OS_ANDROID)
  26. #include "chrome/browser/download/android/download_utils.h"
  27. #endif
  28. using content::BrowserContext;
  29. using content::DownloadManager;
  30. using content::DownloadManagerDelegate;
  31. DownloadCoreServiceImpl::DownloadCoreServiceImpl(Profile* profile)
  32. : download_manager_created_(false), profile_(profile) {}
  33. DownloadCoreServiceImpl::~DownloadCoreServiceImpl() {}
  34. ChromeDownloadManagerDelegate*
  35. DownloadCoreServiceImpl::GetDownloadManagerDelegate() {
  36. DownloadManager* manager = BrowserContext::GetDownloadManager(profile_);
  37. // If we've already created the delegate, just return it.
  38. if (download_manager_created_)
  39. return manager_delegate_.get();
  40. download_manager_created_ = true;
  41. download::SimpleDownloadManagerCoordinator* coordinator =
  42. SimpleDownloadManagerCoordinatorFactory::GetForKey(
  43. profile_->GetProfileKey());
  44. coordinator->SetSimpleDownloadManager(manager, 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_));
  49. manager_delegate_->SetDownloadManager(manager);
  50. #if BUILDFLAG(ENABLE_EXTENSIONS)
  51. extension_event_router_.reset(
  52. new extensions::ExtensionDownloadsEventRouter(profile_, manager));
  53. #endif
  54. if (!profile_->IsOffTheRecord()) {
  55. history::HistoryService* history = HistoryServiceFactory::GetForProfile(
  56. profile_, ServiceAccessType::EXPLICIT_ACCESS);
  57. history->GetNextDownloadId(
  58. manager_delegate_->GetDownloadIdReceiverCallback());
  59. download_history_.reset(new DownloadHistory(
  60. manager, std::unique_ptr<DownloadHistory::HistoryAdapter>(
  61. new DownloadHistory::HistoryAdapter(history))));
  62. }
  63. // Pass an empty delegate when constructing the DownloadUIController. The
  64. // default delegate does all the notifications we need.
  65. download_ui_.reset(new DownloadUIController(
  66. manager, std::unique_ptr<DownloadUIController::Delegate>()));
  67. #if !defined(OS_ANDROID)
  68. download_shelf_controller_.reset(new DownloadShelfController(profile_));
  69. #endif
  70. // Include this download manager in the set monitored by the
  71. // global status updater.
  72. DCHECK(g_browser_process->download_status_updater());
  73. g_browser_process->download_status_updater()->AddManager(manager);
  74. return manager_delegate_.get();
  75. }
  76. DownloadHistory* DownloadCoreServiceImpl::GetDownloadHistory() {
  77. if (!download_manager_created_) {
  78. GetDownloadManagerDelegate();
  79. }
  80. DCHECK(download_manager_created_);
  81. return download_history_.get();
  82. }
  83. #if BUILDFLAG(ENABLE_EXTENSIONS)
  84. extensions::ExtensionDownloadsEventRouter*
  85. DownloadCoreServiceImpl::GetExtensionEventRouter() {
  86. return extension_event_router_.get();
  87. }
  88. #endif
  89. bool DownloadCoreServiceImpl::HasCreatedDownloadManager() {
  90. return download_manager_created_;
  91. }
  92. int DownloadCoreServiceImpl::NonMaliciousDownloadCount() const {
  93. if (!download_manager_created_)
  94. return 0;
  95. return BrowserContext::GetDownloadManager(profile_)
  96. ->NonMaliciousInProgressCount();
  97. }
  98. void DownloadCoreServiceImpl::CancelDownloads() {
  99. if (!download_manager_created_)
  100. return;
  101. DownloadManager* download_manager =
  102. BrowserContext::GetDownloadManager(profile_);
  103. DownloadManager::DownloadVector downloads;
  104. download_manager->GetAllDownloads(&downloads);
  105. for (auto it = downloads.begin(); it != downloads.end(); ++it) {
  106. if ((*it)->GetState() == download::DownloadItem::IN_PROGRESS)
  107. (*it)->Cancel(false);
  108. }
  109. }
  110. void DownloadCoreServiceImpl::SetDownloadManagerDelegateForTesting(
  111. std::unique_ptr<ChromeDownloadManagerDelegate> new_delegate) {
  112. manager_delegate_.swap(new_delegate);
  113. DownloadManager* dm = BrowserContext::GetDownloadManager(profile_);
  114. dm->SetDelegate(manager_delegate_.get());
  115. if (manager_delegate_)
  116. manager_delegate_->SetDownloadManager(dm);
  117. download_manager_created_ = !!manager_delegate_;
  118. if (new_delegate)
  119. new_delegate->Shutdown();
  120. }
  121. void DownloadCoreServiceImpl::SetDownloadHistoryForTesting(
  122. std::unique_ptr<DownloadHistory> download_history) {
  123. download_history_ = std::move(download_history);
  124. }
  125. bool DownloadCoreServiceImpl::IsShelfEnabled() {
  126. #if defined(OS_ANDROID)
  127. return true;
  128. #else
  129. return !extension_event_router_ || extension_event_router_->IsShelfEnabled();
  130. #endif
  131. }
  132. void DownloadCoreServiceImpl::Shutdown() {
  133. if (download_manager_created_) {
  134. // Normally the DownloadManager would be shutdown later, after the Profile
  135. // goes away and BrowserContext's destructor runs. But that would be too
  136. // late for us since we need to use the profile (indirectly through history
  137. // code) when the DownloadManager is shutting down. So we shut it down
  138. // manually earlier. See http://crbug.com/131692
  139. BrowserContext::GetDownloadManager(profile_)->Shutdown();
  140. }
  141. #if BUILDFLAG(ENABLE_EXTENSIONS)
  142. extension_event_router_.reset();
  143. #endif
  144. manager_delegate_.reset();
  145. download_history_.reset();
  146. }