File: eche_app_notification_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (179 lines) | stat: -rw-r--r-- 8,400 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2021 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/eche_app/eche_app_notification_controller.h"

#include <variant>

#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/webui/eche_app_ui/eche_alert_generator.h"
#include "ash/webui/settings/public/constants/routes.mojom.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/ui/settings_window_manager_chromeos.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification_delegate.h"

namespace ash {
namespace eche_app {

namespace {

// Convenience function for creating a Notification.
std::unique_ptr<message_center::Notification> CreateNotification(
    const std::string& id,
    const NotificationCatalogName& catalog_name,
    const std::u16string& title,
    const std::u16string& message,
    const ui::ImageModel& icon,
    const message_center::RichNotificationData& rich_notification_data,
    scoped_refptr<message_center::NotificationDelegate> delegate) {
  return std::make_unique<message_center::Notification>(
      message_center::NotificationType::NOTIFICATION_TYPE_SIMPLE, id, title,
      message, icon, std::u16string() /* display_source */,
      GURL() /* origin_url */,
      message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
                                 id, catalog_name),
      rich_notification_data, delegate);
}

}  // namespace

EcheAppNotificationController::EcheAppNotificationController(
    Profile* profile,
    const base::RepeatingCallback<void(Profile*)>& relaunch_callback)
    : profile_(profile), relaunch_callback_(relaunch_callback) {}

EcheAppNotificationController::~EcheAppNotificationController() = default;

void EcheAppNotificationController::LaunchSettings() {
  // TODO(crbug.com/40785967): Wait for UX confirm.
  chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(
      profile_, chromeos::settings::mojom::kSecurityAndSignInSubpagePathV2);
}

void EcheAppNotificationController::LaunchNetworkSettings() {
  chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(
      profile_, chromeos::settings::mojom::kNetworkSectionPath);
}

void EcheAppNotificationController::LaunchTryAgain() {
  relaunch_callback_.Run(profile_.get());
}

void EcheAppNotificationController::ShowNotificationFromWebUI(
    const std::optional<std::u16string>& title,
    const std::optional<std::u16string>& message,
    std::variant<LaunchAppHelper::NotificationInfo::NotificationType,
                 mojom::WebNotificationType> type) {
  auto web_type = std::get<mojom::WebNotificationType>(type);
  PA_LOG(INFO) << "ShowNotificationFromWebUI web_type: " << web_type;
  if (title && message) {
    if (web_type == mojom::WebNotificationType::CONNECTION_FAILED ||
        web_type == mojom::WebNotificationType::CONNECTION_LOST) {
      message_center::RichNotificationData rich_notification_data;
      rich_notification_data.buttons.push_back(
          message_center::ButtonInfo(l10n_util::GetStringUTF16(
              IDS_ECHE_APP_NOTIFICATION_TRY_AGAIN_BUTTON)));
      ShowNotification(CreateNotification(
          kEcheAppRetryConnectionNotifierId,
          NotificationCatalogName::kEcheAppRetryConnection, title.value(),
          message.value(), ui::ImageModel(), rich_notification_data,
          base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
              base::BindRepeating(
                  &EcheAppNotificationController::LaunchTryAgain,
                  weak_ptr_factory_.GetWeakPtr()))));
    } else if (web_type == mojom::WebNotificationType::DEVICE_IDLE) {
      message_center::RichNotificationData rich_notification_data;
      rich_notification_data.buttons.push_back(
          message_center::ButtonInfo(l10n_util::GetStringUTF16(
              IDS_ECHE_APP_NOTIFICATION_OPEN_AGAIN_BUTTON)));
      ShowNotification(CreateNotification(
          kEcheAppInactivityNotifierId,
          NotificationCatalogName::kEcheAppInactivity, title.value(),
          message.value(), ui::ImageModel(), rich_notification_data,
          base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
              base::BindRepeating(
                  &EcheAppNotificationController::LaunchTryAgain,
                  weak_ptr_factory_.GetWeakPtr()))));
    } else if (web_type == mojom::WebNotificationType::WIFI_NOT_READY) {
      message_center::RichNotificationData rich_notification_data;
      // Reuse the setting string for Eche's setting button.
      rich_notification_data.buttons.emplace_back(
          l10n_util::GetStringUTF16(IDS_ECHE_APP_SCREEN_LOCK_SETTINGS_BUTTON));
      ShowNotification(CreateNotification(
          kEcheAppNetworkSettingNotifierId,
          NotificationCatalogName::kEcheAppNetworkSetting, title.value(),
          message.value(), ui::ImageModel(), rich_notification_data,
          base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
              base::BindRepeating(
                  &EcheAppNotificationController::LaunchNetworkSettings,
                  weak_ptr_factory_.GetWeakPtr()))));
    } else {
      // No need to take the action.
      ShowNotification(CreateNotification(
          kEcheAppFromWebWithoutButtonNotifierId,
          NotificationCatalogName::kEcheAppFromWebWithoutButton, title.value(),
          message.value(), ui::ImageModel(),
          message_center::RichNotificationData(),
          base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
              message_center::HandleNotificationClickDelegate::
                  ButtonClickCallback(base::DoNothing()))));
    }
  } else {
    PA_LOG(ERROR)
        << "Cannot find the title or message to show the notification.";
  }
}

