Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 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 CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_H_ |
| 6 | #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_H_ |
| 7 | |
miguelg | abf610a | 2017-06-02 17:31:43 | [diff] [blame] | 8 | #include <map> |
miguelg | bfe76835 | 2017-03-20 15:34:31 | [diff] [blame] | 9 | #include <memory> |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
miguelg | 87e986d | 2016-07-08 18:04:44 | [diff] [blame] | 12 | |
Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 13 | #include "base/functional/callback_forward.h" |
nancy | 305b7cc | 2020-04-24 02:06:36 | [diff] [blame] | 14 | #include "base/observer_list_types.h" |
miguelg | 87e986d | 2016-07-08 18:04:44 | [diff] [blame] | 15 | #include "chrome/browser/notifications/notification_common.h" |
Peter Beverloo | 7426194d | 2017-12-11 17:39:38 | [diff] [blame] | 16 | #include "chrome/browser/notifications/notification_handler.h" |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 17 | #include "components/keyed_service/core/keyed_service.h" |
| 18 | |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 19 | class Profile; |
| 20 | |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 21 | namespace message_center { |
| 22 | class Notification; |
| 23 | } |
| 24 | |
Peter Beverloo | 7426194d | 2017-12-11 17:39:38 | [diff] [blame] | 25 | // Profile-bound service that enables user-visible notifications to be displayed |
| 26 | // and managed. Notifications may either be presented using a notification |
| 27 | // center provided by the platform, or by Chrome's Message Center. |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 28 | class NotificationDisplayService : public KeyedService { |
| 29 | public: |
nancy | 305b7cc | 2020-04-24 02:06:36 | [diff] [blame] | 30 | class Observer : public base::CheckedObserver { |
| 31 | public: |
nancy | 450ee81 | 2020-04-28 18:19:34 | [diff] [blame] | 32 | // Invoked when the |notification| is displayed. The |metadata| is provided |
| 33 | // for persistent web page notifications only, which require |
| 34 | // |service_worker_scope|. |
| 35 | virtual void OnNotificationDisplayed( |
| 36 | const message_center::Notification& notification, |
| 37 | const NotificationCommon::Metadata* const metadata) = 0; |
nancy | 305b7cc | 2020-04-24 02:06:36 | [diff] [blame] | 38 | |
| 39 | // Invoked when the notification having |notification_id| is closed. |
nancy | 450ee81 | 2020-04-28 18:19:34 | [diff] [blame] | 40 | virtual void OnNotificationClosed(const std::string& notification_id) = 0; |
nancy | 305b7cc | 2020-04-24 02:06:36 | [diff] [blame] | 41 | |
| 42 | // Invoked when the NotificationDisplayService object (the thing that this |
| 43 | // observer observes) will be destroyed. In response, the observer, |this|, |
| 44 | // should call "RemoveObserver(this)", whether directly or indirectly (e.g. |
| 45 | // via ScopedObserver::Remove). |
nancy | 450ee81 | 2020-04-28 18:19:34 | [diff] [blame] | 46 | virtual void OnNotificationDisplayServiceDestroyed( |
| 47 | NotificationDisplayService* service) = 0; |
nancy | 305b7cc | 2020-04-24 02:06:36 | [diff] [blame] | 48 | |
| 49 | protected: |
| 50 | ~Observer() override; |
| 51 | }; |
| 52 | |
Sundoo Kim | dd91d95 | 2020-08-26 17:11:20 | [diff] [blame] | 53 | NotificationDisplayService(const NotificationDisplayService&) = delete; |
| 54 | NotificationDisplayService& operator=(const NotificationDisplayService&) = |
| 55 | delete; |
Peter Beverloo | 7426194d | 2017-12-11 17:39:38 | [diff] [blame] | 56 | ~NotificationDisplayService() override; |
| 57 | |
| 58 | // Callback to be used with the GetDisplayed() method. Includes the set of |
| 59 | // notification ids that is being displayed to the user. The |
| 60 | // |supports_synchronization| callback indicates whether the platform has the |
| 61 | // ability to query which notifications are still being displayed. |
| 62 | // |
| 63 | // TODO(peter): Rename |supports_synchronization| to |supported|. |
miguelg | bfe76835 | 2017-03-20 15:34:31 | [diff] [blame] | 64 | using DisplayedNotificationsCallback = |
Richard Knoll | c638359 | 2019-01-28 18:06:58 | [diff] [blame] | 65 | base::OnceCallback<void(std::set<std::string>, |
| 66 | bool /* supports_synchronization */)>; |
Evan Stade | 6e15415 | 2017-11-10 01:58:25 | [diff] [blame] | 67 | |
Peter Beverloo | 7426194d | 2017-12-11 17:39:38 | [diff] [blame] | 68 | // Displays the |notification| of type |notification_type|. The |metadata| |
| 69 | // may be provided for certain notification types that require additional |
| 70 | // information for the notification to be displayed. |
Evan Stade | 4ebefdf4 | 2017-09-21 23:50:18 | [diff] [blame] | 71 | virtual void Display( |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 72 | NotificationHandler::Type notification_type, |
Evan Stade | 4755cf2 | 2017-10-17 18:35:43 | [diff] [blame] | 73 | const message_center::Notification& notification, |
Lei Zhang | 03c6b78 | 2019-03-21 05:22:24 | [diff] [blame] | 74 | std::unique_ptr<NotificationCommon::Metadata> metadata) = 0; |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 75 | |
Peter Beverloo | 7426194d | 2017-12-11 17:39:38 | [diff] [blame] | 76 | // Closes the notification having |notification_id| of |notification_type|. |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 77 | virtual void Close(NotificationHandler::Type notification_type, |
miguelg | 87e986d | 2016-07-08 18:04:44 | [diff] [blame] | 78 | const std::string& notification_id) = 0; |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 79 | |
Peter Beverloo | 7426194d | 2017-12-11 17:39:38 | [diff] [blame] | 80 | // Gets the IDs of currently displaying notifications and invokes |callback| |
| 81 | // once available. Not all backends support retrieving this information. |
Alison Gale | 3f4203f7 | 2024-04-26 19:27:42 | [diff] [blame] | 82 | // TODO(crbug.com/40283098): Consider refactoring this API and its |
Marijn Kruisselbrink | c5e4c712 | 2023-09-26 22:13:49 | [diff] [blame] | 83 | // usage to something that can get implemented by more backends. |
Finnur Thorarinsson | 0bfb7e7 | 2018-03-20 12:26:06 | [diff] [blame] | 84 | virtual void GetDisplayed(DisplayedNotificationsCallback callback) = 0; |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 85 | |
Marijn Kruisselbrink | c5e4c712 | 2023-09-26 22:13:49 | [diff] [blame] | 86 | // Gets the IDs of currently displaying notifications associated with `origin` |
| 87 | // and invokes `callback` once available. Not all backends support retrieving |
| 88 | // this information. |
| 89 | virtual void GetDisplayedForOrigin( |
| 90 | const GURL& origin, |
| 91 | DisplayedNotificationsCallback callback) = 0; |
| 92 | |
nancy | 305b7cc | 2020-04-24 02:06:36 | [diff] [blame] | 93 | // Adds and removes an observer. |
| 94 | virtual void AddObserver(Observer* observer) = 0; |
| 95 | virtual void RemoveObserver(Observer* observer) = 0; |
| 96 | |
Peter Beverloo | 7426194d | 2017-12-11 17:39:38 | [diff] [blame] | 97 | protected: |
| 98 | NotificationDisplayService() = default; |
miguelg | 23cd2dd7 | 2016-04-21 15:24:03 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | #endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_H_ |