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/extensions/api/file_system/request_file_system_notification.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chromeos/file_manager/volume_manager.h"
12 #include "chrome/browser/extensions/app_icon_loader_impl.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "extensions/common/extension.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/message_center/message_center.h"
17 #include "ui/message_center/notification.h"
18 #include "ui/message_center/notification_delegate.h"
19 #include "ui/message_center/notification_types.h"
20 #include "ui/message_center/notifier_settings.h"
22 using file_manager::Volume
;
23 using message_center::Notification
;
27 // Extension icon size for the notification.
28 const int kIconSize
= 48;
30 scoped_ptr
<Notification
> CreateAutoGrantedNotification(
31 const extensions::Extension
& extension
,
32 const base::WeakPtr
<Volume
>& volume
,
34 message_center::NotificationDelegate
* delegate
) {
37 // If the volume is gone, then do not show the notification.
39 return scoped_ptr
<Notification
>(nullptr);
41 const std::string notification_id
=
42 extension
.id() + "-" + volume
->volume_id();
43 message_center::RichNotificationData data
;
44 data
.clickable
= false;
46 // TODO(mtomasz): Share this code with RequestFileSystemDialogView.
47 const base::string16 display_name
=
48 base::UTF8ToUTF16(!volume
->volume_label().empty() ? volume
->volume_label()
49 : volume
->volume_id());
50 const base::string16 message
= l10n_util::GetStringFUTF16(
52 ? IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_NOTIFICATION_WRITABLE_MESSAGE
53 : IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_NOTIFICATION_MESSAGE
,
56 scoped_ptr
<Notification
> notification(new Notification(
57 message_center::NOTIFICATION_TYPE_SIMPLE
, notification_id
,
58 base::UTF8ToUTF16(extension
.name()), message
,
59 gfx::Image(), // Updated asynchronously later.
60 base::string16(), // display_source
61 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT
,
65 return notification
.Pass();
71 void RequestFileSystemNotification::ShowAutoGrantedNotification(
73 const extensions::Extension
& extension
,
74 const base::WeakPtr
<Volume
>& volume
,
77 scoped_refptr
<RequestFileSystemNotification
>
78 request_file_system_notification
= make_scoped_refptr(
79 new RequestFileSystemNotification(profile
, extension
));
80 scoped_ptr
<message_center::Notification
> notification(
81 CreateAutoGrantedNotification(
82 extension
, volume
, writable
,
83 request_file_system_notification
.get() /* delegate */));
84 if (notification
.get())
85 request_file_system_notification
->Show(notification
.Pass());
88 void RequestFileSystemNotification::SetAppImage(const std::string
& id
,
89 const gfx::ImageSkia
& image
) {
90 extension_icon_
.reset(new gfx::Image(image
));
92 // If there is a pending notification, then show it now.
93 if (pending_notification_
.get()) {
94 pending_notification_
->set_icon(*extension_icon_
.get());
95 g_browser_process
->message_center()->AddNotification(
96 pending_notification_
.Pass());
100 RequestFileSystemNotification::RequestFileSystemNotification(
102 const extensions::Extension
& extension
)
104 new extensions::AppIconLoaderImpl(profile
, kIconSize
, this)) {
105 icon_loader_
->FetchImage(extension
.id());
108 RequestFileSystemNotification::~RequestFileSystemNotification() {
111 void RequestFileSystemNotification::Show(
112 scoped_ptr
<Notification
> notification
) {
113 pending_notification_
.reset(notification
.release());
114 // If the extension icon is not known yet, then defer showing the notification
115 // until it is (from SetAppImage).
116 if (!extension_icon_
.get())
119 pending_notification_
->set_icon(*extension_icon_
.get());
120 g_browser_process
->message_center()->AddNotification(
121 pending_notification_
.Pass());