Tetsui Ohkubo | d819e2a | 2018-04-27 01:19:14 | [diff] [blame] | 1 | // Copyright 2014 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 "ash/system/session/session_limit_notification_controller.h" |
| 6 | |
Evan Stade | 70e2ed4 | 2018-11-08 06:23:05 | [diff] [blame] | 7 | #include "ash/public/cpp/notification_utils.h" |
Tetsui Ohkubo | d819e2a | 2018-04-27 01:19:14 | [diff] [blame] | 8 | #include "ash/resources/vector_icons/vector_icons.h" |
Xiyuan Xia | e7b1954 | 2019-05-06 23:05:18 | [diff] [blame] | 9 | #include "ash/session/session_controller_impl.h" |
Tetsui Ohkubo | d819e2a | 2018-04-27 01:19:14 | [diff] [blame] | 10 | #include "ash/shell.h" |
| 11 | #include "ash/strings/grit/ash_strings.h" |
| 12 | #include "ash/system/model/session_length_limit_model.h" |
| 13 | #include "ash/system/model/system_tray_model.h" |
| 14 | #include "ui/base/l10n/l10n_util.h" |
| 15 | #include "ui/base/l10n/time_format.h" |
| 16 | #include "ui/message_center/message_center.h" |
| 17 | #include "ui/message_center/public/cpp/notification.h" |
| 18 | |
| 19 | namespace ash { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | const char kNotifierSessionLengthTimeout[] = "ash.session-length-timeout"; |
| 24 | |
| 25 | // A notification is shown to the user only if the remaining session time falls |
| 26 | // under this threshold. e.g. If the user has several days left in their |
| 27 | // session, there is no use displaying a notification right now. |
| 28 | constexpr base::TimeDelta kNotificationThreshold = |
| 29 | base::TimeDelta::FromMinutes(60); |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | // static |
| 34 | const char SessionLimitNotificationController::kNotificationId[] = |
| 35 | "chrome://session/timeout"; |
| 36 | |
| 37 | SessionLimitNotificationController::SessionLimitNotificationController() |
| 38 | : model_(Shell::Get()->system_tray_model()->session_length_limit()) { |
| 39 | model_->AddObserver(this); |
| 40 | OnSessionLengthLimitUpdated(); |
| 41 | } |
| 42 | |
| 43 | SessionLimitNotificationController::~SessionLimitNotificationController() { |
| 44 | model_->RemoveObserver(this); |
| 45 | } |
| 46 | |
| 47 | void SessionLimitNotificationController::OnSessionLengthLimitUpdated() { |
| 48 | // Don't show notification until the user is logged in. |
| 49 | if (!Shell::Get()->session_controller()->IsActiveUserSessionStarted()) |
| 50 | return; |
| 51 | |
| 52 | UpdateNotification(); |
| 53 | last_limit_state_ = model_->limit_state(); |
| 54 | } |
| 55 | |
| 56 | void SessionLimitNotificationController::UpdateNotification() { |
| 57 | message_center::MessageCenter* message_center = |
| 58 | message_center::MessageCenter::Get(); |
| 59 | |
| 60 | // If state hasn't changed and the notification has already been acknowledged, |
| 61 | // we won't re-create it. We consider a notification to be acknowledged if it |
| 62 | // was shown before, but is no longer visible. |
| 63 | if (model_->limit_state() == last_limit_state_ && |
| 64 | has_notification_been_shown_ && |
| 65 | !message_center->FindVisibleNotificationById(kNotificationId)) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // After state change, any possibly existing notification is removed to make |
| 70 | // sure it is re-shown even if it had been acknowledged by the user before |
| 71 | // (and in the rare case of state change towards LIMIT_NONE to make the |
| 72 | // notification disappear). |
| 73 | if (model_->limit_state() != last_limit_state_ && |
| 74 | message_center->FindVisibleNotificationById(kNotificationId)) { |
| 75 | message_center::MessageCenter::Get()->RemoveNotification( |
| 76 | kNotificationId, false /* by_user */); |
| 77 | } |
| 78 | |
| 79 | // If the session is unlimited or if the remaining time is too far off into |
| 80 | // the future, there is nothing more to do. |
| 81 | if (model_->limit_state() == SessionLengthLimitModel::LIMIT_NONE || |
| 82 | model_->remaining_session_time() > kNotificationThreshold) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | message_center::RichNotificationData data; |
| 87 | data.should_make_spoken_feedback_for_popup_updates = |
| 88 | (model_->limit_state() != last_limit_state_); |
| 89 | std::unique_ptr<message_center::Notification> notification = |
Manu Cornet | 4151e9e | 2020-01-02 17:40:50 | [diff] [blame] | 90 | CreateSystemNotification( |
Tetsui Ohkubo | d819e2a | 2018-04-27 01:19:14 | [diff] [blame] | 91 | message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId, |
| 92 | ComposeNotificationTitle(), |
| 93 | l10n_util::GetStringUTF16( |
| 94 | IDS_ASH_STATUS_TRAY_NOTIFICATION_SESSION_LENGTH_LIMIT_MESSAGE), |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 95 | std::u16string() /* display_source */, GURL(), |
Tetsui Ohkubo | d819e2a | 2018-04-27 01:19:14 | [diff] [blame] | 96 | message_center::NotifierId( |
Daniel Cheng | a925cbb5 | 2018-11-06 21:52:35 | [diff] [blame] | 97 | message_center::NotifierType::SYSTEM_COMPONENT, |
Tetsui Ohkubo | d819e2a | 2018-04-27 01:19:14 | [diff] [blame] | 98 | kNotifierSessionLengthTimeout), |
| 99 | data, nullptr /* delegate */, kNotificationTimerIcon, |
Tim Song | 9641536 | 2020-05-15 03:02:48 | [diff] [blame] | 100 | message_center::SystemNotificationWarningLevel::WARNING); |
| 101 | notification->set_pinned(true); |
Tetsui Ohkubo | d819e2a | 2018-04-27 01:19:14 | [diff] [blame] | 102 | if (message_center->FindVisibleNotificationById(kNotificationId)) { |
| 103 | message_center->UpdateNotification(kNotificationId, |
| 104 | std::move(notification)); |
| 105 | } else { |
| 106 | message_center->AddNotification(std::move(notification)); |
| 107 | } |
| 108 | has_notification_been_shown_ = true; |
| 109 | } |
| 110 | |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 111 | std::u16string SessionLimitNotificationController::ComposeNotificationTitle() |
Tetsui Ohkubo | d819e2a | 2018-04-27 01:19:14 | [diff] [blame] | 112 | const { |
| 113 | return l10n_util::GetStringFUTF16( |
| 114 | IDS_ASH_STATUS_TRAY_NOTIFICATION_SESSION_LENGTH_LIMIT_TITLE, |
| 115 | ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_DURATION, |
| 116 | ui::TimeFormat::LENGTH_SHORT, |
| 117 | model_->remaining_session_time())); |
| 118 | } |
| 119 | |
| 120 | } // namespace ash |