File: notification_display_service.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (101 lines) | stat: -rw-r--r-- 4,243 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_H_
#define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_H_

#include <map>
#include <memory>
#include <set>
#include <string>

#include "base/functional/callback_forward.h"
#include "base/observer_list_types.h"
#include "chrome/browser/notifications/notification_common.h"
#include "chrome/browser/notifications/notification_handler.h"
#include "components/keyed_service/core/keyed_service.h"

class Profile;

namespace message_center {
class Notification;
}

// Profile-bound service that enables user-visible notifications to be displayed
// and managed. Notifications may either be presented using a notification
// center provided by the platform, or by Chrome's Message Center.
class NotificationDisplayService : public KeyedService {
 public:
  class Observer : public base::CheckedObserver {
   public:
    // Invoked when the |notification| is displayed. The |metadata| is provided
    // for persistent web page notifications only, which require
    // |service_worker_scope|.
    virtual void OnNotificationDisplayed(
        const message_center::Notification& notification,
        const NotificationCommon::Metadata* const metadata) = 0;

    // Invoked when the notification having |notification_id| is closed.
    virtual void OnNotificationClosed(const std::string& notification_id) = 0;

    // Invoked when the NotificationDisplayService object (the thing that this
    // observer observes) will be destroyed. In response, the observer, |this|,
    // should call "RemoveObserver(this)", whether directly or indirectly (e.g.
    // via ScopedObserver::Remove).
    virtual void OnNotificationDisplayServiceDestroyed(
        NotificationDisplayService* service) = 0;

   protected:
    ~Observer() override;
  };

  NotificationDisplayService(const NotificationDisplayService&) = delete;
  NotificationDisplayService& operator=(const NotificationDisplayService&) =
      delete;
  ~NotificationDisplayService() override;

  // Callback to be used with the GetDisplayed() method. Includes the set of
  // notification ids that is being displayed to the user. The
  // |supports_synchronization| callback indicates whether the platform has the
  // ability to query which notifications are still being displayed.
  //
  // TODO(peter): Rename |supports_synchronization| to |supported|.
  using DisplayedNotificationsCallback =
      base::OnceCallback<void(std::set<std::string>,
                              bool /* supports_synchronization */)>;

  // Displays the |notification| of type |notification_type|. The |metadata|
  // may be provided for certain notification types that require additional
  // information for the notification to be displayed.
  virtual void Display(
      NotificationHandler::Type notification_type,
      const message_center::Notification& notification,
      std::unique_ptr<NotificationCommon::Metadata> metadata) = 0;

  // Closes the notification having |notification_id| of |notification_type|.
  virtual void Close(NotificationHandler::Type notification_type,
                     const std::string& notification_id) = 0;

  // Gets the IDs of currently displaying notifications and invokes |callback|
  // once available. Not all backends support retrieving this information.
  // TODO(crbug.com/40283098): Consider refactoring this API and its
  // usage to something that can get implemented by more backends.
  virtual void GetDisplayed(DisplayedNotificationsCallback callback) = 0;

  // Gets the IDs of currently displaying notifications associated with `origin`
  // and invokes `callback` once available. Not all backends support retrieving
  // this information.
  virtual void GetDisplayedForOrigin(
      const GURL& origin,
      DisplayedNotificationsCallback callback) = 0;

  // Adds and removes an observer.
  virtual void AddObserver(Observer* observer) = 0;
  virtual void RemoveObserver(Observer* observer) = 0;

 protected:
  NotificationDisplayService() = default;
};

#endif  // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_DISPLAY_SERVICE_H_