| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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.h" | 5 #include "chrome/browser/download/download_service.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 9 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 10 #include "chrome/browser/download/download_service_factory.h" | 10 #include "chrome/browser/download/download_service_factory.h" |
| 11 #include "chrome/browser/download/download_status_updater.h" | 11 #include "chrome/browser/download/download_status_updater.h" |
| 12 #include "chrome/browser/net/chrome_net_log.h" | 12 #include "chrome/browser/net/chrome_net_log.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
| 15 #include "content/public/browser/download_manager.h" | 15 #include "content/public/browser/download_manager.h" |
| 16 | 16 |
| 17 using content::BrowserContext; |
| 17 using content::DownloadManager; | 18 using content::DownloadManager; |
| 19 using content::DownloadManagerDelegate; |
| 18 | 20 |
| 19 DownloadService::DownloadService(Profile* profile) | 21 DownloadService::DownloadService(Profile* profile) |
| 20 : download_manager_created_(false), | 22 : download_manager_created_(false), |
| 21 profile_(profile) { | 23 profile_(profile) { |
| 22 } | 24 } |
| 23 | 25 |
| 24 DownloadService::~DownloadService() {} | 26 DownloadService::~DownloadService() {} |
| 25 | 27 |
| 26 void DownloadService::OnManagerCreated( | 28 void DownloadService::OnManagerCreated( |
| 27 const DownloadService::OnManagerCreatedCallback& cb) { | 29 const DownloadService::OnManagerCreatedCallback& cb) { |
| 30 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); |
| 28 if (download_manager_created_) { | 31 if (download_manager_created_) { |
| 29 cb.Run(manager_.get()); | 32 cb.Run(dm); |
| 30 } else { | 33 } else { |
| 31 on_manager_created_callbacks_.push_back(cb); | 34 on_manager_created_callbacks_.push_back(cb); |
| 32 } | 35 } |
| 33 } | 36 } |
| 34 | 37 |
| 35 DownloadManager* DownloadService::GetDownloadManager() { | 38 DownloadManagerDelegate* DownloadService::GetDownloadManagerDelegate() { |
| 36 if (!download_manager_created_) { | 39 DCHECK(!download_manager_created_); |
| 37 // In case the delegate has already been set by | 40 download_manager_created_ = true; |
| 38 // SetDownloadManagerDelegateForTesting. | |
| 39 if (!manager_delegate_.get()) | |
| 40 manager_delegate_ = new ChromeDownloadManagerDelegate(profile_); | |
| 41 manager_ = DownloadManager::Create(manager_delegate_.get(), | |
| 42 g_browser_process->net_log()); | |
| 43 manager_->Init(profile_); | |
| 44 manager_delegate_->SetDownloadManager(manager_); | |
| 45 | 41 |
| 46 // Include this download manager in the set monitored by the | 42 // In case the delegate has already been set by |
| 47 // global status updater. | 43 // SetDownloadManagerDelegateForTesting. |
| 48 g_browser_process->download_status_updater()->AddManager(manager_); | 44 if (!manager_delegate_.get()) { |
| 45 manager_delegate_ = new ChromeDownloadManagerDelegate(profile_); |
| 46 } |
| 49 | 47 |
| 50 download_manager_created_ = true; | 48 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); |
| 51 for (std::vector<OnManagerCreatedCallback>::iterator cb | 49 // Include this download manager in the set monitored by the |
| 52 = on_manager_created_callbacks_.begin(); | 50 // global status updater. |
| 53 cb != on_manager_created_callbacks_.end(); ++cb) { | 51 g_browser_process->download_status_updater()->AddManager(dm); |
| 54 cb->Run(manager_.get()); | 52 |
| 55 } | 53 download_manager_created_ = true; |
| 56 on_manager_created_callbacks_.clear(); | 54 for (std::vector<OnManagerCreatedCallback>::iterator cb |
| 55 = on_manager_created_callbacks_.begin(); |
| 56 cb != on_manager_created_callbacks_.end(); ++cb) { |
| 57 cb->Run(dm); |
| 57 } | 58 } |
| 58 return manager_.get(); | 59 on_manager_created_callbacks_.clear(); |
| 60 |
| 61 return manager_delegate_.get(); |
| 59 } | 62 } |
| 60 | 63 |
| 61 bool DownloadService::HasCreatedDownloadManager() { | 64 bool DownloadService::HasCreatedDownloadManager() { |
| 62 return download_manager_created_; | 65 return download_manager_created_; |
| 63 } | 66 } |
| 64 | 67 |
| 65 int DownloadService::DownloadCount() const { | 68 int DownloadService::DownloadCount() const { |
| 66 return download_manager_created_ ? manager_->InProgressCount() : 0; | 69 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); |
| 70 return download_manager_created_ ? dm->InProgressCount() : 0; |
| 67 } | 71 } |
| 68 | 72 |
| 69 // static | 73 // static |
| 70 int DownloadService::DownloadCountAllProfiles() { | 74 int DownloadService::DownloadCountAllProfiles() { |
| 71 std::vector<Profile*> profiles( | 75 std::vector<Profile*> profiles( |
| 72 g_browser_process->profile_manager()->GetLoadedProfiles()); | 76 g_browser_process->profile_manager()->GetLoadedProfiles()); |
| 73 | 77 |
| 74 int count = 0; | 78 int count = 0; |
| 75 for (std::vector<Profile*>::iterator it = profiles.begin(); | 79 for (std::vector<Profile*>::iterator it = profiles.begin(); |
| 76 it < profiles.end(); ++it) { | 80 it < profiles.end(); ++it) { |
| 77 count += DownloadServiceFactory::GetForProfile(*it)->DownloadCount(); | 81 count += DownloadServiceFactory::GetForProfile(*it)->DownloadCount(); |
| 78 if ((*it)->HasOffTheRecordProfile()) | 82 if ((*it)->HasOffTheRecordProfile()) |
| 79 count += DownloadServiceFactory::GetForProfile( | 83 count += DownloadServiceFactory::GetForProfile( |
| 80 (*it)->GetOffTheRecordProfile())->DownloadCount(); | 84 (*it)->GetOffTheRecordProfile())->DownloadCount(); |
| 81 } | 85 } |
| 82 | 86 |
| 83 return count; | 87 return count; |
| 84 } | 88 } |
| 85 | 89 |
| 86 void DownloadService::SetDownloadManagerDelegateForTesting( | 90 void DownloadService::SetDownloadManagerDelegateForTesting( |
| 87 ChromeDownloadManagerDelegate* new_delegate) { | 91 ChromeDownloadManagerDelegate* new_delegate) { |
| 88 // Guarantee everything is properly initialized. | 92 // Guarantee everything is properly initialized. |
| 89 GetDownloadManager(); | 93 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); |
| 90 | 94 dm->SetDownloadManagerDelegate(new_delegate); |
| 91 manager_->SetDownloadManagerDelegate(new_delegate); | |
| 92 new_delegate->SetDownloadManager(manager_); | |
| 93 manager_delegate_ = new_delegate; | 95 manager_delegate_ = new_delegate; |
| 94 } | 96 } |
| 95 | 97 |
| 96 void DownloadService::Shutdown() { | 98 void DownloadService::Shutdown() { |
| 97 if (manager_.get()) { | 99 if (download_manager_created_) { |
| 98 manager_->Shutdown(); | 100 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); |
| 99 | 101 dm->Shutdown(); |
| 100 // The manager reference can be released any time after shutdown; | |
| 101 // it will be destroyed when the last reference is released on the | |
| 102 // FILE thread. | |
| 103 // Resetting here will guarantee that any attempts to get the | |
| 104 // DownloadManager after shutdown will return null. | |
| 105 // | |
| 106 // TODO(rdsmith): Figure out how to guarantee when the last reference | |
| 107 // will be released and make DownloadManager not RefCountedThreadSafe<>. | |
| 108 manager_.release(); | |
| 109 } | 102 } |
| 110 if (manager_delegate_.get()) | 103 if (manager_delegate_.get()) |
| 111 manager_delegate_.release(); | 104 manager_delegate_.release(); |
| 112 } | 105 } |
| OLD | NEW |