platform_notification_service.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE-CHROMIUM file.
  4. #include "shell/browser/notifications/platform_notification_service.h"
  5. #include "base/strings/utf_string_conversions.h"
  6. #include "content/public/browser/notification_event_dispatcher.h"
  7. #include "content/public/browser/render_process_host.h"
  8. #include "shell/browser/electron_browser_client.h"
  9. #include "shell/browser/notifications/notification.h"
  10. #include "shell/browser/notifications/notification_delegate.h"
  11. #include "shell/browser/notifications/notification_presenter.h"
  12. #include "third_party/blink/public/common/notifications/notification_resources.h"
  13. #include "third_party/blink/public/common/notifications/platform_notification_data.h"
  14. #include "third_party/skia/include/core/SkBitmap.h"
  15. namespace electron {
  16. namespace {
  17. void OnWebNotificationAllowed(base::WeakPtr<Notification> notification,
  18. const SkBitmap& icon,
  19. const blink::PlatformNotificationData& data,
  20. bool audio_muted,
  21. bool allowed) {
  22. if (!notification)
  23. return;
  24. if (allowed) {
  25. electron::NotificationOptions options;
  26. options.title = data.title;
  27. options.msg = data.body;
  28. options.tag = data.tag;
  29. options.icon_url = data.icon;
  30. options.icon = icon;
  31. options.silent = audio_muted ? true : data.silent;
  32. options.has_reply = false;
  33. if (data.require_interaction)
  34. options.timeout_type = u"never";
  35. notification->Show(options);
  36. } else {
  37. notification->Destroy();
  38. }
  39. }
  40. class NotificationDelegateImpl final : public electron::NotificationDelegate {
  41. public:
  42. explicit NotificationDelegateImpl(const std::string& notification_id)
  43. : notification_id_(notification_id) {}
  44. // disable copy
  45. NotificationDelegateImpl(const NotificationDelegateImpl&) = delete;
  46. NotificationDelegateImpl& operator=(const NotificationDelegateImpl&) = delete;
  47. // electron::NotificationDelegate
  48. void NotificationDestroyed() override { delete this; }
  49. void NotificationClick() override {
  50. content::NotificationEventDispatcher::GetInstance()
  51. ->DispatchNonPersistentClickEvent(notification_id_, base::DoNothing());
  52. }
  53. void NotificationClosed() override {
  54. content::NotificationEventDispatcher::GetInstance()
  55. ->DispatchNonPersistentCloseEvent(notification_id_, base::DoNothing());
  56. }
  57. void NotificationDisplayed() override {
  58. content::NotificationEventDispatcher::GetInstance()
  59. ->DispatchNonPersistentShowEvent(notification_id_);
  60. }
  61. private:
  62. std::string notification_id_;
  63. };
  64. } // namespace
  65. PlatformNotificationService::PlatformNotificationService(
  66. ElectronBrowserClient* browser_client)
  67. : browser_client_(browser_client) {}
  68. PlatformNotificationService::~PlatformNotificationService() = default;
  69. void PlatformNotificationService::DisplayNotification(
  70. content::RenderFrameHost* render_frame_host,
  71. const std::string& notification_id,
  72. const GURL& origin,
  73. const GURL& document_url,
  74. const blink::PlatformNotificationData& notification_data,
  75. const blink::NotificationResources& notification_resources) {
  76. auto* presenter = browser_client_->GetNotificationPresenter();
  77. if (!presenter)
  78. return;
  79. // If a new notification is created with the same tag as an
  80. // existing one, replace the old notification with the new one.
  81. // The notification_id is generated from the tag, so the only way a
  82. // notification will be closed as a result of this call is if one with
  83. // the same tag is already extant.
  84. //
  85. // See: https://notifications.spec.whatwg.org/#showing-a-notification
  86. presenter->CloseNotificationWithId(notification_id);
  87. auto* delegate = new NotificationDelegateImpl(notification_id);
  88. auto notification = presenter->CreateNotification(delegate, notification_id);
  89. if (notification) {
  90. browser_client_->WebNotificationAllowed(
  91. render_frame_host,
  92. base::BindRepeating(&OnWebNotificationAllowed, notification,
  93. notification_resources.notification_icon,
  94. notification_data));
  95. }
  96. }
  97. void PlatformNotificationService::DisplayPersistentNotification(
  98. const std::string& notification_id,
  99. const GURL& service_worker_scope,
  100. const GURL& origin,
  101. const blink::PlatformNotificationData& notification_data,
  102. const blink::NotificationResources& notification_resources) {}
  103. void PlatformNotificationService::ClosePersistentNotification(
  104. const std::string& notification_id) {}
  105. void PlatformNotificationService::CloseNotification(
  106. const std::string& notification_id) {
  107. auto* presenter = browser_client_->GetNotificationPresenter();
  108. if (!presenter)
  109. return;
  110. presenter->CloseNotificationWithId(notification_id);
  111. }
  112. void PlatformNotificationService::GetDisplayedNotifications(
  113. DisplayedNotificationsCallback callback) {}
  114. void PlatformNotificationService::GetDisplayedNotificationsForOrigin(
  115. const GURL& origin,
  116. DisplayedNotificationsCallback callback) {}
  117. int64_t PlatformNotificationService::ReadNextPersistentNotificationId() {
  118. // Electron doesn't support persistent notifications.
  119. return 0;
  120. }
  121. void PlatformNotificationService::RecordNotificationUkmEvent(
  122. const content::NotificationDatabaseData& data) {}
  123. void PlatformNotificationService::ScheduleTrigger(base::Time timestamp) {}
  124. base::Time PlatformNotificationService::ReadNextTriggerTimestamp() {
  125. return base::Time::Max();
  126. }
  127. } // namespace electron