File: reboot_notification_controller.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (155 lines) | stat: -rw-r--r-- 6,261 bytes parent folder | download | duplicates (5)
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
// Copyright 2022 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/ui/ash/device_scheduled_reboot/reboot_notification_controller.h"

#include <memory>

#include "ash/public/cpp/notification_utils.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/shell.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/time_formatting.h"
#include "base/memory/ptr_util.h"
#include "base/strings/strcat.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/notifications/notification_handler.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/strings/grit/components_strings.h"
#include "components/user_manager/user_manager.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/public/cpp/notification.h"

namespace ash {

// Id of the pending reboot notification
const char kPendingRebootNotificationId[] =
    "ash.device_scheduled_reboot_pending_notification";

// Id of the post reboot notification
const char kPostRebootNotificationId[] =
    "ash.device_scheduled_reboot_post_reboot_notification";
}  // namespace ash

RebootNotificationController::RebootNotificationController() = default;

RebootNotificationController::~RebootNotificationController() = default;

void RebootNotificationController::MaybeShowPendingRebootNotification(
    const base::Time& reboot_time,
    base::RepeatingClosure reboot_callback) {
  if (!ShouldNotifyUser()) {
    return;
  }
  notification_callback_ = std::move(reboot_callback);
  std::u16string reboot_title =
      l10n_util::GetStringUTF16(IDS_POLICY_DEVICE_SCHEDULED_REBOOT_TITLE);
  std::u16string reboot_message =
      l10n_util::GetStringFUTF16(IDS_POLICY_DEVICE_SCHEDULED_REBOOT_MESSAGE,
                                 base::TimeFormatTimeOfDay(reboot_time),
                                 base::TimeFormatShortDate(reboot_time));

  message_center::RichNotificationData notification_data;
  notification_data.pinned = true;
  notification_data.buttons.emplace_back(
      l10n_util::GetStringUTF16(IDS_POLICY_REBOOT_BUTTON));
  scoped_refptr<message_center::NotificationDelegate> delegate =
      base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
          base::BindRepeating(
              &RebootNotificationController::HandleNotificationClick,
              weak_ptr_factory_.GetWeakPtr()));

  ShowNotification(ash::kPendingRebootNotificationId, reboot_title,
                   reboot_message, notification_data, delegate);
}

void RebootNotificationController::MaybeShowPendingRebootDialog(
    const base::Time& reboot_time,
    base::OnceClosure reboot_callback) {
  if (!ShouldNotifyUser()) {
    return;
  }

  gfx::NativeView parent =
      ash::Shell::GetContainer(ash::Shell::GetRootWindowForNewWindows(),
                               ash::kShellWindowId_SystemModalContainer);
  // Closes old dialog if it was active at the moment and shows a new dialog
  // notifying the user about the reboot.
  scheduled_reboot_dialog_ = std::make_unique<ScheduledRebootDialog>(
      reboot_time, parent, std::move(reboot_callback));
}

void RebootNotificationController::MaybeShowPostRebootNotification() const {
  if (!ShouldNotifyUser()) {
    return;
  }
  std::u16string title =
      l10n_util::GetStringUTF16(IDS_POLICY_DEVICE_POST_REBOOT_TITLE);
  scoped_refptr<message_center::NotificationDelegate> delegate =
      base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
          base::BindRepeating(
              &RebootNotificationController::HandleNotificationClick,
              weak_ptr_factory_.GetWeakPtr()));
  ShowNotification(ash::kPostRebootNotificationId, title, std::u16string(),
                   message_center::RichNotificationData(), delegate);
}

void RebootNotificationController::CloseRebootNotification() const {
  if (!ShouldNotifyUser()) {
    return;
  }
  NotificationDisplayService* notification_display_service =
      NotificationDisplayServiceFactory::GetForProfile(
          ProfileManager::GetActiveUserProfile());
  notification_display_service->Close(NotificationHandler::Type::TRANSIENT,
                                      ash::kPendingRebootNotificationId);
}

void RebootNotificationController::CloseRebootDialog() {
  if (scheduled_reboot_dialog_) {
    scheduled_reboot_dialog_.reset();
  }
}

void RebootNotificationController::ShowNotification(
    const std::string& id,
    const std::u16string& title,
    const std::u16string& message,
    const message_center::RichNotificationData& data,
    scoped_refptr<message_center::NotificationDelegate> delegate) const {
  // Create notification.
  message_center::Notification notification = ash::CreateSystemNotification(
      message_center::NOTIFICATION_TYPE_SIMPLE, id, title, message,
      std::u16string(), GURL(), message_center::NotifierId(), data, delegate,
      vector_icons::kBusinessIcon,
      message_center::SystemNotificationWarningLevel::NORMAL);

  NotificationDisplayService* notification_display_service =
      NotificationDisplayServiceFactory::GetForProfile(
          ProfileManager::GetActiveUserProfile());
  // Close old notification.
  notification_display_service->Close(NotificationHandler::Type::TRANSIENT, id);
  // Display new notification.
  notification_display_service->Display(NotificationHandler::Type::TRANSIENT,
                                        notification,
                                        /*metadata=*/nullptr);
}

bool RebootNotificationController::ShouldNotifyUser() const {
  return (user_manager::UserManager::IsInitialized() &&
          user_manager::UserManager::Get()->IsUserLoggedIn() &&
          !user_manager::UserManager::Get()->IsLoggedInAsAnyKioskApp());
}

void RebootNotificationController::HandleNotificationClick(
    std::optional<int> button_index) const {
  // Only request restart when the button is clicked, i.e. ignore the clicks
  // on the body of the notification.
  if (!button_index) {
    return;
  }
  notification_callback_.Run();
}