Chromium Code Reviews| 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 // TODO(mtomasz): Take into account DPI. | |
|
benwells
2015/04/07 08:31:33
You don't need this TODO, it is already taken into
mtomasz
2015/04/08 00:30:46
Done.
| |
| 29 const int kIconSize = 48; | |
| 30 | |
| 31 scoped_ptr<Notification> CreateAutoGrantedNotification( | |
| 32 const extensions::Extension& extension, | |
| 33 const base::WeakPtr<Volume>& volume, | |
| 34 bool writable, | |
| 35 message_center::NotificationDelegate* delegate) { | |
| 36 DCHECK(delegate); | |
| 37 | |
| 38 // If the volume is gone, then do not show the notification. | |
| 39 if (!volume.get()) | |
| 40 return scoped_ptr<Notification>(nullptr); | |
| 41 | |
| 42 const std::string notification_id = | |
| 43 extension.id() + "-" + volume->volume_id(); | |
| 44 message_center::RichNotificationData data; | |
| 45 data.clickable = false; | |
| 46 | |
| 47 // TODO(mtomasz): Share this code with RequestFileSystemDialogView. | |
| 48 const base::string16 display_name = | |
| 49 base::UTF8ToUTF16(!volume->volume_label().empty() ? volume->volume_label() | |
| 50 : volume->volume_id()); | |
| 51 const base::string16 message = l10n_util::GetStringFUTF16( | |
| 52 writable | |
| 53 ? IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_NOTIFICATION_WRITABLE_MESSAGE | |
| 54 : IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_NOTIFICATION_MESSAGE, | |
| 55 display_name); | |
| 56 | |
| 57 scoped_ptr<Notification> notification(new Notification( | |
| 58 message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, | |
| 59 base::UTF8ToUTF16(extension.name()), message, | |
| 60 gfx::Image(), // Updated asynchronously later. | |
| 61 base::string16(), // display_source | |
| 62 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
| 63 notification_id), | |
| 64 data, delegate)); | |
| 65 | |
| 66 return notification.Pass(); | |
| 67 } | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 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()); | |
| 87 } | |
| 88 | |
| 89 void RequestFileSystemNotification::SetAppImage(const std::string& id, | |
| 90 const gfx::ImageSkia& image) { | |
| 91 extension_icon_.reset(new gfx::Image(image)); | |
| 92 | |
| 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()); | |
| 98 } | |
| 99 } | |
| 100 | |
| 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()); | |
| 107 } | |
| 108 | |
| 109 RequestFileSystemNotification::~RequestFileSystemNotification() { | |
| 110 } | |
| 111 | |
| 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; | |
| 119 | |
| 120 pending_notification_->set_icon(*extension_icon_.get()); | |
| 121 g_browser_process->message_center()->AddNotification( | |
| 122 pending_notification_.Pass()); | |
| 123 } | |
| OLD | NEW |