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
|
// Copyright 2020 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/notifications/mac/notification_utils.h"
#include <optional>
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/i18n/number_formatting.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/notification_display_service_impl.h"
#include "chrome/browser/notifications/notification_platform_bridge.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/notifications/notification_constants.h"
#include "chrome/common/notifications/notification_operation.h"
#include "components/url_formatter/elide_url.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "third_party/blink/public/common/notifications/notification_constants.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace {
void DisplayWebAppSettings(const webapps::AppId& web_app_id, Profile* profile) {
if (!profile) {
LOG(WARNING) << "Profile not loaded correctly";
return;
}
chrome::ShowWebAppSettings(
profile, web_app_id,
web_app::AppSettingsPageEntryPoint::kNotificationSettingsButton);
}
// Loads the profile and process the Notification response
void DoProcessMacNotificationResponse(
mac_notifications::mojom::NotificationActionInfoPtr info,
std::optional<webapps::AppId> web_app_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
ProfileManager* profile_manager = g_browser_process->profile_manager();
DCHECK(profile_manager);
std::optional<int> action_index;
if (info->button_index != kNotificationInvalidButtonIndex)
action_index = info->button_index;
auto operation = static_cast<NotificationOperation>(info->operation);
ProfileManager::ProfileLoadedCallback callback =
(operation == NotificationOperation::kSettings && web_app_id.has_value())
? base::BindOnce(&DisplayWebAppSettings, *web_app_id)
: base::BindOnce(
&NotificationDisplayServiceImpl::ProfileLoadedCallback,
operation,
static_cast<NotificationHandler::Type>(info->meta->type),
std::move(info->meta->origin_url),
std::move(info->meta->id->id), std::move(action_index),
std::move(info->reply), /*by_user=*/true, base::DoNothing());
profile_manager->LoadProfile(
NotificationPlatformBridge::GetProfileBaseNameFromProfileId(
info->meta->id->profile->id),
info->meta->id->profile->incognito, std::move(callback));
}
// Get the user data directory.
std::string GetUserDataDir() {
return base::PathService::CheckedGet(chrome::DIR_USER_DATA).value();
}
} // namespace
std::u16string CreateMacNotificationTitle(
const message_center::Notification& notification) {
std::u16string title;
// Show progress percentage if available. We don't support indeterminate
// states on macOS native notifications.
if (notification.type() == message_center::NOTIFICATION_TYPE_PROGRESS &&
notification.progress() >= 0 && notification.progress() <= 100) {
title += base::FormatPercent(notification.progress());
title += u" - ";
}
title += notification.title();
return title;
}
std::u16string CreateMacNotificationContext(
bool isPersistent,
const message_center::Notification& notification,
bool requiresAttribution) {
if (!requiresAttribution)
return notification.context_message();
// Mac OS notifications don't provide a good way to elide the domain (or tell
// you the maximum width of the subtitle field). We have experimentally
// determined the maximum number of characters that fit using the widest
// possible character (m). If the domain fits in those character we show it
// completely. Otherwise we use eTLD + 1.
// These numbers have been obtained through experimentation on various
// Mac OS platforms.
constexpr size_t kMaxDomainLengthAlert = 19;
constexpr size_t kMaxDomainLengthBanner = 28;
size_t maxCharacters =
isPersistent ? kMaxDomainLengthAlert : kMaxDomainLengthBanner;
std::u16string origin = url_formatter::FormatOriginForSecurityDisplay(
url::Origin::Create(notification.origin_url()),
url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
if (origin.size() <= maxCharacters)
return origin;
// Too long, use etld+1
std::u16string etldplusone =
base::UTF8ToUTF16(net::registry_controlled_domains::GetDomainAndRegistry(
notification.origin_url(),
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
// localhost, raw IPs etc. are not handled by GetDomainAndRegistry.
if (etldplusone.empty())
return origin;
return etldplusone;
}
bool VerifyMacNotificationData(
const mac_notifications::mojom::NotificationActionInfoPtr& info) {
if (!info || !info->meta || !info->meta->id || !info->meta->id->profile) {
LOG(ERROR) << "Missing required data";
return false;
}
if (info->meta->user_data_dir != GetUserDataDir()) {
return false;
}
if (info->button_index < kNotificationInvalidButtonIndex ||
info->button_index >= static_cast<int>(blink::kNotificationMaxActions)) {
LOG(ERROR) << "Invalid number of buttons supplied " << info->button_index;
return false;
}
if (info->meta->id->id.empty()) {
LOG(ERROR) << "Notification Id is empty";
return false;
}
if (info->meta->id->profile->id.empty()) {
LOG(ERROR) << "ProfileId not provided";
return false;
}
if (info->meta->type > static_cast<int>(NotificationHandler::Type::MAX)) {
LOG(ERROR) << info->meta->type
<< " Does not correspond to a valid operation.";
return false;
}
// Origin is not actually required but if it's there it should be a valid one.
if (!info->meta->origin_url.is_empty() && !info->meta->origin_url.is_valid())
return false;
return true;
}
void ProcessMacNotificationResponse(
mac_notifications::NotificationStyle notification_style,
mac_notifications::mojom::NotificationActionInfoPtr info,
std::optional<webapps::AppId> web_app_id) {
bool is_valid = VerifyMacNotificationData(info);
if (!is_valid)
return;
std::optional<int> actionIndex;
if (info->button_index != kNotificationInvalidButtonIndex)
actionIndex = info->button_index;
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(DoProcessMacNotificationResponse,
std::move(info), web_app_id));
}
bool IsAlertNotificationMac(const message_center::Notification& notification) {
// Check if the |notification| should be shown as alert.
return notification.never_timeout() ||
notification.type() == message_center::NOTIFICATION_TYPE_PROGRESS;
}
mac_notifications::mojom::NotificationPtr CreateMacNotification(
NotificationHandler::Type notification_type,
Profile* profile,
const message_center::Notification& notification) {
auto profile_identifier = mac_notifications::mojom::ProfileIdentifier::New(
NotificationPlatformBridge::GetProfileId(profile),
profile->IsOffTheRecord());
auto notification_identifier =
mac_notifications::mojom::NotificationIdentifier::New(
notification.id(), std::move(profile_identifier));
auto meta = mac_notifications::mojom::NotificationMetadata::New(
std::move(notification_identifier), static_cast<int>(notification_type),
notification.origin_url(), GetUserDataDir());
std::vector<mac_notifications::mojom::NotificationActionButtonPtr> buttons;
for (const message_center::ButtonInfo& button : notification.buttons()) {
buttons.push_back(mac_notifications::mojom::NotificationActionButton::New(
button.title, button.placeholder));
}
bool is_alert = IsAlertNotificationMac(notification);
bool requires_attribution =
notification.context_message().empty() &&
notification_type != NotificationHandler::Type::EXTENSION;
std::u16string body = notification.items().empty()
? notification.message()
: (notification.items().at(0).title() + u" - " +
notification.items().at(0).message());
return mac_notifications::mojom::Notification::New(
std::move(meta), CreateMacNotificationTitle(notification),
CreateMacNotificationContext(is_alert, notification,
requires_attribution),
std::move(body), notification.renotify(),
notification.should_show_settings_button(), std::move(buttons),
notification.icon().Rasterize(
ThemeServiceFactory::GetForProfile(profile)->GetColorProvider()));
}
|