File: notification_list.cc

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 (530 lines) | stat: -rw-r--r-- 15,985 bytes parent folder | download | duplicates (4)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/message_center/notification_list.h"

#include <string>
#include <utility>

#include "base/check.h"
#include "base/containers/adapters.h"
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "base/values.h"
#include "ui/gfx/image/image.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notification_blocker.h"
#include "ui/message_center/public/cpp/message_center_constants.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_types.h"

#if BUILDFLAG(IS_CHROMEOS)
#include <vector>

#include "ash/constants/ash_features.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace message_center {

namespace {

// Constants -------------------------------------------------------------------

#if BUILDFLAG(IS_CHROMEOS)

// A notification created within this time period is exempted from over-limit
// removal. NOTE: Used only if the notification limit feature is enabled.
constexpr base::TimeDelta kRemovalExemptionPeriod = base::Seconds(1);

#endif  // BUILDFLAG(IS_CHROMEOS)

// Helpers ---------------------------------------------------------------------

bool ShouldShowNotificationAsPopup(const Notification& notification,
                                   const NotificationBlockers& blockers,
                                   const NotificationBlocker* except) {
  for (message_center::NotificationBlocker* blocker : blockers) {
    if (blocker != except &&
        !blocker->ShouldShowNotificationAsPopup(notification)) {
      return false;
    }
  }
  return true;
}

}  // namespace

bool ComparePriorityTimestampSerial::operator()(Notification* n1,
                                                Notification* n2) const {
  if (n1->priority() > n2->priority()) {  // Higher pri go first.
    return true;
  }
  if (n1->priority() < n2->priority()) {
    return false;
  }
  return CompareTimestampSerial()(n1, n2);
}

bool CompareTimestampSerial::operator()(Notification* n1,
                                        Notification* n2) const {
  if (n1->timestamp() > n2->timestamp()) {  // Newer come first.
    return true;
  }
  if (n1->timestamp() < n2->timestamp()) {
    return false;
  }
  if (n1->serial_number() > n2->serial_number()) {  // Newer come first.
    return true;
  }
  if (n1->serial_number() < n2->serial_number()) {
    return false;
  }
  return false;
}

bool NotificationList::NotificationState::operator!=(
    const NotificationState& other) const {
  return shown_as_popup != other.shown_as_popup || is_read != other.is_read;
}

NotificationList::NotificationList(MessageCenter* message_center)
    : message_center_(message_center), quiet_mode_(false) {}

NotificationList::~NotificationList() = default;

#if BUILDFLAG(IS_CHROMEOS)
std::vector<std::string> NotificationList::GetTopKRemovableNotificationIds(
    size_t count) const {
  CHECK(ash::features::IsNotificationLimitEnabled());

  std::vector<std::string> found_ids;
  const base::Time current_time = base::Time::NowFromSystemTime();
  for (const auto& state_by_notification : base::Reversed(notifications_)) {
    const Notification& notification = *state_by_notification.first;

    // Skip the following notifications:
    // 1. Parent notifications with grouped children because this kind
    //    of notification is a container of child notifications.
    // 2. Pinned notifications.
    // 3. Notifications created within a defined time threshold.
    if (notification.pinned() || notification.group_parent() ||
        current_time - notification.timestamp() <= kRemovalExemptionPeriod) {
      continue;
    }

    found_ids.push_back(notification.id());
    if (found_ids.size() == count) {
      break;
    }
  }

  return found_ids;
}
#endif  // BUILDFLAG(IS_CHROMEOS)

void NotificationList::SetNotificationsShown(
    const NotificationBlockers& blockers,
    std::set<std::string>* updated_ids) {
  Notifications notifications = GetVisibleNotifications(blockers);

  for (Notification* notification : notifications) {
    NotificationState* state = &GetNotification(notification->id())->second;
    const NotificationState original_state = *state;
    state->shown_as_popup = true;
    state->is_read = true;
    if (updated_ids && (original_state != *state)) {
      updated_ids->insert(notification->id());
    }
  }
}

void NotificationList::AddNotification(
    std::unique_ptr<Notification> notification) {
  PushNotification(std::move(notification));
}

void NotificationList::UpdateNotificationMessage(
    const std::string& old_id,
    std::unique_ptr<Notification> new_notification) {
  auto iter = GetNotification(old_id);
  if (iter == notifications_.end()) {
    return;
  }

  NotificationState state = iter->second;

  if ((new_notification->renotify() ||
       !message_center_->HasMessageCenterView()) &&
      !quiet_mode_) {
    state = NotificationState();
  }

  // Do not use EraseNotification and PushNotification, since we don't want to
  // change unread counts nor to update is_read/shown_as_popup states.
  notifications_.erase(iter);

  // We really don't want duplicate IDs.
  DCHECK(GetNotification(new_notification->id()) == notifications_.end());
  notifications_.emplace(std::move(new_notification), state);
}

void NotificationList::RemoveNotification(const std::string& id) {
  EraseNotification(GetNotification(id));
}

