1 // Copyright 2014 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/chromeos/file_system_provider/notification_manager.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/extensions/app_icon_loader_impl.h"
10 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/message_center/message_center.h"
14 #include "ui/message_center/notification.h"
15 #include "ui/message_center/notification_delegate.h"
16 #include "ui/message_center/notification_types.h"
17 #include "ui/message_center/notifier_settings.h"
20 namespace file_system_provider
{
23 // Extension icon size for the notification.
24 const int kIconSize
= 48;
26 // Forwards notification events to the notification manager.
27 class ProviderNotificationDelegate
28 : public message_center::NotificationDelegate
{
30 // Passing a raw pointer is safe here, since the life of each notification is
31 // shorter than life of the |notification_manager|.
32 explicit ProviderNotificationDelegate(
33 NotificationManager
* notification_manager
)
34 : notification_manager_(notification_manager
) {}
36 void ButtonClick(int button_index
) override
{
37 notification_manager_
->OnButtonClick(button_index
);
40 void Close(bool by_user
) override
{ notification_manager_
->OnClose(); }
43 ~ProviderNotificationDelegate() override
{}
44 NotificationManager
* notification_manager_
; // Not owned.
46 DISALLOW_COPY_AND_ASSIGN(ProviderNotificationDelegate
);
51 NotificationManager::NotificationManager(
53 const ProvidedFileSystemInfo
& file_system_info
)
55 file_system_info_(file_system_info
),
57 new extensions::AppIconLoaderImpl(profile
, kIconSize
, this)) {
60 NotificationManager::~NotificationManager() {
61 if (callbacks_
.size()) {
62 g_browser_process
->message_center()->RemoveNotification(
63 file_system_info_
.mount_path().value(), false /* by_user */);
67 void NotificationManager::ShowUnresponsiveNotification(
69 const NotificationCallback
& callback
) {
70 callbacks_
[id
] = callback
;
72 if (callbacks_
.size() == 1) {
73 g_browser_process
->message_center()->AddNotification(CreateNotification());
75 g_browser_process
->message_center()->UpdateNotification(
76 file_system_info_
.mount_path().value(), CreateNotification());
80 void NotificationManager::HideUnresponsiveNotification(int id
) {
83 if (callbacks_
.size()) {
84 g_browser_process
->message_center()->UpdateNotification(
85 file_system_info_
.mount_path().value(), CreateNotification());
89 g_browser_process
->message_center()->RemoveNotification(
90 file_system_info_
.mount_path().value(), false /* by_user */);
93 void NotificationManager::OnButtonClick(int button_index
) {
94 OnNotificationResult(ABORT
);
97 void NotificationManager::OnClose() {
98 OnNotificationResult(CONTINUE
);
101 void NotificationManager::SetAppImage(const std::string
& id
,
102 const gfx::ImageSkia
& image
) {
103 extension_icon_
.reset(new gfx::Image(image
));
104 g_browser_process
->message_center()->UpdateNotification(
105 file_system_info_
.mount_path().value(), CreateNotification());
108 scoped_ptr
<message_center::Notification
>
109 NotificationManager::CreateNotification() {
110 if (!extension_icon_
.get())
111 icon_loader_
->FetchImage(file_system_info_
.extension_id());
113 message_center::RichNotificationData rich_notification_data
;
114 rich_notification_data
.buttons
.push_back(
115 message_center::ButtonInfo(l10n_util::GetStringUTF16(
116 IDS_FILE_SYSTEM_PROVIDER_UNRESPONSIVE_ABORT_BUTTON
)));
118 message_center::NotifierId
notifier_id(
119 message_center::NotifierId::SYSTEM_COMPONENT
,
120 file_system_info_
.mount_path().value());
121 notifier_id
.profile_id
= multi_user_util::GetUserIDFromProfile(profile_
);
123 scoped_ptr
<message_center::Notification
> notification(
124 new message_center::Notification(
125 message_center::NOTIFICATION_TYPE_SIMPLE
,
126 file_system_info_
.mount_path().value(),
127 base::UTF8ToUTF16(file_system_info_
.display_name()),
128 l10n_util::GetStringUTF16(
129 callbacks_
.size() == 1
130 ? IDS_FILE_SYSTEM_PROVIDER_UNRESPONSIVE_WARNING
131 : IDS_FILE_SYSTEM_PROVIDER_MANY_UNRESPONSIVE_WARNING
),
132 extension_icon_
.get() ? *extension_icon_
.get() : gfx::Image(),
133 base::string16(), // display_source
134 GURL(), notifier_id
, rich_notification_data
,
135 new ProviderNotificationDelegate(this)));
137 notification
->SetSystemPriority();
138 return notification
.Pass();
141 void NotificationManager::OnNotificationResult(NotificationResult result
) {
142 CallbackMap::iterator it
= callbacks_
.begin();
143 while (it
!= callbacks_
.end()) {
144 CallbackMap::iterator current_it
= it
++;
145 NotificationCallback callback
= current_it
->second
;
146 callbacks_
.erase(current_it
);
147 callback
.Run(result
);
151 } // namespace file_system_provider
152 } // namespace chromeos