void EcheAppNotificationController::ShowScreenLockNotification(
    const std::u16string& title) {
  message_center::RichNotificationData rich_notification_data;
  rich_notification_data.buttons.push_back(message_center::ButtonInfo(
      l10n_util::GetStringUTF16(IDS_ECHE_APP_SCREEN_LOCK_SETTINGS_BUTTON)));
  ShowNotification(CreateNotification(
      kEcheAppScreenLockNotifierId, NotificationCatalogName::kEcheAppScreenLock,
      l10n_util::GetStringFUTF16(IDS_ECHE_APP_SCREEN_LOCK_NOTIFICATION_TITLE,
                                 title),
      l10n_util::GetStringUTF16(IDS_ECHE_APP_SCREEN_LOCK_NOTIFICATION_MESSAGE),
      ui::ImageModel(), rich_notification_data,
      base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
          base::BindRepeating(&EcheAppNotificationController::LaunchSettings,
                              weak_ptr_factory_.GetWeakPtr()))));
}

void EcheAppNotificationController::ShowNotification(
    std::unique_ptr<message_center::Notification> notification) {
  notification->SetSystemPriority();
  NotificationDisplayServiceFactory::GetForProfile(profile_)->Display(
      NotificationHandler::Type::TRANSIENT, *notification,
      /*metadata=*/nullptr);
}

void EcheAppNotificationController::CloseNotification(
    const std::string& notification_id) {
  NotificationDisplayServiceFactory::GetForProfile(profile_)->Close(
      NotificationHandler::Type::TRANSIENT, notification_id);
}

void EcheAppNotificationController::
    CloseConnectionOrLaunchErrorNotifications() {
  NotificationDisplayServiceFactory::GetForProfile(profile_)->Close(
      NotificationHandler::Type::TRANSIENT, kEcheAppRetryConnectionNotifierId);
  NotificationDisplayServiceFactory::GetForProfile(profile_)->Close(
      NotificationHandler::Type::TRANSIENT, kEcheAppInactivityNotifierId);
  NotificationDisplayServiceFactory::GetForProfile(profile_)->Close(
      NotificationHandler::Type::TRANSIENT,
      kEcheAppFromWebWithoutButtonNotifierId);
  NotificationDisplayServiceFactory::GetForProfile(profile_)->Close(
      NotificationHandler::Type::TRANSIENT, kEcheAppNetworkSettingNotifierId);
}

}  // namespace eche_app
}  // namespace ash