platform_notification_service.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "brightray/browser/platform_notification_service.h"
  5. #include "base/strings/utf_string_conversions.h"
  6. #include "brightray/browser/browser_client.h"
  7. #include "brightray/browser/notification.h"
  8. #include "brightray/browser/notification_delegate.h"
  9. #include "brightray/browser/notification_presenter.h"
  10. #include "content/public/browser/notification_event_dispatcher.h"
  11. #include "content/public/common/notification_resources.h"
  12. #include "content/public/common/platform_notification_data.h"
  13. #include "third_party/skia/include/core/SkBitmap.h"
  14. namespace brightray {
  15. namespace {
  16. void RemoveNotification(base::WeakPtr<Notification> notification) {
  17. if (notification)
  18. notification->Dismiss();
  19. }
  20. void OnWebNotificationAllowed(base::WeakPtr<Notification> notification,
  21. const SkBitmap& icon,
  22. const content::PlatformNotificationData& data,
  23. bool audio_muted,
  24. bool allowed) {
  25. if (!notification)
  26. return;
  27. if (allowed) {
  28. brightray::NotificationOptions options;
  29. options.title = data.title;
  30. options.msg = data.body;
  31. options.tag = data.tag;
  32. options.icon_url = data.icon;
  33. options.icon = icon;
  34. options.silent = audio_muted ? true : data.silent;
  35. options.has_reply = false;
  36. notification->Show(options);
  37. } else {
  38. notification->Destroy();
  39. }
  40. }
  41. class NotificationDelegateImpl final : public brightray::NotificationDelegate {
  42. public:
  43. explicit NotificationDelegateImpl(const std::string& notification_id)
  44. : notification_id_(notification_id) {}
  45. void NotificationDestroyed() override { delete this; }
  46. void NotificationClick() override {
  47. content::NotificationEventDispatcher::GetInstance()
  48. ->DispatchNonPersistentClickEvent(notification_id_);
  49. }
  50. void NotificationClosed() override {
  51. content::NotificationEventDispatcher::GetInstance()
  52. ->DispatchNonPersistentCloseEvent(notification_id_);
  53. }
  54. void NotificationDisplayed() override {
  55. content::NotificationEventDispatcher::GetInstance()
  56. ->DispatchNonPersistentShowEvent(notification_id_);
  57. }
  58. private:
  59. std::string notification_id_;
  60. DISALLOW_COPY_AND_ASSIGN(NotificationDelegateImpl);
  61. };
  62. } // namespace
  63. PlatformNotificationService::PlatformNotificationService(
  64. BrowserClient* browser_client)
  65. : browser_client_(browser_client), render_process_id_(-1) {}
  66. PlatformNotificationService::~PlatformNotificationService() {}
  67. blink::mojom::PermissionStatus
  68. PlatformNotificationService::CheckPermissionOnUIThread(
  69. content::BrowserContext* browser_context,
  70. const GURL& origin,
  71. int render_process_id) {
  72. render_process_id_ = render_process_id;
  73. return blink::mojom::PermissionStatus::GRANTED;
  74. }
  75. blink::mojom::PermissionStatus
  76. PlatformNotificationService::CheckPermissionOnIOThread(
  77. content::ResourceContext* resource_context,
  78. const GURL& origin,
  79. int render_process_id) {
  80. return blink::mojom::PermissionStatus::GRANTED;
  81. }
  82. void PlatformNotificationService::DisplayNotification(
  83. content::BrowserContext* browser_context,
  84. const std::string& notification_id,
  85. const GURL& origin,
  86. const content::PlatformNotificationData& notification_data,
  87. const content::NotificationResources& notification_resources,
  88. base::Closure* cancel_callback) {
  89. auto* presenter = browser_client_->GetNotificationPresenter();
  90. if (!presenter)
  91. return;
  92. NotificationDelegateImpl* delegate =
  93. new NotificationDelegateImpl(notification_id);
  94. auto notification = presenter->CreateNotification(delegate);
  95. if (notification) {
  96. *cancel_callback = base::Bind(&RemoveNotification, notification);
  97. browser_client_->WebNotificationAllowed(
  98. render_process_id_, base::Bind(&OnWebNotificationAllowed, notification,
  99. notification_resources.notification_icon,
  100. notification_data));
  101. }
  102. }
  103. void PlatformNotificationService::DisplayPersistentNotification(
  104. content::BrowserContext* browser_context,
  105. const std::string& notification_id,
  106. const GURL& service_worker_scope,
  107. const GURL& origin,
  108. const content::PlatformNotificationData& notification_data,
  109. const content::NotificationResources& notification_resources) {}
  110. void PlatformNotificationService::ClosePersistentNotification(
  111. content::BrowserContext* browser_context,
  112. const std::string& notification_id) {}
  113. void PlatformNotificationService::GetDisplayedNotifications(
  114. content::BrowserContext* browser_context,
  115. const DisplayedNotificationsCallback& callback) {}
  116. } // namespace brightray