1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/eol/eol_notification.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/public/cpp/resources/grit/ash_public_unscaled_resources.h"
#include "ash/public/cpp/style/dark_light_mode_controller.h"
#include "ash/public/cpp/system_notification_builder.h"
#include "ash/style/dark_light_mode_controller_impl.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/i18n/time_formatting.h"
#include "base/time/default_clock.h"
#include "base/time/time.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ash/extended_updates/extended_updates_controller.h"
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/system/system_tray_client_impl.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/ash/components/dbus/update_engine/update_engine_client.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/chromeos/devicetype_utils.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/paint_vector_icon.h"
namespace ash {
namespace {
using ::l10n_util::GetStringUTF16;
const char kEolNotificationId[] = "chrome://product_eol";
constexpr int kSecondWarningDaysInAdvance = 90;
base::Time SecondWarningDate(const base::Time& eol_date) {
return eol_date - base::Days(kSecondWarningDaysInAdvance);
}
} // namespace
// static
bool EolNotification::ShouldShowEolNotification() {
// Do not show end of life notification if this device is managed by
// enterprise user.
if (g_browser_process->platform_part()
->browser_policy_connector_ash()
->IsDeviceEnterpriseManaged()) {
return false;
}
return true;
}
EolNotification::EolNotification(Profile* profile)
: clock_(base::DefaultClock::GetInstance()), profile_(profile) {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEolResetDismissedPrefs)) {
ResetDismissedPrefs();
}
}
EolNotification::~EolNotification() = default;
void EolNotification::CheckEolInfo() {
// Request the Eol Info.
UpdateEngineClient::Get()->GetEolInfo(base::BindOnce(
&EolNotification::OnEolInfo, weak_ptr_factory_.GetWeakPtr()));
}
void EolNotification::OnEolInfo(UpdateEngineClient::EolInfo eol_info) {
MaybeShowEolNotification(eol_info.eol_date);
ExtendedUpdatesController::Get()->OnEolInfo(profile_, eol_info);
}
void EolNotification::MaybeShowEolNotification(base::Time eol_date) {
// Do not show warning Eol notification if invalid |eol_date|.
if (eol_date.is_null()) {
return;
}
const base::Time now = clock_->Now();
const base::Time prev_eol_date =
profile_->GetPrefs()->GetTime(prefs::kEndOfLifeDate);
profile_->GetPrefs()->SetTime(prefs::kEndOfLifeDate, eol_date);
if (!now.is_null() && eol_date != prev_eol_date && now < eol_date) {
// Reset showed warning prefs if the Eol date changed.
ResetDismissedPrefs();
}
if (eol_date <= now) {
dismiss_pref_ = prefs::kEolNotificationDismissed;
} else if (SecondWarningDate(eol_date) <= now) {
dismiss_pref_ = prefs::kSecondEolWarningDismissed;
} else {
dismiss_pref_ = std::nullopt;
return;
}
// Do not show if notification has already been dismissed or is out of range.
if (!dismiss_pref_ || profile_->GetPrefs()->GetBoolean(*dismiss_pref_))
return;
CreateNotification(eol_date, now);
}
void EolNotification::CreateNotification(base::Time eol_date, base::Time now) {
CHECK(!eol_date.is_null());
CHECK(!now.is_null());
message_center::RichNotificationData data;
ash::SystemNotificationBuilder notification_builder;
DCHECK_EQ(BUTTON_MORE_INFO, data.buttons.size());
data.buttons.emplace_back(GetStringUTF16(IDS_LEARN_MORE));
if (now < eol_date) {
// Notifies user that updates will stop occurring at a month and year.
notification_builder
.SetTitleWithArgs(IDS_PENDING_EOL_NOTIFICATION_TITLE,
{TimeFormatMonthAndYearForTimeZone(
eol_date, icu::TimeZone::getGMT())})
.SetMessageWithArgs(IDS_PENDING_EOL_NOTIFICATION_MESSAGE,
{ui::GetChromeOSDeviceName()})
.SetCatalogName(NotificationCatalogName::kPendingEOL)
.SetSmallImage(vector_icons::kBusinessIcon);
} else {
DCHECK_EQ(BUTTON_DISMISS, data.buttons.size());
data.buttons.emplace_back(GetStringUTF16(IDS_EOL_DISMISS_BUTTON));
// Notifies user that updates will no longer occur after this final update.
notification_builder.SetTitleId(IDS_EOL_NOTIFICATION_TITLE)
.SetMessageWithArgs(IDS_EOL_NOTIFICATION_EOL,
{ui::GetChromeOSDeviceName()})
.SetCatalogName(NotificationCatalogName::kEOL)
.SetSmallImage(kNotificationEndOfSupportIcon);
}
NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
NotificationHandler::Type::TRANSIENT,
notification_builder.SetId(kEolNotificationId)
.SetOriginUrl(GURL(kEolNotificationId))
.SetOptionalFields(data)
.SetDelegate(
base::MakeRefCounted<message_center::ThunkNotificationDelegate>(
weak_ptr_factory_.GetWeakPtr()))
.Build(false),
/*metadata=*/nullptr);
}
void EolNotification::Close(bool by_user) {
// Only the final Eol notification has an explicit dismiss button, and
// is only dismissible by that button. The first and second warning
// buttons do not have an explicit dismiss button.
if (!by_user || !dismiss_pref_ ||
dismiss_pref_ == prefs::kEolNotificationDismissed) {
return;
}
profile_->GetPrefs()->SetBoolean(*dismiss_pref_, true);
}
void EolNotification::Click(const std::optional<int>& button_index,
const std::optional<std::u16string>& reply) {
if (!button_index) {
return;
}
switch (*button_index) {
case BUTTON_MORE_INFO: {
const GURL url(dismiss_pref_ == prefs::kEolNotificationDismissed
? chrome::kEolNotificationURL
: chrome::kAutoUpdatePolicyURL);
// Show eol link.
NewWindowDelegate::GetInstance()->OpenUrl(
url, NewWindowDelegate::OpenUrlFrom::kUserInteraction,
NewWindowDelegate::Disposition::kNewForegroundTab);
break;
}
case BUTTON_DISMISS:
CHECK(dismiss_pref_);
// Set dismiss pref.
profile_->GetPrefs()->SetBoolean(*dismiss_pref_, true);
break;
}
if (dismiss_pref_ && (*dismiss_pref_ != prefs::kEolNotificationDismissed)) {
profile_->GetPrefs()->SetBoolean(*dismiss_pref_, true);
}
NotificationDisplayServiceFactory::GetForProfile(profile_)->Close(
NotificationHandler::Type::TRANSIENT, kEolNotificationId);
}
void EolNotification::OverrideClockForTesting(base::Clock* clock) {
if (!clock) {
clock_ = base::DefaultClock::GetInstance();
} else {
clock_ = clock;
}
}
void EolNotification::ResetDismissedPrefs() {
profile_->GetPrefs()->SetBoolean(prefs::kFirstEolWarningDismissed, false);
profile_->GetPrefs()->SetBoolean(prefs::kSecondEolWarningDismissed, false);
profile_->GetPrefs()->SetBoolean(prefs::kEolNotificationDismissed, false);
}
} // namespace ash
|