1 // Copyright 2019 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 file. 4 5 #ifndef CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_SERVICE_PROXY_H_ 6 #define CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_SERVICE_PROXY_H_ 7 8 #include <memory> 9 #include <string> 10 11 #include "base/callback_forward.h" 12 #include "base/macros.h" 13 #include "base/memory/scoped_refptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "content/common/content_export.h" 16 17 class GURL; 18 19 namespace blink { 20 enum class ServiceWorkerStatusCode; 21 } // namespace blink 22 23 namespace content { 24 25 class BrowserContext; 26 struct NotificationDatabaseData; 27 class PlatformNotificationService; 28 class ServiceWorkerContextWrapper; 29 class ServiceWorkerRegistration; 30 31 class CONTENT_EXPORT PlatformNotificationServiceProxy { 32 public: 33 using DisplayResultCallback = 34 base::OnceCallback<void(bool /* success */, 35 const std::string& /* notification_id */)>; 36 37 PlatformNotificationServiceProxy( 38 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, 39 BrowserContext* browser_context); 40 41 ~PlatformNotificationServiceProxy(); 42 43 // To be called when the |browser_context_| has been shutdown. This 44 // invalidates all weak pointers. Must be called on the UI thread. 45 void Shutdown(); 46 47 // Gets a weak pointer to be used on the UI thread. 48 base::WeakPtr<PlatformNotificationServiceProxy> AsWeakPtr(); 49 50 // Displays a notification with |data| and calls |callback| with the result. 51 // This will verify against the given |service_worker_context_| if available. 52 void DisplayNotification(const NotificationDatabaseData& data, 53 DisplayResultCallback callback); 54 55 // Closes the notification with |notification_id|. 56 void CloseNotification(const std::string& notification_id); 57 58 // Schedules a notification trigger for |timestamp|. 59 void ScheduleTrigger(base::Time timestamp); 60 61 // Schedules a notification with |data|. 62 void ScheduleNotification(const NotificationDatabaseData& data); 63 64 // Gets the next notification trigger or base::Time::Max if none set. Must be 65 // called on the UI thread. 66 base::Time GetNextTrigger(); 67 68 // Records a given notification to UKM. Must be called on the UI thread. 69 void RecordNotificationUkmEvent(const NotificationDatabaseData& data); 70 71 // Returns if we should log a notification close event by calling LogClose. 72 // Must be called on the UI thread. 73 bool ShouldLogClose(const GURL& origin); 74 75 // Logs the event of closing a notification. 76 void LogClose(const NotificationDatabaseData& data); 77 78 private: 79 // Actually calls |notification_service_| to display the notification after 80 // verifying the |service_worker_scope|. Must be called on the UI thread. 81 void DoDisplayNotification(const NotificationDatabaseData& data, 82 const GURL& service_worker_scope, 83 DisplayResultCallback callback); 84 85 // Actually closes the notification with |notification_id|. Must be called on 86 // the UI thread. 87 void DoCloseNotification(const std::string& notification_id); 88 89 // Actually calls |notification_service_| to schedule a trigger. Must be 90 // called on the UI thread. 91 void DoScheduleTrigger(base::Time timestamp); 92 93 // Actually calls |notification_service_| to schedule a notification. Must be 94 // called on the UI thread. 95 void DoScheduleNotification(const NotificationDatabaseData& data); 96 97 // Actually logs the event of closing a notification. Must be called on the UI 98 // thread. 99 void DoLogClose(const NotificationDatabaseData& data); 100 101 // Verifies that the service worker exists and is valid for the given 102 // notification origin. 103 void VerifyServiceWorkerScope( 104 const NotificationDatabaseData& data, 105 DisplayResultCallback callback, 106 blink::ServiceWorkerStatusCode status, 107 scoped_refptr<ServiceWorkerRegistration> registration); 108 109 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; 110 BrowserContext* browser_context_; 111 PlatformNotificationService* notification_service_; 112 base::WeakPtrFactory<PlatformNotificationServiceProxy> weak_ptr_factory_ui_{ 113 this}; 114 base::WeakPtrFactory<PlatformNotificationServiceProxy> weak_ptr_factory_io_{ 115 this}; 116 117 DISALLOW_COPY_AND_ASSIGN(PlatformNotificationServiceProxy); 118 }; 119 120 } // namespace content 121 122 #endif // CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_SERVICE_PROXY_H_ 123