OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/notification/download_notification_manager.h" | 5 #include "chrome/browser/download/notification/download_notification_manager.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/ptr_util.h" |
9 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
12 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/download/download_item_model.h" | 14 #include "chrome/browser/download/download_item_model.h" |
14 #include "chrome/browser/download/notification/download_item_notification.h" | 15 #include "chrome/browser/download/notification/download_item_notification.h" |
15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/grit/chromium_strings.h" | 17 #include "chrome/grit/chromium_strings.h" |
17 #include "content/public/browser/download_item.h" | 18 #include "content/public/browser/download_item.h" |
18 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
19 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
20 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
21 #include "ui/message_center/message_center.h" | 22 #include "ui/message_center/message_center.h" |
22 #include "ui/message_center/notification.h" | 23 #include "ui/message_center/notification.h" |
23 #include "ui/message_center/notification_delegate.h" | 24 #include "ui/message_center/notification_delegate.h" |
24 | 25 |
25 /////////////////////////////////////////////////////////////////////////////// | 26 /////////////////////////////////////////////////////////////////////////////// |
26 // DownloadNotificationManager implementation: | 27 // DownloadNotificationManager implementation: |
27 /////////////////////////////////////////////////////////////////////////////// | 28 /////////////////////////////////////////////////////////////////////////////// |
28 | 29 |
29 DownloadNotificationManager::DownloadNotificationManager(Profile* profile) | 30 DownloadNotificationManager::DownloadNotificationManager(Profile* profile) |
30 : main_profile_(profile), | 31 : main_profile_(profile) {} |
31 items_deleter_(&manager_for_profile_) { | |
32 } | |
33 | 32 |
34 DownloadNotificationManager::~DownloadNotificationManager() { | 33 DownloadNotificationManager::~DownloadNotificationManager() { |
35 } | 34 } |
36 | 35 |
37 void DownloadNotificationManager::OnAllDownloadsRemoving(Profile* profile) { | 36 void DownloadNotificationManager::OnAllDownloadsRemoving(Profile* profile) { |
38 DownloadNotificationManagerForProfile* manager_for_profile = | 37 std::unique_ptr<DownloadNotificationManagerForProfile> manager_for_profile = |
39 manager_for_profile_[profile]; | 38 std::move(manager_for_profile_[profile]); |
40 manager_for_profile_.erase(profile); | 39 manager_for_profile_.erase(profile); |
41 | 40 |
42 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, | 41 base::ThreadTaskRunnerHandle::Get()->DeleteSoon( |
43 manager_for_profile); | 42 FROM_HERE, manager_for_profile.release()); |
44 } | 43 } |
45 | 44 |
46 void DownloadNotificationManager::OnNewDownloadReady( | 45 void DownloadNotificationManager::OnNewDownloadReady( |
47 content::DownloadItem* download) { | 46 content::DownloadItem* download) { |
48 Profile* profile = Profile::FromBrowserContext(download->GetBrowserContext()); | 47 Profile* profile = Profile::FromBrowserContext(download->GetBrowserContext()); |
49 | 48 |
50 if (manager_for_profile_.find(profile) == manager_for_profile_.end()) { | 49 if (manager_for_profile_.find(profile) == manager_for_profile_.end()) { |
51 manager_for_profile_[profile] = | 50 manager_for_profile_[profile] = |
52 new DownloadNotificationManagerForProfile(profile, this); | 51 base::MakeUnique<DownloadNotificationManagerForProfile>(profile, this); |
53 } | 52 } |
54 | 53 |
55 manager_for_profile_[profile]->OnNewDownloadReady(download); | 54 manager_for_profile_[profile]->OnNewDownloadReady(download); |
56 } | 55 } |
57 | 56 |
58 DownloadNotificationManagerForProfile* | 57 DownloadNotificationManagerForProfile* |
59 DownloadNotificationManager::GetForProfile(Profile* profile) const { | 58 DownloadNotificationManager::GetForProfile(Profile* profile) const { |
60 return manager_for_profile_.at(profile); | 59 return manager_for_profile_.at(profile).get(); |
61 } | 60 } |
62 | 61 |
63 /////////////////////////////////////////////////////////////////////////////// | 62 /////////////////////////////////////////////////////////////////////////////// |
64 // DownloadNotificationManagerForProfile implementation: | 63 // DownloadNotificationManagerForProfile implementation: |
65 /////////////////////////////////////////////////////////////////////////////// | 64 /////////////////////////////////////////////////////////////////////////////// |
66 | 65 |
67 DownloadNotificationManagerForProfile::DownloadNotificationManagerForProfile( | 66 DownloadNotificationManagerForProfile::DownloadNotificationManagerForProfile( |
68 Profile* profile, | 67 Profile* profile, |
69 DownloadNotificationManager* parent_manager) | 68 DownloadNotificationManager* parent_manager) |
70 : profile_(profile), | 69 : profile_(profile), |
71 parent_manager_(parent_manager), | 70 parent_manager_(parent_manager), |
72 message_center_(g_browser_process->message_center()), | 71 message_center_(g_browser_process->message_center()) {} |
73 items_deleter_(&items_) { | |
74 } | |
75 | 72 |
76 DownloadNotificationManagerForProfile:: | 73 DownloadNotificationManagerForProfile:: |
77 ~DownloadNotificationManagerForProfile() { | 74 ~DownloadNotificationManagerForProfile() { |
78 for (const auto& download : items_) { | 75 for (const auto& download : items_) { |
79 download.first->RemoveObserver(this); | 76 download.first->RemoveObserver(this); |
80 } | 77 } |
81 } | 78 } |
82 | 79 |
83 void DownloadNotificationManagerForProfile::OnDownloadUpdated( | 80 void DownloadNotificationManagerForProfile::OnDownloadUpdated( |
84 content::DownloadItem* changed_download) { | 81 content::DownloadItem* changed_download) { |
85 DCHECK(items_.find(changed_download) != items_.end()); | 82 DCHECK(items_.find(changed_download) != items_.end()); |
86 | 83 |
87 items_[changed_download]->OnDownloadUpdated(changed_download); | 84 items_[changed_download]->OnDownloadUpdated(changed_download); |
88 } | 85 } |
89 | 86 |
90 void DownloadNotificationManagerForProfile::OnDownloadOpened( | 87 void DownloadNotificationManagerForProfile::OnDownloadOpened( |
91 content::DownloadItem* changed_download) { | 88 content::DownloadItem* changed_download) { |
92 items_[changed_download]->OnDownloadUpdated(changed_download); | 89 items_[changed_download]->OnDownloadUpdated(changed_download); |
93 } | 90 } |
94 | 91 |
95 void DownloadNotificationManagerForProfile::OnDownloadRemoved( | 92 void DownloadNotificationManagerForProfile::OnDownloadRemoved( |
96 content::DownloadItem* download) { | 93 content::DownloadItem* download) { |
97 DCHECK(items_.find(download) != items_.end()); | 94 DCHECK(items_.find(download) != items_.end()); |
98 | 95 |
99 DownloadItemNotification* item = items_[download]; | 96 std::unique_ptr<DownloadItemNotification> item = std::move(items_[download]); |
100 items_.erase(download); | 97 items_.erase(download); |
101 | 98 |
102 download->RemoveObserver(this); | 99 download->RemoveObserver(this); |
103 | 100 |
104 // notify | 101 // notify |
105 item->OnDownloadRemoved(download); | 102 item->OnDownloadRemoved(download); |
106 | 103 |
107 // This removing might be initiated from DownloadNotificationItem, so delaying | 104 // This removing might be initiated from DownloadNotificationItem, so delaying |
108 // deleting for item to do remaining cleanups. | 105 // deleting for item to do remaining cleanups. |
109 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, item); | 106 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, item.release()); |
110 | 107 |
111 if (items_.size() == 0 && parent_manager_) | 108 if (items_.size() == 0 && parent_manager_) |
112 parent_manager_->OnAllDownloadsRemoving(profile_); | 109 parent_manager_->OnAllDownloadsRemoving(profile_); |
113 } | 110 } |
114 | 111 |
115 void DownloadNotificationManagerForProfile::OnDownloadDestroyed( | 112 void DownloadNotificationManagerForProfile::OnDownloadDestroyed( |
116 content::DownloadItem* download) { | 113 content::DownloadItem* download) { |
117 // Do nothing. Cleanup is done in OnDownloadRemoved(). | 114 // Do nothing. Cleanup is done in OnDownloadRemoved(). |
118 DownloadItemNotification* item = items_[download]; | 115 std::unique_ptr<DownloadItemNotification> item = std::move(items_[download]); |
119 items_.erase(download); | 116 items_.erase(download); |
120 | 117 |
121 item->OnDownloadRemoved(download); | 118 item->OnDownloadRemoved(download); |
122 | 119 |
123 // This removing might be initiated from DownloadNotificationItem, so delaying | 120 // This removing might be initiated from DownloadNotificationItem, so delaying |
124 // deleting for item to do remaining cleanups. | 121 // deleting for item to do remaining cleanups. |
125 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, item); | 122 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, item.release()); |
126 | 123 |
127 if (items_.size() == 0 && parent_manager_) | 124 if (items_.size() == 0 && parent_manager_) |
128 parent_manager_->OnAllDownloadsRemoving(profile_); | 125 parent_manager_->OnAllDownloadsRemoving(profile_); |
129 } | 126 } |
130 | 127 |
131 void DownloadNotificationManagerForProfile::OnNewDownloadReady( | 128 void DownloadNotificationManagerForProfile::OnNewDownloadReady( |
132 content::DownloadItem* download) { | 129 content::DownloadItem* download) { |
133 DCHECK_EQ(profile_, | 130 DCHECK_EQ(profile_, |
134 Profile::FromBrowserContext(download->GetBrowserContext())); | 131 Profile::FromBrowserContext(download->GetBrowserContext())); |
135 | 132 |
136 download->AddObserver(this); | 133 download->AddObserver(this); |
137 | 134 |
138 for (auto& item : items_) { | 135 for (auto& item : items_) { |
139 content::DownloadItem* download_item = item.first; | 136 content::DownloadItem* download_item = item.first; |
140 DownloadItemNotification* download_notification = item.second; | 137 DownloadItemNotification* download_notification = item.second.get(); |
141 if (download_item->GetState() == content::DownloadItem::IN_PROGRESS) | 138 if (download_item->GetState() == content::DownloadItem::IN_PROGRESS) |
142 download_notification->DisablePopup(); | 139 download_notification->DisablePopup(); |
143 } | 140 } |
144 | 141 |
145 DownloadItemNotification* item = new DownloadItemNotification(download, this); | 142 items_[download] = base::MakeUnique<DownloadItemNotification>(download, this); |
146 items_.insert(std::make_pair(download, item)); | |
147 } | 143 } |
148 | 144 |
149 void DownloadNotificationManagerForProfile::OverrideMessageCenterForTest( | 145 void DownloadNotificationManagerForProfile::OverrideMessageCenterForTest( |
150 message_center::MessageCenter* message_center) { | 146 message_center::MessageCenter* message_center) { |
151 DCHECK(message_center); | 147 DCHECK(message_center); |
152 message_center_ = message_center; | 148 message_center_ = message_center; |
153 } | 149 } |
OLD | NEW |