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
3 // found in the LICENSE file.
5 #include "chrome/browser/download/notification/download_notification_manager.h"
7 #include "base/command_line.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/download/download_item_model.h"
11 #include "chrome/browser/download/notification/download_group_notification.h"
12 #include "chrome/browser/download/notification/download_item_notification.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/grit/chromium_strings.h"
15 #include "content/public/browser/download_item.h"
16 #include "grit/theme_resources.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/message_center/message_center.h"
20 #include "ui/message_center/notification.h"
21 #include "ui/message_center/notification_delegate.h"
23 ///////////////////////////////////////////////////////////////////////////////
24 // DownloadNotificationManager implementation:
25 ///////////////////////////////////////////////////////////////////////////////
27 bool DownloadNotificationManager::IsEnabled() {
28 #if defined(OS_CHROMEOS)
29 bool enable_download_notification
= true;
31 bool enable_download_notification
= false;
34 std::string arg
= base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
35 switches::kEnableDownloadNotification
);
38 enable_download_notification
= true;
39 else if (arg
== "disabled")
40 enable_download_notification
= false;
42 return enable_download_notification
;
45 DownloadNotificationManager::DownloadNotificationManager(Profile
* profile
)
46 : main_profile_(profile
),
47 items_deleter_(&manager_for_profile_
) {
50 DownloadNotificationManager::~DownloadNotificationManager() {
53 void DownloadNotificationManager::OnAllDownloadsRemoving(Profile
* profile
) {
54 DownloadNotificationManagerForProfile
* manager_for_profile
=
55 manager_for_profile_
[profile
];
56 manager_for_profile_
.erase(profile
);
58 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, manager_for_profile
);
61 void DownloadNotificationManager::OnNewDownloadReady(
62 content::DownloadItem
* download
) {
63 Profile
* profile
= Profile::FromBrowserContext(download
->GetBrowserContext());
65 if (manager_for_profile_
.find(profile
) == manager_for_profile_
.end()) {
66 manager_for_profile_
[profile
] =
67 new DownloadNotificationManagerForProfile(profile
, this);
70 manager_for_profile_
[profile
]->OnNewDownloadReady(download
);
73 DownloadNotificationManagerForProfile
*
74 DownloadNotificationManager::GetForProfile(Profile
* profile
) const {
75 return manager_for_profile_
.at(profile
);
78 ///////////////////////////////////////////////////////////////////////////////
79 // DownloadNotificationManagerForProfile implementation:
80 ///////////////////////////////////////////////////////////////////////////////
82 DownloadNotificationManagerForProfile::DownloadNotificationManagerForProfile(
84 DownloadNotificationManager
* parent_manager
)
86 parent_manager_(parent_manager
),
87 items_deleter_(&items_
) {
88 group_notification_
.reset(
89 new DownloadGroupNotification(profile_
, this));
92 DownloadNotificationManagerForProfile::
93 ~DownloadNotificationManagerForProfile() {
94 for (auto download
: items_
) {
95 download
.first
->RemoveObserver(this);
99 void DownloadNotificationManagerForProfile::OnDownloadUpdated(
100 content::DownloadItem
* changed_download
) {
101 DCHECK(items_
.find(changed_download
) != items_
.end());
103 items_
[changed_download
]->OnDownloadUpdated(changed_download
);
104 group_notification_
->OnDownloadUpdated(changed_download
);
107 void DownloadNotificationManagerForProfile::OnDownloadOpened(
108 content::DownloadItem
* changed_download
) {
109 items_
[changed_download
]->OnDownloadUpdated(changed_download
);
110 group_notification_
->OnDownloadUpdated(changed_download
);
113 void DownloadNotificationManagerForProfile::OnDownloadRemoved(
114 content::DownloadItem
* download
) {
115 DCHECK(items_
.find(download
) != items_
.end());
117 DownloadItemNotification
* item
= items_
[download
];
118 items_
.erase(download
);
120 download
->RemoveObserver(this);
123 item
->OnDownloadRemoved(download
);
124 group_notification_
->OnDownloadRemoved(download
);
126 // This removing might be initiated from DownloadNotificationItem, so delaying
127 // deleting for item to do remaining cleanups.
128 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, item
);
130 if (items_
.size() == 0 && parent_manager_
)
131 parent_manager_
->OnAllDownloadsRemoving(profile_
);
134 void DownloadNotificationManagerForProfile::OnDownloadDestroyed(
135 content::DownloadItem
* download
) {
136 // Do nothing. Cleanup is done in OnDownloadRemoved().
137 DownloadItemNotification
* item
= items_
[download
];
138 items_
.erase(download
);
140 item
->OnDownloadRemoved(download
);
141 group_notification_
->OnDownloadRemoved(download
);
143 // This removing might be initiated from DownloadNotificationItem, so delaying
144 // deleting for item to do remaining cleanups.
145 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, item
);
147 if (items_
.size() == 0 && parent_manager_
)
148 parent_manager_
->OnAllDownloadsRemoving(profile_
);
151 void DownloadNotificationManagerForProfile::OnNewDownloadReady(
152 content::DownloadItem
* download
) {
154 Profile::FromBrowserContext(download
->GetBrowserContext()));
156 download
->AddObserver(this);
158 // |item| object will be inserted to |items_| by |OnCreated()| called in the
160 DownloadItemNotification
* item
= new DownloadItemNotification(download
, this);
161 items_
.insert(std::make_pair(download
, item
));
163 group_notification_
->OnDownloadAdded(download
);
166 DownloadGroupNotification
*
167 DownloadNotificationManagerForProfile::GetGroupNotification() const {
168 return group_notification_
.get();