[go: nahoru, domu]

blob: 5ac83291da5cc387c88e51778f04a731c9cd6dd7 [file] [log] [blame]
Tetsui Ohkubod819e2a2018-04-27 01:19:141// 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 Stade70e2ed42018-11-08 06:23:057#include "ash/public/cpp/notification_utils.h"
Tetsui Ohkubod819e2a2018-04-27 01:19:148#include "ash/resources/vector_icons/vector_icons.h"
Xiyuan Xiae7b19542019-05-06 23:05:189#include "ash/session/session_controller_impl.h"
Tetsui Ohkubod819e2a2018-04-27 01:19:1410#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
19namespace ash {
20
21namespace {
22
23const 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.
28constexpr base::TimeDelta kNotificationThreshold =
29 base::TimeDelta::FromMinutes(60);
30
31} // namespace
32
33// static
34const char SessionLimitNotificationController::kNotificationId[] =
35 "chrome://session/timeout";
36
37SessionLimitNotificationController::SessionLimitNotificationController()
38 : model_(Shell::Get()->system_tray_model()->session_length_limit()) {
39 model_->AddObserver(this);
40 OnSessionLengthLimitUpdated();
41}
42
43SessionLimitNotificationController::~SessionLimitNotificationController() {
44 model_->RemoveObserver(this);
45}
46
47void 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
56void 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 Cornet4151e9e2020-01-02 17:40:5090 CreateSystemNotification(
Tetsui Ohkubod819e2a2018-04-27 01:19:1491 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örrie85285b02021-03-11 23:38:4795 std::u16string() /* display_source */, GURL(),
Tetsui Ohkubod819e2a2018-04-27 01:19:1496 message_center::NotifierId(
Daniel Chenga925cbb52018-11-06 21:52:3597 message_center::NotifierType::SYSTEM_COMPONENT,
Tetsui Ohkubod819e2a2018-04-27 01:19:1498 kNotifierSessionLengthTimeout),
99 data, nullptr /* delegate */, kNotificationTimerIcon,
Tim Song96415362020-05-15 03:02:48100 message_center::SystemNotificationWarningLevel::WARNING);
101 notification->set_pinned(true);
Tetsui Ohkubod819e2a2018-04-27 01:19:14102 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örrie85285b02021-03-11 23:38:47111std::u16string SessionLimitNotificationController::ComposeNotificationTitle()
Tetsui Ohkubod819e2a2018-04-27 01:19:14112 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