| 
									
										
										
										
											2015-03-08 19:37:13 -07: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 20:01:11 +02:00
										 |  |  | #include "atom/browser/notifications/platform_notification_service.h"
 | 
					
						
							| 
									
										
										
										
											2015-03-08 19:37:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 20:01:11 +02: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 20:02:33 +10:00
										 |  |  | #include "base/strings/utf_string_conversions.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-01 23:14:27 +01:00
										 |  |  | #include "content/public/browser/notification_event_dispatcher.h"
 | 
					
						
							| 
									
										
										
										
											2018-10-05 14:24:38 -07:00
										 |  |  | #include "content/public/browser/render_process_host.h"
 | 
					
						
							| 
									
										
										
										
											2019-01-12 06:30:43 +05:30
										 |  |  | #include "third_party/blink/public/common/notifications/notification_resources.h"
 | 
					
						
							|  |  |  | #include "third_party/blink/public/common/notifications/platform_notification_data.h"
 | 
					
						
							| 
									
										
										
										
											2016-01-23 19:02:30 +05:30
										 |  |  | #include "third_party/skia/include/core/SkBitmap.h"
 | 
					
						
							| 
									
										
										
										
											2015-03-08 19:37:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 20:01:11 +02:00
										 |  |  | namespace atom { | 
					
						
							| 
									
										
										
										
											2015-03-08 19:37:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-25 10:16:07 +08:00
										 |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 19:02:06 +05:30
										 |  |  | void OnWebNotificationAllowed(base::WeakPtr<Notification> notification, | 
					
						
							|  |  |  |                               const SkBitmap& icon, | 
					
						
							| 
									
										
										
										
											2019-01-12 06:30:43 +05:30
										 |  |  |                               const blink::PlatformNotificationData& data, | 
					
						
							| 
									
										
										
										
											2016-10-25 19:02:06 +05:30
										 |  |  |                               bool audio_muted, | 
					
						
							|  |  |  |                               bool allowed) { | 
					
						
							| 
									
										
										
										
											2016-10-28 10:54:55 -07:00
										 |  |  |   if (!notification) | 
					
						
							| 
									
										
										
										
											2016-01-23 19:02:30 +05:30
										 |  |  |     return; | 
					
						
							| 
									
										
										
										
											2017-06-24 21:03:27 +10:00
										 |  |  |   if (allowed) { | 
					
						
							| 
									
										
										
										
											2018-10-17 20:01:11 +02:00
										 |  |  |     atom::NotificationOptions options; | 
					
						
							| 
									
										
										
										
											2017-06-24 21:03:27 +10: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 10:54:55 -07:00
										 |  |  |     notification->Destroy(); | 
					
						
							| 
									
										
										
										
											2017-06-24 21:03:27 +10:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-23 19:02:30 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 20:01:11 +02:00
										 |  |  | class NotificationDelegateImpl final : public atom::NotificationDelegate { | 
					
						
							| 
									
										
										
										
											2017-11-01 23:14:27 +01: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 13:05:57 -07:00
										 |  |  |         ->DispatchNonPersistentClickEvent(notification_id_, base::DoNothing()); | 
					
						
							| 
									
										
										
										
											2017-11-01 23:14:27 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void NotificationClosed() override { | 
					
						
							|  |  |  |     content::NotificationEventDispatcher::GetInstance() | 
					
						
							| 
									
										
										
										
											2018-09-14 17:21:59 -07:00
										 |  |  |         ->DispatchNonPersistentCloseEvent(notification_id_, base::DoNothing()); | 
					
						
							| 
									
										
										
										
											2017-11-01 23:14:27 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void NotificationDisplayed() override { | 
					
						
							|  |  |  |     content::NotificationEventDispatcher::GetInstance() | 
					
						
							|  |  |  |         ->DispatchNonPersistentShowEvent(notification_id_); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  private: | 
					
						
							|  |  |  |   std::string notification_id_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   DISALLOW_COPY_AND_ASSIGN(NotificationDelegateImpl); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-25 10:16:07 +08:00
										 |  |  | }  // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 22:20:47 +08:00
										 |  |  | PlatformNotificationService::PlatformNotificationService( | 
					
						
							| 
									
										
										
										
											2018-10-17 20:01:11 +02:00
										 |  |  |     AtomBrowserClient* browser_client) | 
					
						
							| 
									
										
										
										
											2018-10-05 14:24:38 -07:00
										 |  |  |     : browser_client_(browser_client) {} | 
					
						
							| 
									
										
										
										
											2015-03-08 19:37:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 22:20:47 +08:00
										 |  |  | PlatformNotificationService::~PlatformNotificationService() {} | 
					
						
							| 
									
										
										
										
											2015-03-08 19:37:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 22:20:47 +08:00
										 |  |  | void PlatformNotificationService::DisplayNotification( | 
					
						
							| 
									
										
										
										
											2018-10-05 14:24:38 -07:00
										 |  |  |     content::RenderProcessHost* render_process_host, | 
					
						
							| 
									
										
										
										
											2017-01-23 17:48:16 +09:00
										 |  |  |     const std::string& notification_id, | 
					
						
							| 
									
										
										
										
											2015-03-08 19:37:13 -07:00
										 |  |  |     const GURL& origin, | 
					
						
							| 
									
										
										
										
											2019-01-12 06:30:43 +05:30
										 |  |  |     const blink::PlatformNotificationData& notification_data, | 
					
						
							|  |  |  |     const blink::NotificationResources& notification_resources) { | 
					
						
							| 
									
										
										
										
											2018-04-17 15:41:47 -07:00
										 |  |  |   auto* presenter = browser_client_->GetNotificationPresenter(); | 
					
						
							| 
									
										
										
										
											2016-10-25 19:02:06 +05:30
										 |  |  |   if (!presenter) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2017-11-01 23:14:27 +01:00
										 |  |  |   NotificationDelegateImpl* delegate = | 
					
						
							|  |  |  |       new NotificationDelegateImpl(notification_id); | 
					
						
							| 
									
										
										
										
											2018-04-09 00:54:08 +05:30
										 |  |  |   auto notification = presenter->CreateNotification(delegate, notification_id); | 
					
						
							| 
									
										
										
										
											2016-10-25 19:02:06 +05:30
										 |  |  |   if (notification) { | 
					
						
							|  |  |  |     browser_client_->WebNotificationAllowed( | 
					
						
							| 
									
										
										
										
											2018-10-05 14:24:38 -07:00
										 |  |  |         render_process_host->GetID(), | 
					
						
							| 
									
										
										
										
											2019-05-03 12:08:41 -07:00
										 |  |  |         base::BindRepeating(&OnWebNotificationAllowed, notification, | 
					
						
							|  |  |  |                             notification_resources.notification_icon, | 
					
						
							|  |  |  |                             notification_data)); | 
					
						
							| 
									
										
										
										
											2016-10-25 19:02:06 +05:30
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-03-08 19:37:13 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 22:20:47 +08:00
										 |  |  | void PlatformNotificationService::DisplayPersistentNotification( | 
					
						
							| 
									
										
										
										
											2017-01-23 17:48:16 +09:00
										 |  |  |     const std::string& notification_id, | 
					
						
							|  |  |  |     const GURL& service_worker_scope, | 
					
						
							| 
									
										
										
										
											2015-03-08 19:37:13 -07:00
										 |  |  |     const GURL& origin, | 
					
						
							| 
									
										
										
										
											2019-01-12 06:30:43 +05:30
										 |  |  |     const blink::PlatformNotificationData& notification_data, | 
					
						
							|  |  |  |     const blink::NotificationResources& notification_resources) {} | 
					
						
							| 
									
										
										
										
											2015-03-08 19:37:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 22:20:47 +08:00
										 |  |  | void PlatformNotificationService::ClosePersistentNotification( | 
					
						
							| 
									
										
										
										
											2018-04-17 21:56:12 -04:00
										 |  |  |     const std::string& notification_id) {} | 
					
						
							| 
									
										
										
										
											2015-07-23 14:53:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-09 00:54:08 +05:30
										 |  |  | void PlatformNotificationService::CloseNotification( | 
					
						
							|  |  |  |     const std::string& notification_id) { | 
					
						
							| 
									
										
										
										
											2018-04-25 15:02:00 +05:30
										 |  |  |   auto* presenter = browser_client_->GetNotificationPresenter(); | 
					
						
							| 
									
										
										
										
											2018-04-09 00:54:08 +05:30
										 |  |  |   if (!presenter) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   presenter->CloseNotificationWithId(notification_id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 00:48:59 +03:00
										 |  |  | void PlatformNotificationService::GetDisplayedNotifications( | 
					
						
							| 
									
										
										
										
											2019-02-21 13:20:18 +05:30
										 |  |  |     DisplayedNotificationsCallback callback) {} | 
					
						
							| 
									
										
										
										
											2015-03-08 19:37:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-20 13:20:37 -04:00
										 |  |  | int64_t PlatformNotificationService::ReadNextPersistentNotificationId() { | 
					
						
							| 
									
										
										
										
											2018-10-02 13:52:21 -07:00
										 |  |  |   // Electron doesn't support persistent notifications.
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-25 22:49:44 +05:30
										 |  |  | void PlatformNotificationService::RecordNotificationUkmEvent( | 
					
						
							|  |  |  |     const content::NotificationDatabaseData& data) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-20 13:20:37 -04:00
										 |  |  | void PlatformNotificationService::ScheduleTrigger(base::Time timestamp) {} | 
					
						
							| 
									
										
										
										
											2019-03-21 20:25:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-20 13:20:37 -04:00
										 |  |  | base::Time PlatformNotificationService::ReadNextTriggerTimestamp() { | 
					
						
							| 
									
										
										
										
											2019-03-21 20:25:48 -07:00
										 |  |  |   return base::Time::Max(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 20:01:11 +02:00
										 |  |  | }  // namespace atom
 |