platform_notification_service.cc 5.4 KB

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