File: notification_blocker.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rw-r--r-- 3,755 bytes parent folder | download | duplicates (9)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_MESSAGE_CENTER_NOTIFICATION_BLOCKER_H_
#define UI_MESSAGE_CENTER_NOTIFICATION_BLOCKER_H_

#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "ui/message_center/message_center_export.h"
#include "ui/message_center/public/cpp/notification.h"

namespace message_center {
class MessageCenter;

// NotificationBlocker manages the availability of notifications based on the
// current system status. Each NotificationBlocker implementation covers a
// single state such as screen lock or fullscreen.
//
// The current notification state is automatically updated when a blocker is
// initialized or destroyed. For instance, the `WelcomeTourNotificationBlocker`
// blocks all existing notifications when it is initialized (see below for how
// to properly initialize a `NotificationBlocker` subclass), and unblocks
// notifications when it is destructed.
//
// An object of a class that derives from this is not ready until `Init()` has
// been called. The following example demonstrates how to properly initialize
// such an object:
//
//   class MyNotificationBlocker : public NotificationBlocker {
//    public:
//     MyNotificationBlocker(MessageCenter* message_center, ...)
//         : NotificationBlocker(message_center), ... { ... }
//    ...
//   };
//
//   void foo() {
//     MessageCenter* message_center = ...;
//     std::unique_ptr<MyNotificationBlocker> my_blocker =
//         std::make_unique<MyNotificationBlocker>(message_center, ...);
//     my_blocker->Init();  /* DON'T FORGET THIS LINE! */
//     ...
//   }
//
// TODO(b/290637034): Improve upon the `Init()` pattern as it is easy to forget
// to call `Init()` when initializing a `NotificationBlocker` subclass.
class MESSAGE_CENTER_EXPORT NotificationBlocker {
 public:
  class Observer : public base::CheckedObserver {
   public:
    virtual void OnBlockingStateChanged(NotificationBlocker* blocker) = 0;
  };

  explicit NotificationBlocker(MessageCenter* message_center);
  virtual ~NotificationBlocker();

  // Registers this `NotificationBlocker` with the `MessageCenter`. This cannot
  // be done in the ctor because registering a notification blocker with the
  // message center may indirectly call the virtual functions below (e.g.
  // `ShouldShowNotification()` and `ShouldShowNotificationAsPopup()`), and
  // virtual functions should not be called from a ctor.
  void Init();

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Checks the current state and updates the availability.
  virtual void CheckState() {}

  // Returns true should be shown in the message center. Default returns true
  // always.
  virtual bool ShouldShowNotification(
      const Notification& notification) const;

  // Returns true if this notification should be shown as popups on screen.
  // If it's false, those notifications should be queued.
  // When a blocker starts returning false for a notification which is already
  // shown as a popup, the notification should be closed as a popup immediately.
  virtual bool ShouldShowNotificationAsPopup(
      const Notification& notification) const = 0;

 protected:
  MessageCenter* message_center() { return message_center_; }
  void NotifyBlockingStateChanged();

 private:
  bool is_initialized_ = false;
  base::ObserverList<Observer> observers_;
  raw_ptr<MessageCenter> message_center_;  // weak
};

typedef std::vector<raw_ptr<NotificationBlocker, VectorExperimental>>
    NotificationBlockers;

}  // namespace message_center

#endif  // UI_MESSAGE_CENTER_NOTIFICATION_BLOCKER_H_