2015-03-08 19:37:13 -07: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 13:46:59 -07:00
|
|
|
#include "shell/browser/notifications/platform_notification_service.h"
|
2015-03-08 19:37:13 -07:00
|
|
|
|
2017-05-29 20:02:33 +10:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2017-11-01 23:14:27 +01:00
|
|
|
#include "content/public/browser/notification_event_dispatcher.h"
|
2018-10-05 14:24:38 -07:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2020-02-04 12:19:40 -08:00
|
|
|
#include "shell/browser/electron_browser_client.h"
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/browser/notifications/notification.h"
|
|
|
|
#include "shell/browser/notifications/notification_delegate.h"
|
|
|
|
#include "shell/browser/notifications/notification_presenter.h"
|
2019-01-12 06:30:43 +05:30
|
|
|
#include "third_party/blink/public/common/notifications/notification_resources.h"
|
|
|
|
#include "third_party/blink/public/common/notifications/platform_notification_data.h"
|
2016-01-23 19:02:30 +05:30
|
|
|
#include "third_party/skia/include/core/SkBitmap.h"
|
2015-03-08 19:37:13 -07:00
|
|
|
|
2019-06-19 14:23:04 -07:00
|
|
|
namespace electron {
|
2015-03-08 19:37:13 -07:00
|
|
|
|
2015-12-25 10:16:07 +08:00
|
|
|
namespace {
|
|
|
|
|
2016-10-25 19:02:06 +05:30
|
|
|
void OnWebNotificationAllowed(base::WeakPtr<Notification> notification,
|
|
|
|
const SkBitmap& icon,
|
2019-01-12 06:30:43 +05:30
|
|
|
const blink::PlatformNotificationData& data,
|
2016-10-25 19:02:06 +05:30
|
|
|
bool audio_muted,
|
|
|
|
bool allowed) {
|
2016-10-28 10:54:55 -07:00
|
|
|
if (!notification)
|
2016-01-23 19:02:30 +05:30
|
|
|
return;
|
2017-06-24 21:03:27 +10:00
|
|
|
if (allowed) {
|
2019-06-19 14:23:04 -07:00
|
|
|
electron::NotificationOptions options;
|
2017-06-24 21:03:27 +10: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 10:54:55 -07:00
|
|
|
notification->Destroy();
|
2017-06-24 21:03:27 +10:00
|
|
|
}
|
2016-01-23 19:02:30 +05:30
|
|
|
}
|
|
|
|
|
2019-06-19 14:23:04 -07:00
|
|
|
class NotificationDelegateImpl final : public electron::NotificationDelegate {
|
2017-11-01 23:14:27 +01: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 13:05:57 -07:00
|
|
|
->DispatchNonPersistentClickEvent(notification_id_, base::DoNothing());
|
2017-11-01 23:14:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationClosed() override {
|
|
|
|
content::NotificationEventDispatcher::GetInstance()
|
2018-09-14 17:21:59 -07:00
|
|
|
->DispatchNonPersistentCloseEvent(notification_id_, base::DoNothing());
|
2017-11-01 23:14:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationDisplayed() override {
|
|
|
|
content::NotificationEventDispatcher::GetInstance()
|
|
|
|
->DispatchNonPersistentShowEvent(notification_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string notification_id_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(NotificationDelegateImpl);
|
|
|
|
};
|
|
|
|
|
2015-12-25 10:16:07 +08:00
|
|
|
} // namespace
|
|
|
|
|
2015-12-24 22:20:47 +08:00
|
|
|
PlatformNotificationService::PlatformNotificationService(
|
2020-02-04 12:19:40 -08:00
|
|
|
ElectronBrowserClient* browser_client)
|
2018-10-05 14:24:38 -07:00
|
|
|
: browser_client_(browser_client) {}
|
2015-03-08 19:37:13 -07:00
|
|
|
|
2019-09-16 18:12:00 -04:00
|
|
|
PlatformNotificationService::~PlatformNotificationService() = default;
|
2015-03-08 19:37:13 -07:00
|
|
|
|
2015-12-24 22:20:47 +08:00
|
|
|
void PlatformNotificationService::DisplayNotification(
|
2021-04-29 11:23:28 -07:00
|
|
|
content::RenderFrameHost* render_frame_host,
|
2017-01-23 17:48:16 +09:00
|
|
|
const std::string& notification_id,
|
2015-03-08 19:37:13 -07:00
|
|
|
const GURL& origin,
|
2021-04-27 14:27:34 -07:00
|
|
|
const GURL& document_url,
|
2019-01-12 06:30:43 +05:30
|
|
|
const blink::PlatformNotificationData& notification_data,
|
|
|
|
const blink::NotificationResources& notification_resources) {
|
2018-04-17 15:41:47 -07:00
|
|
|
auto* presenter = browser_client_->GetNotificationPresenter();
|
2016-10-25 19:02:06 +05:30
|
|
|
if (!presenter)
|
|
|
|
return;
|
2020-07-02 23:04:32 -07: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 11:56:31 -07:00
|
|
|
auto* delegate = new NotificationDelegateImpl(notification_id);
|
2020-07-02 23:04:32 -07:00
|
|
|
|
2018-04-09 00:54:08 +05:30
|
|
|
auto notification = presenter->CreateNotification(delegate, notification_id);
|
2016-10-25 19:02:06 +05:30
|
|
|
if (notification) {
|
|
|
|
browser_client_->WebNotificationAllowed(
|
2021-04-29 11:23:28 -07:00
|
|
|
render_frame_host,
|
2019-05-03 12:08:41 -07:00
|
|
|
base::BindRepeating(&OnWebNotificationAllowed, notification,
|
|
|
|
notification_resources.notification_icon,
|
|
|
|
notification_data));
|
2016-10-25 19:02:06 +05:30
|
|
|
}
|
2015-03-08 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
2015-12-24 22:20:47 +08:00
|
|
|
void PlatformNotificationService::DisplayPersistentNotification(
|
2017-01-23 17:48:16 +09:00
|
|
|
const std::string& notification_id,
|
|
|
|
const GURL& service_worker_scope,
|
2015-03-08 19:37:13 -07:00
|
|
|
const GURL& origin,
|
2019-01-12 06:30:43 +05:30
|
|
|
const blink::PlatformNotificationData& notification_data,
|
|
|
|
const blink::NotificationResources& notification_resources) {}
|
2015-03-08 19:37:13 -07:00
|
|
|
|
2015-12-24 22:20:47 +08:00
|
|
|
void PlatformNotificationService::ClosePersistentNotification(
|
2018-04-17 21:56:12 -04:00
|
|
|
const std::string& notification_id) {}
|
2015-07-23 14:53:42 +08:00
|
|
|
|
2018-04-09 00:54:08 +05:30
|
|
|
void PlatformNotificationService::CloseNotification(
|
|
|
|
const std::string& notification_id) {
|
2018-04-25 15:02:00 +05:30
|
|
|
auto* presenter = browser_client_->GetNotificationPresenter();
|
2018-04-09 00:54:08 +05:30
|
|
|
if (!presenter)
|
|
|
|
return;
|
|
|
|
presenter->CloseNotificationWithId(notification_id);
|
|
|
|
}
|
|
|
|
|
2017-06-17 00:48:59 +03:00
|
|
|
void PlatformNotificationService::GetDisplayedNotifications(
|
2019-02-21 13:20:18 +05:30
|
|
|
DisplayedNotificationsCallback callback) {}
|
2015-03-08 19:37:13 -07:00
|
|
|
|
2019-04-20 13:20:37 -04:00
|
|
|
int64_t PlatformNotificationService::ReadNextPersistentNotificationId() {
|
2018-10-02 13:52:21 -07:00
|
|
|
// Electron doesn't support persistent notifications.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-25 22:49:44 +05:30
|
|
|
void PlatformNotificationService::RecordNotificationUkmEvent(
|
|
|
|
const content::NotificationDatabaseData& data) {}
|
|
|
|
|
2019-04-20 13:20:37 -04:00
|
|
|
void PlatformNotificationService::ScheduleTrigger(base::Time timestamp) {}
|
2019-03-21 20:25:48 -07:00
|
|
|
|
2019-04-20 13:20:37 -04:00
|
|
|
base::Time PlatformNotificationService::ReadNextTriggerTimestamp() {
|
2019-03-21 20:25:48 -07:00
|
|
|
return base::Time::Max();
|
|
|
|
}
|
|
|
|
|
2019-06-19 14:23:04 -07:00
|
|
|
} // namespace electron
|