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
|
// Copyright 2023 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/device_notifications/device_pinned_notification_renderer.h"
#include "ash/constants/ash_features.h"
#include "base/i18n/message_formatter.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/device_notifications/device_connection_tracker.h"
#include "chrome/browser/notifications/system_notification_helper.h"
#include "components/vector_icons/vector_icons.h"
#include "extensions/buildflags/buildflags.h"
#include "ui/base/l10n/l10n_util.h"
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
namespace {
std::u16string GetMessageLabel(DeviceConnectionTracker* connection_tracker,
int message_id) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
std::vector<std::u16string> extension_names;
for (const auto& [origin, state] : connection_tracker->origins()) {
CHECK_EQ(origin.scheme(), extensions::kExtensionScheme);
extension_names.push_back(base::UTF8ToUTF16(state.name));
}
CHECK(!extension_names.empty());
if (extension_names.size() == 1) {
return base::i18n::MessageFormatter::FormatWithNumberedArgs(
l10n_util::GetStringUTF16(message_id), 1, extension_names[0]);
} else if (extension_names.size() == 2) {
return base::i18n::MessageFormatter::FormatWithNumberedArgs(
l10n_util::GetStringUTF16(message_id), 2, extension_names[0],
extension_names[1]);
}
return base::i18n::MessageFormatter::FormatWithNumberedArgs(
l10n_util::GetStringUTF16(message_id),
static_cast<int>(extension_names.size()), extension_names[0],
extension_names[1], static_cast<int>(extension_names.size() - 2));
#else
NOTREACHED();
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
}
} // namespace
DevicePinnedNotificationRenderer::DevicePinnedNotificationRenderer(
DeviceSystemTrayIcon* device_system_tray_icon,
const std::string& notification_id_prefix,
#if BUILDFLAG(IS_CHROMEOS)
const ash::NotificationCatalogName notification_catalog_name,
#endif
const int message_id)
: DeviceSystemTrayIconRenderer(device_system_tray_icon),
notification_id_prefix_(notification_id_prefix),
#if BUILDFLAG(IS_CHROMEOS)
notification_catalog_name_(notification_catalog_name),
#endif
message_id_(message_id) {}
DevicePinnedNotificationRenderer::~DevicePinnedNotificationRenderer() = default;
void DevicePinnedNotificationRenderer::AddProfile(Profile* profile) {
DisplayNotification(CreateNotification(profile));
}
void DevicePinnedNotificationRenderer::RemoveProfile(Profile* profile) {
SystemNotificationHelper::GetInstance()->Close(GetNotificationId(profile));
}
void DevicePinnedNotificationRenderer::NotifyConnectionUpdated(
Profile* profile) {
DisplayNotification(CreateNotification(profile));
}
void DevicePinnedNotificationRenderer::DisplayNotification(
std::unique_ptr<message_center::Notification> notification) {
SystemNotificationHelper::GetInstance()->Display(*notification);
}
std::string DevicePinnedNotificationRenderer::GetNotificationId(
Profile* profile) {
return base::StrCat({notification_id_prefix_, profile->UniqueId()});
}
std::unique_ptr<message_center::Notification>
DevicePinnedNotificationRenderer::CreateNotification(Profile* profile) {
message_center::RichNotificationData data;
#if BUILDFLAG(IS_CHROMEOS)
// The new pinned notification view uses a settings icon button.
if (ash::features::AreOngoingProcessesEnabled()) {
data.buttons.emplace_back(message_center::ButtonInfo(
/*vector_icon=*/&vector_icons::kSettingsIcon,
/*accessible_name=*/device_system_tray_icon_
->GetContentSettingsLabel()));
} else {
data.buttons.emplace_back(
device_system_tray_icon_->GetContentSettingsLabel());
}
#else
data.buttons.emplace_back(
device_system_tray_icon_->GetContentSettingsLabel());
#endif // BUILDFLAG(IS_CHROMEOS)
auto* device_connection_tracker =
device_system_tray_icon_->GetConnectionTracker(profile->GetWeakPtr());
DCHECK(device_connection_tracker);
auto delegate =
base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
base::BindRepeating(
[](DeviceConnectionTracker* connection_tracker,
std::optional<int> button_index) {
// ConnectionTracker guarantees that RemoveProfile() is
// called on Profile destruction so it is impossible to interact
// with the notification after `connection_tracker` becomes a
// dangling pointer.
if (!button_index) {
return;
}
DCHECK_EQ(*button_index, 0);
connection_tracker->ShowContentSettingsExceptions();
},
device_connection_tracker));
auto notification_id = GetNotificationId(profile);
auto notification = std::make_unique<message_center::Notification>(
message_center::NOTIFICATION_TYPE_SIMPLE, notification_id,
device_system_tray_icon_->GetTitleLabel(
device_connection_tracker->origins().size(),
device_connection_tracker->total_connection_count()),
GetMessageLabel(device_connection_tracker, message_id_),
/*icon=*/ui::ImageModel(),
/*display_source=*/std::u16string(), /*origin_url=*/GURL(),
#if BUILDFLAG(IS_CHROMEOS)
message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
notification_id, notification_catalog_name_),
#else
message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
notification_id),
#endif
data, std::move(delegate));
notification->set_vector_small_image(device_system_tray_icon_->GetIcon());
notification->set_pinned(true);
// Set to low priority so it doesn't create a popup.
notification->set_priority(message_center::LOW_PRIORITY);
return notification;
}
|