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.
|
|
|
|
|
2017-05-18 22:58:12 +00:00
|
|
|
#include "brightray/browser/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-05-18 22:58:12 +00:00
|
|
|
#include "brightray/browser/browser_client.h"
|
|
|
|
#include "brightray/browser/notification.h"
|
2017-08-17 16:19:14 +00:00
|
|
|
#include "brightray/browser/notification_delegate.h"
|
2017-05-18 22:58:12 +00:00
|
|
|
#include "brightray/browser/notification_presenter.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
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
2015-12-25 02:16:07 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void RemoveNotification(base::WeakPtr<Notification> notification) {
|
|
|
|
if (notification)
|
|
|
|
notification->Dismiss();
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
brightray::NotificationOptions options;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-25 02:16:07 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-12-24 14:20:47 +00:00
|
|
|
PlatformNotificationService::PlatformNotificationService(
|
|
|
|
BrowserClient* browser_client)
|
2016-01-23 13:32:30 +00:00
|
|
|
: browser_client_(browser_client),
|
2016-01-27 15:18:42 +00:00
|
|
|
render_process_id_(-1) {
|
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
|
|
|
|
2017-03-23 22:47:30 +00:00
|
|
|
blink::mojom::PermissionStatus
|
|
|
|
PlatformNotificationService::CheckPermissionOnUIThread(
|
2015-04-21 10:54:57 +00:00
|
|
|
content::BrowserContext* browser_context,
|
|
|
|
const GURL& origin,
|
|
|
|
int render_process_id) {
|
2016-01-23 13:32:30 +00:00
|
|
|
render_process_id_ = render_process_id;
|
2016-07-04 06:06:05 +00:00
|
|
|
return blink::mojom::PermissionStatus::GRANTED;
|
2015-04-21 10:54:57 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 22:47:30 +00:00
|
|
|
blink::mojom::PermissionStatus
|
|
|
|
PlatformNotificationService::CheckPermissionOnIOThread(
|
2015-03-09 02:37:13 +00:00
|
|
|
content::ResourceContext* resource_context,
|
|
|
|
const GURL& origin,
|
|
|
|
int render_process_id) {
|
2016-07-04 06:06:05 +00:00
|
|
|
return blink::mojom::PermissionStatus::GRANTED;
|
2015-03-09 02:37:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 14:20:47 +00:00
|
|
|
void PlatformNotificationService::DisplayNotification(
|
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,
|
|
|
|
const content::NotificationResources& notification_resources,
|
2015-03-09 02:37:13 +00:00
|
|
|
base::Closure* cancel_callback) {
|
2016-10-25 13:32:06 +00:00
|
|
|
auto presenter = browser_client_->GetNotificationPresenter();
|
|
|
|
if (!presenter)
|
|
|
|
return;
|
2017-08-17 16:19:14 +00:00
|
|
|
brightray::NotificationDelegate* delegate =
|
|
|
|
new NotificationDelegate(notification_id);
|
|
|
|
auto notification = presenter->CreateNotification(delegate);
|
2016-10-25 13:32:06 +00:00
|
|
|
if (notification) {
|
|
|
|
*cancel_callback = base::Bind(&RemoveNotification, notification);
|
|
|
|
browser_client_->WebNotificationAllowed(
|
|
|
|
render_process_id_, base::Bind(&OnWebNotificationAllowed, notification,
|
|
|
|
notification_resources.notification_icon,
|
|
|
|
notification_data));
|
|
|
|
}
|
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,
|
|
|
|
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,
|
2017-01-23 08:48:16 +00:00
|
|
|
const std::string& notification_id) {
|
2015-07-23 06:53:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-16 21:48:59 +00:00
|
|
|
void PlatformNotificationService::GetDisplayedNotifications(
|
2015-07-23 06:53:42 +00:00
|
|
|
content::BrowserContext* browser_context,
|
2017-06-16 21:48:59 +00:00
|
|
|
const DisplayedNotificationsCallback& callback) {
|
2015-03-09 02:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace brightray
|