2015-03-09 02:37:13 +00:00
|
|
|
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE-CHROMIUM file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/notifications/platform_notification_service.h"
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2017-05-29 10:02:33 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2017-11-01 22:14:27 +00:00
|
|
|
#include "content/public/browser/notification_event_dispatcher.h"
|
2018-10-05 21:24:38 +00:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/electron_browser_client.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/notifications/notification.h"
|
|
|
|
#include "shell/browser/notifications/notification_delegate.h"
|
|
|
|
#include "shell/browser/notifications/notification_presenter.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "third_party/blink/public/common/notifications/notification_resources.h"
|
|
|
|
#include "third_party/blink/public/common/notifications/platform_notification_data.h"
|
2016-01-23 13:32:30 +00:00
|
|
|
#include "third_party/skia/include/core/SkBitmap.h"
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2015-12-25 02:16:07 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-10-25 13:32:06 +00:00
|
|
|
void OnWebNotificationAllowed(base::WeakPtr<Notification> notification,
|
|
|
|
const SkBitmap& icon,
|
2019-01-12 01:00:43 +00:00
|
|
|
const blink::PlatformNotificationData& data,
|
2016-10-25 13:32:06 +00:00
|
|
|
bool audio_muted,
|
|
|
|
bool allowed) {
|
2016-10-28 17:54:55 +00:00
|
|
|
if (!notification)
|
2016-01-23 13:32:30 +00:00
|
|
|
return;
|
2017-06-24 11:03:27 +00:00
|
|
|
if (allowed) {
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::NotificationOptions options;
|
2017-06-24 11:03:27 +00:00
|
|
|
options.title = data.title;
|
|
|
|
options.msg = data.body;
|
|
|
|
options.tag = data.tag;
|
|
|
|
options.icon_url = data.icon;
|
|
|
|
options.icon = icon;
|
|
|
|
options.silent = audio_muted ? true : data.silent;
|
|
|
|
options.has_reply = false;
|
|
|
|
notification->Show(options);
|
|
|
|
} else {
|
2016-10-28 17:54:55 +00:00
|
|
|
notification->Destroy();
|
2017-06-24 11:03:27 +00:00
|
|
|
}
|
2016-01-23 13:32:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
class NotificationDelegateImpl final : public electron::NotificationDelegate {
|
2017-11-01 22:14:27 +00:00
|
|
|
public:
|
|
|
|
explicit NotificationDelegateImpl(const std::string& notification_id)
|
|
|
|
: notification_id_(notification_id) {}
|
|
|
|
|
|
|
|
void NotificationDestroyed() override { delete this; }
|
|
|
|
|
|
|
|
void NotificationClick() override {
|
|
|
|
content::NotificationEventDispatcher::GetInstance()
|
2018-10-02 20:05:57 +00:00
|
|
|
->DispatchNonPersistentClickEvent(notification_id_, base::DoNothing());
|
2017-11-01 22:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationClosed() override {
|
|
|
|
content::NotificationEventDispatcher::GetInstance()
|
2018-09-15 00:21:59 +00:00
|
|
|
->DispatchNonPersistentCloseEvent(notification_id_, base::DoNothing());
|
2017-11-01 22:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationDisplayed() override {
|
|
|
|
content::NotificationEventDispatcher::GetInstance()
|
|
|
|
->DispatchNonPersistentShowEvent(notification_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string notification_id_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(NotificationDelegateImpl);
|
|
|
|
};
|
|
|
|
|
2015-12-25 02:16:07 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-12-24 14:20:47 +00:00
|
|
|
PlatformNotificationService::PlatformNotificationService(
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserClient* browser_client)
|
2018-10-05 21:24:38 +00:00
|
|
|
: browser_client_(browser_client) {}
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
PlatformNotificationService::~PlatformNotificationService() = default;
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2015-12-24 14:20:47 +00:00
|
|
|
void PlatformNotificationService::DisplayNotification(
|
2021-04-29 18:23:28 +00:00
|
|
|
content::RenderFrameHost* render_frame_host,
|
2017-01-23 08:48:16 +00:00
|
|
|
const std::string& notification_id,
|
2015-03-09 02:37:13 +00:00
|
|
|
const GURL& origin,
|
2021-04-27 21:27:34 +00:00
|
|
|
const GURL& document_url,
|
2019-01-12 01:00:43 +00:00
|
|
|
const blink::PlatformNotificationData& notification_data,
|
|
|
|
const blink::NotificationResources& notification_resources) {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* presenter = browser_client_->GetNotificationPresenter();
|
2016-10-25 13:32:06 +00:00
|
|
|
if (!presenter)
|
|
|
|
return;
|
2020-07-03 06:04:32 +00:00
|
|
|
|
|
|
|
// If a new notification is created with the same tag as an
|
|
|
|
// existing one, replace the old notification with the new one.
|
|
|
|
// The notification_id is generated from the tag, so the only way a
|
|
|
|
// notification will be closed as a result of this call is if one with
|
|
|
|
// the same tag is already extant.
|
|
|
|
//
|
|
|
|
// See: https://notifications.spec.whatwg.org/#showing-a-notification
|
|
|
|
presenter->CloseNotificationWithId(notification_id);
|
|
|
|
|
2020-10-26 18:56:31 +00:00
|
|
|
auto* delegate = new NotificationDelegateImpl(notification_id);
|
2020-07-03 06:04:32 +00:00
|
|
|
|
2018-04-08 19:24:08 +00:00
|
|
|
auto notification = presenter->CreateNotification(delegate, notification_id);
|
2016-10-25 13:32:06 +00:00
|
|
|
if (notification) {
|
|
|
|
browser_client_->WebNotificationAllowed(
|
2021-04-29 18:23:28 +00:00
|
|
|
render_frame_host,
|
2019-05-03 19:08:41 +00:00
|
|
|
base::BindRepeating(&OnWebNotificationAllowed, notification,
|
|
|
|
notification_resources.notification_icon,
|
|
|
|
notification_data));
|
2016-10-25 13:32:06 +00:00
|
|
|
}
|
2015-03-09 02:37:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 14:20:47 +00:00
|
|
|
void PlatformNotificationService::DisplayPersistentNotification(
|
2017-01-23 08:48:16 +00:00
|
|
|
const std::string& notification_id,
|
|
|
|
const GURL& service_worker_scope,
|
2015-03-09 02:37:13 +00:00
|
|
|
const GURL& origin,
|
2019-01-12 01:00:43 +00:00
|
|
|
const blink::PlatformNotificationData& notification_data,
|
|
|
|
const blink::NotificationResources& notification_resources) {}
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2015-12-24 14:20:47 +00:00
|
|
|
void PlatformNotificationService::ClosePersistentNotification(
|
2018-04-18 01:56:12 +00:00
|
|
|
const std::string& notification_id) {}
|
2015-07-23 06:53:42 +00:00
|
|
|
|
2018-04-08 19:24:08 +00:00
|
|
|
void PlatformNotificationService::CloseNotification(
|
|
|
|
const std::string& notification_id) {
|
2018-04-25 09:32:00 +00:00
|
|
|
auto* presenter = browser_client_->GetNotificationPresenter();
|
2018-04-08 19:24:08 +00:00
|
|
|
if (!presenter)
|
|
|
|
return;
|
|
|
|
presenter->CloseNotificationWithId(notification_id);
|
|
|
|
}
|
|
|
|
|
2017-06-16 21:48:59 +00:00
|
|
|
void PlatformNotificationService::GetDisplayedNotifications(
|
2019-02-21 07:50:18 +00:00
|
|
|
DisplayedNotificationsCallback callback) {}
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2019-04-20 17:20:37 +00:00
|
|
|
int64_t PlatformNotificationService::ReadNextPersistentNotificationId() {
|
2018-10-02 20:52:21 +00:00
|
|
|
// Electron doesn't support persistent notifications.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-25 17:19:44 +00:00
|
|
|
void PlatformNotificationService::RecordNotificationUkmEvent(
|
|
|
|
const content::NotificationDatabaseData& data) {}
|
|
|
|
|
2019-04-20 17:20:37 +00:00
|
|
|
void PlatformNotificationService::ScheduleTrigger(base::Time timestamp) {}
|
2019-03-22 03:25:48 +00:00
|
|
|
|
2019-04-20 17:20:37 +00:00
|
|
|
base::Time PlatformNotificationService::ReadNextTriggerTimestamp() {
|
2019-03-22 03:25:48 +00:00
|
|
|
return base::Time::Max();
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|