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 "content/public/browser/browser_thread.h" |
| 20 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/base/resource/resource_bundle.h" |
| 22 #include "ui/chromeos/resources/grit/ui_chromeos_resources.h" |
| 23 #include "ui/message_center/message_center.h" |
| 24 #include "ui/message_center/notification.h" |
| 25 #include "ui/message_center/notification_types.h" |
| 26 #include "ui/message_center/notifier_settings.h" |
| 27 |
| 28 namespace { |
| 29 |
| 30 const char kLowDiskId[] = "low_disk"; |
| 31 const char kStoragePage[] = "storage"; |
| 32 const uint64_t kNotificationThreshold = 1 << 30; // 1GB |
| 33 const uint64_t kNotificationSevereThreshold = 512 << 20; // 512MB |
| 34 constexpr base::TimeDelta kNotificationInterval = |
| 35 base::TimeDelta::FromMinutes(2); |
| 36 |
| 37 class LowDiskNotificationDelegate |
| 38 : public message_center::NotificationDelegate { |
| 39 public: |
| 40 LowDiskNotificationDelegate() {} |
| 41 |
| 42 // message_center::NotificationDelegate |
| 43 void ButtonClick(int button_index) override { |
| 44 chrome::ShowSettingsSubPageForProfile( |
| 45 ProfileManager::GetActiveUserProfile(), kStoragePage); |
| 46 } |
| 47 |
| 48 private: |
| 49 ~LowDiskNotificationDelegate() override {} |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(LowDiskNotificationDelegate); |
| 52 }; |
| 53 |
| 54 } // namespace |
| 55 |
| 56 namespace chromeos { |
| 57 |
| 58 LowDiskNotification::LowDiskNotification() |
| 59 : message_center_(g_browser_process->message_center()), |
| 60 notification_interval_(kNotificationInterval), |
| 61 weak_ptr_factory_(this) { |
| 62 DCHECK(DBusThreadManager::Get()->GetCryptohomeClient()); |
| 63 DBusThreadManager::Get()->GetCryptohomeClient()->SetLowDiskSpaceHandler( |
| 64 base::Bind(&LowDiskNotification::OnLowDiskSpace, |
| 65 weak_ptr_factory_.GetWeakPtr())); |
| 66 } |
| 67 |
| 68 LowDiskNotification::~LowDiskNotification() { |
| 69 DCHECK(DBusThreadManager::Get()->GetCryptohomeClient()); |
| 70 DBusThreadManager::Get()->GetCryptohomeClient()->SetLowDiskSpaceHandler( |
| 71 CryptohomeClient::LowDiskSpaceHandler()); |
| 72 } |
| 73 |
| 74 void LowDiskNotification::OnLowDiskSpace(uint64_t free_disk_bytes) { |
| 75 DCHECK(thread_checker_.CalledOnValidThread()); |
| 76 Severity severity = GetSeverity(free_disk_bytes); |
| 77 base::Time now = base::Time::Now(); |
| 78 if (severity != last_notification_severity_ || |
| 79 (severity == HIGH && |
| 80 now - last_notification_time_ > notification_interval_)) { |
| 81 message_center_->AddNotification(CreateNotification(severity)); |
| 82 last_notification_time_ = now; |
| 83 last_notification_severity_ = severity; |
| 84 } |
| 85 } |
| 86 |
| 87 std::unique_ptr<message_center::Notification> |
| 88 LowDiskNotification::CreateNotification(Severity severity) { |
| 89 base::string16 title; |
| 90 base::string16 message; |
| 91 gfx::Image icon; |
| 92 switch (severity) { |
| 93 case HIGH: |
| 94 title = |
| 95 l10n_util::GetStringUTF16(IDS_CRITICALLY_LOW_DISK_NOTIFICATION_TITLE); |
| 96 message = l10n_util::GetStringUTF16( |
| 97 IDS_CRITICALLY_LOW_DISK_NOTIFICATION_MESSAGE); |
| 98 icon = gfx::Image(ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 99 IDR_DISK_SPACE_NOTIFICATION_CRITICAL)); |
| 100 break; |
| 101 default: |
| 102 title = l10n_util::GetStringUTF16(IDS_LOW_DISK_NOTIFICATION_TITLE); |
| 103 message = l10n_util::GetStringUTF16(IDS_LOW_DISK_NOTIFICATION_MESSAGE); |
| 104 icon = gfx::Image(ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 105 IDR_DISK_SPACE_NOTIFICATION_LOW)); |
| 106 } |
| 107 |
| 108 message_center::ButtonInfo storage_settings( |
| 109 l10n_util::GetStringUTF16(IDS_LOW_DISK_NOTIFICATION_BUTTON)); |
| 110 storage_settings.icon = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 111 IDR_STORAGE_MANAGER_BUTTON); |
| 112 message_center::RichNotificationData optional_fields; |
| 113 optional_fields.buttons.push_back(storage_settings); |
| 114 |
| 115 message_center::NotifierId notifier_id( |
| 116 message_center::NotifierId::SYSTEM_COMPONENT, |
| 117 ash::system_notifier::kNotifierDisk); |
| 118 |
| 119 std::unique_ptr<message_center::Notification> notification( |
| 120 new message_center::Notification( |
| 121 message_center::NOTIFICATION_TYPE_SIMPLE, kLowDiskId, title, message, |
| 122 icon, base::string16(), GURL(), notifier_id, optional_fields, |
| 123 new LowDiskNotificationDelegate())); |
| 124 |
| 125 return notification; |
| 126 } |
| 127 |
| 128 LowDiskNotification::Severity LowDiskNotification::GetSeverity( |
| 129 uint64_t free_disk_bytes) { |
| 130 if (free_disk_bytes < kNotificationSevereThreshold) |
| 131 return Severity::HIGH; |
| 132 if (free_disk_bytes < kNotificationThreshold) |
| 133 return Severity::MEDIUM; |
| 134 return Severity::NONE; |
| 135 } |
| 136 |
| 137 void LowDiskNotification::SetMessageCenterForTest( |
| 138 message_center::MessageCenter* message_center) { |
| 139 message_center_ = message_center; |
| 140 } |
| 141 |
| 142 void LowDiskNotification::SetNotificationIntervalForTest( |
| 143 base::TimeDelta notification_interval) { |
| 144 notification_interval_ = notification_interval; |
| 145 } |
| 146 |
| 147 } // namespace chromeos |
OLD | NEW |