NotificationList::Notifications NotificationList::GetNotifications() const {
  Notifications notifications;
  for (const auto& tuple : notifications_) {
    notifications.insert(tuple.first.get());
  }
  return notifications;
}

NotificationList::Notifications NotificationList::GetNotificationsByNotifierId(
    const NotifierId& notifier_id) const {
  Notifications notifications;
  for (const auto& tuple : notifications_) {
    Notification* notification = tuple.first.get();
    if (notification->notifier_id() == notifier_id) {
      notifications.insert(notification);
    }
  }
  return notifications;
}

NotificationList::Notifications NotificationList::GetNotificationsByAppId(
    const std::string& app_id) const {
  Notifications notifications;
  for (const auto& tuple : notifications_) {
    Notification* notification = tuple.first.get();
    if (notification->notifier_id().id == app_id) {
      notifications.insert(notification);
    }
  }
  return notifications;
}

NotificationList::Notifications NotificationList::GetNotificationsByOriginUrl(
    const GURL& source_url) const {
  Notifications notifications;
  for (const auto& tuple : notifications_) {
    Notification* notification = tuple.first.get();
    if (notification->origin_url() == source_url) {
      notifications.insert(notification);
    }
  }
  return notifications;
}

bool NotificationList::SetNotificationIcon(const std::string& notification_id,
                                           const ui::ImageModel& image) {
  auto iter = GetNotification(notification_id);
  if (iter == notifications_.end()) {
    return false;
  }
  iter->first->set_icon(image);
  return true;
}

bool NotificationList::SetNotificationImage(const std::string& notification_id,
                                            const gfx::Image& image) {
  auto iter = GetNotification(notification_id);
  if (iter == notifications_.end()) {
    return false;
  }
  iter->first->SetImage(image);
  return true;
}

bool NotificationList::HasNotificationOfType(
    const std::string& id,
    const NotificationType type) const {
  auto iter = GetNotification(id);
  if (iter == notifications_.end()) {
    return false;
  }

  return iter->first->type() == type;
}

bool NotificationList::HasPopupNotifications(
    const NotificationBlockers& blockers) const {
  for (const auto& tuple : notifications_) {
    if (tuple.first->priority() < DEFAULT_PRIORITY) {
      break;
    }
    if (!tuple.second.shown_as_popup &&
        ShouldShowNotificationAsPopup(*tuple.first, blockers,
                                      /*except=*/nullptr)) {
      return true;
    }
  }
  return false;
}

NotificationList::PopupNotifications NotificationList::GetPopupNotifications(
    const NotificationBlockers& blockers,
    std::list<std::string>* blocked) {
  PopupNotifications result;
  size_t default_priority_popup_count = 0;

  // Collect notifications that should be shown as popups. Start from oldest.
  for (auto& [notification, state] : base::Reversed(notifications_)) {
    if (state.shown_as_popup) {
      continue;
    }

    // No popups for LOW/MIN priority.
    if (notification->priority() < DEFAULT_PRIORITY) {
      continue;
    }

    // Group child notifications are shown in their parent's popup.
    if (notification->group_child()) {
      continue;
    }

    if (!ShouldShowNotificationAsPopup(*notification, blockers,
                                       /*except=*/nullptr)) {
      if (state.is_read) {
        state.shown_as_popup = true;
      }
      if (blocked) {
        blocked->push_back(notification->id());
      }
      continue;
    }

    // Checking limits. No limits for HIGH/MAX priority. DEFAULT priority
    // will return at most kMaxVisiblePopupNotifications entries. If the
    // popup entries are more, older entries are used. see crbug.com/165768
    if (notification->priority() == DEFAULT_PRIORITY &&
        default_priority_popup_count++ >= kMaxVisiblePopupNotifications) {
      continue;
    }

    result.insert(notification.get());
  }
  return result;
}

NotificationList::PopupNotifications
NotificationList::GetPopupNotificationsWithoutBlocker(
    const NotificationBlockers& blockers,
    const NotificationBlocker& blocker) const {
  PopupNotifications result;

  // Collect notifications that should be shown as popups, starting with the
  // newest.
  // TODO(1276903): see if we can merge this logic with `GetPopupNotifications`.
  // In particular, we could pass an optional blocker argument that would be
  // bypassed if specified.
  for (const auto& iter : notifications_) {
    const NotificationState* state = &iter.second;
    Notification* notification = iter.first.get();
    if (state->shown_as_popup) {
      continue;
    }

    // No popups for LOW/MIN priority.
    if (notification->priority() < DEFAULT_PRIORITY) {
      continue;
    }

    // Group child notifications are shown in their parent's popup.
    if (notification->group_child()) {
      continue;
    }

    if (!ShouldShowNotificationAsPopup(*notification, blockers, &blocker)) {
      continue;
    }

    result.insert(notification);
  }

  return result;
}

