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/grit/generated_resources.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/message_center/message_center.h"
13 #include "ui/message_center/notification.h"
14 #include "ui/message_center/notification_delegate.h"
15 #include "ui/message_center/notification_types.h"
16 #include "ui/message_center/notifier_settings.h"
19 namespace file_system_provider
{
22 // Extension icon size for the notification.
23 const int kIconSize
= 48;
25 // Forwards notification events to the notification manager.
26 class ProviderNotificationDelegate
27 : public message_center::NotificationDelegate
{
29 // Passing a raw pointer is safe here, since the life of each notification is
30 // shorter than life of the |notification_manager|.
31 explicit ProviderNotificationDelegate(
32 NotificationManager
* notification_manager
)
33 : notification_manager_(notification_manager
) {}
35 void ButtonClick(int button_index
) override
{
36 notification_manager_
->OnButtonClick(button_index
);
39 void Close(bool by_user
) override
{ notification_manager_
->OnClose(); }
42 ~ProviderNotificationDelegate() override
{}
43 NotificationManager
* notification_manager_
; // Not owned.
45 DISALLOW_COPY_AND_ASSIGN(ProviderNotificationDelegate
);
50 NotificationManager::NotificationManager(
52 const ProvidedFileSystemInfo
& file_system_info
)
53 : file_system_info_(file_system_info
),
55 new extensions::AppIconLoaderImpl(profile
, kIconSize
, this)) {
58 NotificationManager::~NotificationManager() {
59 if (callbacks_
.size()) {
60 g_browser_process
->message_center()->RemoveNotification(
61 file_system_info_
.mount_path().value(), false /* by_user */);
65 void NotificationManager::ShowUnresponsiveNotification(
67 const NotificationCallback
& callback
) {
68 callbacks_
[id
] = callback
;
70 if (callbacks_
.size() == 1) {
71 g_browser_process
->message_center()->AddNotification(CreateNotification());
73 g_browser_process
->message_center()->UpdateNotification(
74 file_system_info_
.mount_path().value(), CreateNotification());
78 void NotificationManager::HideUnresponsiveNotification(int id
) {
81 if (callbacks_
.size()) {
82 g_browser_process
->message_center()->UpdateNotification(
83 file_system_info_
.mount_path().value(), CreateNotification());
87 g_browser_process
->message_center()->RemoveNotification(
88 file_system_info_
.mount_path().value(), false /* by_user */);
91 void NotificationManager::OnButtonClick(int button_index
) {
92 OnNotificationResult(ABORT
);
95 void NotificationManager::OnClose() {
96 OnNotificationResult(CONTINUE
);
99 void NotificationManager::SetAppImage(const std::string
& id
,
100 const gfx::ImageSkia
& image
) {
101 extension_icon_
.reset(new gfx::Image(image
));
102 g_browser_process
->message_center()->UpdateNotification(
103 file_system_info_
.mount_path().value(), CreateNotification());
106 scoped_ptr
<message_center::Notification
>
107 NotificationManager::CreateNotification() {
108 if (!extension_icon_
.get())
109 icon_loader_
->FetchImage(file_system_info_
.extension_id());
111 message_center::RichNotificationData rich_notification_data
;
112 rich_notification_data
.buttons
.push_back(
113 message_center::ButtonInfo(l10n_util::GetStringUTF16(
114 IDS_FILE_SYSTEM_PROVIDER_UNRESPONSIVE_ABORT_BUTTON
)));
116 scoped_ptr
<message_center::Notification
> notification(
117 new message_center::Notification(
118 message_center::NOTIFICATION_TYPE_SIMPLE
,
119 file_system_info_
.mount_path().value(),
120 base::UTF8ToUTF16(file_system_info_
.display_name()),
121 l10n_util::GetStringUTF16(
122 callbacks_
.size() == 1
123 ? IDS_FILE_SYSTEM_PROVIDER_UNRESPONSIVE_WARNING
124 : IDS_FILE_SYSTEM_PROVIDER_MANY_UNRESPONSIVE_WARNING
),
125 extension_icon_
.get() ? *extension_icon_
.get() : gfx::Image(),
126 base::string16(), // display_source
127 message_center::NotifierId(
128 message_center::NotifierId::SYSTEM_COMPONENT
,
129 file_system_info_
.mount_path().value()),
130 rich_notification_data
,
131 new ProviderNotificationDelegate(this)));
133 notification
->SetSystemPriority();
134 return notification
.Pass();
137 void NotificationManager::OnNotificationResult(NotificationResult result
) {
138 CallbackMap::iterator it
= callbacks_
.begin();
139 while (it
!= callbacks_
.end()) {
140 CallbackMap::iterator current_it
= it
++;
141 NotificationCallback callback
= current_it
->second
;
142 callbacks_
.erase(current_it
);
143 callback
.Run(result
);
147 } // namespace file_system_provider
148 } // namespace chromeos