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