blob: 46cce4cb193fce83913e481c54d64655faef6078 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2016 The Chromium Authors
miguelg23cd2dd72016-04-21 15:24:032// 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
miguelgabf610a2017-06-02 17:31:438#include <map>
miguelgbfe768352017-03-20 15:34:319#include <memory>
miguelg23cd2dd72016-04-21 15:24:0310#include <set>
11#include <string>
miguelg87e986d2016-07-08 18:04:4412
Avi Drissman9269d4ed2023-01-07 01:38:0613#include "base/functional/callback_forward.h"
nancy305b7cc2020-04-24 02:06:3614#include "base/observer_list_types.h"
miguelg87e986d2016-07-08 18:04:4415#include "chrome/browser/notifications/notification_common.h"
Peter Beverloo7426194d2017-12-11 17:39:3816#include "chrome/browser/notifications/notification_handler.h"
miguelg23cd2dd72016-04-21 15:24:0317#include "components/keyed_service/core/keyed_service.h"
18
miguelg23cd2dd72016-04-21 15:24:0319class Profile;
20
Evan Stade4755cf22017-10-17 18:35:4321namespace message_center {
22class Notification;
23}
24
Peter Beverloo7426194d2017-12-11 17:39:3825// 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.
miguelg23cd2dd72016-04-21 15:24:0328class NotificationDisplayService : public KeyedService {
29 public:
nancy305b7cc2020-04-24 02:06:3630 class Observer : public base::CheckedObserver {
31 public:
nancy450ee812020-04-28 18:19:3432 // 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;
nancy305b7cc2020-04-24 02:06:3638
39 // Invoked when the notification having |notification_id| is closed.
nancy450ee812020-04-28 18:19:3440 virtual void OnNotificationClosed(const std::string& notification_id) = 0;
nancy305b7cc2020-04-24 02:06:3641
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).
nancy450ee812020-04-28 18:19:3446 virtual void OnNotificationDisplayServiceDestroyed(
47 NotificationDisplayService* service) = 0;
nancy305b7cc2020-04-24 02:06:3648
49 protected:
50 ~Observer() override;
51 };
52
Sundoo Kimdd91d952020-08-26 17:11:2053 NotificationDisplayService(const NotificationDisplayService&) = delete;
54 NotificationDisplayService& operator=(const NotificationDisplayService&) =
55 delete;
Peter Beverloo7426194d2017-12-11 17:39:3856 ~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|.
miguelgbfe768352017-03-20 15:34:3164 using DisplayedNotificationsCallback =
Richard Knollc6383592019-01-28 18:06:5865 base::OnceCallback<void(std::set<std::string>,
66 bool /* supports_synchronization */)>;
Evan Stade6e154152017-11-10 01:58:2567
Peter Beverloo7426194d2017-12-11 17:39:3868 // 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 Stade4ebefdf42017-09-21 23:50:1871 virtual void Display(
Peter Beverloo6dba2e102017-11-23 17:46:3372 NotificationHandler::Type notification_type,
Evan Stade4755cf22017-10-17 18:35:4373 const message_center::Notification& notification,
Lei Zhang03c6b782019-03-21 05:22:2474 std::unique_ptr<NotificationCommon::Metadata> metadata) = 0;
miguelg23cd2dd72016-04-21 15:24:0375
Peter Beverloo7426194d2017-12-11 17:39:3876 // Closes the notification having |notification_id| of |notification_type|.
Peter Beverloo6dba2e102017-11-23 17:46:3377 virtual void Close(NotificationHandler::Type notification_type,
miguelg87e986d2016-07-08 18:04:4478 const std::string& notification_id) = 0;
miguelg23cd2dd72016-04-21 15:24:0379
Peter Beverloo7426194d2017-12-11 17:39:3880 // Gets the IDs of currently displaying notifications and invokes |callback|
81 // once available. Not all backends support retrieving this information.
Alison Gale3f4203f72024-04-26 19:27:4282 // TODO(crbug.com/40283098): Consider refactoring this API and its
Marijn Kruisselbrinkc5e4c7122023-09-26 22:13:4983 // usage to something that can get implemented by more backends.
Finnur Thorarinsson0bfb7e72018-03-20 12:26:0684 virtual void GetDisplayed(DisplayedNotificationsCallback callback) = 0;
miguelg23cd2dd72016-04-21 15:24:0385
Marijn Kruisselbrinkc5e4c7122023-09-26 22:13:4986 // 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
nancy305b7cc2020-04-24 02:06:3693 // Adds and removes an observer.
94 virtual void AddObserver(Observer* observer) = 0;
95 virtual void RemoveObserver(Observer* observer) = 0;
96
Peter Beverloo7426194d2017-12-11 17:39:3897 protected:
98 NotificationDisplayService() = default;
miguelg23cd2dd72016-04-21 15:24:0399};
100
101#endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_H_