Chromium Code Reviews| 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_); | |
|
Randy Smith (Not in Mondays)
2012/06/06 15:35:52
This looks like it will unilaterally create a down
jam
2012/06/06 15:49:36
my bad, I meant to call GetDownloadManager in the
Randy Smith (Not in Mondays)
2012/06/06 16:34:45
Cool, thanks. I'll probably ask Ben to review it
| |
| 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() { |
|
Randy Smith (Not in Mondays)
2012/06/06 15:51:59
This basic change (shifting from GetDownloadManage
jam
2012/06/06 16:06:15
I've added a comment to browser_context.h, since t
| |
| 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_); | |
| 49 | 46 |
| 50 download_manager_created_ = true; | 47 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); |
| 51 for (std::vector<OnManagerCreatedCallback>::iterator cb | 48 manager_delegate_->SetDownloadManager(dm); |
| 52 = on_manager_created_callbacks_.begin(); | 49 |
| 53 cb != on_manager_created_callbacks_.end(); ++cb) { | 50 // Include this download manager in the set monitored by the |
| 54 cb->Run(manager_.get()); | 51 // global status updater. |
| 55 } | 52 g_browser_process->download_status_updater()->AddManager(dm); |
| 56 on_manager_created_callbacks_.clear(); | 53 |
| 54 download_manager_created_ = true; | |
| 55 for (std::vector<OnManagerCreatedCallback>::iterator cb | |
| 56 = on_manager_created_callbacks_.begin(); | |
| 57 cb != on_manager_created_callbacks_.end(); ++cb) { | |
| 58 cb->Run(dm); | |
| 57 } | 59 } |
| 58 return manager_.get(); | 60 on_manager_created_callbacks_.clear(); |
| 61 | |
| 62 return manager_delegate_.get(); | |
| 59 } | 63 } |
| 60 | 64 |
| 61 bool DownloadService::HasCreatedDownloadManager() { | 65 bool DownloadService::HasCreatedDownloadManager() { |
| 62 return download_manager_created_; | 66 return download_manager_created_; |
| 63 } | 67 } |
| 64 | 68 |
| 65 int DownloadService::DownloadCount() const { | 69 int DownloadService::DownloadCount() const { |
| 66 return download_manager_created_ ? manager_->InProgressCount() : 0; | 70 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); |
| 71 return download_manager_created_ ? dm->InProgressCount() : 0; | |
| 67 } | 72 } |
| 68 | 73 |
| 69 // static | 74 // static |
| 70 int DownloadService::DownloadCountAllProfiles() { | 75 int DownloadService::DownloadCountAllProfiles() { |
| 71 std::vector<Profile*> profiles( | 76 std::vector<Profile*> profiles( |
| 72 g_browser_process->profile_manager()->GetLoadedProfiles()); | 77 g_browser_process->profile_manager()->GetLoadedProfiles()); |
| 73 | 78 |
| 74 int count = 0; | 79 int count = 0; |
| 75 for (std::vector<Profile*>::iterator it = profiles.begin(); | 80 for (std::vector<Profile*>::iterator it = profiles.begin(); |
| 76 it < profiles.end(); ++it) { | 81 it < profiles.end(); ++it) { |
| 77 count += DownloadServiceFactory::GetForProfile(*it)->DownloadCount(); | 82 count += DownloadServiceFactory::GetForProfile(*it)->DownloadCount(); |
| 78 if ((*it)->HasOffTheRecordProfile()) | 83 if ((*it)->HasOffTheRecordProfile()) |
| 79 count += DownloadServiceFactory::GetForProfile( | 84 count += DownloadServiceFactory::GetForProfile( |
| 80 (*it)->GetOffTheRecordProfile())->DownloadCount(); | 85 (*it)->GetOffTheRecordProfile())->DownloadCount(); |
| 81 } | 86 } |
| 82 | 87 |
| 83 return count; | 88 return count; |
| 84 } | 89 } |
| 85 | 90 |
| 86 void DownloadService::SetDownloadManagerDelegateForTesting( | 91 void DownloadService::SetDownloadManagerDelegateForTesting( |
| 87 ChromeDownloadManagerDelegate* new_delegate) { | 92 ChromeDownloadManagerDelegate* new_delegate) { |
| 88 // Guarantee everything is properly initialized. | 93 // Guarantee everything is properly initialized. |
| 89 GetDownloadManager(); | 94 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); |
| 90 | 95 dm->SetDownloadManagerDelegate(new_delegate); |
| 91 manager_->SetDownloadManagerDelegate(new_delegate); | 96 new_delegate->SetDownloadManager(dm); |
| 92 new_delegate->SetDownloadManager(manager_); | |
| 93 manager_delegate_ = new_delegate; | 97 manager_delegate_ = new_delegate; |
| 94 } | 98 } |
| 95 | 99 |
| 96 void DownloadService::Shutdown() { | 100 void DownloadService::Shutdown() { |
| 97 if (manager_.get()) { | 101 if (manager_delegate_.get()) { |
| 98 manager_->Shutdown(); | 102 manager_delegate_->ProfileShutdown(); |
| 99 | 103 manager_delegate_.release(); |
| 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 } | 104 } |
| 110 if (manager_delegate_.get()) | |
| 111 manager_delegate_.release(); | |
| 112 } | 105 } |
| OLD | NEW |