void NotificationList::MarkSinglePopupAsShown(const std::string& id,
                                              bool mark_notification_as_read) {
  auto iter = GetNotification(id);
  CHECK(iter != notifications_.end());

  NotificationState* state = &iter->second;
  if (iter->second.shown_as_popup) {
    return;
  }

  state->shown_as_popup = true;

  // The popup notification is already marked as read when it's displayed.
  // Set the is_read back to false if necessary.
  if (!mark_notification_as_read) {
    state->is_read = false;
  }
}

void NotificationList::MarkSinglePopupAsDisplayed(const std::string& id) {
  auto iter = GetNotification(id);
  if (iter == notifications_.end()) {
    return;
  }

  NotificationState* state = &iter->second;

  if (state->shown_as_popup) {
    return;
  }

  state->is_read = true;
}

void NotificationList::ResetSinglePopup(const std::string& id) {
  auto iter = GetNotification(id);
  CHECK(iter != notifications_.end());

  NotificationState* state = &iter->second;
  // `shown_as_popup` should be true if quiet mode is enabled.
  state->shown_as_popup = quiet_mode_;
  state->is_read = false;
  state->expand_state = ExpandState::DEFAULT;
}

ExpandState NotificationList::GetNotificationExpandState(
    const std::string& id) {
  auto iter = GetNotification(id);
  if (iter == notifications_.end()) {
    return ExpandState::DEFAULT;
  }

  return iter->second.expand_state;
}

void NotificationList::SetNotificationExpandState(
    const std::string& id,
    const ExpandState expand_state) {
  auto iter = GetNotification(id);
  if (iter == notifications_.end()) {
    return;
  }

  iter->second.expand_state = expand_state;
}

NotificationDelegate* NotificationList::GetNotificationDelegate(
    const std::string& id) {
  auto iter = GetNotification(id);
  if (iter == notifications_.end()) {
    return nullptr;
  }
  return iter->first->delegate();
}

void NotificationList::SetQuietMode(bool quiet_mode) {
  quiet_mode_ = quiet_mode;
  if (quiet_mode_) {
    // To prevent popups showing in quiet mode, mark all notifications'
    // `shown_as_popup` to true.
    for (auto& tuple : notifications_) {
      tuple.second.shown_as_popup = true;
    }
  }
}

Notification* NotificationList::GetNotificationById(const std::string& id) {
  auto iter = GetNotification(id);
  if (iter != notifications_.end()) {
    return iter->first.get();
  }
  return nullptr;
}

NotificationList::Notifications NotificationList::GetVisibleNotifications(
    const NotificationBlockers& blockers) const {
  return GetVisibleNotificationsWithoutBlocker(blockers, nullptr);
}

NotificationList::Notifications
NotificationList::GetVisibleNotificationsWithoutBlocker(
    const NotificationBlockers& blockers,
    const NotificationBlocker* ignored_blocker) const {
  Notifications result;
  for (const auto& tuple : notifications_) {
    auto it = (std::ranges::find_if(
        blockers, [&ignored_blocker,
                   &tuple](message_center::NotificationBlocker* blocker) {
          return blocker != ignored_blocker &&
                 !blocker->ShouldShowNotification(*tuple.first);
        }));

    if (it == blockers.end()) {
      result.insert(tuple.first.get());
    }
  }

  return result;
}

size_t NotificationList::NotificationCount(
    const NotificationBlockers& blockers) const {
  return GetVisibleNotifications(blockers).size();
}

NotificationList::OwnedNotifications::iterator
NotificationList::GetNotification(const std::string& id) {
  for (auto iter = notifications_.begin(); iter != notifications_.end();
       ++iter) {
    if (iter->first->id() == id) {
      return iter;
    }
  }
  return notifications_.end();
}

NotificationList::OwnedNotifications::const_iterator
NotificationList::GetNotification(const std::string& id) const {
  for (auto iter = notifications_.begin(); iter != notifications_.end();
       ++iter) {
    if (iter->first->id() == id) {
      return iter;
    }
  }
  return notifications_.end();
}

void NotificationList::EraseNotification(OwnedNotifications::iterator iter) {
  notifications_.erase(iter);
}

void NotificationList::PushNotification(
    std::unique_ptr<Notification> notification) {
  // Ensure that notification.id is unique by erasing any existing
  // notification with the same id (shouldn't normally happen).
  auto iter = GetNotification(notification->id());
  NotificationState state;
  if (iter != notifications_.end()) {
    state = iter->second;
    EraseNotification(iter);
  } else {
    // For critical ChromeOS system notifications, we ignore the standard quiet
    // mode behaviour and show the notification anyways.
    bool effective_quiet_mode = quiet_mode_;
#if BUILDFLAG(IS_CHROMEOS)
    effective_quiet_mode &= notification->system_notification_warning_level() !=
                            SystemNotificationWarningLevel::CRITICAL_WARNING;
#endif

    // TODO(mukai): needs to distinguish if a notification is dismissed by
    // the quiet mode or user operation.
    state.shown_as_popup =
        message_center_->IsMessageCenterVisible() || effective_quiet_mode;
  }
  if (notification->priority() == MIN_PRIORITY) {
    state.is_read = true;
  }
  notifications_.emplace(std::move(notification), state);
}

}  // namespace message_center