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 | |
Evan Stade | 70e2ed4 | 2018-11-08 06:23:05 | [diff] [blame] | 7 | #include "ash/public/cpp/notification_utils.h" |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 8 | #include "base/bind.h" |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 9 | #include "base/i18n/time_formatting.h" |
| 10 | #include "base/time/default_clock.h" |
Evan Stade | 82ba4b6 | 2019-07-11 01:58:01 | [diff] [blame] | 11 | #include "chrome/app/vector_icons/vector_icons.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 12 | #include "chrome/browser/browser_process.h" |
Hans Wennborg | 6334445 | 2019-10-15 10:15:21 | [diff] [blame] | 13 | #include "chrome/browser/browser_process_platform_part.h" |
Sarah Hu | 4ad394b | 2017-11-27 19:03:00 | [diff] [blame] | 14 | #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 15 | #include "chrome/browser/notifications/notification_display_service.h" |
| 16 | #include "chrome/browser/notifications/notification_display_service_factory.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 17 | #include "chrome/browser/ui/browser_navigator.h" |
| 18 | #include "chrome/browser/ui/browser_navigator_params.h" |
| 19 | #include "chrome/common/pref_names.h" |
| 20 | #include "chrome/common/url_constants.h" |
| 21 | #include "chrome/grit/generated_resources.h" |
| 22 | #include "chromeos/dbus/dbus_thread_manager.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 23 | #include "components/prefs/pref_service.h" |
dpapad | 943ef18 | 2018-01-20 02:58:20 | [diff] [blame] | 24 | #include "components/strings/grit/components_strings.h" |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 25 | #include "components/vector_icons/vector_icons.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 26 | #include "ui/base/l10n/l10n_util.h" |
Vladislav Kaznacheev | e980522 | 2019-05-28 19:44:40 | [diff] [blame] | 27 | #include "ui/chromeos/devicetype_utils.h" |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 28 | #include "ui/gfx/color_palette.h" |
| 29 | #include "ui/gfx/paint_vector_icon.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 30 | |
Tetsui Ohkubo | e8f95e8 | 2017-08-23 06:10:47 | [diff] [blame] | 31 | using l10n_util::GetStringUTF16; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 32 | |
| 33 | namespace chromeos { |
| 34 | namespace { |
| 35 | |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 36 | const char kEolNotificationId[] = "chrome://product_eol"; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 37 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 38 | constexpr int kFirstWarningDaysInAdvance = 180; |
| 39 | constexpr int kSecondWarningDaysInAdvance = 90; |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 40 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 41 | base::Time FirstWarningDate(base::Time eol_date) { |
| 42 | return eol_date - base::TimeDelta::FromDays(kFirstWarningDaysInAdvance); |
| 43 | } |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 44 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 45 | base::Time SecondWarningDate(const base::Time& eol_date) { |
| 46 | return eol_date - base::TimeDelta::FromDays(kSecondWarningDaysInAdvance); |
| 47 | } |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 48 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 49 | } // namespace |
| 50 | |
Sarah Hu | 4ad394b | 2017-11-27 19:03:00 | [diff] [blame] | 51 | // static |
| 52 | bool EolNotification::ShouldShowEolNotification() { |
Sarah Hu | 4ad394b | 2017-11-27 19:03:00 | [diff] [blame] | 53 | // Do not show end of life notification if this device is managed by |
| 54 | // enterprise user. |
| 55 | if (g_browser_process->platform_part() |
| 56 | ->browser_policy_connector_chromeos() |
| 57 | ->IsEnterpriseManaged()) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 64 | EolNotification::EolNotification(Profile* profile) |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 65 | : clock_(base::DefaultClock::GetInstance()), profile_(profile) {} |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 66 | |
| 67 | EolNotification::~EolNotification() {} |
| 68 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 69 | void EolNotification::CheckEolInfo() { |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 70 | UpdateEngineClient* update_engine_client = |
| 71 | DBusThreadManager::Get()->GetUpdateEngineClient(); |
| 72 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 73 | // Request the Eol Info. |
| 74 | update_engine_client->GetEolInfo(base::BindOnce( |
| 75 | &EolNotification::OnEolInfo, weak_ptr_factory_.GetWeakPtr())); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 76 | } |
| 77 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 78 | void EolNotification::OnEolInfo(UpdateEngineClient::EolInfo eol_info) { |
| 79 | // Do not show warning Eol notification if invalid |eol_info.eol_date|. |
| 80 | if (eol_info.eol_date.is_null()) |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 81 | return; |
| 82 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 83 | const base::Time now = clock_->Now(); |
| 84 | const base::Time eol_date = eol_info.eol_date; |
| 85 | const base::Time prev_eol_date = |
| 86 | profile_->GetPrefs()->GetTime(prefs::kEndOfLifeDate); |
| 87 | |
| 88 | profile_->GetPrefs()->SetTime(prefs::kEndOfLifeDate, eol_date); |
| 89 | |
| 90 | if (!now.is_null() && eol_date != prev_eol_date && now < eol_date) { |
| 91 | // Reset showed warning prefs if the Eol date changed. |
| 92 | profile_->GetPrefs()->SetBoolean(prefs::kFirstEolWarningDismissed, false); |
| 93 | profile_->GetPrefs()->SetBoolean(prefs::kSecondEolWarningDismissed, false); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 94 | profile_->GetPrefs()->SetBoolean(prefs::kEolNotificationDismissed, false); |
| 95 | } |
| 96 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 97 | if (eol_date <= now) { |
| 98 | dismiss_pref_ = prefs::kEolNotificationDismissed; |
| 99 | } else if (SecondWarningDate(eol_date) <= now) { |
| 100 | dismiss_pref_ = prefs::kSecondEolWarningDismissed; |
| 101 | } else if (FirstWarningDate(eol_date) <= now) { |
| 102 | dismiss_pref_ = prefs::kFirstEolWarningDismissed; |
| 103 | } else { |
| 104 | // |now| < FirstWarningDate() so don't show anything. |
| 105 | dismiss_pref_ = base::nullopt; |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | // Do not show if notification has already been dismissed or is out of range. |
| 110 | if (!dismiss_pref_ || profile_->GetPrefs()->GetBoolean(*dismiss_pref_)) |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 111 | return; |
| 112 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 113 | CreateNotification(eol_date, now); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 114 | } |
| 115 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 116 | void EolNotification::CreateNotification(base::Time eol_date, base::Time now) { |
| 117 | CHECK(!eol_date.is_null()); |
| 118 | CHECK(!now.is_null()); |
| 119 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 120 | message_center::RichNotificationData data; |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 121 | std::unique_ptr<message_center::Notification> notification; |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 122 | |
| 123 | DCHECK_EQ(BUTTON_MORE_INFO, data.buttons.size()); |
dpapad | 943ef18 | 2018-01-20 02:58:20 | [diff] [blame] | 124 | data.buttons.emplace_back(GetStringUTF16(IDS_LEARN_MORE)); |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 125 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 126 | if (now < eol_date) { |
| 127 | // Notifies user that updates will stop occurring at a month and year. |
| 128 | notification = ash::CreateSystemNotification( |
| 129 | message_center::NOTIFICATION_TYPE_SIMPLE, kEolNotificationId, |
Regan Hsu | d502ad61 | 2021-01-15 15:48:10 | [diff] [blame] | 130 | l10n_util::GetStringFUTF16( |
| 131 | IDS_PENDING_EOL_NOTIFICATION_TITLE, |
| 132 | TimeFormatMonthAndYear(eol_date, |
| 133 | /*time_zone=*/icu::TimeZone::getGMT())), |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 134 | l10n_util::GetStringFUTF16(IDS_PENDING_EOL_NOTIFICATION_MESSAGE, |
| 135 | ui::GetChromeOSDeviceName()), |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame^] | 136 | std::u16string() /* display_source */, GURL(kEolNotificationId), |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 137 | message_center::NotifierId( |
| 138 | message_center::NotifierType::SYSTEM_COMPONENT, kEolNotificationId), |
| 139 | data, |
| 140 | base::MakeRefCounted<message_center::ThunkNotificationDelegate>( |
| 141 | weak_ptr_factory_.GetWeakPtr()), |
| 142 | vector_icons::kBusinessIcon, |
| 143 | message_center::SystemNotificationWarningLevel::NORMAL); |
| 144 | } else { |
| 145 | DCHECK_EQ(BUTTON_DISMISS, data.buttons.size()); |
| 146 | data.buttons.emplace_back(GetStringUTF16(IDS_EOL_DISMISS_BUTTON)); |
Regan Hsu | 8de051a | 2019-09-03 17:17:16 | [diff] [blame] | 147 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 148 | // Notifies user that updates will no longer occur after this final update. |
| 149 | notification = ash::CreateSystemNotification( |
| 150 | message_center::NOTIFICATION_TYPE_SIMPLE, kEolNotificationId, |
| 151 | GetStringUTF16(IDS_EOL_NOTIFICATION_TITLE), |
| 152 | l10n_util::GetStringFUTF16(IDS_EOL_NOTIFICATION_EOL, |
| 153 | ui::GetChromeOSDeviceName()), |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame^] | 154 | std::u16string() /* display_source */, GURL(kEolNotificationId), |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 155 | message_center::NotifierId( |
| 156 | message_center::NotifierType::SYSTEM_COMPONENT, kEolNotificationId), |
| 157 | data, |
| 158 | base::MakeRefCounted<message_center::ThunkNotificationDelegate>( |
| 159 | weak_ptr_factory_.GetWeakPtr()), |
| 160 | kNotificationEndOfSupportIcon, |
| 161 | message_center::SystemNotificationWarningLevel::NORMAL); |
| 162 | } |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 163 | |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 164 | NotificationDisplayServiceFactory::GetForProfile(profile_)->Display( |
Lei Zhang | 03c6b78 | 2019-03-21 05:22:24 | [diff] [blame] | 165 | NotificationHandler::Type::TRANSIENT, *notification, |
| 166 | /*metadata=*/nullptr); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 167 | } |
| 168 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 169 | void EolNotification::Close(bool by_user) { |
| 170 | // Only the final Eol notification has an explicit dismiss button, and |
| 171 | // is only dismissible by that button. The first and second warning |
| 172 | // buttons do not have an explicit dismiss button. |
| 173 | if (!by_user || !dismiss_pref_ || |
| 174 | dismiss_pref_ == prefs::kEolNotificationDismissed) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | profile_->GetPrefs()->SetBoolean(*dismiss_pref_, true); |
| 179 | } |
| 180 | |
| 181 | void EolNotification::Click(const base::Optional<int>& button_index, |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame^] | 182 | const base::Optional<std::u16string>& reply) { |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 183 | if (!button_index) |
| 184 | return; |
| 185 | |
| 186 | switch (*button_index) { |
| 187 | case BUTTON_MORE_INFO: { |
Regan Hsu | f5b3115 | 2020-08-07 02:56:55 | [diff] [blame] | 188 | const GURL url = dismiss_pref_ == prefs::kEolNotificationDismissed |
| 189 | ? GURL(chrome::kEolNotificationURL) |
| 190 | : GURL(chrome::kAutoUpdatePolicyURL); |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 191 | // show eol link |
Regan Hsu | f5b3115 | 2020-08-07 02:56:55 | [diff] [blame] | 192 | NavigateParams params(profile_, url, ui::PAGE_TRANSITION_LINK); |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 193 | params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; |
| 194 | params.window_action = NavigateParams::SHOW_WINDOW; |
| 195 | Navigate(¶ms); |
| 196 | break; |
| 197 | } |
| 198 | case BUTTON_DISMISS: |
| 199 | CHECK(dismiss_pref_); |
| 200 | // set dismiss pref. |
| 201 | profile_->GetPrefs()->SetBoolean(*dismiss_pref_, true); |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | if (dismiss_pref_ && (*dismiss_pref_ != prefs::kEolNotificationDismissed)) |
| 206 | profile_->GetPrefs()->SetBoolean(*dismiss_pref_, true); |
| 207 | |
| 208 | NotificationDisplayServiceFactory::GetForProfile(profile_)->Close( |
| 209 | NotificationHandler::Type::TRANSIENT, kEolNotificationId); |
| 210 | } |
| 211 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 212 | } // namespace chromeos |