OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
8 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 8 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
9 #include "chrome/browser/download/download_service_factory.h" | |
9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/profiles/profile_manager.h" | |
10 #include "content/browser/download/download_manager.h" | 12 #include "content/browser/download/download_manager.h" |
11 | 13 |
12 DownloadService::DownloadService(Profile* profile) | 14 DownloadService::DownloadService(Profile* profile) |
13 : download_manager_created_(false), | 15 : download_manager_created_(false), |
14 profile_(profile) {} | 16 profile_(profile) {} |
15 | 17 |
16 DownloadService::~DownloadService() {} | 18 DownloadService::~DownloadService() {} |
17 | 19 |
18 DownloadManager* DownloadService::GetDownloadManager() { | 20 DownloadManager* DownloadService::GetDownloadManager() { |
19 if (!download_manager_created_) { | 21 if (!download_manager_created_) { |
20 // In case the delegate has already been set by | 22 // In case the delegate has already been set by |
21 // SetDownloadManagerDelegateForTesting. | 23 // SetDownloadManagerDelegateForTesting. |
22 if (!manager_delegate_.get()) | 24 if (!manager_delegate_.get()) |
23 manager_delegate_ = new ChromeDownloadManagerDelegate(profile_); | 25 manager_delegate_ = new ChromeDownloadManagerDelegate(profile_); |
24 manager_ = new DownloadManager( | 26 manager_ = new DownloadManager( |
25 manager_delegate_.get(), g_browser_process->download_status_updater()); | 27 manager_delegate_.get(), g_browser_process->download_status_updater()); |
26 manager_->Init(profile_); | 28 manager_->Init(profile_); |
27 manager_delegate_->SetDownloadManager(manager_); | 29 manager_delegate_->SetDownloadManager(manager_); |
28 download_manager_created_ = true; | 30 download_manager_created_ = true; |
29 } | 31 } |
30 return manager_.get(); | 32 return manager_.get(); |
31 } | 33 } |
32 | 34 |
33 bool DownloadService::HasCreatedDownloadManager() { | 35 bool DownloadService::HasCreatedDownloadManager() { |
34 return download_manager_created_; | 36 return download_manager_created_; |
35 } | 37 } |
36 | 38 |
39 int DownloadService::DownloadCount() { | |
40 if (!download_manager_created_) | |
41 return 0; | |
42 | |
43 return manager_->in_progress_count(); | |
achuithb
2011/10/14 19:22:10
nit: this is fine, but have you considered
return
Randy Smith (Not in Mondays)
2011/10/14 19:30:47
Done.
| |
44 } | |
45 | |
46 // static | |
47 int DownloadService::TotalDownloadCount() { | |
48 std::vector<Profile*> profiles( | |
49 g_browser_process->profile_manager()->GetLoadedProfiles()); | |
50 | |
51 int count = 0; | |
52 for (std::vector<Profile*>::iterator it = profiles.begin(); | |
53 it < profiles.end(); ++it) { | |
54 count += DownloadServiceFactory::GetForProfile(*it)->DownloadCount(); | |
55 if ((*it)->HasOffTheRecordProfile()) | |
56 count += DownloadServiceFactory::GetForProfile( | |
57 (*it)->GetOffTheRecordProfile())->DownloadCount(); | |
58 } | |
59 | |
60 return count; | |
61 } | |
62 | |
37 void DownloadService::SetDownloadManagerDelegateForTesting( | 63 void DownloadService::SetDownloadManagerDelegateForTesting( |
38 ChromeDownloadManagerDelegate* new_delegate) { | 64 ChromeDownloadManagerDelegate* new_delegate) { |
39 // Guarantee everything is properly initialized. | 65 // Guarantee everything is properly initialized. |
40 GetDownloadManager(); | 66 GetDownloadManager(); |
41 | 67 |
42 manager_->SetDownloadManagerDelegate(new_delegate); | 68 manager_->SetDownloadManagerDelegate(new_delegate); |
43 new_delegate->SetDownloadManager(manager_); | 69 new_delegate->SetDownloadManager(manager_); |
44 manager_delegate_ = new_delegate; | 70 manager_delegate_ = new_delegate; |
45 } | 71 } |
46 | 72 |
47 void DownloadService::Shutdown() { | 73 void DownloadService::Shutdown() { |
48 if (manager_.get()) { | 74 if (manager_.get()) { |
49 manager_->Shutdown(); | 75 manager_->Shutdown(); |
50 | 76 |
51 // The manager reference can be released any time after shutdown; | 77 // The manager reference can be released any time after shutdown; |
52 // it will be destroyed when the last reference is released on the | 78 // it will be destroyed when the last reference is released on the |
53 // FILE thread. | 79 // FILE thread. |
54 // Resetting here will guarantee that any attempts to get the | 80 // Resetting here will guarantee that any attempts to get the |
55 // DownloadManager after shutdown will return null. | 81 // DownloadManager after shutdown will return null. |
56 // | 82 // |
57 // TODO(rdsmith): Figure out how to guarantee when the last reference | 83 // TODO(rdsmith): Figure out how to guarantee when the last reference |
58 // will be released and make DownloadManager not RefCountedThreadSafe<>. | 84 // will be released and make DownloadManager not RefCountedThreadSafe<>. |
59 manager_.release(); | 85 manager_.release(); |
60 } | 86 } |
61 if (manager_delegate_.get()) | 87 if (manager_delegate_.get()) |
62 manager_delegate_.release(); | 88 manager_delegate_.release(); |
63 } | 89 } |
OLD | NEW |