File: notification_display_queue.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (170 lines) | stat: -rw-r--r-- 6,032 bytes parent folder | download | duplicates (3)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/notifications/notification_display_queue.h"

#include <algorithm>
#include <utility>

#include "chrome/browser/notifications/notification_display_service.h"
#include "url/origin.h"

namespace {

bool IsWebNotification(NotificationHandler::Type notification_type) {
  return notification_type == NotificationHandler::Type::WEB_PERSISTENT ||
         notification_type == NotificationHandler::Type::WEB_NON_PERSISTENT ||
         notification_type == NotificationHandler::Type::EXTENSION;
}

}  // namespace

NotificationDisplayQueue::NotificationDisplayQueue(
    NotificationDisplayService* notification_display_service)
    : notification_display_service_(notification_display_service) {}

NotificationDisplayQueue::~NotificationDisplayQueue() = default;

void NotificationDisplayQueue::OnBlockingStateChanged() {
  MaybeDisplayQueuedNotifications();
}

bool NotificationDisplayQueue::ShouldEnqueueNotification(
    NotificationHandler::Type notification_type,
    const message_center::Notification& notification) const {
  return IsWebNotification(notification_type) &&
         IsAnyNotificationBlockerActive(notification);
}

void NotificationDisplayQueue::EnqueueNotification(
    NotificationHandler::Type notification_type,
    const message_center::Notification& notification,
    std::unique_ptr<NotificationCommon::Metadata> metadata) {
  bool replaced =
      DoRemoveQueuedNotification(notification.id(), /*notify=*/false);
  queued_notifications_.emplace_back(notification_type, notification,
                                     std::move(metadata));
  // Notify blockers that a new notification has been blocked.
  for (auto& blocker : blockers_) {
    if (blocker->ShouldBlockNotification(notification))
      blocker->OnBlockedNotification(notification, replaced);
  }
}

void NotificationDisplayQueue::RemoveQueuedNotification(
    const std::string& notification_id) {
  DoRemoveQueuedNotification(notification_id, /*notify=*/true);
}

std::set<std::string> NotificationDisplayQueue::GetQueuedNotificationIds()
    const {
  std::set<std::string> notification_ids;
  for (const QueuedNotification& queued : queued_notifications_) {
    notification_ids.insert(queued.notification.id());
  }

  return notification_ids;
}

std::set<std::string>
NotificationDisplayQueue::GetQueuedNotificationIdsForOrigin(
    const GURL& origin) const {
  std::set<std::string> notification_ids;
  for (const QueuedNotification& queued : queued_notifications_) {
    if (url::IsSameOriginWith(queued.notification.origin_url(), origin)) {
      notification_ids.insert(queued.notification.id());
    }
  }

  return notification_ids;
}

void NotificationDisplayQueue::SetNotificationBlockers(
    NotificationBlockers blockers) {
  // Remove old blockers from the observer.
  for (auto& blocker : blockers_)
    notification_blocker_observations_.RemoveObservation(blocker.get());

  // Add new blockers and observe them.
  blockers_ = std::move(blockers);
  for (auto& blocker : blockers_)
    notification_blocker_observations_.AddObservation(blocker.get());

  // Update new state with new blockers.
  MaybeDisplayQueuedNotifications();
}

void NotificationDisplayQueue::AddNotificationBlocker(
    std::unique_ptr<NotificationBlocker> blocker) {
  notification_blocker_observations_.AddObservation(blocker.get());
  blockers_.push_back(std::move(blocker));
}

bool NotificationDisplayQueue::DoRemoveQueuedNotification(
    const std::string& notification_id,
    bool notify) {
  auto it = std::ranges::find(queued_notifications_, notification_id,
                              [](const QueuedNotification& queued) {
                                return queued.notification.id();
                              });

  if (it == queued_notifications_.end())
    return false;

  if (notify) {
    for (auto& blocker : blockers_) {
      if (blocker->ShouldBlockNotification(it->notification))
        blocker->OnClosedNotification(it->notification);
    }
  }

  queued_notifications_.erase(it);
  return true;
}

void NotificationDisplayQueue::MaybeDisplayQueuedNotifications() {
  auto show_begin = std::stable_partition(
      queued_notifications_.begin(), queued_notifications_.end(),
      [&](const QueuedNotification& queued) {
        return IsAnyNotificationBlockerActive(queued.notification);
      });

  std::vector<QueuedNotification> notifications;
  notifications.insert(notifications.end(), std::make_move_iterator(show_begin),
                       std::make_move_iterator(queued_notifications_.end()));

  queued_notifications_.erase(show_begin, queued_notifications_.end());

  for (QueuedNotification& queued : notifications) {
    notification_display_service_->Display(queued.notification_type,
                                           queued.notification,
                                           std::move(queued.metadata));
  }
}

bool NotificationDisplayQueue::IsAnyNotificationBlockerActive(
    const message_center::Notification& notification) const {
  return std::ranges::any_of(
      blockers_,
      [&notification](const std::unique_ptr<NotificationBlocker>& blocker) {
        return blocker->ShouldBlockNotification(notification);
      });
}

NotificationDisplayQueue::QueuedNotification::QueuedNotification(
    NotificationHandler::Type notification_type,
    const message_center::Notification& notification,
    std::unique_ptr<NotificationCommon::Metadata> metadata)
    : notification_type(notification_type),
      notification(notification),
      metadata(std::move(metadata)) {}

NotificationDisplayQueue::QueuedNotification::QueuedNotification(
    QueuedNotification&&) = default;

NotificationDisplayQueue::QueuedNotification&
NotificationDisplayQueue::QueuedNotification::operator=(QueuedNotification&&) =
    default;

NotificationDisplayQueue::QueuedNotification::~QueuedNotification() = default;