Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / file_system / request_file_system_notification.cc
blob7a5bbb5cf4b700a65dc69a45e6131c6df54860bf
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;
25 namespace {
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,
33 bool writable,
34 message_center::NotificationDelegate* delegate) {
35 DCHECK(delegate);
37 // If the volume is gone, then do not show the notification.
38 if (!volume.get())
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(
51 writable
52 ? IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_NOTIFICATION_WRITABLE_MESSAGE
53 : IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_NOTIFICATION_MESSAGE,
54 display_name);
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 GURL(),
62 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
63 notification_id),
64 data, delegate));
66 return notification.Pass();
69 } // namespace
71 // static
72 void RequestFileSystemNotification::ShowAutoGrantedNotification(
73 Profile* profile,
74 const extensions::Extension& extension,
75 const base::WeakPtr<Volume>& volume,
76 bool writable) {
77 DCHECK(profile);
78 scoped_refptr<RequestFileSystemNotification>
79 request_file_system_notification = make_scoped_refptr(
80 new RequestFileSystemNotification(profile, extension));
81 scoped_ptr<message_center::Notification> notification(
82 CreateAutoGrantedNotification(
83 extension, volume, writable,
84 request_file_system_notification.get() /* delegate */));
85 if (notification.get())
86 request_file_system_notification->Show(notification.Pass());
89 void RequestFileSystemNotification::SetAppImage(const std::string& id,
90 const gfx::ImageSkia& image) {
91 extension_icon_.reset(new gfx::Image(image));
93 // If there is a pending notification, then show it now.
94 if (pending_notification_.get()) {
95 pending_notification_->set_icon(*extension_icon_.get());
96 g_browser_process->message_center()->AddNotification(
97 pending_notification_.Pass());
101 RequestFileSystemNotification::RequestFileSystemNotification(
102 Profile* profile,
103 const extensions::Extension& extension)
104 : icon_loader_(
105 new extensions::AppIconLoaderImpl(profile, kIconSize, this)) {
106 icon_loader_->FetchImage(extension.id());
109 RequestFileSystemNotification::~RequestFileSystemNotification() {
112 void RequestFileSystemNotification::Show(
113 scoped_ptr<Notification> notification) {
114 pending_notification_.reset(notification.release());
115 // If the extension icon is not known yet, then defer showing the notification
116 // until it is (from SetAppImage).
117 if (!extension_icon_.get())
118 return;
120 pending_notification_->set_icon(*extension_icon_.get());
121 g_browser_process->message_center()->AddNotification(
122 pending_notification_.Pass());