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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
ash / system / pcie_peripheral / pcie_peripheral_notification_controller.cc [blame]
// 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 "ash/system/pcie_peripheral/pcie_peripheral_notification_controller.h"
#include <optional>
#include <string>
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/public/cpp/notification_utils.h"
#include "ash/public/cpp/system_tray_client.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/model/system_tray_model.h"
#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_types.h"
#include "url/gurl.h"
namespace ash {
namespace {
const char kNotifierPciePeripheral[] = "ash.pcie_peripheral";
const char kLearnMoreHelpUrl[] =
"https://www.support.google.com/chromebook?p=connect_thblt_usb4_accy";
const int kNotificationsClicksThreshold = 3;
const char kPciePeripheralLimitedPerformanceNotificationId[] =
"cros_pcie_peripheral_limited_performance_notification_id";
const char kPciePeripheralLimitedPerformanceGuestModeNotificationId[] =
"cros_pcie_peripheral_limited_performance_guest_mode_notification_id";
const char kPciePeripheralGuestModeNotSupportedNotificationId[] =
"cros_pcie_peripheral_guest_mode_not_supported_notifcation_id";
const char kPciePeripheralDeviceBlockedNotificationId[] =
"cros_pcie_peripheral_device_blocked_notifcation_id";
const char kPciePeripheralBillboardDeviceNotificationId[] =
"cros_pcie_peripheral_billboard_device_notifcation_id";
// Represents the buttons in the notification.
enum ButtonIndex { kSettings, kLearnMore };
int GetNotificationClickPrefCount() {
PrefService* prefs =
Shell::Get()->session_controller()->GetActivePrefService();
return prefs->GetInteger(prefs::kPciePeripheralDisplayNotificationRemaining);
}
void UpdateNotificationPrefCount(bool clicked_settings) {
int current_pref_val = GetNotificationClickPrefCount();
// We're already not showing any new notifications, don't update.
if (current_pref_val == 0)
return;
PrefService* prefs =
Shell::Get()->session_controller()->GetActivePrefService();
// If the user has reached the settings page through the notification, do
// not show any more new notifications.
if (clicked_settings) {
prefs->SetInteger(prefs::kPciePeripheralDisplayNotificationRemaining, 0);
return;
}
// Otherwise, decrement the pref count.
prefs->SetInteger(prefs::kPciePeripheralDisplayNotificationRemaining,
current_pref_val - 1);
}
void ShowPrivacyAndSecuritySettings() {
Shell::Get()->system_tray_model()->client()->ShowPrivacyAndSecuritySettings();
}
void RemoveNotification(const std::string& notification_id) {
message_center::MessageCenter::Get()->RemoveNotification(notification_id,
/*from_user=*/true);
}
void OnPeripheralLimitedNotificationClicked(std::optional<int> button_index) {
// Clicked on body.
if (!button_index) {
ShowPrivacyAndSecuritySettings();
UpdateNotificationPrefCount(/*clicked_settings=*/true);
RemoveNotification(kPciePeripheralLimitedPerformanceNotificationId);
return;
}
switch (*button_index) {
case ButtonIndex::kSettings:
ShowPrivacyAndSecuritySettings();
UpdateNotificationPrefCount(/*clicked_settings=*/true);
break;
case ButtonIndex::kLearnMore:
NewWindowDelegate::GetPrimary()->OpenUrl(
GURL(kLearnMoreHelpUrl),
NewWindowDelegate::OpenUrlFrom::kUserInteraction,
NewWindowDelegate::Disposition::kNewForegroundTab);
break;
}
RemoveNotification(kPciePeripheralLimitedPerformanceNotificationId);
}
void OnGuestNotificationClicked(bool is_thunderbolt_only) {
NewWindowDelegate::GetPrimary()->OpenUrl(
GURL(kLearnMoreHelpUrl), NewWindowDelegate::OpenUrlFrom::kUserInteraction,
NewWindowDelegate::Disposition::kNewForegroundTab);
if (is_thunderbolt_only) {
RemoveNotification(kPciePeripheralGuestModeNotSupportedNotificationId);
return;
}
RemoveNotification(kPciePeripheralLimitedPerformanceGuestModeNotificationId);
}
void OnPeripheralBlockedNotificationClicked() {
NewWindowDelegate::GetPrimary()->OpenUrl(
GURL(kLearnMoreHelpUrl), NewWindowDelegate::OpenUrlFrom::kUserInteraction,
NewWindowDelegate::Disposition::kNewForegroundTab);
RemoveNotification(kPciePeripheralDeviceBlockedNotificationId);
}
void OnBillboardNotificationClicked() {
NewWindowDelegate::GetPrimary()->OpenUrl(
GURL(kLearnMoreHelpUrl), NewWindowDelegate::OpenUrlFrom::kUserInteraction,
NewWindowDelegate::Disposition::kNewForegroundTab);
RemoveNotification(kPciePeripheralBillboardDeviceNotificationId);
}
// We only display notifications for active user sessions (signed-in/guest with
// desktop ready). Also do not show notifications in signin or lock screen.
bool ShouldDisplayNotification() {
return Shell::Get()->session_controller()->GetSessionState() ==
session_manager::SessionState::ACTIVE &&
!Shell::Get()->session_controller()->IsUserSessionBlocked();
}
} // namespace
PciePeripheralNotificationController::PciePeripheralNotificationController(
message_center::MessageCenter* message_center)
: message_center_(message_center) {
DCHECK(message_center_);
}
PciePeripheralNotificationController::~PciePeripheralNotificationController() {
if (ash::PeripheralNotificationManager::IsInitialized())
ash::PeripheralNotificationManager::Get()->RemoveObserver(this);
}
void PciePeripheralNotificationController::
OnPeripheralNotificationManagerInitialized() {
DCHECK(ash::PeripheralNotificationManager::IsInitialized());
ash::PeripheralNotificationManager::Get()->AddObserver(this);
}
void PciePeripheralNotificationController::NotifyBillboardDevice() {
std::unique_ptr<message_center::Notification> notification =
CreateSystemNotificationPtr(
message_center::NOTIFICATION_TYPE_SIMPLE,
kPciePeripheralBillboardDeviceNotificationId,
/*title=*/std::u16string(),
l10n_util::GetStringUTF16(
IDS_ASH_PCIE_PERIPHERAL_NOTIFICATION_BILLBOARD_DEVICE),
/*display_source=*/std::u16string(), GURL(),
message_center::NotifierId(
message_center::NotifierType::SYSTEM_COMPONENT,
kNotifierPciePeripheral,
NotificationCatalogName::kPcieBillboardDevice),
message_center::RichNotificationData(),
base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
base::BindRepeating(&OnBillboardNotificationClicked)),
kSettingsIcon,
message_center::SystemNotificationWarningLevel::CRITICAL_WARNING);
message_center_->AddNotification(std::move(notification));
}
void PciePeripheralNotificationController::NotifyLimitedPerformance() {
// Don't show the notification if the user has already clicked on the
// notification three times.
if (!ShouldDisplayNotification() || GetNotificationClickPrefCount() == 0)
return;
message_center::RichNotificationData optional;
optional.buttons.push_back(
message_center::ButtonInfo(l10n_util::GetStringUTF16(
IDS_ASH_PCIE_PERIPHERAL_NOTIFICATION_SETTINGS_BUTTON_TEXT)));
optional.buttons.push_back(
message_center::ButtonInfo(l10n_util::GetStringUTF16(
IDS_ASH_PCIE_PERIPHERAL_NOTIFICATION_LEARN_MORE_BUTTON_TEXT)));
std::unique_ptr<message_center::Notification> notification =
CreateSystemNotificationPtr(
message_center::NOTIFICATION_TYPE_SIMPLE,
kPciePeripheralLimitedPerformanceNotificationId,
l10n_util::GetStringUTF16(
IDS_ASH_PCIE_PERIPHERAL_NOTIFICATION_PERFORMANCE_LIMITED_TITLE),
l10n_util::GetStringUTF16(
IDS_ASH_PCIE_PERIPHERAL_NOTIFICATION_PERFORMANCE_LIMITED_BODY),
/*display_source=*/std::u16string(), GURL(),
message_center::NotifierId(
message_center::NotifierType::SYSTEM_COMPONENT,
kNotifierPciePeripheral,
NotificationCatalogName::kPcieLimitedPerformance),
optional,
base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
base::BindRepeating(&OnPeripheralLimitedNotificationClicked)),
kSettingsIcon,
message_center::SystemNotificationWarningLevel::WARNING);
message_center_->AddNotification(std::move(notification));
UpdateNotificationPrefCount(/*clicked_settings=*/false);
}
void PciePeripheralNotificationController::NotifyGuestModeNotification(
bool is_thunderbolt_only) {
if (!ShouldDisplayNotification())
return;
std::unique_ptr<message_center::Notification> notification =
CreateSystemNotificationPtr(
message_center::NOTIFICATION_TYPE_SIMPLE,
is_thunderbolt_only
? kPciePeripheralGuestModeNotSupportedNotificationId
: kPciePeripheralLimitedPerformanceGuestModeNotificationId,
/*title=*/std::u16string(),
is_thunderbolt_only
? l10n_util::GetStringUTF16(
IDS_ASH_PCIE_PERIPHERAL_NOTIFICATION_GUEST_MODE_NOT_SUPPORTED)
: l10n_util::GetStringUTF16(
IDS_ASH_PCIE_PERIPHERAL_NOTIFICATION_PERFORMANCE_LIMITED_GUEST_MODE),
/*display_source=*/std::u16string(), GURL(),
message_center::NotifierId(
message_center::NotifierType::SYSTEM_COMPONENT,
kNotifierPciePeripheral, NotificationCatalogName::kPcieGuestMode),
message_center::RichNotificationData(),
base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
base::BindRepeating(&OnGuestNotificationClicked,
is_thunderbolt_only)),
kSettingsIcon,
is_thunderbolt_only
? message_center::SystemNotificationWarningLevel::CRITICAL_WARNING
: message_center::SystemNotificationWarningLevel::WARNING);
message_center_->AddNotification(std::move(notification));
}
void PciePeripheralNotificationController::
NotifyPeripheralBlockedNotification() {
std::unique_ptr<message_center::Notification> notification =
CreateSystemNotificationPtr(
message_center::NOTIFICATION_TYPE_SIMPLE,
kPciePeripheralDeviceBlockedNotificationId,
l10n_util::GetStringUTF16(
IDS_ASH_PCIE_PERIPHERAL_NOTIFICATION_DEVICE_BLOCKED_TITLE),
l10n_util::GetStringUTF16(
IDS_ASH_PCIE_PERIPHERAL_NOTIFICATION_DEVICE_BLOCKED_BODY),
/*display_source=*/std::u16string(), GURL(),
message_center::NotifierId(
message_center::NotifierType::SYSTEM_COMPONENT,
kNotifierPciePeripheral,
NotificationCatalogName::kPciePeripheralBlocked),
message_center::RichNotificationData(),
base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
base::BindRepeating(&OnPeripheralBlockedNotificationClicked)),
kSettingsIcon,
message_center::SystemNotificationWarningLevel::CRITICAL_WARNING);
message_center_->AddNotification(std::move(notification));
}
void PciePeripheralNotificationController::
OnLimitedPerformancePeripheralReceived() {
NotifyLimitedPerformance();
}
void PciePeripheralNotificationController::OnGuestModeNotificationReceived(
bool is_thunderbolt_only) {
NotifyGuestModeNotification(is_thunderbolt_only);
}
void PciePeripheralNotificationController::OnPeripheralBlockedReceived() {
NotifyPeripheralBlockedNotification();
}
void PciePeripheralNotificationController::OnBillboardDeviceConnected() {
NotifyBillboardDevice();
}
// static
void PciePeripheralNotificationController::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
// By default, we let the user click on the notifications three times before
// hiding future notifications.
registry->RegisterIntegerPref(
prefs::kPciePeripheralDisplayNotificationRemaining,
kNotificationsClicksThreshold);
}
} // namespace ash