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
|
// Copyright 2017 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/notification_platform_bridge_message_center.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/no_destructor.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/notification_display_service_impl.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/browser/notifications/profile_notification.h"
#include "chrome/common/notifications/notification_operation.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "ui/message_center/public/cpp/notification.h"
namespace {
// A NotificationDelegate that passes through click actions to the notification
// display service (and on to the appropriate handler). This is a temporary
// class to ease the transition from NotificationDelegate to
// NotificationHandler.
// TODO(estade): also handle other NotificationDelegate actions as needed.
class PassThroughDelegate : public message_center::NotificationDelegate {
public:
PassThroughDelegate(Profile* profile,
const message_center::Notification& notification,
NotificationHandler::Type notification_type)
: profile_(profile),
notification_(notification),
notification_type_(notification_type) {
DCHECK_NE(notification_type, NotificationHandler::Type::TRANSIENT);
}
PassThroughDelegate(const PassThroughDelegate&) = delete;
PassThroughDelegate& operator=(const PassThroughDelegate&) = delete;
void SettingsClick() override {
NotificationDisplayServiceImpl::GetForProfile(profile_)
->ProcessNotificationOperation(
NotificationOperation::kSettings, notification_type_,
notification_.origin_url(), notification_.id(), std::nullopt,
std::nullopt, std::nullopt /* by_user */, base::DoNothing());
}
void DisableNotification() override {
NotificationDisplayServiceImpl::GetForProfile(profile_)
->ProcessNotificationOperation(
NotificationOperation::kDisablePermission, notification_type_,
notification_.origin_url(), notification_.id(),
std::nullopt /* action_index */, std::nullopt /* reply */,
std::nullopt /* by_user */, base::DoNothing());
}
void Close(bool by_user) override {
NotificationDisplayServiceImpl::GetForProfile(profile_)
->ProcessNotificationOperation(
NotificationOperation::kClose, notification_type_,
notification_.origin_url(), notification_.id(),
std::nullopt /* action_index */, std::nullopt /* reply */, by_user,
base::DoNothing());
}
void Click(const std::optional<int>& button_index,
const std::optional<std::u16string>& reply) override {
NotificationDisplayServiceImpl::GetForProfile(profile_)
->ProcessNotificationOperation(
NotificationOperation::kClick, notification_type_,
notification_.origin_url(), notification_.id(), button_index, reply,
std::nullopt /* by_user */, base::DoNothing());
}
protected:
~PassThroughDelegate() override = default;
private:
raw_ptr<Profile> profile_;
message_center::Notification notification_;
NotificationHandler::Type notification_type_;
};
} // namespace
// static
NotificationPlatformBridgeMessageCenter*
NotificationPlatformBridgeMessageCenter::Get() {
static base::NoDestructor<NotificationPlatformBridgeMessageCenter> instance;
return instance.get();
}
NotificationPlatformBridgeMessageCenter::
NotificationPlatformBridgeMessageCenter() = default;
NotificationPlatformBridgeMessageCenter::
~NotificationPlatformBridgeMessageCenter() = default;
void NotificationPlatformBridgeMessageCenter::Display(
NotificationHandler::Type notification_type,
Profile* profile,
const message_center::Notification& notification,
std::unique_ptr<NotificationCommon::Metadata> /* metadata */) {
NotificationUIManager* ui_manager =
g_browser_process->notification_ui_manager();
if (!ui_manager)
return; // The process is shutting down.
if (notification.delegate() ||
notification_type == NotificationHandler::Type::TRANSIENT) {
ui_manager->Add(notification, profile);
return;
}
// If there's no delegate, replace it with a PassThroughDelegate so clicks
// go back to the appropriate handler.
message_center::Notification notification_with_delegate(notification);
notification_with_delegate.set_delegate(base::WrapRefCounted(
new PassThroughDelegate(profile, notification, notification_type)));
ui_manager->Add(notification_with_delegate, profile);
}
void NotificationPlatformBridgeMessageCenter::Close(
Profile* profile,
const std::string& notification_id) {
NotificationUIManager* ui_manager =
g_browser_process->notification_ui_manager();
if (!ui_manager)
return; // the process is shutting down
ui_manager->CancelById(notification_id,
ProfileNotification::GetProfileID(profile));
}
void NotificationPlatformBridgeMessageCenter::GetDisplayed(
Profile* profile,
GetDisplayedNotificationsCallback callback) const {
std::set<std::string> displayed_notifications;
NotificationUIManager* ui_manager =
g_browser_process->notification_ui_manager();
if (ui_manager) {
displayed_notifications = ui_manager->GetAllIdsByProfile(
ProfileNotification::GetProfileID(profile));
}
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), std::move(displayed_notifications),
true /* supports_synchronization */));
}
void NotificationPlatformBridgeMessageCenter::GetDisplayedForOrigin(
Profile* profile,
const GURL& origin,
GetDisplayedNotificationsCallback callback) const {
std::set<std::string> displayed_notifications;
NotificationUIManager* ui_manager =
g_browser_process->notification_ui_manager();
if (ui_manager) {
displayed_notifications = ui_manager->GetAllIdsByProfileAndOrigin(
ProfileNotification::GetProfileID(profile), origin);
}
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), std::move(displayed_notifications),
true /* supports_synchronization */));
}
void NotificationPlatformBridgeMessageCenter::SetReadyCallback(
NotificationBridgeReadyCallback callback) {
std::move(callback).Run(true /* success */);
}
void NotificationPlatformBridgeMessageCenter::DisplayServiceShutDown(
Profile* profile) {}
|