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.
|
|
|
|
|
2018-10-17 18:01:11 +00:00
|
|
|
#include "atom/browser/notifications/platform_notification_service.h"
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2018-10-17 18:01:11 +00:00
|
|
|
#include "atom/browser/atom_browser_client.h"
|
|
|
|
#include "atom/browser/notifications/notification.h"
|
|
|
|
#include "atom/browser/notifications/notification_delegate.h"
|
|
|
|
#include "atom/browser/notifications/notification_presenter.h"
|
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"
|
2016-04-27 02:52:26 +00:00
|
|
|
#include "content/public/common/notification_resources.h"
|
2017-05-18 22:29:22 +00:00
|
|
|
#include "content/public/common/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
|
|
|
|
2018-10-17 18:01:11 +00:00
|
|
|
namespace atom {
|
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,
|
|
|
|
const content::PlatformNotificationData& data,
|
|
|
|
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) {
|
2018-10-17 18:01:11 +00:00
|
|
|
atom::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
|
|
|
}
|
|
|
|
|
2018-10-17 18:01:11 +00:00
|
|
|
class NotificationDelegateImpl final : public atom::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(
|
2018-10-17 18:01:11 +00:00
|
|
|
AtomBrowserClient* browser_client)
|
2018-10-05 21:24:38 +00:00
|
|
|
: browser_client_(browser_client) {}
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2015-12-24 14:20:47 +00:00
|
|
|
PlatformNotificationService::~PlatformNotificationService() {}
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2015-12-24 14:20:47 +00:00
|
|
|
void PlatformNotificationService::DisplayNotification(
|
2018-10-05 21:24:38 +00:00
|
|
|
content::RenderProcessHost* render_process_host,
|
2015-03-09 02:37:13 +00:00
|
|
|
content::BrowserContext* browser_context,
|
2017-01-23 08:48:16 +00:00
|
|
|
const std::string& notification_id,
|
2015-03-09 02:37:13 +00:00
|
|
|
const GURL& origin,
|
2016-04-27 02:52:26 +00:00
|
|
|
const content::PlatformNotificationData& notification_data,
|
2018-04-08 19:24:08 +00:00
|
|
|
const content::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;
|
2017-11-01 22:14:27 +00:00
|
|
|
NotificationDelegateImpl* delegate =
|
|
|
|
new NotificationDelegateImpl(notification_id);
|
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(
|
2018-10-05 21:24:38 +00:00
|
|
|
render_process_host->GetID(),
|
|
|
|
base::Bind(&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(
|
2015-03-09 02:37:13 +00:00
|
|
|
content::BrowserContext* browser_context,
|
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,
|
2016-04-27 02:52:26 +00:00
|
|
|
const content::PlatformNotificationData& notification_data,
|
2018-04-18 01:56:12 +00:00
|
|
|
const content::NotificationResources& notification_resources) {}
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2015-12-24 14:20:47 +00:00
|
|
|
void PlatformNotificationService::ClosePersistentNotification(
|
2015-03-09 02:37:13 +00:00
|
|
|
content::BrowserContext* browser_context,
|
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(
|
|
|
|
content::BrowserContext* browser_context,
|
|
|
|
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(
|
2015-07-23 06:53:42 +00:00
|
|
|
content::BrowserContext* browser_context,
|
2018-04-18 01:56:12 +00:00
|
|
|
const DisplayedNotificationsCallback& callback) {}
|
2015-03-09 02:37:13 +00:00
|
|
|
|
2018-10-02 20:52:21 +00:00
|
|
|
int64_t PlatformNotificationService::ReadNextPersistentNotificationId(
|
|
|
|
content::BrowserContext* browser_context) {
|
|
|
|
// Electron doesn't support persistent notifications.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-17 18:01:11 +00:00
|
|
|
} // namespace atom
|