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 "base/callback.h" |
7 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
8 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 9 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
9 #include "chrome/browser/download/download_service_factory.h" | 10 #include "chrome/browser/download/download_service_factory.h" |
10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/profiles/profile_manager.h" | 12 #include "chrome/browser/profiles/profile_manager.h" |
12 #include "content/browser/download/download_id_factory.h" | 13 #include "content/browser/download/download_id_factory.h" |
13 #include "content/browser/download/download_manager_impl.h" | 14 #include "content/browser/download/download_manager_impl.h" |
14 | 15 |
15 DownloadService::DownloadService(Profile* profile) | 16 DownloadService::DownloadService(Profile* profile) |
16 : download_manager_created_(false), | 17 : download_manager_created_(false), |
17 profile_(profile) { | 18 profile_(profile) { |
18 if (profile_->IsOffTheRecord()) { | 19 if (profile_->IsOffTheRecord()) { |
19 id_factory_ = DownloadServiceFactory::GetForProfile( | 20 id_factory_ = DownloadServiceFactory::GetForProfile( |
20 profile_->GetOriginalProfile())->GetDownloadIdFactory(); | 21 profile_->GetOriginalProfile())->GetDownloadIdFactory(); |
21 } else { | 22 } else { |
22 id_factory_ = new DownloadIdFactory(this); | 23 id_factory_ = new DownloadIdFactory(this); |
23 } | 24 } |
24 } | 25 } |
25 | 26 |
26 DownloadService::~DownloadService() {} | 27 DownloadService::~DownloadService() {} |
27 | 28 |
28 DownloadIdFactory* DownloadService::GetDownloadIdFactory() const { | 29 DownloadIdFactory* DownloadService::GetDownloadIdFactory() const { |
29 return id_factory_.get(); | 30 return id_factory_.get(); |
30 } | 31 } |
31 | 32 |
| 33 void DownloadService::OnManagerCreated( |
| 34 const DownloadService::OnManagerCreatedCallback& cb) { |
| 35 if (download_manager_created_) { |
| 36 cb.Run(manager_.get()); |
| 37 } else { |
| 38 on_manager_created_callbacks_.push_back(cb); |
| 39 } |
| 40 } |
| 41 |
32 DownloadManager* DownloadService::GetDownloadManager() { | 42 DownloadManager* DownloadService::GetDownloadManager() { |
33 if (!download_manager_created_) { | 43 if (!download_manager_created_) { |
34 // In case the delegate has already been set by | 44 // In case the delegate has already been set by |
35 // SetDownloadManagerDelegateForTesting. | 45 // SetDownloadManagerDelegateForTesting. |
36 if (!manager_delegate_.get()) | 46 if (!manager_delegate_.get()) |
37 manager_delegate_ = new ChromeDownloadManagerDelegate(profile_); | 47 manager_delegate_ = new ChromeDownloadManagerDelegate(profile_); |
38 manager_ = new DownloadManagerImpl( | 48 manager_ = new DownloadManagerImpl( |
39 manager_delegate_.get(), | 49 manager_delegate_.get(), |
40 id_factory_.get(), | 50 id_factory_.get(), |
41 g_browser_process->download_status_updater()); | 51 g_browser_process->download_status_updater()); |
42 manager_->Init(profile_); | 52 manager_->Init(profile_); |
43 manager_delegate_->SetDownloadManager(manager_); | 53 manager_delegate_->SetDownloadManager(manager_); |
44 download_manager_created_ = true; | 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(manager_.get()); |
| 59 } |
| 60 on_manager_created_callbacks_.clear(); |
45 } | 61 } |
46 return manager_.get(); | 62 return manager_.get(); |
47 } | 63 } |
48 | 64 |
49 bool DownloadService::HasCreatedDownloadManager() { | 65 bool DownloadService::HasCreatedDownloadManager() { |
50 return download_manager_created_; | 66 return download_manager_created_; |
51 } | 67 } |
52 | 68 |
53 int DownloadService::DownloadCount() const { | 69 int DownloadService::DownloadCount() const { |
54 return download_manager_created_ ? manager_->InProgressCount() : 0; | 70 return download_manager_created_ ? manager_->InProgressCount() : 0; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 // Resetting here will guarantee that any attempts to get the | 107 // Resetting here will guarantee that any attempts to get the |
92 // DownloadManager after shutdown will return null. | 108 // DownloadManager after shutdown will return null. |
93 // | 109 // |
94 // TODO(rdsmith): Figure out how to guarantee when the last reference | 110 // TODO(rdsmith): Figure out how to guarantee when the last reference |
95 // will be released and make DownloadManager not RefCountedThreadSafe<>. | 111 // will be released and make DownloadManager not RefCountedThreadSafe<>. |
96 manager_.release(); | 112 manager_.release(); |
97 } | 113 } |
98 if (manager_delegate_.get()) | 114 if (manager_delegate_.get()) |
99 manager_delegate_.release(); | 115 manager_delegate_.release(); |
100 } | 116 } |
OLD | NEW |