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 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 49 | base::string16 FormatMonthAndYearWithOffset(base::Time eol_date) { |
| 50 | // TODO(crbug/998983): This is not the ideal way to correct months shifts. |
| 51 | // A follow up CL will modify base/i18n/time_formatting.h so that |
| 52 | // setting the time can be formatted based off UTC rather than only local. |
| 53 | // |
| 54 | // If the EOL date is on the first day of the month, then notifications with |
| 55 | // different month names may be shown to different users by |
| 56 | // base::TimeFormatMonthAndYear(), depending on their time zone. There are |
| 57 | // devices in Goldeneye with EOL dates on the first and last day of the month. |
| 58 | // Since only the month is shown, the day is set to the 15th to prevent any |
| 59 | // forward or backward month shifts. |
| 60 | constexpr int kApproxMidPointDayInMonth = 15; |
Evan Stade | e092109 | 2018-04-04 21:13:09 | [diff] [blame] | 61 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 62 | base::Time adjusted_date; |
| 63 | base::Time::Exploded exploded; |
| 64 | eol_date.UTCExplode(&exploded); |
| 65 | exploded.day_of_month = kApproxMidPointDayInMonth; |
| 66 | if (!base::Time::FromUTCExploded(exploded, &adjusted_date)) { |
| 67 | return base::TimeFormatMonthAndYear(eol_date); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 68 | } |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 69 | return base::TimeFormatMonthAndYear(adjusted_date); |
| 70 | } |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 71 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 72 | } // namespace |
| 73 | |
Sarah Hu | 4ad394b | 2017-11-27 19:03:00 | [diff] [blame] | 74 | // static |
| 75 | bool EolNotification::ShouldShowEolNotification() { |
Sarah Hu | 4ad394b | 2017-11-27 19:03:00 | [diff] [blame] | 76 | // Do not show end of life notification if this device is managed by |
| 77 | // enterprise user. |
| 78 | if (g_browser_process->platform_part() |
| 79 | ->browser_policy_connector_chromeos() |
| 80 | ->IsEnterpriseManaged()) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 87 | EolNotification::EolNotification(Profile* profile) |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 88 | : clock_(base::DefaultClock::GetInstance()), profile_(profile) {} |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 89 | |
| 90 | EolNotification::~EolNotification() {} |
| 91 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 92 | void EolNotification::CheckEolInfo() { |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 93 | UpdateEngineClient* update_engine_client = |
| 94 | DBusThreadManager::Get()->GetUpdateEngineClient(); |
| 95 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 96 | // Request the Eol Info. |
| 97 | update_engine_client->GetEolInfo(base::BindOnce( |
| 98 | &EolNotification::OnEolInfo, weak_ptr_factory_.GetWeakPtr())); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 99 | } |
| 100 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 101 | void EolNotification::OnEolInfo(UpdateEngineClient::EolInfo eol_info) { |
| 102 | // Do not show warning Eol notification if invalid |eol_info.eol_date|. |
| 103 | if (eol_info.eol_date.is_null()) |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 104 | return; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 105 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 106 | const base::Time now = clock_->Now(); |
| 107 | const base::Time eol_date = eol_info.eol_date; |
| 108 | const base::Time prev_eol_date = |
| 109 | profile_->GetPrefs()->GetTime(prefs::kEndOfLifeDate); |
| 110 | |
| 111 | profile_->GetPrefs()->SetTime(prefs::kEndOfLifeDate, eol_date); |
| 112 | |
| 113 | if (!now.is_null() && eol_date != prev_eol_date && now < eol_date) { |
| 114 | // Reset showed warning prefs if the Eol date changed. |
| 115 | profile_->GetPrefs()->SetBoolean(prefs::kFirstEolWarningDismissed, false); |
| 116 | profile_->GetPrefs()->SetBoolean(prefs::kSecondEolWarningDismissed, false); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 117 | profile_->GetPrefs()->SetBoolean(prefs::kEolNotificationDismissed, false); |
| 118 | } |
| 119 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 120 | if (eol_date <= now) { |
| 121 | dismiss_pref_ = prefs::kEolNotificationDismissed; |
| 122 | } else if (SecondWarningDate(eol_date) <= now) { |
| 123 | dismiss_pref_ = prefs::kSecondEolWarningDismissed; |
| 124 | } else if (FirstWarningDate(eol_date) <= now) { |
| 125 | dismiss_pref_ = prefs::kFirstEolWarningDismissed; |
| 126 | } else { |
| 127 | // |now| < FirstWarningDate() so don't show anything. |
| 128 | dismiss_pref_ = base::nullopt; |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | // Do not show if notification has already been dismissed or is out of range. |
| 133 | if (!dismiss_pref_ || profile_->GetPrefs()->GetBoolean(*dismiss_pref_)) |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 134 | return; |
| 135 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 136 | CreateNotification(eol_date, now); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 137 | } |
| 138 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 139 | void EolNotification::CreateNotification(base::Time eol_date, base::Time now) { |
| 140 | CHECK(!eol_date.is_null()); |
| 141 | CHECK(!now.is_null()); |
| 142 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 143 | message_center::RichNotificationData data; |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 144 | std::unique_ptr<message_center::Notification> notification; |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 145 | |
| 146 | DCHECK_EQ(BUTTON_MORE_INFO, data.buttons.size()); |
dpapad | 943ef18 | 2018-01-20 02:58:20 | [diff] [blame] | 147 | data.buttons.emplace_back(GetStringUTF16(IDS_LEARN_MORE)); |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 148 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 149 | if (now < eol_date) { |
| 150 | // Notifies user that updates will stop occurring at a month and year. |
| 151 | notification = ash::CreateSystemNotification( |
| 152 | message_center::NOTIFICATION_TYPE_SIMPLE, kEolNotificationId, |
| 153 | l10n_util::GetStringFUTF16(IDS_PENDING_EOL_NOTIFICATION_TITLE, |
| 154 | FormatMonthAndYearWithOffset(eol_date)), |
| 155 | l10n_util::GetStringFUTF16(IDS_PENDING_EOL_NOTIFICATION_MESSAGE, |
| 156 | ui::GetChromeOSDeviceName()), |
| 157 | base::string16() /* display_source */, GURL(kEolNotificationId), |
| 158 | message_center::NotifierId( |
| 159 | message_center::NotifierType::SYSTEM_COMPONENT, kEolNotificationId), |
| 160 | data, |
| 161 | base::MakeRefCounted<message_center::ThunkNotificationDelegate>( |
| 162 | weak_ptr_factory_.GetWeakPtr()), |
| 163 | vector_icons::kBusinessIcon, |
| 164 | message_center::SystemNotificationWarningLevel::NORMAL); |
| 165 | } else { |
| 166 | DCHECK_EQ(BUTTON_DISMISS, data.buttons.size()); |
| 167 | data.buttons.emplace_back(GetStringUTF16(IDS_EOL_DISMISS_BUTTON)); |
Regan Hsu | 8de051a | 2019-09-03 17:17:16 | [diff] [blame] | 168 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 169 | // Notifies user that updates will no longer occur after this final update. |
| 170 | notification = ash::CreateSystemNotification( |
| 171 | message_center::NOTIFICATION_TYPE_SIMPLE, kEolNotificationId, |
| 172 | GetStringUTF16(IDS_EOL_NOTIFICATION_TITLE), |
| 173 | l10n_util::GetStringFUTF16(IDS_EOL_NOTIFICATION_EOL, |
| 174 | ui::GetChromeOSDeviceName()), |
| 175 | base::string16() /* display_source */, GURL(kEolNotificationId), |
| 176 | message_center::NotifierId( |
| 177 | message_center::NotifierType::SYSTEM_COMPONENT, kEolNotificationId), |
| 178 | data, |
| 179 | base::MakeRefCounted<message_center::ThunkNotificationDelegate>( |
| 180 | weak_ptr_factory_.GetWeakPtr()), |
| 181 | kNotificationEndOfSupportIcon, |
| 182 | message_center::SystemNotificationWarningLevel::NORMAL); |
| 183 | } |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 184 | |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 185 | NotificationDisplayServiceFactory::GetForProfile(profile_)->Display( |
Lei Zhang | 03c6b78 | 2019-03-21 05:22:24 | [diff] [blame] | 186 | NotificationHandler::Type::TRANSIENT, *notification, |
| 187 | /*metadata=*/nullptr); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 188 | } |
| 189 | |
Regan Hsu | 62dc18f | 2019-10-03 20:30:44 | [diff] [blame] | 190 | void EolNotification::Close(bool by_user) { |
| 191 | // Only the final Eol notification has an explicit dismiss button, and |
| 192 | // is only dismissible by that button. The first and second warning |
| 193 | // buttons do not have an explicit dismiss button. |
| 194 | if (!by_user || !dismiss_pref_ || |
| 195 | dismiss_pref_ == prefs::kEolNotificationDismissed) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | profile_->GetPrefs()->SetBoolean(*dismiss_pref_, true); |
| 200 | } |
| 201 | |
| 202 | void EolNotification::Click(const base::Optional<int>& button_index, |
| 203 | const base::Optional<base::string16>& reply) { |
| 204 | if (!button_index) |
| 205 | return; |
| 206 | |
| 207 | switch (*button_index) { |
| 208 | case BUTTON_MORE_INFO: { |
| 209 | // show eol link |
| 210 | NavigateParams params(profile_, GURL(chrome::kEolNotificationURL), |
| 211 | ui::PAGE_TRANSITION_LINK); |
| 212 | params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; |
| 213 | params.window_action = NavigateParams::SHOW_WINDOW; |
| 214 | Navigate(¶ms); |
| 215 | break; |
| 216 | } |
| 217 | case BUTTON_DISMISS: |
| 218 | CHECK(dismiss_pref_); |
| 219 | // set dismiss pref. |
| 220 | profile_->GetPrefs()->SetBoolean(*dismiss_pref_, true); |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | if (dismiss_pref_ && (*dismiss_pref_ != prefs::kEolNotificationDismissed)) |
| 225 | profile_->GetPrefs()->SetBoolean(*dismiss_pref_, true); |
| 226 | |
| 227 | NotificationDisplayServiceFactory::GetForProfile(profile_)->Close( |
| 228 | NotificationHandler::Type::TRANSIENT, kEolNotificationId); |
| 229 | } |
| 230 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 231 | } // namespace chromeos |