blob: f2642fea14fe6ae5d3949263a945d214436b9664 [file] [log] [blame]
miguelg23cd2dd72016-04-21 15:24:031// Copyright 2016 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 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
miguelgbfe768352017-03-20 15:34:3113#include "base/callback_forward.h"
miguelg23cd2dd72016-04-21 15:24:0314#include "base/macros.h"
nancy305b7cc2020-04-24 02:06:3615#include "base/observer_list_types.h"
miguelg87e986d2016-07-08 18:04:4416#include "chrome/browser/notifications/notification_common.h"
Peter Beverloo7426194d2017-12-11 17:39:3817#include "chrome/browser/notifications/notification_handler.h"
miguelg23cd2dd72016-04-21 15:24:0318#include "components/keyed_service/core/keyed_service.h"
19
miguelg23cd2dd72016-04-21 15:24:0320class Profile;
21
Evan Stade4755cf22017-10-17 18:35:4322namespace message_center {
23class Notification;
24}
25
Peter Beverloo7426194d2017-12-11 17:39:3826// Profile-bound service that enables user-visible notifications to be displayed
27// and managed. Notifications may either be presented using a notification
28// center provided by the platform, or by Chrome's Message Center.
miguelg23cd2dd72016-04-21 15:24:0329class NotificationDisplayService : public KeyedService {
30 public:
nancy305b7cc2020-04-24 02:06:3631 class Observer : public base::CheckedObserver {
32 public:
nancy450ee812020-04-28 18:19:3433 // Invoked when the |notification| is displayed. The |metadata| is provided
34 // for persistent web page notifications only, which require
35 // |service_worker_scope|.
36 virtual void OnNotificationDisplayed(
37 const message_center::Notification& notification,
38 const NotificationCommon::Metadata* const metadata) = 0;
nancy305b7cc2020-04-24 02:06:3639
40 // Invoked when the notification having |notification_id| is closed.
nancy450ee812020-04-28 18:19:3441 virtual void OnNotificationClosed(const std::string& notification_id) = 0;
nancy305b7cc2020-04-24 02:06:3642
43 // Invoked when the NotificationDisplayService object (the thing that this
44 // observer observes) will be destroyed. In response, the observer, |this|,
45 // should call "RemoveObserver(this)", whether directly or indirectly (e.g.
46 // via ScopedObserver::Remove).
nancy450ee812020-04-28 18:19:3447 virtual void OnNotificationDisplayServiceDestroyed(
48 NotificationDisplayService* service) = 0;
nancy305b7cc2020-04-24 02:06:3649
50 protected:
51 ~Observer() override;
52 };
53
Peter Beverloo7426194d2017-12-11 17:39:3854 ~NotificationDisplayService() override;
55
56 // Callback to be used with the GetDisplayed() method. Includes the set of
57 // notification ids that is being displayed to the user. The
58 // |supports_synchronization| callback indicates whether the platform has the
59 // ability to query which notifications are still being displayed.
60 //
61 // TODO(peter): Rename |supports_synchronization| to |supported|.
miguelgbfe768352017-03-20 15:34:3162 using DisplayedNotificationsCallback =
Richard Knollc6383592019-01-28 18:06:5863 base::OnceCallback<void(std::set<std::string>,
64 bool /* supports_synchronization */)>;
Evan Stade6e154152017-11-10 01:58:2565
Peter Beverloo7426194d2017-12-11 17:39:3866 // Returns an instance of the display service for the given |profile|.
Evan Stade6e154152017-11-10 01:58:2567 static NotificationDisplayService* GetForProfile(Profile* profile);
68
Peter Beverloo7426194d2017-12-11 17:39:3869 // Displays the |notification| of type |notification_type|. The |metadata|
70 // may be provided for certain notification types that require additional
71 // information for the notification to be displayed.
Evan Stade4ebefdf42017-09-21 23:50:1872 virtual void Display(
Peter Beverloo6dba2e102017-11-23 17:46:3373 NotificationHandler::Type notification_type,
Evan Stade4755cf22017-10-17 18:35:4374 const message_center::Notification& notification,
Lei Zhang03c6b782019-03-21 05:22:2475 std::unique_ptr<NotificationCommon::Metadata> metadata) = 0;
miguelg23cd2dd72016-04-21 15:24:0376
Peter Beverloo7426194d2017-12-11 17:39:3877 // Closes the notification having |notification_id| of |notification_type|.
Peter Beverloo6dba2e102017-11-23 17:46:3378 virtual void Close(NotificationHandler::Type notification_type,
miguelg87e986d2016-07-08 18:04:4479 const std::string& notification_id) = 0;
miguelg23cd2dd72016-04-21 15:24:0380
Peter Beverloo7426194d2017-12-11 17:39:3881 // Gets the IDs of currently displaying notifications and invokes |callback|
82 // once available. Not all backends support retrieving this information.
Finnur Thorarinsson0bfb7e72018-03-20 12:26:0683 virtual void GetDisplayed(DisplayedNotificationsCallback callback) = 0;
miguelg23cd2dd72016-04-21 15:24:0384
nancy305b7cc2020-04-24 02:06:3685 // Adds and removes an observer.
86 virtual void AddObserver(Observer* observer) = 0;
87 virtual void RemoveObserver(Observer* observer) = 0;
88
Peter Beverloo7426194d2017-12-11 17:39:3889 protected:
90 NotificationDisplayService() = default;
Evan Stade0bf465e52017-10-03 18:12:0091
Evan Stadecc63b182017-09-26 16:05:1292 private:
miguelg23cd2dd72016-04-21 15:24:0393 DISALLOW_COPY_AND_ASSIGN(NotificationDisplayService);
94};
95
96#endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_H_