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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 void DownloadService::OnManagerCreated( | 28 void DownloadService::OnManagerCreated( |
29 const DownloadService::OnManagerCreatedCallback& cb) { | 29 const DownloadService::OnManagerCreatedCallback& cb) { |
30 if (download_manager_created_) { | 30 if (download_manager_created_) { |
31 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); | 31 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); |
32 cb.Run(dm); | 32 cb.Run(dm); |
33 } else { | 33 } else { |
34 on_manager_created_callbacks_.push_back(cb); | 34 on_manager_created_callbacks_.push_back(cb); |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 DownloadManagerDelegate* DownloadService::GetDownloadManagerDelegate() { | 38 ChromeDownloadManagerDelegate* DownloadService::GetDownloadManagerDelegate() { |
39 DCHECK(!download_manager_created_); | 39 DownloadManager* manager = BrowserContext::GetDownloadManager(profile_); |
| 40 // If we've already created the delegate, just return it. |
| 41 if (download_manager_created_) { |
| 42 DCHECK(static_cast<DownloadManagerDelegate*>(manager_delegate_.get()) == |
| 43 manager->GetDelegate()); |
| 44 return manager_delegate_.get(); |
| 45 } |
40 download_manager_created_ = true; | 46 download_manager_created_ = true; |
41 | 47 |
42 // In case the delegate has already been set by | 48 // In case the delegate has already been set by |
43 // SetDownloadManagerDelegateForTesting. | 49 // SetDownloadManagerDelegateForTesting. |
44 if (!manager_delegate_.get()) | 50 if (!manager_delegate_.get()) |
45 manager_delegate_ = new ChromeDownloadManagerDelegate(profile_); | 51 manager_delegate_ = new ChromeDownloadManagerDelegate(profile_); |
46 | 52 |
47 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); | 53 manager_delegate_->SetDownloadManager(manager); |
48 manager_delegate_->SetDownloadManager(dm); | |
49 | 54 |
50 // Include this download manager in the set monitored by the | 55 // Include this download manager in the set monitored by the |
51 // global status updater. | 56 // global status updater. |
52 g_browser_process->download_status_updater()->AddManager(dm); | 57 g_browser_process->download_status_updater()->AddManager(manager); |
53 | 58 |
54 download_manager_created_ = true; | 59 download_manager_created_ = true; |
55 for (std::vector<OnManagerCreatedCallback>::iterator cb | 60 for (std::vector<OnManagerCreatedCallback>::iterator cb = |
56 = on_manager_created_callbacks_.begin(); | 61 on_manager_created_callbacks_.begin(); |
57 cb != on_manager_created_callbacks_.end(); ++cb) { | 62 cb != on_manager_created_callbacks_.end(); ++cb) { |
58 cb->Run(dm); | 63 cb->Run(manager); |
59 } | 64 } |
60 on_manager_created_callbacks_.clear(); | 65 on_manager_created_callbacks_.clear(); |
61 | 66 |
62 return manager_delegate_.get(); | 67 return manager_delegate_.get(); |
63 } | 68 } |
64 | 69 |
65 bool DownloadService::HasCreatedDownloadManager() { | 70 bool DownloadService::HasCreatedDownloadManager() { |
66 return download_manager_created_; | 71 return download_manager_created_; |
67 } | 72 } |
68 | 73 |
(...skipping 15 matching lines...) Expand all Loading... |
84 if ((*it)->HasOffTheRecordProfile()) | 89 if ((*it)->HasOffTheRecordProfile()) |
85 count += DownloadServiceFactory::GetForProfile( | 90 count += DownloadServiceFactory::GetForProfile( |
86 (*it)->GetOffTheRecordProfile())->DownloadCount(); | 91 (*it)->GetOffTheRecordProfile())->DownloadCount(); |
87 } | 92 } |
88 | 93 |
89 return count; | 94 return count; |
90 } | 95 } |
91 | 96 |
92 void DownloadService::SetDownloadManagerDelegateForTesting( | 97 void DownloadService::SetDownloadManagerDelegateForTesting( |
93 ChromeDownloadManagerDelegate* new_delegate) { | 98 ChromeDownloadManagerDelegate* new_delegate) { |
| 99 // Set the new delegate first so that if BrowserContext::GetDownloadManager() |
| 100 // causes a new download manager to be created, we won't create a redundant |
| 101 // ChromeDownloadManagerDelegate(). |
| 102 manager_delegate_ = new_delegate; |
94 // Guarantee everything is properly initialized. | 103 // Guarantee everything is properly initialized. |
95 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); | 104 DownloadManager* dm = BrowserContext::GetDownloadManager(profile_); |
96 dm->SetDelegate(new_delegate); | 105 if (dm->GetDelegate() != new_delegate) { |
97 new_delegate->SetDownloadManager(dm); | 106 dm->SetDelegate(new_delegate); |
98 manager_delegate_ = new_delegate; | 107 new_delegate->SetDownloadManager(dm); |
| 108 } |
99 } | 109 } |
100 | 110 |
101 void DownloadService::Shutdown() { | 111 void DownloadService::Shutdown() { |
102 if (download_manager_created_) { | 112 if (download_manager_created_) { |
103 // Normally the DownloadManager would be shutdown later, after the Profile | 113 // Normally the DownloadManager would be shutdown later, after the Profile |
104 // goes away and BrowserContext's destructor runs. But that would be too | 114 // goes away and BrowserContext's destructor runs. But that would be too |
105 // late for us since we need to use the profile (indirectly through history | 115 // late for us since we need to use the profile (indirectly through history |
106 // code) when the DownloadManager is shutting down. So we shut it down | 116 // code) when the DownloadManager is shutting down. So we shut it down |
107 // manually earlier. See http://crbug.com/131692 | 117 // manually earlier. See http://crbug.com/131692 |
108 BrowserContext::GetDownloadManager(profile_)->Shutdown(); | 118 BrowserContext::GetDownloadManager(profile_)->Shutdown(); |
109 } | 119 } |
110 manager_delegate_ = NULL; | 120 manager_delegate_ = NULL; |
111 } | 121 } |
OLD | NEW |