xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 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/eol_notification.h" |
| 6 | |
estade | 3b0d2025 | 2017-03-09 07:25:18 | [diff] [blame] | 7 | #include "chrome/app/vector_icons/vector_icons.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 8 | #include "chrome/browser/browser_process.h" |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 9 | #include "chrome/browser/notifications/notification_common.h" |
| 10 | #include "chrome/browser/notifications/notification_display_service.h" |
| 11 | #include "chrome/browser/notifications/notification_display_service_factory.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 12 | #include "chrome/browser/ui/browser_navigator.h" |
| 13 | #include "chrome/browser/ui/browser_navigator_params.h" |
| 14 | #include "chrome/common/pref_names.h" |
| 15 | #include "chrome/common/url_constants.h" |
| 16 | #include "chrome/grit/generated_resources.h" |
| 17 | #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 | #include "chromeos/dbus/update_engine_client.h" |
| 19 | #include "components/prefs/pref_service.h" |
Evan Stade | 619a89be | 2017-07-17 20:24:26 | [diff] [blame] | 20 | #include "components/vector_icons/vector_icons.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 21 | #include "ui/base/l10n/l10n_util.h" |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 22 | #include "ui/gfx/color_palette.h" |
| 23 | #include "ui/gfx/paint_vector_icon.h" |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 24 | #include "ui/message_center/notification.h" |
Evan Stade | 353058c | 2017-09-08 21:31:13 | [diff] [blame] | 25 | #include "ui/message_center/public/cpp/message_center_constants.h" |
| 26 | #include "ui/message_center/public/cpp/message_center_switches.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 27 | |
| 28 | using message_center::MessageCenter; |
Tetsui Ohkubo | e8f95e8 | 2017-08-23 06:10:47 | [diff] [blame] | 29 | using l10n_util::GetStringUTF16; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 30 | |
| 31 | namespace chromeos { |
| 32 | namespace { |
| 33 | |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 34 | const char kEolNotificationId[] = "chrome://product_eol"; |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 35 | const SkColor kButtonIconColor = SkColorSetRGB(150, 150, 152); |
| 36 | const SkColor kNotificationIconColor = SkColorSetRGB(219, 68, 55); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 37 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame^] | 38 | // Buttons that appear in notifications. |
| 39 | enum ButtonIndex { |
| 40 | BUTTON_MORE_INFO = 0, |
| 41 | BUTTON_DISMISS, |
| 42 | BUTTON_SIZE = BUTTON_DISMISS |
| 43 | }; |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 44 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame^] | 45 | class EolNotificationDelegate : public message_center::NotificationDelegate { |
| 46 | public: |
| 47 | explicit EolNotificationDelegate(Profile* profile) : profile_(profile) {} |
| 48 | |
| 49 | private: |
| 50 | ~EolNotificationDelegate() override = default; |
| 51 | |
| 52 | // NotificationDelegate overrides: |
| 53 | void ButtonClick(int button_index) override { |
| 54 | switch (button_index) { |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 55 | case BUTTON_MORE_INFO: { |
| 56 | // show eol link |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame^] | 57 | chrome::NavigateParams params(profile_, |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 58 | GURL(chrome::kEolNotificationURL), |
| 59 | ui::PAGE_TRANSITION_LINK); |
| 60 | params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; |
| 61 | params.window_action = chrome::NavigateParams::SHOW_WINDOW; |
| 62 | chrome::Navigate(¶ms); |
| 63 | break; |
| 64 | } |
| 65 | case BUTTON_DISMISS: |
| 66 | // set dismiss pref. |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame^] | 67 | profile_->GetPrefs()->SetBoolean(prefs::kEolNotificationDismissed, |
| 68 | true); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 69 | break; |
| 70 | } |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame^] | 71 | NotificationDisplayServiceFactory::GetForProfile(profile_)->Close( |
| 72 | NotificationCommon::TRANSIENT, kEolNotificationId); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 73 | } |
| 74 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame^] | 75 | Profile* const profile_; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 76 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame^] | 77 | DISALLOW_COPY_AND_ASSIGN(EolNotificationDelegate); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 78 | }; |
| 79 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 80 | } // namespace |
| 81 | |
| 82 | EolNotification::EolNotification(Profile* profile) |
| 83 | : profile_(profile), |
| 84 | status_(update_engine::EndOfLifeStatus::kSupported), |
| 85 | weak_factory_(this) {} |
| 86 | |
| 87 | EolNotification::~EolNotification() {} |
| 88 | |
| 89 | void EolNotification::CheckEolStatus() { |
| 90 | UpdateEngineClient* update_engine_client = |
| 91 | DBusThreadManager::Get()->GetUpdateEngineClient(); |
| 92 | |
| 93 | // Request the Eol Status. |
| 94 | update_engine_client->GetEolStatus( |
| 95 | base::Bind(&EolNotification::OnEolStatus, weak_factory_.GetWeakPtr())); |
| 96 | } |
| 97 | |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 98 | void EolNotification::OnEolStatus(update_engine::EndOfLifeStatus status) { |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 99 | status_ = status; |
| 100 | |
| 101 | const int pre_eol_status = |
| 102 | profile_->GetPrefs()->GetInteger(prefs::kEolStatus); |
| 103 | profile_->GetPrefs()->SetInteger(prefs::kEolStatus, status_); |
| 104 | |
xiaoyinh | f4e5b5b | 2017-02-27 23:22:46 | [diff] [blame] | 105 | // Security only state is no longer supported. |
| 106 | if (status_ == update_engine::EndOfLifeStatus::kSupported || |
| 107 | status_ == update_engine::EndOfLifeStatus::kSecurityOnly) { |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 108 | return; |
xiaoyinh | f4e5b5b | 2017-02-27 23:22:46 | [diff] [blame] | 109 | } |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 110 | |
| 111 | if (pre_eol_status != status_) { |
| 112 | // If Eol status has changed, we should reset |
| 113 | // kEolNotificationDismissed and show notification. |
| 114 | profile_->GetPrefs()->SetBoolean(prefs::kEolNotificationDismissed, false); |
| 115 | } |
| 116 | |
| 117 | bool user_dismissed_eol_notification = |
| 118 | profile_->GetPrefs()->GetBoolean(prefs::kEolNotificationDismissed); |
| 119 | if (user_dismissed_eol_notification) |
| 120 | return; |
| 121 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 122 | Update(); |
| 123 | } |
| 124 | |
| 125 | void EolNotification::Update() { |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 126 | message_center::RichNotificationData data; |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame^] | 127 | |
| 128 | DCHECK_EQ(BUTTON_MORE_INFO, data.buttons.size()); |
| 129 | data.buttons.emplace_back(GetStringUTF16(IDS_EOL_MORE_INFO_BUTTON)); |
| 130 | data.buttons.back().icon = gfx::Image( |
| 131 | CreateVectorIcon(vector_icons::kInfoOutlineIcon, kButtonIconColor)); |
| 132 | |
| 133 | DCHECK_EQ(BUTTON_DISMISS, data.buttons.size()); |
| 134 | data.buttons.emplace_back(GetStringUTF16(IDS_EOL_DISMISS_BUTTON)); |
| 135 | data.buttons.back().icon = gfx::Image( |
| 136 | CreateVectorIcon(vector_icons::kNotificationsOffIcon, kButtonIconColor)); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 137 | |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 138 | message_center::Notification notification( |
Evan Stade | 7ff08dc | 2017-09-13 01:52:00 | [diff] [blame] | 139 | message_center::NOTIFICATION_TYPE_SIMPLE, kEolNotificationId, |
Tetsui Ohkubo | e8f95e8 | 2017-08-23 06:10:47 | [diff] [blame] | 140 | GetStringUTF16(IDS_EOL_NOTIFICATION_TITLE), |
| 141 | GetStringUTF16(IDS_EOL_NOTIFICATION_EOL), |
Evan Stade | 353058c | 2017-09-08 21:31:13 | [diff] [blame] | 142 | message_center::IsNewStyleNotificationEnabled() |
Tetsui Ohkubo | e8f95e8 | 2017-08-23 06:10:47 | [diff] [blame] | 143 | ? gfx::Image() |
| 144 | : gfx::Image(CreateVectorIcon(kEolIcon, kNotificationIconColor)), |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 145 | GetStringUTF16(IDS_EOL_NOTIFICATION_DISPLAY_SOURCE), |
| 146 | GURL(kEolNotificationId), |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 147 | message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
| 148 | kEolNotificationId), |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame^] | 149 | data, new EolNotificationDelegate(profile_)); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 150 | |
Evan Stade | 353058c | 2017-09-08 21:31:13 | [diff] [blame] | 151 | if (message_center::IsNewStyleNotificationEnabled()) { |
Tetsui Ohkubo | e8f95e8 | 2017-08-23 06:10:47 | [diff] [blame] | 152 | notification.set_accent_color( |
| 153 | message_center::kSystemNotificationColorCriticalWarning); |
| 154 | notification.set_small_image(gfx::Image(gfx::CreateVectorIcon( |
| 155 | kNotificationEndOfSupportIcon, |
| 156 | message_center::kSystemNotificationColorCriticalWarning))); |
| 157 | notification.set_vector_small_image(kNotificationEndOfSupportIcon); |
| 158 | } |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 159 | |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 160 | NotificationDisplayServiceFactory::GetForProfile(profile_)->Display( |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame^] | 161 | NotificationCommon::TRANSIENT, notification); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 162 | } |
| 163 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 164 | } // namespace chromeos |