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_item_notification.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/grit/chromium_strings.h"
14 #include "content/public/browser/download_item.h"
15 #include "grit/theme_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/message_center/message_center.h"
19 #include "ui/message_center/notification.h"
20 #include "ui/message_center/notification_delegate.h"
22 ///////////////////////////////////////////////////////////////////////////////
23 // DownloadNotificationManager implementation:
24 ///////////////////////////////////////////////////////////////////////////////
26 bool DownloadNotificationManager::IsEnabled() {
27 #if defined(OS_CHROMEOS)
28 bool enable_download_notification
= true;
30 bool enable_download_notification
= false;
33 std::string arg
= base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
34 switches::kEnableDownloadNotification
);
37 enable_download_notification
= true;
38 else if (arg
== "disabled")
39 enable_download_notification
= false;
41 return enable_download_notification
;
44 DownloadNotificationManager::DownloadNotificationManager(Profile
* profile
)
45 : main_profile_(profile
),
46 items_deleter_(&manager_for_profile_
) {
49 DownloadNotificationManager::~DownloadNotificationManager() {
52 void DownloadNotificationManager::OnAllDownloadsRemoving(Profile
* profile
) {
53 DownloadNotificationManagerForProfile
* manager_for_profile
=
54 manager_for_profile_
[profile
];
55 manager_for_profile_
.erase(profile
);
57 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, manager_for_profile
);
60 void DownloadNotificationManager::OnNewDownloadReady(
61 content::DownloadItem
* download
) {
62 Profile
* profile
= Profile::FromBrowserContext(download
->GetBrowserContext());
64 if (manager_for_profile_
.find(profile
) == manager_for_profile_
.end()) {
65 manager_for_profile_
[profile
] =
66 new DownloadNotificationManagerForProfile(profile
, this);
69 manager_for_profile_
[profile
]->OnNewDownloadReady(download
);
72 DownloadNotificationManagerForProfile
*
73 DownloadNotificationManager::GetForProfile(Profile
* profile
) const {
74 return manager_for_profile_
.at(profile
);
77 ///////////////////////////////////////////////////////////////////////////////
78 // DownloadNotificationManagerForProfile implementation:
79 ///////////////////////////////////////////////////////////////////////////////
81 DownloadNotificationManagerForProfile::DownloadNotificationManagerForProfile(
83 DownloadNotificationManager
* parent_manager
)
85 parent_manager_(parent_manager
),
86 items_deleter_(&items_
) {
89 DownloadNotificationManagerForProfile::
90 ~DownloadNotificationManagerForProfile() {
91 for (auto download
: items_
) {
92 download
.first
->RemoveObserver(this);
96 void DownloadNotificationManagerForProfile::OnDownloadUpdated(
97 content::DownloadItem
* changed_download
) {
98 DCHECK(items_
.find(changed_download
) != items_
.end());
100 items_
[changed_download
]->OnDownloadUpdated(changed_download
);
103 void DownloadNotificationManagerForProfile::OnDownloadOpened(
104 content::DownloadItem
* changed_download
) {
105 items_
[changed_download
]->OnDownloadUpdated(changed_download
);
108 void DownloadNotificationManagerForProfile::OnDownloadRemoved(
109 content::DownloadItem
* download
) {
110 DCHECK(items_
.find(download
) != items_
.end());
112 DownloadItemNotification
* item
= items_
[download
];
113 items_
.erase(download
);
115 download
->RemoveObserver(this);
118 item
->OnDownloadRemoved(download
);
120 // This removing might be initiated from DownloadNotificationItem, so delaying
121 // deleting for item to do remaining cleanups.
122 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, item
);
124 if (items_
.size() == 0 && parent_manager_
)
125 parent_manager_
->OnAllDownloadsRemoving(profile_
);
128 void DownloadNotificationManagerForProfile::OnDownloadDestroyed(
129 content::DownloadItem
* download
) {
130 // Do nothing. Cleanup is done in OnDownloadRemoved().
131 DownloadItemNotification
* item
= items_
[download
];
132 items_
.erase(download
);
134 item
->OnDownloadRemoved(download
);
136 // This removing might be initiated from DownloadNotificationItem, so delaying
137 // deleting for item to do remaining cleanups.
138 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, item
);
140 if (items_
.size() == 0 && parent_manager_
)
141 parent_manager_
->OnAllDownloadsRemoving(profile_
);
144 void DownloadNotificationManagerForProfile::OnNewDownloadReady(
145 content::DownloadItem
* download
) {
147 Profile::FromBrowserContext(download
->GetBrowserContext()));
149 download
->AddObserver(this);
151 // |item| object will be inserted to |items_| by |OnCreated()| called in the
153 DownloadItemNotification
* item
= new DownloadItemNotification(download
, this);
154 items_
.insert(std::make_pair(download
, item
));