Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/chromeos/ui/low_disk_notification.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include "ash/common/system/system_notifier.h" | |
| 10 #include "base/bind.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "chrome/browser/profiles/profile_manager.h" | |
| 15 #include "chrome/browser/ui/chrome_pages.h" | |
| 16 #include "chrome/grit/generated_resources.h" | |
| 17 #include "chromeos/dbus/cryptohome_client.h" | |
| 18 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 19 #include "ui/base/l10n/l10n_util.h" | |
| 20 #include "ui/base/resource/resource_bundle.h" | |
| 21 #include "ui/chromeos/resources/grit/ui_chromeos_resources.h" | |
| 22 #include "ui/message_center/message_center.h" | |
| 23 #include "ui/message_center/notification.h" | |
| 24 #include "ui/message_center/notification_types.h" | |
| 25 #include "ui/message_center/notifier_settings.h" | |
| 26 | |
| 27 namespace { | |
|
satorux1
2016/06/27 07:46:39
nit: add a blank line?
dspaid1
2016/06/28 00:39:05
Done.
| |
| 28 const char kLowDiskId[] = "low_disk"; | |
| 29 const char kStoragePage[] = "storage"; | |
| 30 const uint64_t kNotificationThreshold = 1 << 30; // 1GB | |
| 31 const uint64_t kNotificationSevereThreshold = 512 << 20; // 512MB | |
| 32 constexpr base::TimeDelta kNotificationInterval = | |
| 33 base::TimeDelta::FromMinutes(2); | |
| 34 | |
| 35 class LowDiskNotificationDelegate | |
| 36 : public message_center::NotificationDelegate { | |
| 37 public: | |
| 38 LowDiskNotificationDelegate() {} | |
| 39 | |
| 40 // message_center::NotificationDelegate | |
| 41 void ButtonClick(int button_index) override { | |
| 42 chrome::ShowSettingsSubPageForProfile( | |
| 43 ProfileManager::GetActiveUserProfile(), kStoragePage); | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 ~LowDiskNotificationDelegate() override {} | |
|
yoshiki
2016/06/27 08:14:02
nit: please add DISALLOW_COPY_AND_ASSIGN(LowDiskNo
dspaid1
2016/06/28 00:39:06
Done.
| |
| 48 }; | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 namespace chromeos { | |
| 53 | |
| 54 LowDiskNotification::LowDiskNotification() | |
| 55 : message_center_(g_browser_process->message_center()), | |
| 56 notification_interval_(kNotificationInterval), | |
| 57 weak_ptr_factory_(this) { | |
| 58 DCHECK(DBusThreadManager::Get()->GetCryptohomeClient()); | |
| 59 DBusThreadManager::Get()->GetCryptohomeClient()->SetLowDiskSpaceHandler( | |
| 60 base::Bind(&LowDiskNotification::OnLowDiskSpace, | |
| 61 weak_ptr_factory_.GetWeakPtr())); | |
| 62 } | |
| 63 | |
| 64 LowDiskNotification::~LowDiskNotification() { | |
| 65 DCHECK(DBusThreadManager::Get()->GetCryptohomeClient()); | |
| 66 DBusThreadManager::Get()->GetCryptohomeClient()->ResetLowDiskSpaceHandler(); | |
| 67 } | |
| 68 | |
| 69 void LowDiskNotification::OnLowDiskSpace(uint64_t free_disk_bytes) { | |
|
yoshiki
2016/06/27 08:14:02
I don't know much about DBusThreadManager, but is
dspaid1
2016/06/28 00:39:06
As far as I can tell based on comments this should
| |
| 70 LOG(ERROR) << "LowDiskNotification received"; | |
| 71 Severity severity = GetSeverity(free_disk_bytes); | |
| 72 base::Time now = base::Time::Now(); | |
| 73 if (severity != last_notification_severity_ || | |
| 74 now - last_notification_time_ > notification_interval_) { | |
| 75 message_center_->AddNotification(CreateNotification(severity)); | |
| 76 last_notification_time_ = now; | |
| 77 last_notification_severity_ = severity; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 std::unique_ptr<message_center::Notification> | |
| 82 LowDiskNotification::CreateNotification(Severity severity) { | |
| 83 base::string16 title; | |
| 84 base::string16 message; | |
| 85 gfx::Image icon; | |
| 86 switch (severity) { | |
| 87 case HIGH: | |
| 88 title = | |
| 89 l10n_util::GetStringUTF16(IDS_CRITICALLY_LOW_DISK_NOTIFICATION_TITLE); | |
| 90 message = l10n_util::GetStringUTF16( | |
| 91 IDS_CRITICALLY_LOW_DISK_NOTIFICATION_MESSAGE); | |
| 92 icon = gfx::Image(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 93 IDR_DISK_SPACE_NOTIFICATION_CRITICAL)); | |
| 94 break; | |
| 95 default: | |
| 96 title = l10n_util::GetStringUTF16(IDS_LOW_DISK_NOTIFICATION_TITLE); | |
| 97 message = l10n_util::GetStringUTF16(IDS_LOW_DISK_NOTIFICATION_MESSAGE); | |
| 98 icon = gfx::Image(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 99 IDR_DISK_SPACE_NOTIFICATION_LOW)); | |
| 100 } | |
| 101 | |
| 102 message_center::ButtonInfo storage_settings( | |
| 103 l10n_util::GetStringUTF16(IDS_LOW_DISK_NOTIFICATION_BUTTON)); | |
| 104 storage_settings.icon = ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 105 IDR_STORAGE_MANAGER_BUTTON); | |
| 106 message_center::RichNotificationData optional_fields; | |
| 107 optional_fields.buttons.push_back(storage_settings); | |
| 108 | |
| 109 message_center::NotifierId notifier_id( | |
| 110 message_center::NotifierId::SYSTEM_COMPONENT, | |
| 111 ash::system_notifier::kNotifierDisk); | |
| 112 | |
| 113 std::unique_ptr<message_center::Notification> notification( | |
| 114 new message_center::Notification( | |
| 115 message_center::NOTIFICATION_TYPE_SIMPLE, kLowDiskId, title, message, | |
| 116 icon, base::string16(), GURL(), notifier_id, optional_fields, | |
| 117 new LowDiskNotificationDelegate())); | |
| 118 notification->set_pinned(true); | |
|
yoshiki
2016/06/27 08:14:02
If you use pinned notification, you need to add th
dspaid1
2016/06/28 00:39:05
Since we don't have an easy way to determine when
| |
| 119 | |
| 120 return notification; | |
| 121 } | |
| 122 | |
| 123 LowDiskNotification::Severity LowDiskNotification::GetSeverity( | |
| 124 uint64_t free_disk_bytes) { | |
| 125 if (free_disk_bytes < kNotificationSevereThreshold) | |
| 126 return Severity::HIGH; | |
| 127 if (free_disk_bytes < kNotificationThreshold) | |
| 128 return Severity::MEDIUM; | |
| 129 return Severity::NONE; | |
| 130 } | |
| 131 | |
| 132 void LowDiskNotification::SetMessageCenterForTest( | |
| 133 message_center::MessageCenter* message_center) { | |
| 134 message_center_ = message_center; | |
| 135 } | |
| 136 | |
| 137 void LowDiskNotification::SetNotificationIntervalForTest( | |
| 138 base::TimeDelta notification_interval) { | |
| 139 notification_interval_ = notification_interval; | |
| 140 } | |
| 141 | |
| 142 } // namespace chromeos | |
| OLD | NEW |