| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/extensions/api/file_system/request_file_system_notifica
tion.h" |
| 6 |
| 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" |
| 21 |
| 22 using file_manager::Volume; |
| 23 using message_center::Notification; |
| 24 |
| 25 namespace { |
| 26 |
| 27 // Extension icon size for the notification. |
| 28 const int kIconSize = 48; |
| 29 |
| 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); |
| 36 |
| 37 // If the volume is gone, then do not show the notification. |
| 38 if (!volume.get()) |
| 39 return scoped_ptr<Notification>(nullptr); |
| 40 |
| 41 const std::string notification_id = |
| 42 extension.id() + "-" + volume->volume_id(); |
| 43 message_center::RichNotificationData data; |
| 44 data.clickable = false; |
| 45 |
| 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); |
| 55 |
| 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, |
| 62 notification_id), |
| 63 data, delegate)); |
| 64 |
| 65 return notification.Pass(); |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 |
| 70 // static |
| 71 void RequestFileSystemNotification::ShowAutoGrantedNotification( |
| 72 Profile* profile, |
| 73 const extensions::Extension& extension, |
| 74 const base::WeakPtr<Volume>& volume, |
| 75 bool writable) { |
| 76 DCHECK(profile); |
| 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()); |
| 86 } |
| 87 |
| 88 void RequestFileSystemNotification::SetAppImage(const std::string& id, |
| 89 const gfx::ImageSkia& image) { |
| 90 extension_icon_.reset(new gfx::Image(image)); |
| 91 |
| 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()); |
| 97 } |
| 98 } |
| 99 |
| 100 RequestFileSystemNotification::RequestFileSystemNotification( |
| 101 Profile* profile, |
| 102 const extensions::Extension& extension) |
| 103 : icon_loader_( |
| 104 new extensions::AppIconLoaderImpl(profile, kIconSize, this)) { |
| 105 icon_loader_->FetchImage(extension.id()); |
| 106 } |
| 107 |
| 108 RequestFileSystemNotification::~RequestFileSystemNotification() { |
| 109 } |
| 110 |
| 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()) |
| 117 return; |
| 118 |
| 119 pending_notification_->set_icon(*extension_icon_.get()); |
| 120 g_browser_process->message_center()->AddNotification( |
| 121 pending_notification_.Pass()); |
| 122 } |
| OLD | NEW |